/
UploadStepdefs.java
154 lines (133 loc) · 7.1 KB
/
UploadStepdefs.java
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
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
package org.apache.james.jmap.methods.integration.cucumber;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.BufferedInputStream;
import java.net.URI;
import java.net.URISyntaxException;
import javax.inject.Inject;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Executor;
import org.apache.http.client.fluent.Request;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.james.GuiceJamesServerImpl;
import org.apache.james.JmapServer;
import org.apache.james.WebAdminServer;
import org.apache.james.jmap.api.access.AccessToken;
import org.apache.james.util.ZeroedInputStream;
import com.google.common.base.Charsets;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import cucumber.runtime.java.guice.ScenarioScoped;
@ScenarioScoped
public class UploadStepdefs <T extends GuiceJamesServerImpl & JmapServer & WebAdminServer>{
private static final String _1M_ZEROED_FILE_BLOB_ID = "3b71f43ff30f4b15b5cd85dd9e95ebc7e84eb5a3";
private static final int _1M = 1024 * 1024;
private static final int _10M = 10 * _1M;
private final UserStepdefs<T> userStepdefs;
private final MainStepdefs<T> mainStepdefs;
private final URI uploadUri;
private HttpResponse response;
@Inject
private UploadStepdefs(UserStepdefs<T> userStepdefs, MainStepdefs<T> mainStepdefs) throws URISyntaxException {
this.userStepdefs = userStepdefs;
this.mainStepdefs = mainStepdefs;
uploadUri = mainStepdefs.baseUri().setPath("/upload").build();
}
@When("^\"([^\"]*)\" upload a content$")
public void userUploadContent(String username) throws Throwable {
AccessToken accessToken = userStepdefs.tokenByUser.get(username);
Request request = Request.Post(uploadUri)
.bodyStream(new BufferedInputStream(new ZeroedInputStream(_1M), _1M), org.apache.http.entity.ContentType.DEFAULT_BINARY);
if (accessToken != null) {
request.addHeader("Authorization", accessToken.serialize());
}
response = Executor.newInstance(HttpClientBuilder.create().disableAutomaticRetries().build()).execute(request).returnResponse();
}
@When("^\"([^\"]*)\" upload a content without content type$")
public void userUploadContentWithoutContentType(String username) throws Throwable {
AccessToken accessToken = userStepdefs.tokenByUser.get(username);
Request request = Request.Post(uploadUri)
.bodyByteArray("some text".getBytes(Charsets.UTF_8));
if (accessToken != null) {
request.addHeader("Authorization", accessToken.serialize());
}
response = request.execute().returnResponse();
}
@When("^\"([^\"]*)\" upload a too big content$")
public void userUploadTooBigContent(String username) throws Throwable {
AccessToken accessToken = userStepdefs.tokenByUser.get(username);
Request request = Request.Post(uploadUri)
.bodyStream(new BufferedInputStream(new ZeroedInputStream(_10M), _10M), org.apache.http.entity.ContentType.DEFAULT_BINARY);
if (accessToken != null) {
request.addHeader("Authorization", accessToken.serialize());
}
response = request.execute().returnResponse();
}
@When("^\"([^\"]*)\" checks for the availability of the upload endpoint$")
public void optionUpload(String username) throws Throwable {
AccessToken accessToken = userStepdefs.tokenByUser.get(username);
Request request = Request.Options(uploadUri);
if (accessToken != null) {
request.addHeader("Authorization", accessToken.serialize());
}
response = request.execute().returnResponse();
}
@Then("^the user should receive an authorized response$")
public void httpAuthorizedStatus() throws Exception {
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
}
@Then("^the user should receive a created response$")
public void httpCreatedStatus() throws Exception {
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(201);
}
@Then("^the user should receive bad request response$")
public void httpBadRequestStatus() throws Throwable {
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(400);
}
@Then("^the user should receive a not authorized response$")
public void httpUnauthorizedStatus() throws Exception {
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(401);
}
@Then("^the user should receive a request entity too large response$")
public void httpRequestEntityTooBigStatus() throws Exception {
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(413);
}
@Then("^the user should receive a specified JSON content$")
public void jsonResponse() throws Exception {
assertThat(response.getHeaders("Content-Type")).extracting(Header::getValue).containsExactly(org.apache.http.entity.ContentType.APPLICATION_JSON.toString());
DocumentContext jsonPath = JsonPath.parse(response.getEntity().getContent());
assertThat(jsonPath.<String>read("blobId")).isEqualTo(_1M_ZEROED_FILE_BLOB_ID);
assertThat(jsonPath.<String>read("type")).isEqualTo("application/octet-stream");
assertThat(jsonPath.<Integer>read("size")).isEqualTo(_1M);
}
@Then("^\"([^\"]*)\" should be able to retrieve the content$")
public void contentShouldBeRetrievable(String username) throws Exception {
AccessToken accessToken = userStepdefs.tokenByUser.get(username);
Request request = Request.Get(mainStepdefs.baseUri().setPath("/download/" + _1M_ZEROED_FILE_BLOB_ID).build());
if (accessToken != null) {
request.addHeader("Authorization", accessToken.serialize());
}
response = request.execute().returnResponse();
httpAuthorizedStatus();
}
}