summaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationContentHandlerTest.java
blob: 0db6aea312dab25af8f81b369ad00cc21b1f1351 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.http.v2;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.TenantName;
import com.yahoo.config.provision.Zone;
import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.jdisc.Response;
import com.yahoo.vespa.config.server.ApplicationRepository;
import com.yahoo.vespa.config.server.TestComponentRegistry;
import com.yahoo.vespa.config.server.application.OrchestratorMock;
import com.yahoo.vespa.config.server.http.ContentHandlerTestBase;
import com.yahoo.vespa.config.server.session.LocalSession;
import com.yahoo.vespa.config.server.session.PrepareParams;
import com.yahoo.vespa.config.server.session.Session;
import com.yahoo.vespa.config.server.tenant.Tenant;
import com.yahoo.vespa.config.server.tenant.TenantRepository;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.time.Clock;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

/**
 * @author Ulf Lilleengen
 */
public class ApplicationContentHandlerTest extends ContentHandlerTestBase {

    private static final File testApp = new File("src/test/apps/content");
    private static final File testApp2 = new File("src/test/apps/content2");

    private final TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder().build();
    private final Clock clock = componentRegistry.getClock();

    private final TenantName tenantName1 = TenantName.from("mofet");
    private final TenantName tenantName2 = TenantName.from("bla");
    private final String baseServer = "http://foo:1337";

    private final ApplicationId appId1 = new ApplicationId.Builder().tenant(tenantName1).applicationName("foo").instanceName("quux").build();
    private final ApplicationId appId2 = new ApplicationId.Builder().tenant(tenantName2).applicationName("foo").instanceName("quux").build();

    private ApplicationRepository applicationRepository;
    private ApplicationHandler handler;

    @Before
    public void setupHandler() {
        TenantRepository tenantRepository = new TenantRepository(componentRegistry, false);
        tenantRepository.addTenant(tenantName1);
        tenantRepository.addTenant(tenantName2);

        applicationRepository = new ApplicationRepository(tenantRepository,
                                                          new MockProvisioner(),
                                                          new OrchestratorMock(),
                                                          clock);

        applicationRepository.deploy(testApp, prepareParams(appId1));
        applicationRepository.deploy(testApp2, prepareParams(appId2));

        handler = new ApplicationHandler(ApplicationHandler.testOnlyContext(),
                                         Zone.defaultZone(),
                                         applicationRepository);
        pathPrefix = createPath(appId1, Zone.defaultZone());
        baseUrl = baseServer + pathPrefix;
    }

    private String createPath(ApplicationId applicationId, Zone zone) {
        return "/application/v2/tenant/"
             + applicationId.tenant().value()
             + "/application/"
             + applicationId.application().value()
             + "/environment/"
             + zone.environment().value()
             + "/region/"
             + zone.region().value()
             + "/instance/"
             + applicationId.instance().value()
             + "/content/";
    }

    @Test
    public void require_that_nonexistant_application_returns_not_found() {
        assertNotFound(HttpRequest.createTestRequest(baseServer + createPath(new ApplicationId.Builder()
                                                                             .tenant("tenant")
                                                                             .applicationName("notexist").instanceName("baz").build(), Zone.defaultZone()),
                                                     com.yahoo.jdisc.http.HttpRequest.Method.GET));
        assertNotFound(HttpRequest.createTestRequest(baseServer + createPath(new ApplicationId.Builder()
                                                                             .tenant("unknown")
                                                                             .applicationName("notexist").instanceName("baz").build(), Zone.defaultZone()),
                                                     com.yahoo.jdisc.http.HttpRequest.Method.GET));
    }

    @Test
    public void require_that_multiple_tenants_are_handled() throws IOException {
        assertContent("/test.txt", "foo\n");
        pathPrefix = createPath(appId2, Zone.defaultZone());
        baseUrl = baseServer + pathPrefix;
        assertContent("/test.txt", "bar\n");
    }

    @Test
    public void require_that_get_does_not_set_write_flag() throws IOException {
        Tenant tenant1 = applicationRepository.getTenant(appId1);
        LocalSession session = applicationRepository.getActiveLocalSession(tenant1, appId1);
        assertContent("/test.txt", "foo\n");
        assertThat(session.getStatus(), is(Session.Status.ACTIVATE));
    }

    private void assertNotFound(HttpRequest request) {
        HttpResponse response = handler.handle(request);
        assertNotNull(response);
        assertThat(response.getStatus(), is(Response.Status.NOT_FOUND));
    }

    @Override
    protected HttpResponse doRequest(com.yahoo.jdisc.http.HttpRequest.Method method, String path) {
        HttpRequest request = HttpRequest.createTestRequest(baseUrl + path, method);
        return handler.handle(request);
    }

    private PrepareParams prepareParams(ApplicationId applicationId) {
        return new PrepareParams.Builder().applicationId(applicationId).build();
    }

}