summaryrefslogtreecommitdiffstats
path: root/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/FeedClientTest.java
blob: 8d6530d83055d6eb2ac89cb4afa44464e0d19720 (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
// Copyright 2017 Yahoo Holdings. 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 com.yahoo.vespa.http.client.core.api.FeedClientImpl;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;

/**
 * Tests for the API, using dryrun option to mock gateway.
 * @author dybis
 */
public class FeedClientTest {

    private final static String DOCID = "doc_id";

    SessionParams sessionParams = new SessionParams.Builder()
            .addCluster(new Cluster.Builder()
                    .addEndpoint(Endpoint.create("hostname"))
                    .build())
            .setConnectionParams(new ConnectionParams.Builder()
                    .setDryRun(true)
                    .build())
            .build();
    final AtomicInteger resultsReceived = new AtomicInteger(0);
    FeedClient.ResultCallback resultCallback = (docId, documentResult) -> {
        assert(documentResult.isSuccess());
        assertThat(docId, is(DOCID));
        resultsReceived.incrementAndGet();
    };

    FeedClient feedClient = new FeedClientImpl(sessionParams, resultCallback, SessionFactory.createTimeoutExecutor());

    @Test
    public void testStreamAndClose() throws Exception {
        feedClient.stream(DOCID, "blob");
        feedClient.close();
        assertThat(resultsReceived.get(), is(1));
    }

    @Test
    public void testGetStatsAsJson() throws Exception {
        feedClient.stream(DOCID, "blob");
        while (resultsReceived.get() == 0) {Thread.sleep(3); }
        String stats = feedClient.getStatsAsJson();
        assertTrue(stats.contains("\"dryRun\":true"));
        feedClient.close();
    }

    @Test
    public void testFeedJson() throws Exception {
        InputStream stream = new ByteArrayInputStream((String.format("[{\"remove\": \"%s\"}]", DOCID)
                .getBytes(StandardCharsets.UTF_8)));
        AtomicInteger docCounter = new AtomicInteger(0);
        FeedClient.feedJson(stream, feedClient, docCounter);
        assertThat(docCounter.get(), is(1));
        feedClient.close();
        assertThat(resultsReceived.get(), is(1));
    }

    @Test
    public void testFeedXml() throws Exception {
        InputStream stream = new ByteArrayInputStream((String.format(
                "<document documenttype=\"music\" documentid=\"%s\">\n</document>\n", DOCID)
                .getBytes(StandardCharsets.UTF_8)));
        AtomicInteger docCounter = new AtomicInteger(0);
        FeedClient.feedXml(stream, feedClient, docCounter);
        assertThat(docCounter.get(), is(1));
        feedClient.close();
        assertThat(resultsReceived.get(), is(1));
    }
}