aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionContentHandlerTest.java
blob: 26a35b1e1956616eea84ed7aa1ab4095f380fe55 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// 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.google.common.io.Files;
import com.yahoo.config.model.application.provider.FilesApplicationPackage;
import com.yahoo.config.provision.TenantName;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.container.logging.AccessLog;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.http.HttpRequest;
import com.yahoo.text.Utf8;
import com.yahoo.vespa.config.server.ApplicationRepository;
import com.yahoo.vespa.config.server.http.ContentHandlerTestBase;
import com.yahoo.vespa.config.server.http.SessionHandlerTest;
import com.yahoo.vespa.curator.mock.MockCurator;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.time.Clock;
import java.util.concurrent.Executor;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

/**
 * @author lulf
 * @since 5.1
 */
public class SessionContentHandlerTest extends ContentHandlerTestBase {
    private static final TenantName tenant = TenantName.from("contenttest");
    private SessionContentHandler handler = null;
    
    @Before
    public void setupHandler() throws Exception {
        handler = createHandler();
        pathPrefix = "/application/v2/tenant/" + tenant + "/session/";
        baseUrl = "http://foo:1337/application/v2/tenant/" + tenant + "/session/1/content/";
    }

    @Test
    public void require_that_directories_can_be_created() throws IOException {
        assertMkdir("/bar/");
        assertMkdir("/bar/brask/");
        assertMkdir("/bar/brask/");
        assertMkdir("/bar/brask/bram/");
        assertMkdir("/brask/og/bram/");
    }// TODO: Enable when we have a predictable way of checking request body existence.

    @Test
    @Ignore
    public void require_that_mkdir_with_body_is_illegal() throws IOException {
        HttpResponse response = put("/foobio/", "foo");
        assertNotNull(response);
        assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST));
    }

    @Test
    public void require_that_nonexistant_session_returns_not_found() throws IOException {
        HttpResponse response = doRequest(HttpRequest.Method.GET, "/test.txt", 2l);
        assertNotNull(response);
        assertThat(response.getStatus(), is(Response.Status.NOT_FOUND));
    }

    protected HttpResponse put(String path, String content) {
        ByteArrayInputStream data = new ByteArrayInputStream(Utf8.toBytes(content));
        return doRequest(HttpRequest.Method.PUT, path, data);
    }

    @Test
    public void require_that_file_write_without_body_is_illegal() throws IOException {
        HttpResponse response = doRequest(HttpRequest.Method.PUT, "/foobio.txt");
        assertNotNull(response);
        assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST));
    }

    @Test
    public void require_that_files_can_be_written() throws IOException {
        assertWriteFile("/foo/minfil.txt", "Mycontent");
        assertWriteFile("/foo/minfil.txt", "Differentcontent");
    }

    @Test
    public void require_that_nonexistant_file_returs_not_found_when_deleted() throws IOException {
        assertDeleteFile(Response.Status.NOT_FOUND, "/test2.txt", "{\"error-code\":\"NOT_FOUND\",\"message\":\"Session 1 does not contain a file 'test2.txt'\"}");
    }

    @Test
    public void require_that_files_can_be_deleted() throws IOException {
        assertDeleteFile(Response.Status.OK, "/test.txt");
        assertDeleteFile(Response.Status.NOT_FOUND, "/test.txt", "{\"error-code\":\"NOT_FOUND\",\"message\":\"Session 1 does not contain a file 'test.txt'\"}");
        assertDeleteFile(Response.Status.BAD_REQUEST, "/newtest", "{\"error-code\":\"BAD_REQUEST\",\"message\":\"File 'newtest' is not an empty directory\"}");
        assertDeleteFile(Response.Status.OK, "/newtest/testfile.txt");
        assertDeleteFile(Response.Status.OK, "/newtest");
    }

    @Test
    public void require_that_status_is_given_for_new_files() throws IOException {
        assertStatus("/test.txt?return=status",
                     "{\"status\":\"new\",\"md5\":\"d3b07384d113edec49eaa6238ad5ff00\",\"name\":\"http://foo:1337" + pathPrefix + "1/content/test.txt\"}");
        assertWriteFile("/test.txt", "Mycontent");
        assertStatus("/test.txt?return=status",
                     "{\"status\":\"changed\",\"md5\":\"01eabd73c69d78d0009ec93cd62d7f77\",\"name\":\"http://foo:1337" + pathPrefix + "1/content/test.txt\"}");
    }

    private void assertWriteFile(String path, String content) throws IOException {
        HttpResponse response = put(path, content);
        assertNotNull(response);
        assertThat(response.getStatus(), is(Response.Status.OK));
        assertContent(path, content);
        assertThat(SessionHandlerTest.getRenderedString(response),
                   is("{\"prepared\":\"http://foo:1337" + pathPrefix + "1/prepared\"}"));
    }

    private void assertDeleteFile(int statusCode, String filePath) throws IOException {
        assertDeleteFile(statusCode, filePath, "{\"prepared\":\"http://foo:1337" + pathPrefix + "1/prepared\"}");
    }

    private void assertDeleteFile(int statusCode, String filePath, String expectedResponse) throws IOException {
        HttpResponse response = doRequest(HttpRequest.Method.DELETE, filePath);
        assertNotNull(response);
        assertThat(response.getStatus(), is(statusCode));
        assertThat(SessionHandlerTest.getRenderedString(response), is(expectedResponse));
    }

    private void assertMkdir(String path) throws IOException {
        HttpResponse response = doRequest(HttpRequest.Method.PUT, path);
        assertNotNull(response);
        assertThat(response.getStatus(), is(Response.Status.OK));
        assertThat(SessionHandlerTest.getRenderedString(response),
                   is("{\"prepared\":\"http://foo:1337" + pathPrefix + "1/prepared\"}"));
    }

    protected File createTestApp() throws IOException {
        File testApp = Files.createTempDir();
        FileUtils.copyDirectory(new File("src/test/apps/content"), testApp);
        return testApp;
    }

    protected HttpResponse doRequest(HttpRequest.Method method, String path) {
        return doRequest(method, path, 1l);
    }

    protected HttpResponse doRequest(HttpRequest.Method method, String path, long sessionId) {
        return handler.handle(SessionHandlerTest.createTestRequest(pathPrefix, method, Cmd.CONTENT, sessionId, path));
    }

    protected HttpResponse doRequest(HttpRequest.Method method, String path, InputStream data) {
        return doRequest(method, path, 1l, data);
    }

    protected HttpResponse doRequest(HttpRequest.Method method, String path, long sessionId, InputStream data) {
        return handler.handle(SessionHandlerTest.createTestRequest(pathPrefix, method, Cmd.CONTENT, sessionId, path, data));
    }

    private SessionContentHandler createHandler() throws Exception {
        TestTenantBuilder testTenantBuilder = new TestTenantBuilder();
        testTenantBuilder.createTenant(tenant).getLocalSessionRepo().addSession(new MockSession(1l, FilesApplicationPackage.fromFile(createTestApp())));
        return new SessionContentHandler(new Executor() {
            @SuppressWarnings("NullableProblems")
            @Override
            public void execute(Runnable command) {
                command.run();
            }
        }, AccessLog.voidAccessLog(), testTenantBuilder.createTenants(),
                                         new ApplicationRepository(testTenantBuilder.createTenants(),
                                                                   new SessionHandlerTest.MockProvisioner(),
                                                                   new MockCurator(),
                                                                   Clock.systemUTC()));
    }
}