Search in sources :

Example 1 with ConnectionParams

use of com.yahoo.vespa.http.client.config.ConnectionParams in project vespa by vespa-engine.

the class ApacheGatewayConnectionTest method testProtocolV3.

@Test
public void testProtocolV3() throws Exception {
    final Endpoint endpoint = Endpoint.create("hostname", 666, false);
    final FeedParams feedParams = new FeedParams.Builder().setDataFormat(FeedParams.DataFormat.JSON_UTF8).build();
    final String clusterSpecificRoute = "";
    final ConnectionParams connectionParams = new ConnectionParams.Builder().setEnableV3Protocol(true).build();
    final List<Document> documents = new ArrayList<>();
    final CountDownLatch verifyContentSentLatch = new CountDownLatch(1);
    final String vespaDocContent = "Hello, I a JSON doc.";
    final String docId = "42";
    final AtomicInteger requestsReceived = new AtomicInteger(0);
    // This is the fake server, takes header client ID and uses this as session Id.
    ApacheGatewayConnection.HttpClientFactory mockFactory = mockHttpClientFactory(post -> {
        final Header clientIdHeader = post.getFirstHeader(Headers.CLIENT_ID);
        verifyContentSentLatch.countDown();
        return httpResponse(clientIdHeader.getValue(), "3");
    });
    ApacheGatewayConnection apacheGatewayConnection = new ApacheGatewayConnection(endpoint, feedParams, clusterSpecificRoute, connectionParams, mockFactory, "clientId");
    apacheGatewayConnection.connect();
    apacheGatewayConnection.handshake();
    documents.add(createDoc(docId, vespaDocContent, true));
    apacheGatewayConnection.writeOperations(documents);
    assertTrue(verifyContentSentLatch.await(10, TimeUnit.SECONDS));
}
Also used : ArrayList(java.util.ArrayList) Document(com.yahoo.vespa.http.client.core.Document) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionParams(com.yahoo.vespa.http.client.config.ConnectionParams) FeedParams(com.yahoo.vespa.http.client.config.FeedParams) Endpoint(com.yahoo.vespa.http.client.config.Endpoint) Header(org.apache.http.Header) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 2 with ConnectionParams

use of com.yahoo.vespa.http.client.config.ConnectionParams in project vespa by vespa-engine.

the class ApacheGatewayConnectionTest method dynamic_headers_are_added_to_the_response.

@Test
public void dynamic_headers_are_added_to_the_response() throws IOException, ServerResponseException, InterruptedException {
    ConnectionParams.HeaderProvider headerProvider = mock(ConnectionParams.HeaderProvider.class);
    when(headerProvider.getHeaderValue()).thenReturn("v1").thenReturn("v2").thenReturn("v3");
    ConnectionParams connectionParams = new ConnectionParams.Builder().addDynamicHeader("foo", headerProvider).build();
    CountDownLatch verifyContentSentLatch = new CountDownLatch(1);
    AtomicInteger counter = new AtomicInteger(1);
    ApacheGatewayConnection.HttpClientFactory mockFactory = mockHttpClientFactory(post -> {
        Header[] fooHeader = post.getHeaders("foo");
        assertEquals(1, fooHeader.length);
        assertEquals("foo", fooHeader[0].getName());
        assertEquals("v" + counter.getAndIncrement(), fooHeader[0].getValue());
        verifyContentSentLatch.countDown();
        return httpResponse("clientId", "3");
    });
    ApacheGatewayConnection apacheGatewayConnection = new ApacheGatewayConnection(Endpoint.create("hostname", 666, false), new FeedParams.Builder().build(), "", connectionParams, mockFactory, "clientId");
    apacheGatewayConnection.connect();
    apacheGatewayConnection.handshake();
    List<Document> documents = new ArrayList<>();
    documents.add(createDoc("42", "content", true));
    apacheGatewayConnection.writeOperations(documents);
    apacheGatewayConnection.writeOperations(documents);
    assertTrue(verifyContentSentLatch.await(10, TimeUnit.SECONDS));
    // 1x connect(), 2x writeOperations()
    verify(headerProvider, times(3)).getHeaderValue();
}
Also used : Header(org.apache.http.Header) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Document(com.yahoo.vespa.http.client.core.Document) ConnectionParams(com.yahoo.vespa.http.client.config.ConnectionParams) Test(org.junit.Test)

Example 3 with ConnectionParams

use of com.yahoo.vespa.http.client.config.ConnectionParams in project vespa by vespa-engine.

the class ApacheGatewayConnectionTest method testBadConfigParameters.

