blob: 28e899ea9fd5848e6316c9a4cad8ed0f38d8e78a [file] [log] [blame]
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +03001/*-
2 * ============LICENSE_START=======================================================
3 * OSAM
4 * ================================================================================
5 * Copyright (C) 2018 AT&T
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20
21
22
23package org.onap.osam.client;
24
25import com.fasterxml.jackson.core.JsonProcessingException;
26import com.fasterxml.jackson.databind.ObjectMapper;
27import com.google.common.collect.ImmutableMap;
28import com.xebialabs.restito.semantics.Condition;
29import com.xebialabs.restito.server.StubServer;
30import io.joshworks.restclient.http.HttpResponse;
31import io.joshworks.restclient.http.JsonNode;
32import org.glassfish.grizzly.http.util.HttpStatus;
33import com.xebialabs.restito.semantics.Action;
34import org.testng.annotations.BeforeMethod;
35import org.testng.annotations.AfterMethod;
36import org.glassfish.grizzly.http.Method;
37import org.testng.annotations.Test;
38
39import java.util.Collections;
40import java.util.Map;
41
42import static com.xebialabs.restito.builder.verify.VerifyHttp.verifyHttp;
43import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp;
44import static com.xebialabs.restito.semantics.Action.stringContent;
45import static com.xebialabs.restito.semantics.Action.contentType;
46import static com.xebialabs.restito.semantics.Action.status;
47import static com.xebialabs.restito.semantics.Action.ok;
48import static org.testng.Assert.assertEquals;
49
50public class SyncRestClientForHttpServerTest {
51
52 private static final SyncRestClientModel.TestModel testObject = new SyncRestClientModel.TestModel(1, "test");
53 private static final String NOT_EXISTING_OBJECT = "NOT EXISTING OBJECT";
54
55 private StubServer stubServer;
56 private ObjectMapper objectMapper = new ObjectMapper();
57 private SyncRestClient syncRestClient;
58
59 @BeforeMethod
60 public void setUp() {
61 stubServer = new StubServer();
62 stubServer.run();
63 syncRestClient = new SyncRestClient();
64 }
65
66 @AfterMethod
67 public void tearDown() {
68 stubServer.stop();
69 syncRestClient.destroy();
70 }
71
72 @Test
73 public void testJsonResponseFromGet() throws JsonProcessingException {
74 // given
75 stubGetCall();
76 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
77 // when
78 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient
79 .get(url, Collections.emptyMap(), Collections.emptyMap());
80 // then
81 verifyHttp(stubServer).once(Condition.method(Method.GET), Condition.url(url));
82 assertEquals(jsonNodeHttpResponse.getStatus(), 200);
83 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1);
84 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test");
85 }
86
87 @Test
88 public void testObjectResponseFromGet() throws JsonProcessingException {
89 // given
90 stubGetCall();
91 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
92 // when
93 HttpResponse<SyncRestClientModel.TestModel> testModelHttpResponse = syncRestClient
94 .get(url, Collections.emptyMap(), Collections.emptyMap(), SyncRestClientModel.TestModel.class);
95 // then
96 verifyHttp(stubServer).once(Condition.method(Method.GET), Condition.url(url));
97 assertEquals(testModelHttpResponse.getStatus(), 200);
98 assertEquals(testModelHttpResponse.getBody().getKey(), 1);
99 assertEquals(testModelHttpResponse.getBody().getValue(), "test");
100 }
101
102 @Test
103 public void testJsonResponseFromPost() throws JsonProcessingException {
104 // given
105 stubPostCall();
106 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
107 // when
108 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.post(url, Collections.emptyMap(), testObject);
109 // then
110 verifyHttp(stubServer).once(Condition.method(Method.POST), Condition.url(url));
111 assertEquals(jsonNodeHttpResponse.getStatus(), 200);
112 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1);
113 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test");
114 }
115
116 @Test
117 public void test404JsonResponseFromPost() throws JsonProcessingException {
118 // given
119 stubPostCall();
120 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
121 // when
122 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient
123 .post(url, Collections.emptyMap(), NOT_EXISTING_OBJECT);
124 // then
125 assertEquals(jsonNodeHttpResponse.getStatus(), 404);
126 assertEquals(jsonNodeHttpResponse.getStatusText(), "Not Found");
127 }
128
129 @Test
130 public void testHeadersWerePassedToPost() throws JsonProcessingException {
131 // given
132 stubPostCall();
133 Map headers = ImmutableMap.<String, String>builder().put("Authorization", "Basic anyHash").build();
134 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
135 // when
136 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.post(url, headers, testObject);
137 // then
138 verifyHttp(stubServer).once(Condition.withHeader("Authorization"));
139 assertEquals(jsonNodeHttpResponse.getStatus(), 200);
140 }
141
142 @Test(expectedExceptions = {Exception.class})
143 public void testFailedJsonResponseFromPost() throws JsonProcessingException {
144 // given
145 stubPostCall();
146 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
147 // when
148 stubServer.stop();
149 syncRestClient.post(url, Collections.emptyMap(), testObject);
150 }
151
152 @Test
153 public void testObjectResponseFromPost() throws JsonProcessingException {
154 // given
155 stubPostCall();
156 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
157 // when
158 HttpResponse<SyncRestClientModel.TestModel> objectHttpResponse = syncRestClient
159 .post(url, Collections.emptyMap(), testObject, SyncRestClientModel.TestModel.class);
160 // then
161 verifyHttp(stubServer).once(Condition.method(Method.POST), Condition.url(url));
162 assertEquals(objectHttpResponse.getStatus(), 200);
163 assertEquals(objectHttpResponse.getBody().getKey(), 1);
164 assertEquals(objectHttpResponse.getBody().getValue(), "test");
165 }
166
167 @Test
168 public void testJsonResponseFromPut() throws JsonProcessingException {
169 // given
170 stubPutCall();
171 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
172 // when
173 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.put(url, Collections.emptyMap(), testObject);
174 // then
175 verifyHttp(stubServer).once(Condition.method(Method.PUT), Condition.url(url));
176 assertEquals(jsonNodeHttpResponse.getStatus(), 201);
177 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1);
178 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test");
179 }
180
181 @Test
182 public void testObjectResponseFromPut() throws JsonProcessingException {
183 // given
184 stubPutCall();
185 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
186 // when
187 HttpResponse<SyncRestClientModel.TestModel> modelHttpResponse = syncRestClient
188 .put(url, Collections.emptyMap(), testObject, SyncRestClientModel.TestModel.class);
189 // then
190 verifyHttp(stubServer).once(Condition.method(Method.PUT), Condition.url(url));
191 assertEquals(modelHttpResponse.getStatus(), 201);
192 assertEquals(modelHttpResponse.getBody().getKey(), 1);
193 assertEquals(modelHttpResponse.getBody().getValue(), "test");
194 }
195
196 @Test
197 public void testJsonResponseFromDelete() throws JsonProcessingException {
198 // given
199 stubDeleteCall();
200 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
201 // when
202 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.delete(url, Collections.emptyMap());
203 // then
204 verifyHttp(stubServer).once(Condition.method(Method.DELETE), Condition.url(url));
205 assertEquals(jsonNodeHttpResponse.getStatus(), 200);
206 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1);
207 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test");
208 }
209
210 @Test
211 public void testObjectResponseFromDelete() throws JsonProcessingException {
212 // given
213 stubDeleteCall();
214 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
215 // when
216 HttpResponse<SyncRestClientModel.TestModel> modelHttpResponse = syncRestClient
217 .delete(url, Collections.emptyMap(), SyncRestClientModel.TestModel.class);
218 // then
219 verifyHttp(stubServer).once(Condition.method(Method.DELETE), Condition.url(url));
220 assertEquals(modelHttpResponse.getStatus(), 200);
221 assertEquals(modelHttpResponse.getBody().getKey(), 1);
222 assertEquals(modelHttpResponse.getBody().getValue(), "test");
223 }
224
225 @Test
226 public void testRedirectToHttp() throws JsonProcessingException {
227 // given
228 stubGetCall();
229 String secured_url = "https://0.0.0.0:" + stubServer.getPort() + "/test";
230 String available_url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
231 // when
232 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient
233 .get(secured_url, Collections.emptyMap(), Collections.emptyMap());
234 // then
235 verifyHttp(stubServer).once(Condition.method(Method.GET), Condition.url(available_url),
236 Condition.not(Condition.url(secured_url)));
237 assertEquals(jsonNodeHttpResponse.getStatus(), 200);
238 }
239
240 private void stubGetCall() throws JsonProcessingException {
241 whenHttp(stubServer)
242 .match(Condition.get("/test"))
243 .then(ok(), jsonContent(), contentType("application/json"));
244 }
245
246 private void stubDeleteCall() throws JsonProcessingException {
247 whenHttp(stubServer)
248 .match(Condition.delete("/test"))
249 .then(ok(), jsonContent(), contentType("application/json"));
250 }
251
252 private void stubPostCall() throws JsonProcessingException {
253 whenHttp(stubServer)
254 .match(Condition.post("/test"),
255 Condition.withPostBodyContaining(objectMapper.writeValueAsString(testObject)))
256 .then(ok(), jsonContent(), contentType("application/json"));
257 }
258
259 private void stubPutCall() throws JsonProcessingException {
260 whenHttp(stubServer)
261 .match(Condition.put("/test"),
262 Condition.withPostBodyContaining(objectMapper.writeValueAsString(testObject)))
263 .then(status(HttpStatus.CREATED_201), jsonContent(), contentType("application/json"));
264 }
265
266 private Action jsonContent() throws JsonProcessingException {
267 return stringContent(objectMapper.writeValueAsString(testObject));
268 }
269
270}