summaryrefslogtreecommitdiffstats
path: root/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/SyncFeedClientTest.java
blob: 6e2c6cafc9bf42c668dede39ff19c5c2f6538c6f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2020 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.http.client;

import com.yahoo.vespa.http.client.config.Cluster;
import com.yahoo.vespa.http.client.config.ConnectionParams;
import com.yahoo.vespa.http.client.config.Endpoint;
import com.yahoo.vespa.http.client.config.FeedParams;
import com.yahoo.vespa.http.client.config.SessionParams;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import com.yahoo.vespa.http.client.SyncFeedClient.SyncOperation;
import com.yahoo.vespa.http.client.SyncFeedClient.SyncResult;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNull;

/**
 * Tests the sync wrapper to the feed client
 *
 * @author bratseth
 */
public class SyncFeedClientTest {

    @Test
    public void testFeedJson() {
        SessionParams sessionParams = new SessionParams.Builder()
                                              .addCluster(new Cluster.Builder()
                                                                  .addEndpoint(Endpoint.create("localhost"))
                                                                  .build())
                                              .setConnectionParams(new ConnectionParams.Builder()
                                                                           .setDryRun(true)
                                                                           .build())
                                              .build();
        SyncFeedClient feedClient = new SyncFeedClient(sessionParams);

        assertFeedSuccessful(feedClient);
        assertFeedSuccessful(feedClient); // ensure the client can be reused
        feedClient.close();
    }

    private void assertFeedSuccessful(SyncFeedClient feedClient) {
        List<SyncOperation> operations = new ArrayList<>();

        operations.add(new SyncOperation("id::test::1",
                                         "{" +
                                                       "    \"put\": \"id::test::1\"," +
                                                       "    \"fields\": {" +
                                                       "        \"title\": \"Title 1\"" +
                                                       "    }" +
                                                       "}"));
        operations.add(new SyncOperation("id::test::2",
                                         "{" +
                                         "    \"put\": \"id::test::2\"," +
                                         "    \"fields\": {" +
                                         "        \"title\": \"Title 2\"" +
                                         "    }" +
                                         "}"));
        operations.add(new SyncOperation("id::test::3",
                                         "{" +
                                         "    \"put\": \"id::test::3\"," +
                                         "    \"fields\": {" +
                                         "        \"title\": \"Title 3\"" +
                                         "    }" +
                                         "}"));
        operations.add(new SyncOperation("id::test::3", // Another operation for the same document
                                         "{" +
                                         "    \"put\": \"id::test::3\"," +
                                         "    \"fields\": {" +
                                         "        \"title\": \"Title 4\"" +
                                         "    }" +
                                         "}"));
        operations.add(new SyncOperation("id::test::4",
            "{" +
                "    \"put\": \"id::test::4\"," +
                "    \"fields\": {" +
                "        \"title\": \"Title 4\"" +
                "    }" +
                "}", "opId_4", null));
        operations.add(new SyncOperation("id::test::4", // Another operation for the same document
            "{" +
                "    \"put\": \"id::test::4\"," +
                "    \"fields\": {" +
                "        \"title\": \"Title 44\"" +
                "    }" +
                "}", "opId_44", null));

        SyncResult result = feedClient.stream(operations);

        assertTrue(result.isSuccess());
        assertEquals(6, result.results().size());
        assertNull(result.exception());
        assertEquals("id::test::1", result.results().get(0).getDocumentId());
        assertEquals("id::test::2", result.results().get(1).getDocumentId());
        assertEquals("id::test::3", result.results().get(2).getDocumentId());
        assertEquals("id::test::3", result.results().get(3).getDocumentId());
        assertEquals("id::test::4", result.results().get(4).getDocumentId());
        assertEquals("id::test::4", result.results().get(5).getDocumentId());
        assertEquals("opId_4", result.results().get(4).getOperationId());
        assertEquals("opId_44", result.results().get(5).getOperationId());
        assertTrue(result.results().get(4).getDocumentDataAsCharSequence().toString().contains("\"Title 4\""));
        assertTrue(result.results().get(5).getDocumentDataAsCharSequence().toString().contains("\"Title 44\""));

        result.results().forEach(r -> assertNotNull(r.getOperationId()));
    }

}