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\"" +
                                         "    }" +
                                         "}"));

        SyncResult result = feedClient.stream(operations);

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

}

@@ -3074,16 +3074,475 @@
   %22%7D%22));
+%0A        operations.add(new SyncOperation(%22id::test::3%22, // Another operation for the same document%0A                                         %22%7B%22 +%0A                                         %22    %5C%22put%5C%22: %5C%22id::test::3%5C%22,%22 +%0A                                         %22    %5C%22fields%5C%22: %7B%22 +%0A                                         %22        %5C%22title%5C%22: %5C%22Title 4%5C%22%22 +%0A                                         %22    %7D%22 +%0A                                         %22%7D%22));
 %0A%0A      
@@ -3656,17 +3656,17 @@
 tEquals(
-3
+4
 , result
@@ -3873,32 +3873,32 @@
 tDocumentId());%0A
-
         assertEq
@@ -3955,21 +3955,99 @@
 umentId());%0A
+        assertEquals(%22id::test::3%22, result.results().get(3).getDocumentId());%0A
     %7D%0A%0A%7D%0A

Test multiple operations on a single document