@Test(expected = RuntimeException.class)
public void testBadConfigParameters() throws Exception {
    final Endpoint endpoint = Endpoint.create("hostname", 666, false);
    final FeedParams feedParams = new FeedParams.Builder().setDataFormat(FeedParams.DataFormat.JSON_UTF8).build();
    final String clusterSpecificRoute = "";
    final ConnectionParams connectionParams = new ConnectionParams.Builder().setEnableV3Protocol(true).build();
    final ApacheGatewayConnection.HttpClientFactory mockFactory = mock(ApacheGatewayConnection.HttpClientFactory.class);
    new ApacheGatewayConnection(endpoint, feedParams, clusterSpecificRoute, connectionParams, mockFactory, null);
}
Also used : Endpoint(com.yahoo.vespa.http.client.config.Endpoint) ConnectionParams(com.yahoo.vespa.http.client.config.ConnectionParams) FeedParams(com.yahoo.vespa.http.client.config.FeedParams) Test(org.junit.Test)

Example 4 with ConnectionParams

use of com.yahoo.vespa.http.client.config.ConnectionParams in project vespa by vespa-engine.

the class ApacheGatewayConnectionTest method testJsonDocumentHeader.

@Test
public void testJsonDocumentHeader() throws Exception {
    final Endpoint endpoint = Endpoint.create("hostname", 666, false);
    final FeedParams feedParams = new FeedParams.Builder().setDataFormat(FeedParams.DataFormat.JSON_UTF8).build();
    final String clusterSpecificRoute = "";
    final ConnectionParams connectionParams = new ConnectionParams.Builder().setUseCompression(true).build();
    final List<Document> documents = new ArrayList<>();
    final CountDownLatch verifyContentSentLatch = new CountDownLatch(1);
    final String vespaDocContent = "Hello, I a JSON doc.";
    final String docId = "42";
    final AtomicInteger requestsReceived = new AtomicInteger(0);
    // This is the fake server, checks that DATA_FORMAT header is set properly.
    ApacheGatewayConnection.HttpClientFactory mockFactory = mockHttpClientFactory(post -> {
        final Header header = post.getFirstHeader(Headers.DATA_FORMAT);
        if (requestsReceived.incrementAndGet() == 1) {
            // This is handshake, it is not json.
            assert (header == null);
            return httpResponse("clientId", "3");
        }
        assertNotNull(header);
        assertThat(header.getValue(), is(FeedParams.DataFormat.JSON_UTF8.name()));
        // Test is done.
        verifyContentSentLatch.countDown();
        return httpResponse("clientId", "3");
    });
    ApacheGatewayConnection apacheGatewayConnection = new ApacheGatewayConnection(endpoint, feedParams, clusterSpecificRoute, connectionParams, mockFactory, "clientId");
    apacheGatewayConnection.connect();
    apacheGatewayConnection.handshake();
    documents.add(createDoc(docId, vespaDocContent, true));
    apacheGatewayConnection.writeOperations(documents);
    assertTrue(verifyContentSentLatch.await(10, TimeUnit.SECONDS));
}
Also used : ArrayList(java.util.ArrayList) Document(com.yahoo.vespa.http.client.core.Document) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionParams(com.yahoo.vespa.http.client.config.ConnectionParams) FeedParams(com.yahoo.vespa.http.client.config.FeedParams) Endpoint(com.yahoo.vespa.http.client.config.Endpoint) Header(org.apache.http.Header) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 5 with ConnectionParams

use of com.yahoo.vespa.http.client.config.ConnectionParams in project vespa by vespa-engine.

the class ApacheGatewayConnectionTest method testServerReturnsBadSessionInV3.

@Test(expected = IllegalArgumentException.class)
public void testServerReturnsBadSessionInV3() throws Exception {
    final Endpoint endpoint = Endpoint.create("hostname", 666, false);
    final FeedParams feedParams = new FeedParams.Builder().setDataFormat(FeedParams.DataFormat.JSON_UTF8).build();
    final String clusterSpecificRoute = "";
    final ConnectionParams connectionParams = new ConnectionParams.Builder().setEnableV3Protocol(true).build();
    // This is the fake server, returns wrong session Id.
    ApacheGatewayConnection.HttpClientFactory mockFactory = mockHttpClientFactory(post -> httpResponse("Wrong Id from server", "3"));
    ApacheGatewayConnection apacheGatewayConnection = new ApacheGatewayConnection(endpoint, feedParams, clusterSpecificRoute, connectionParams, mockFactory, "clientId");
    apacheGatewayConnection.connect();
    final List<Document> documents = new ArrayList<>();
    apacheGatewayConnection.writeOperations(documents);
}
Also used : Endpoint(com.yahoo.vespa.http.client.config.Endpoint) ArrayList(java.util.ArrayList) Document(com.yahoo.vespa.http.client.core.Document) ConnectionParams(com.yahoo.vespa.http.client.config.ConnectionParams) FeedParams(com.yahoo.vespa.http.client.config.FeedParams) Test(org.junit.Test)

Aggregations

ConnectionParams (com.yahoo.vespa.http.client.config.ConnectionParams)6 Test (org.junit.Test)6 Endpoint (com.yahoo.vespa.http.client.config.Endpoint)5 FeedParams (com.yahoo.vespa.http.client.config.FeedParams)5 Document (com.yahoo.vespa.http.client.core.Document)5 ArrayList (java.util.ArrayList)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 Header (org.apache.http.Header)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 StatusLine (org.apache.http.StatusLine)1