make Logging a service and inject it to SyncRestClient
[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 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;
31
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;
41 import java.util.Map;
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;
48
49 public class SyncRestClientForHttpServerTest {
50
51     private static final SyncRestClientModel.TestModel testObject = new SyncRestClientModel.TestModel(1, "test");
52     private static final String NOT_EXISTING_OBJECT = "NOT EXISTING OBJECT";
53
54     private StubServer stubServer;
55     private ObjectMapper objectMapper = new ObjectMapper();
56     private SyncRestClient syncRestClient;
57     private Logging mockLoggingService;
58
59     @BeforeMethod
60     public void setUp() {
61         stubServer = new StubServer();
62         stubServer.run();
63         mockLoggingService = mock(Logging.class);
64         syncRestClient = new SyncRestClient(mockLoggingService);
65     }
66
67     @AfterMethod
68     public void tearDown() {
69         stubServer.stop();
70         syncRestClient.destroy();
71     }
72
73     @Test
74     public void testJsonResponseFromGet() throws JsonProcessingException {
75         // given
76         stubGetCall();
77         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
78         // when
79         HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient
80             .get(url, Collections.emptyMap(), Collections.emptyMap());
81         // then
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");
86     }
87
88     @Test
89     public void testObjectResponseFromGet() throws JsonProcessingException {
90         // given
91         stubGetCall();
92         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
93         // when
94         HttpResponse<SyncRestClientModel.TestModel> testModelHttpResponse = syncRestClient
95             .get(url, Collections.emptyMap(), Collections.emptyMap(), SyncRestClientModel.TestModel.class);
96         // then
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");
101     }
102
103     @Test
104     public void testJsonResponseFromPost() throws JsonProcessingException {
105         // given
106         stubPostCall();
107         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
108         // when
109         HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.post(url, Collections.emptyMap(), testObject);
110         // then
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");
115     }
116
117     @Test
118     public void test404JsonResponseFromPost() throws JsonProcessingException {
119         // given
120         stubPostCall();
121         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
122         // when
123         HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient
124             .post(url, Collections.emptyMap(), NOT_EXISTING_OBJECT);
125         // then
126         assertEquals(jsonNodeHttpResponse.getStatus(), 404);
127         assertEquals(jsonNodeHttpResponse.getStatusText(), "Not Found");
128     }
129
130     @Test
131     public void testHeadersWerePassedToPost() throws JsonProcessingException {
132         // given
133         stubPostCall();
134         Map headers = ImmutableMap.<String, String>builder().put("Authorization", "Basic anyHash").build();
135         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
136         // when
137         HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.post(url, headers, testObject);
138         // then
139         verifyHttp(stubServer).once(Condition.withHeader("Authorization"));
140         assertEquals(jsonNodeHttpResponse.getStatus(), 200);
141     }
142
143     @Test(expectedExceptions = {Exception.class})
144     public void testFailedJsonResponseFromPost() throws JsonProcessingException {
145         // given
146         stubPostCall();
147         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
148         // when
149         stubServer.stop();
150         syncRestClient.post(url, Collections.emptyMap(), testObject);
151     }
152
153     @Test
154     public void testObjectResponseFromPost() throws JsonProcessingException {
155         // given
156         stubPostCall();
157         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
158         // when
159         HttpResponse<SyncRestClientModel.TestModel> objectHttpResponse = syncRestClient
160             .post(url, Collections.emptyMap(), testObject, SyncRestClientModel.TestModel.class);
161         // then
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");
166     }
167
168     @Test
169     public void testJsonResponseFromPut() throws JsonProcessingException {
170         // given
171         stubPutCall();
172         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
173         // when
174         HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.put(url, Collections.emptyMap(), testObject);
175         // then
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");
180     }
181
182     @Test
183     public void testObjectResponseFromPut() throws JsonProcessingException {
184         // given
185         stubPutCall();
186         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
187         // when
188         HttpResponse<SyncRestClientModel.TestModel> modelHttpResponse = syncRestClient
189             .put(url, Collections.emptyMap(), testObject, SyncRestClientModel.TestModel.class);
190         // then
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");
195     }
196
197     @Test
198     public void testJsonResponseFromDelete() throws JsonProcessingException {
199         // given
200         stubDeleteCall();
201         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
202         // when
203         HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient.delete(url, Collections.emptyMap());
204         // then
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");
209     }
210
211     @Test
212     public void testObjectResponseFromDelete() throws JsonProcessingException {
213         // given
214         stubDeleteCall();
215         String url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
216         // when
217         HttpResponse<SyncRestClientModel.TestModel> modelHttpResponse = syncRestClient
218             .delete(url, Collections.emptyMap(),  SyncRestClientModel.TestModel.class);
219         // then
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");
224     }
225
226     @Test
227     public void testRedirectToHttp() throws JsonProcessingException {
228         // given
229         stubGetCall();
230         String secured_url = "https://0.0.0.0:" + stubServer.getPort() + "/test";
231         String available_url = "http://0.0.0.0:" + stubServer.getPort() + "/test";
232         // when
233         HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient
234             .get(secured_url, Collections.emptyMap(), Collections.emptyMap());
235         // then
236         verifyHttp(stubServer).once(Condition.method(Method.GET), Condition.url(available_url),
237             Condition.not(Condition.url(secured_url)));
238         assertEquals(jsonNodeHttpResponse.getStatus(), 200);
239     }
240
241     private void stubGetCall() throws JsonProcessingException {
242         whenHttp(stubServer)
243             .match(Condition.get("/test"))
244             .then(ok(), jsonContent(), contentType("application/json"));
245     }
246
247     private void stubDeleteCall() throws JsonProcessingException {
248         whenHttp(stubServer)
249             .match(Condition.delete("/test"))
250             .then(ok(), jsonContent(), contentType("application/json"));
251     }
252
253     private void stubPostCall() throws JsonProcessingException {
254         whenHttp(stubServer)
255             .match(Condition.post("/test"),
256                 Condition.withPostBodyContaining(objectMapper.writeValueAsString(testObject)))
257             .then(ok(), jsonContent(), contentType("application/json"));
258     }
259
260     private void stubPutCall() throws JsonProcessingException {
261         whenHttp(stubServer)
262             .match(Condition.put("/test"),
263                 Condition.withPostBodyContaining(objectMapper.writeValueAsString(testObject)))
264             .then(status(HttpStatus.CREATED_201), jsonContent(), contentType("application/json"));
265     }
266
267     private Action jsonContent() throws JsonProcessingException {
268         return stringContent(objectMapper.writeValueAsString(testObject));
269     }
270
271 }