18b4089b58f8fac7a6961903456bf28723de8186
[vid.git] / vid-app-common / src / test / java / org / onap / vid / client / SyncRestClientForHttpServerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
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 package org.onap.vid.client;
22
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;
36
37 import java.util.Collections;
38 import java.util.Map;
39
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;
47
48 public class SyncRestClientForHttpServerTest {
49
50     private static final SyncRestClientModel.TestModel testObject = new SyncRestClientModel.TestModel(1, "test");
51     private static final String NOT_EXISTING_OBJECT = "NOT EXISTING OBJECT";
52
53     private StubServer stubServer;
54     private ObjectMapper objectMapper = new ObjectMapper();
55     private SyncRestClient syncRestClient;
56
57     @BeforeMethod
58     public void setUp() {
59         stubServer = new StubServer();
60         stubServer.run();
61         syncRestClient = new SyncRestClient();
62     }
63
64     @AfterMethod
65     public void tearDown() {
66         stubServer.stop();
67         syncRestClient.destroy();
68     }
69
70     @Test
71     public void testJsonResponseFromGet() throws JsonProcessingException {
72         // given
73         stubGetCall();
74         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
75         // when
76         HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient
77             .get(url, Collections.emptyMap(), Collections.emptyMap());
78         // then
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");
83     }
84
85     @Test
86     public void testObjectResponseFromGet() throws JsonProcessingException {
87         // given
88         stubGetCall();
89         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
90         // when
91         HttpResponse<SyncRestClientModel.TestModel> testModelHttpResponse = syncRestClient
92             .get(url, Collections.emptyMap(), Collections.emptyMap(), SyncRestClientModel.TestModel.class);
93         // then
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");
98     }
99
100     @Test
101     public void testJsonResponseFromPost() throws JsonProcessingException {
102         // given
103         stubPostCall();
104         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
105         // when
106         HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.post(url, Collections.emptyMap(), testObject);
107         // then
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");
112     }
113
114     @Test
115     public void test404JsonResponseFromPost() throws JsonProcessingException {
116         // given
117         stubPostCall();
118         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
119         // when
120         HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient
121             .post(url, Collections.emptyMap(), NOT_EXISTING_OBJECT);
122         // then
123         assertEquals(jsonNodeHttpResponse.getStatus(), 404);
124         assertEquals(jsonNodeHttpResponse.getStatusText(), "Not Found");
125     }
126
127     @Test
128     public void testHeadersWerePassedToPost() throws JsonProcessingException {
129         // given
130         stubPostCall();
131         Map headers = ImmutableMap.<String, String>builder().put("Authorization", "Basic anyHash").build();
132         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
133         // when
134         HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.post(url, headers, testObject);
135         // then
136         verifyHttp(stubServer).once(Condition.withHeader("Authorization"));
137         assertEquals(jsonNodeHttpResponse.getStatus(), 200);
138     }
139
140     @Test(expectedExceptions = {Exception.class})
141     public void testFailedJsonResponseFromPost() throws JsonProcessingException {
142         // given
143         stubPostCall();
144         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
145         // when
146         stubServer.stop();
147         syncRestClient.post(url, Collections.emptyMap(), testObject);
148     }
149
150     @Test
151     public void testObjectResponseFromPost() throws JsonProcessingException {
152         // given
153         stubPostCall();
154         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
155         // when
156         HttpResponse<SyncRestClientModel.TestModel> objectHttpResponse = syncRestClient
157             .post(url, Collections.emptyMap(), testObject, SyncRestClientModel.TestModel.class);
158         // then
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");
163     }
164
165     @Test
166     public void testJsonResponseFromPut() throws JsonProcessingException {
167         // given
168         stubPutCall();
169         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
170         // when
171         HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.put(url, Collections.emptyMap(), testObject);
172         // then
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");
177     }
178
179     @Test
180     public void testObjectResponseFromPut() throws JsonProcessingException {
181         // given
182         stubPutCall();
183         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
184         // when
185         HttpResponse<SyncRestClientModel.TestModel> modelHttpResponse = syncRestClient
186             .put(url, Collections.emptyMap(), testObject, SyncRestClientModel.TestModel.class);
187         // then
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");
192     }
193
194     @Test
195     public void testJsonResponseFromDelete() throws JsonProcessingException {
196         // given
197         stubDeleteCall();
198         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
199         // when
200         HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.delete(url, Collections.emptyMap());
201         // then
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");
206     }
207
208     @Test
209     public void testObjectResponseFromDelete() throws JsonProcessingException {
210         // given
211         stubDeleteCall();
212         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
213         // when
214         HttpResponse<SyncRestClientModel.TestModel> modelHttpResponse = syncRestClient
215             .delete(url, Collections.emptyMap(),  SyncRestClientModel.TestModel.class);
216         // then
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");
221     }
222
223     @Test
224     public void testRedirectToHttp() throws JsonProcessingException {
225         // given
226         stubGetCall();
227         String secured_url = "https://0.0.0.0:" + stubServer.getPort() + "/test";
228         String available_url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
229         // when
230         HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient
231             .get(secured_url, Collections.emptyMap(), Collections.emptyMap());
232         // then
233         verifyHttp(stubServer).once(Condition.method(Method.GET), Condition.url(available_url),
234             Condition.not(Condition.url(secured_url)));
235         assertEquals(jsonNodeHttpResponse.getStatus(), 200);
236     }
237
238     private void stubGetCall() throws JsonProcessingException {
239         whenHttp(stubServer)
240             .match(Condition.get("/test"))
241             .then(ok(), jsonContent(), contentType("application/json"));
242     }
243
244     private void stubDeleteCall() throws JsonProcessingException {
245         whenHttp(stubServer)
246             .match(Condition.delete("/test"))
247             .then(ok(), jsonContent(), contentType("application/json"));
248     }
249
250     private void stubPostCall() throws JsonProcessingException {
251         whenHttp(stubServer)
252             .match(Condition.post("/test"),
253                 Condition.withPostBodyContaining(objectMapper.writeValueAsString(testObject)))
254             .then(ok(), jsonContent(), contentType("application/json"));
255     }
256
257     private void stubPutCall() throws JsonProcessingException {
258         whenHttp(stubServer)
259             .match(Condition.put("/test"),
260                 Condition.withPostBodyContaining(objectMapper.writeValueAsString(testObject)))
261             .then(status(HttpStatus.CREATED_201), jsonContent(), contentType("application/json"));
262     }
263
264     private Action jsonContent() throws JsonProcessingException {
265         return stringContent(objectMapper.writeValueAsString(testObject));
266     }
267
268 }