aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-reindexer/src/test/java/ai/vespa/reindexing/http/ReindexingV1ApiTest.java
blob: 1eb9d9ec142abbb3cca0281a1ffcfc1235be46e9 (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
88
89
90
91
92
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.reindexing.http;

import ai.vespa.reindexing.Reindexing;
import ai.vespa.reindexing.Reindexing.Status;
import ai.vespa.reindexing.ReindexingCurator;
import com.yahoo.container.jdisc.RequestHandlerTestDriver;
import com.yahoo.document.DocumentType;
import com.yahoo.document.DocumentTypeManager;
import com.yahoo.document.config.DocumentmanagerConfig;
import com.yahoo.documentapi.ProgressToken;
import com.yahoo.jdisc.test.MockMetric;
import com.yahoo.schema.derived.Deriver;
import com.yahoo.vespa.curator.mock.MockCurator;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.util.List;
import java.util.concurrent.Executors;

import static com.yahoo.jdisc.http.HttpRequest.Method.POST;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * @author jonmv
 */
class ReindexingV1ApiTest {

    DocumentmanagerConfig musicConfig = Deriver.getDocumentManagerConfig("src/test/resources/schemas/music.sd").build();
    DocumentTypeManager manager = new DocumentTypeManager(musicConfig);
    DocumentType musicType = manager.getDocumentType("music");
    ReindexingCurator database = new ReindexingCurator(new MockCurator(), manager);
    ReindexingV1ApiHandler handler = new ReindexingV1ApiHandler(Executors.newSingleThreadExecutor(), new MockMetric(),
                                                                List.of("cluster", "oyster"), database);

    @Test
    void testResponses() {
        RequestHandlerTestDriver driver = new RequestHandlerTestDriver(handler);

        // GET at root
        var response = driver.sendRequest("http://localhost/reindexing/v1/");
        assertEquals("{\"resources\":[{\"url\":\"/reindexing/v1/status\"}]}", response.readAll());
        assertEquals("application/json; charset=UTF-8", response.getResponse().headers().getFirst("Content-Type"));
        assertEquals(200, response.getStatus());

        // GET at status with empty database
        response = driver.sendRequest("http://localhost/reindexing/v1/status");
        assertEquals("{\"clusters\":{\"cluster\":{\"documentTypes\":{}},\"oyster\":{\"documentTypes\":{}}}}", response.readAll());
        assertEquals(200, response.getStatus());

        // GET at status with a failed status
        database.writeReindexing(Reindexing.empty().with(musicType, Status.ready(Instant.EPOCH)
                                                                          .running()
                                                                          .progressed(new ProgressToken())
                                                                          .failed(Instant.ofEpochMilli(123), "ヽ(。_°)ノ")),
                                 "cluster");
        response = driver.sendRequest("http://localhost/reindexing/v1/status");
        assertEquals("{" +
                     "\"clusters\":{" +
                       "\"cluster\":{" +
                         "\"documentTypes\":{" +
                           "\"music\":{" +
                             "\"startedMillis\":0," +
                             "\"endedMillis\":123," +
                             "\"progress\":1.0," +
                             "\"state\":\"failed\"," +
                             "\"message\":\"ヽ(。_°)ノ\"}" +
                           "}" +
                         "}," +
                         "\"oyster\":{" +
                           "\"documentTypes\":{}" +
                         "}" +
                       "}" +
                     "}",
                     response.readAll());
        assertEquals(200, response.getStatus());

        // POST at root
        response = driver.sendRequest("http://localhost/reindexing/v1/status", POST);
        assertEquals("{\"error-code\":\"METHOD_NOT_ALLOWED\",\"message\":\"Only GET is supported under /reindexing/v1/\"}",
                     response.readAll());
        assertEquals(405, response.getStatus());

        // GET at non-existent path
        response = driver.sendRequest("http://localhost/reindexing/v1/moo");
        assertEquals("{\"error-code\":\"NOT_FOUND\",\"message\":\"Nothing at /reindexing/v1/moo\"}",
                     response.readAll());
        assertEquals(404, response.getStatus());

    }

}