aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/FeedClientTest.java
blob: b70fbaf3096f4ee79a70f5351061ff664f1993c6 (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
// 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.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.time.Clock;
import java.util.concurrent.atomic.AtomicInteger;

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

/**
 * 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("localhost"))
                    .build())
            .setConnectionParams(new ConnectionParams.Builder()
                    .setDryRun(true)
                    .build())
            .build();
    final AtomicInteger resultsReceived = new AtomicInteger(0);

    FeedClient.ResultCallback resultCallback = (docId, documentResult) -> {
        assertTrue(documentResult.isSuccess());
        assertEquals(DOCID, docId);
        resultsReceived.incrementAndGet();
    };

    FeedClient feedClient = new FeedClientImpl(sessionParams, resultCallback, FeedClientFactory.createTimeoutExecutor(), Clock.systemUTC());

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

    @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() {
        InputStream stream = new ByteArrayInputStream((String.format("[{\"remove\": \"%s\"}]", DOCID)
                .getBytes(StandardCharsets.UTF_8)));
        AtomicInteger docCounter = new AtomicInteger(0);
        FeedClient.feedJson(stream, feedClient, docCounter);
        assertEquals(1, docCounter.get());
        feedClient.close();
        assertEquals(1, resultsReceived.get());
    }

    @Test
    public void testFeedXml() {
        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);
        assertEquals(1, docCounter.get());
        feedClient.close();
        assertEquals(1, resultsReceived.get());
    }

}