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 static com.xebialabs.restito.builder.stub.StubHttp.whenHttp;
24 import static com.xebialabs.restito.builder.verify.VerifyHttp.verifyHttp;
25 import static com.xebialabs.restito.semantics.Action.contentType;
26 import static com.xebialabs.restito.semantics.Action.ok;
27 import static com.xebialabs.restito.semantics.Action.status;
28 import static com.xebialabs.restito.semantics.Action.stringContent;
29 import static org.mockito.Mockito.mock;
30 import static org.testng.Assert.assertEquals;
32 import com.fasterxml.jackson.core.JsonProcessingException;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34 import com.google.common.collect.ImmutableMap;
35 import com.xebialabs.restito.semantics.Action;
36 import com.xebialabs.restito.semantics.Condition;
37 import com.xebialabs.restito.server.StubServer;
38 import io.joshworks.restclient.http.HttpResponse;
39 import io.joshworks.restclient.http.JsonNode;
40 import java.util.Collections;
42 import org.glassfish.grizzly.http.Method;
43 import org.glassfish.grizzly.http.util.HttpStatus;
44 import org.onap.vid.utils.Logging;
45 import org.testng.annotations.AfterMethod;
46 import org.testng.annotations.BeforeMethod;
47 import org.testng.annotations.Test;
49 public class SyncRestClientForHttpServerTest {
51 private static final SyncRestClientModel.TestModel testObject = new SyncRestClientModel.TestModel(1, "test");
52 private static final String NOT_EXISTING_OBJECT = "NOT EXISTING OBJECT";
54 private StubServer stubServer;
55 private ObjectMapper objectMapper = new ObjectMapper();
56 private SyncRestClient syncRestClient;
57 private Logging mockLoggingService;
61 stubServer = new StubServer();
63 mockLoggingService = mock(Logging.class);
64 syncRestClient = new SyncRestClient(mockLoggingService);
68 public void tearDown() {
70 syncRestClient.destroy();
74 public void testJsonResponseFromGet() throws JsonProcessingException {
77 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
79 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient
80 .get(url, Collections.emptyMap(), Collections.emptyMap());
82 verifyHttp(stubServer).once(Condition.method(Method.GET), Condition.url(url));
83 assertEquals(jsonNodeHttpResponse.getStatus(), 200);
84 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1);
85 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test");
89 public void testObjectResponseFromGet() throws JsonProcessingException {
92 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
94 HttpResponse<SyncRestClientModel.TestModel> testModelHttpResponse = syncRestClient
95 .get(url, Collections.emptyMap(), Collections.emptyMap(), SyncRestClientModel.TestModel.class);
97 verifyHttp(stubServer).once(Condition.method(Method.GET), Condition.url(url));
98 assertEquals(testModelHttpResponse.getStatus(), 200);
99 assertEquals(testModelHttpResponse.getBody().getKey(), 1);
100 assertEquals(testModelHttpResponse.getBody().getValue(), "test");
104 public void testJsonResponseFromPost() throws JsonProcessingException {
107 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
109 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.post(url, Collections.emptyMap(), testObject);
111 verifyHttp(stubServer).once(Condition.method(Method.POST), Condition.url(url));
112 assertEquals(jsonNodeHttpResponse.getStatus(), 200);
113 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1);
114 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test");
118 public void test404JsonResponseFromPost() throws JsonProcessingException {
121 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
123 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient
124 .post(url, Collections.emptyMap(), NOT_EXISTING_OBJECT);
126 assertEquals(jsonNodeHttpResponse.getStatus(), 404);
127 assertEquals(jsonNodeHttpResponse.getStatusText(), "Not Found");
131 public void testHeadersWerePassedToPost() throws JsonProcessingException {
134 Map headers = ImmutableMap.<String, String>builder().put("Authorization", "Basic anyHash").build();
135 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
137 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.post(url, headers, testObject);
139 verifyHttp(stubServer).once(Condition.withHeader("Authorization"));
140 assertEquals(jsonNodeHttpResponse.getStatus(), 200);
143 @Test(expectedExceptions = {Exception.class})
144 public void testFailedJsonResponseFromPost() throws JsonProcessingException {
147 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
150 syncRestClient.post(url, Collections.emptyMap(), testObject);
154 public void testObjectResponseFromPost() throws JsonProcessingException {
157 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
159 HttpResponse<SyncRestClientModel.TestModel> objectHttpResponse = syncRestClient
160 .post(url, Collections.emptyMap(), testObject, SyncRestClientModel.TestModel.class);
162 verifyHttp(stubServer).once(Condition.method(Method.POST), Condition.url(url));
163 assertEquals(objectHttpResponse.getStatus(), 200);
164 assertEquals(objectHttpResponse.getBody().getKey(), 1);
165 assertEquals(objectHttpResponse.getBody().getValue(), "test");
169 public void testJsonResponseFromPut() throws JsonProcessingException {
172 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
174 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.put(url, Collections.emptyMap(), testObject);
176 verifyHttp(stubServer).once(Condition.method(Method.PUT), Condition.url(url));
177 assertEquals(jsonNodeHttpResponse.getStatus(), 201);
178 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1);
179 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test");
183 public void testObjectResponseFromPut() throws JsonProcessingException {
186 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
188 HttpResponse<SyncRestClientModel.TestModel> modelHttpResponse = syncRestClient
189 .put(url, Collections.emptyMap(), testObject, SyncRestClientModel.TestModel.class);
191 verifyHttp(stubServer).once(Condition.method(Method.PUT), Condition.url(url));
192 assertEquals(modelHttpResponse.getStatus(), 201);
193 assertEquals(modelHttpResponse.getBody().getKey(), 1);
194 assertEquals(modelHttpResponse.getBody().getValue(), "test");
198 public void testJsonResponseFromDelete() throws JsonProcessingException {
201 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
203 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.delete(url, Collections.emptyMap());
205 verifyHttp(stubServer).once(Condition.method(Method.DELETE), Condition.url(url));
206 assertEquals(jsonNodeHttpResponse.getStatus(), 200);
207 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1);
208 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test");
212 public void testObjectResponseFromDelete() throws JsonProcessingException {
215 String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
217 HttpResponse<SyncRestClientModel.TestModel> modelHttpResponse = syncRestClient
218 .delete(url, Collections.emptyMap(), SyncRestClientModel.TestModel.class);
220 verifyHttp(stubServer).once(Condition.method(Method.DELETE), Condition.url(url));
221 assertEquals(modelHttpResponse.getStatus(), 200);
222 assertEquals(modelHttpResponse.getBody().getKey(), 1);
223 assertEquals(modelHttpResponse.getBody().getValue(), "test");
227 public void testRedirectToHttp() throws JsonProcessingException {
230 String secured_url = "https://0.0.0.0:" + stubServer.getPort() + "/test";
231 String available_url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
233 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient
234 .get(secured_url, Collections.emptyMap(), Collections.emptyMap());
236 verifyHttp(stubServer).once(Condition.method(Method.GET), Condition.url(available_url),
237 Condition.not(Condition.url(secured_url)));
238 assertEquals(jsonNodeHttpResponse.getStatus(), 200);
241 private void stubGetCall() throws JsonProcessingException {
243 .match(Condition.get("/test"))
244 .then(ok(), jsonContent(), contentType("application/json"));
247 private void stubDeleteCall() throws JsonProcessingException {
249 .match(Condition.delete("/test"))
250 .then(ok(), jsonContent(), contentType("application/json"));
253 private void stubPostCall() throws JsonProcessingException {
255 .match(Condition.post("/test"),
256 Condition.withPostBodyContaining(objectMapper.writeValueAsString(testObject)))
257 .then(ok(), jsonContent(), contentType("application/json"));
260 private void stubPutCall() throws JsonProcessingException {
262 .match(Condition.put("/test"),
263 Condition.withPostBodyContaining(objectMapper.writeValueAsString(testObject)))
264 .then(status(HttpStatus.CREATED_201), jsonContent(), contentType("application/json"));
267 private Action jsonContent() throws JsonProcessingException {
268 return stringContent(objectMapper.writeValueAsString(testObject));