aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/SyncFeedClientTest.java
blob: fce356e6677ed49d73ce328543ae5217be2da028 (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
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.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.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("hostname"))
                                                                  .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\"" +
                                         "    }" +
                                         "}"));

        SyncResult result = feedClient.stream(operations);

        assertTrue(result.isSuccess());
        assertEquals(4, 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());
    }

}