2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 - 2019 Nokia. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.client;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.google.common.collect.ImmutableMap;
26 import com.xebialabs.restito.semantics.Condition;
27 import com.xebialabs.restito.server.StubServer;
28 import io.joshworks.restclient.http.HttpResponse;
29 import io.joshworks.restclient.http.JsonNode;
30 import org.glassfish.grizzly.http.util.HttpStatus;
31 import com.xebialabs.restito.semantics.Action;
32 import org.testng.annotations.BeforeMethod;
33 import org.testng.annotations.AfterMethod;
34 import org.glassfish.grizzly.http.Method;
35 import org.testng.annotations.Test;
37 import java.util.Collections;
40 import static com.xebialabs.restito.builder.verify.VerifyHttp.verifyHttp;
41 import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp;
42 import static com.xebialabs.restito.semantics.Action.stringContent;
43 import static com.xebialabs.restito.semantics.Action.contentType;
44 import static com.xebialabs.restito.semantics.Action.status;
45 import static com.xebialabs.restito.semantics.Action.ok;
46 import static org.testng.Assert.assertEquals;
48 public class SyncRestClientForHttpServerTest {
50 private static final SyncRestClientModel.TestModel testObject = new SyncRestClientModel.TestModel(1, "test");
51 private static final String NOT_EXISTING_OBJECT = "NOT EXISTING OBJECT";
53 private StubServer stubServer;
54 private ObjectMapper objectMapper = new ObjectMapper();
55 private SyncRestClient syncRestClient;
59 stubServer = new StubServer();
61 syncRestClient = new SyncRestClient();
65 public void tearDown() {
67 syncRestClient.destroy();
71 public void testJsonResponseFromGet() throws JsonProcessingException {
74 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
76 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient
77 .get(url, Collections.emptyMap(), Collections.emptyMap());
79 verifyHttp(stubServer).once(Condition.method(Method.GET), Condition.url(url));
80 assertEquals(jsonNodeHttpResponse.getStatus(), 200);
81 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1);
82 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test");
86 public void testObjectResponseFromGet() throws JsonProcessingException {
89 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
91 HttpResponse<SyncRestClientModel.TestModel> testModelHttpResponse = syncRestClient
92 .get(url, Collections.emptyMap(), Collections.emptyMap(), SyncRestClientModel.TestModel.class);
94 verifyHttp(stubServer).once(Condition.method(Method.GET), Condition.url(url));
95 assertEquals(testModelHttpResponse.getStatus(), 200);
96 assertEquals(testModelHttpResponse.getBody().getKey(), 1);
97 assertEquals(testModelHttpResponse.getBody().getValue(), "test");
101 public void testJsonResponseFromPost() throws JsonProcessingException {
104 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
106 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.post(url, Collections.emptyMap(), testObject);
108 verifyHttp(stubServer).once(Condition.method(Method.POST), Condition.url(url));
109 assertEquals(jsonNodeHttpResponse.getStatus(), 200);
110 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1);
111 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test");
115 public void test404JsonResponseFromPost() throws JsonProcessingException {
118 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
120 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient
121 .post(url, Collections.emptyMap(), NOT_EXISTING_OBJECT);
123 assertEquals(jsonNodeHttpResponse.getStatus(), 404);
124 assertEquals(jsonNodeHttpResponse.getStatusText(), "Not Found");
128 public void testHeadersWerePassedToPost() throws JsonProcessingException {
131 Map headers = ImmutableMap.<String, String>builder().put("Authorization", "Basic anyHash").build();
132 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
134 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.post(url, headers, testObject);
136 verifyHttp(stubServer).once(Condition.withHeader("Authorization"));
137 assertEquals(jsonNodeHttpResponse.getStatus(), 200);
140 @Test(expectedExceptions = {Exception.class})
141 public void testFailedJsonResponseFromPost() throws JsonProcessingException {
144 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
147 syncRestClient.post(url, Collections.emptyMap(), testObject);
151 public void testObjectResponseFromPost() throws JsonProcessingException {
154 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
156 HttpResponse<SyncRestClientModel.TestModel> objectHttpResponse = syncRestClient
157 .post(url, Collections.emptyMap(), testObject, SyncRestClientModel.TestModel.class);
159 verifyHttp(stubServer).once(Condition.method(Method.POST), Condition.url(url));
160 assertEquals(objectHttpResponse.getStatus(), 200);
161 assertEquals(objectHttpResponse.getBody().getKey(), 1);
162 assertEquals(objectHttpResponse.getBody().getValue(), "test");
166 public void testJsonResponseFromPut() throws JsonProcessingException {
169 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
171 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.put(url, Collections.emptyMap(), testObject);
173 verifyHttp(stubServer).once(Condition.method(Method.PUT), Condition.url(url));
174 assertEquals(jsonNodeHttpResponse.getStatus(), 201);
175 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1);
176 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test");
180 public void testObjectResponseFromPut() throws JsonProcessingException {
183 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
185 HttpResponse<SyncRestClientModel.TestModel> modelHttpResponse = syncRestClient
186 .put(url, Collections.emptyMap(), testObject, SyncRestClientModel.TestModel.class);
188 verifyHttp(stubServer).once(Condition.method(Method.PUT), Condition.url(url));
189 assertEquals(modelHttpResponse.getStatus(), 201);
190 assertEquals(modelHttpResponse.getBody().getKey(), 1);
191 assertEquals(modelHttpResponse.getBody().getValue(), "test");
195 public void testJsonResponseFromDelete() throws JsonProcessingException {
198 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
200 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.delete(url, Collections.emptyMap());
202 verifyHttp(stubServer).once(Condition.method(Method.DELETE), Condition.url(url));
203 assertEquals(jsonNodeHttpResponse.getStatus(), 200);
204 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1);
205 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test");
209 public void testObjectResponseFromDelete() throws JsonProcessingException {
212 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
214 HttpResponse<SyncRestClientModel.TestModel> modelHttpResponse = syncRestClient
215 .delete(url, Collections.emptyMap(), SyncRestClientModel.TestModel.class);
217 verifyHttp(stubServer).once(Condition.method(Method.DELETE), Condition.url(url));
218 assertEquals(modelHttpResponse.getStatus(), 200);
219 assertEquals(modelHttpResponse.getBody().getKey(), 1);
220 assertEquals(modelHttpResponse.getBody().getValue(), "test");
224 public void testRedirectToHttp() throws JsonProcessingException {
227 String secured_url = "https://0.0.0.0:" + stubServer.getPort() + "/test";
228 String available_url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
230 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient
231 .get(secured_url, Collections.emptyMap(), Collections.emptyMap());
233 verifyHttp(stubServer).once(Condition.method(Method.GET), Condition.url(available_url),
234 Condition.not(Condition.url(secured_url)));
235 assertEquals(jsonNodeHttpResponse.getStatus(), 200);
238 private void stubGetCall() throws JsonProcessingException {
240 .match(Condition.get("/test"))
241 .then(ok(), jsonContent(), contentType("application/json"));
244 private void stubDeleteCall() throws JsonProcessingException {
246 .match(Condition.delete("/test"))
247 .then(ok(), jsonContent(), contentType("application/json"));
250 private void stubPostCall() throws JsonProcessingException {
252 .match(Condition.post("/test"),
253 Condition.withPostBodyContaining(objectMapper.writeValueAsString(testObject)))
254 .then(ok(), jsonContent(), contentType("application/json"));
257 private void stubPutCall() throws JsonProcessingException {
259 .match(Condition.put("/test"),
260 Condition.withPostBodyContaining(objectMapper.writeValueAsString(testObject)))
261 .then(status(HttpStatus.CREATED_201), jsonContent(), contentType("application/json"));
264 private Action jsonContent() throws JsonProcessingException {
265 return stringContent(objectMapper.writeValueAsString(testObject));