aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/statuspage/StatusPageProxyApiTest.java
blob: f39914fd8fb10738754386742ad4cfcaf30fcea0 (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
93
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.restapi.statuspage;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.yahoo.application.Networking;
import com.yahoo.application.container.JDisc;
import com.yahoo.application.container.handler.Request;
import com.yahoo.application.container.handler.Response;
import com.yahoo.vespa.hosted.controller.integration.SecretStoreMock;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.IOException;

import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.junit.Assert.assertEquals;

/**
 * @author mpolden
 */
public class StatusPageProxyApiTest {

    private JDisc container;

    @Before
    public void startContainer() {
        container = JDisc.fromServicesXml(servicesXml(), Networking.disable);
        secretStore().setSecret("vespa_hosted.controller.statuspage_api_key", "page-id:secret");
    }

    @After
    public void stopContainer() {
        container.close();
        secretStore().clear();
    }

    @Rule
    public final WireMockRule wireMock = new WireMockRule(options().dynamicPort(), true);

    @Test
    public void test_proxy() throws Exception {
        // Invalid requests
        assertResponse("/statuspage/v1/", "{\"error-code\":\"NOT_FOUND\",\"message\":\"Nothing at path '/statuspage/v1'\"}", 404);
        assertResponse("/statuspage/v1/invalid", "{\"error-code\":\"BAD_REQUEST\",\"message\":\"Invalid resource: 'invalid'\"}", 400);
        assertResponse(Request.Method.POST, "/statuspage/v1/invalid", "{\"error-code\":\"METHOD_NOT_ALLOWED\",\"message\":\"Method 'POST' is not supported\"}", 405);

        // Mock responses from StatusPage
        wireMock.stubFor(get(urlEqualTo("/api/v2/incidents.json?api_key=secret"))
                                 .willReturn(okJson("{\"incidents\":[]}")));
        wireMock.stubFor(get(urlEqualTo("/api/v2/scheduled-maintenances.json?api_key=secret"))
                                 .willReturn(okJson("{\"scheduled_maintenances\":[]}")));

        assertResponse("/statuspage/v1/incidents", "{\"incidents\":[]}", 200);
        assertResponse("/statuspage/v1/scheduled-maintenances", "{\"scheduled_maintenances\":[]}", 200);
    }

    private void assertResponse(String path, String expectedResponse, int expectedStatusCode) throws IOException {
        assertResponse(Request.Method.GET, path, expectedResponse, expectedStatusCode);
    }

    private void assertResponse(Request.Method method, String path, String expectedResponse, int expectedStatusCode) throws IOException {
        Response response = container.handleRequest(new Request("http://localhost:8080" + path, new byte[0], method));
        assertEquals(expectedResponse, response.getBodyAsString());
        assertEquals("Status code", expectedStatusCode, response.getStatus());
        assertEquals("application/json; charset=UTF-8", response.getHeaders().getFirst("Content-Type"));
    }

    private SecretStoreMock secretStore() {
        return (SecretStoreMock) container.components().getComponent(SecretStoreMock.class.getName());
    }

    private String servicesXml() {
        String statusPageApiUrl = "http://127.0.0.1:" + wireMock.port();
        return "<container version='1.0'>\n" +
               "  <config name='vespa.hosted.controller.statuspage.config.statuspage'>\n" +
               "    <apiUrl>" + statusPageApiUrl + "</apiUrl>\n" +
               "  </config>\n" +
               "  <component id='com.yahoo.vespa.hosted.controller.integration.SecretStoreMock'/>\n" +
               "  <handler id='com.yahoo.vespa.hosted.controller.restapi.statuspage.StatusPageProxyHandler'>\n" +
               "    <binding>http://*/statuspage/v1/*</binding>\n" +
               "  </handler>\n" +
               "  <http>\n" +
               "    <server id='default' port='8080'/>\n" +
               "  </http>\n" +
               "</container>";
    }

}