[DMAAP-CLIENT] Release 1.1.14
[dmaap/messagerouter/dmaapclient.git] / src / test / java / org / onap / dmaap / mr / client / impl / MRBaseClientTest.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Modifications Copyright © 2021 Orange.
8  *  ================================================================================
9  *  Licensed under the Apache License, Version 2.0 (the "License");
10  *  you may not use this file except in compliance with the License.
11  *  You may obtain a copy of the License at
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  *  ============LICENSE_END=========================================================
20  *
21  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  *
23  *******************************************************************************/
24
25 package org.onap.dmaap.mr.client.impl;
26
27 import java.nio.charset.StandardCharsets;
28 import org.apache.http.HttpException;
29 import org.glassfish.jersey.client.ClientConfig;
30 import org.apache.commons.codec.binary.Base64;
31 import org.glassfish.jersey.internal.util.collection.StringKeyIgnoreCaseMultivaluedMap;
32 import org.json.JSONException;
33 import org.json.JSONObject;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mockito;
38 import org.powermock.api.mockito.PowerMockito;
39 import org.powermock.core.classloader.annotations.PowerMockIgnore;
40 import org.powermock.core.classloader.annotations.PrepareForTest;
41 import org.powermock.modules.junit4.PowerMockRunner;
42
43 import javax.ws.rs.core.MultivaluedMap;
44 import javax.ws.rs.core.Response;
45 import javax.ws.rs.core.Response.ResponseBuilder;
46 import java.net.MalformedURLException;
47 import java.util.Arrays;
48 import java.util.Collection;
49 import java.util.HashSet;
50
51 import static org.junit.Assert.assertEquals;
52 import static org.junit.Assert.assertTrue;
53 import static org.mockito.Mockito.atLeast;
54 import static org.mockito.Mockito.verify;
55
56
57 @RunWith(PowerMockRunner.class)
58 @PowerMockIgnore({"org.apache.http.conn.ssl.*", "jdk.internal.reflect.*"})
59 @PrepareForTest({DmaapClientUtil.class})
60 public class MRBaseClientTest {
61
62     // @InjectMocks
63     private MRBaseClient mrBaseClient;
64     private Collection<String> hosts = new HashSet<>(Arrays.asList("localhost:8080"));
65     private String clientSignature = "topic" + "::" + "cg" + "::" + "cid";
66     private ClientConfig config = null;
67
68     @Before
69     public void setup() throws MalformedURLException {
70         mrBaseClient = new MRBaseClient(hosts, clientSignature);
71         PowerMockito.mockStatic(DmaapClientUtil.class);
72     }
73
74     @Test
75     public void testGet() throws JSONException, HttpException {
76
77         Response response = Mockito.mock(Response.class);
78         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
79         map.add("transactionid", "transactionid");
80
81         Mockito.when(response.getStatus()).thenReturn(200);
82         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
83         Mockito.when(response.getHeaders()).thenReturn(map);
84
85         Mockito.when(
86                 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
87                 .thenReturn(response);
88
89         JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
90         assertEquals(200, result.getInt("status"));
91         assertEquals("test", result.getString("test"));
92         verify(response, atLeast(1)).getStatus();
93         verify(response).readEntity(String.class);
94         verify(response).getHeaders();
95     }
96
97     @Test
98     public void testGet_403() throws JSONException, HttpException {
99         ResponseBuilder responseBuilder = Response.status(403);
100         Mockito
101                 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
102                         "password"))
103                 .thenReturn(
104                         responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
105         JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
106         assertEquals(403, result.getInt("status"));
107     }
108
109     @Test
110     public void testGet_basicauth() throws JSONException, HttpException {
111
112         Response response = Mockito.mock(Response.class);
113         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
114         map.add("transactionid", "transactionid");
115
116         Mockito.when(response.getStatus()).thenReturn(200);
117         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
118         Mockito.when(response.getHeaders()).thenReturn(map);
119         Base64 base64 = new Base64();
120         Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
121             base64.encodeAsString(("username:password").getBytes(StandardCharsets.UTF_8)))).thenReturn(response);
122
123         JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAAF");
124         assertEquals(200, result.getInt("status"));
125         verify(response, atLeast(1)).getStatus();
126         verify(response).readEntity(String.class);
127         verify(response).getHeaders();
128
129     }
130
131     @Test(expected = HttpException.class)
132     public void testGet_error() throws JSONException, HttpException {
133
134         ResponseBuilder responseBuilder = Response.ok();
135         Mockito.when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
136                 "password"))
137                 .thenReturn(
138                         responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
139
140         mrBaseClient.get("/path", null, null, "HTTPAUTH");
141     }
142
143     @Test
144     public void testGet_wrongjson() throws JSONException, HttpException {
145
146         Response response = Mockito.mock(Response.class);
147         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
148         map.add("transactionid", "transactionid");
149
150         Mockito.when(response.getStatus()).thenReturn(200);
151         Mockito.when(response.readEntity(String.class)).thenReturn("[[");
152         Mockito.when(response.getHeaders()).thenReturn(map);
153
154         Mockito.when(
155                 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
156                 .thenReturn(response);
157
158         mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
159         verify(response, atLeast(1)).getStatus();
160         verify(response).readEntity(String.class);
161         verify(response).getHeaders();
162     }
163
164     @Test
165     public void testGetResponse() throws JSONException, HttpException {
166
167         Response response = Mockito.mock(Response.class);
168         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
169         map.add("transactionid", "transactionid");
170
171         Mockito.when(response.getStatus()).thenReturn(200);
172         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
173         Mockito.when(response.getHeaders()).thenReturn(map);
174
175         Mockito.when(
176                 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
177                 .thenReturn(response);
178
179         mrBaseClient.getResponse("/path", "username", "password", "HTTPAUTH");
180         assertTrue(true);
181
182     }
183
184     @Test
185     public void testGetResponse_aaf() throws JSONException, HttpException {
186
187         Response response = Mockito.mock(Response.class);
188         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
189         map.add("transactionid", "transactionid");
190
191         Mockito.when(response.getStatus()).thenReturn(200);
192         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
193         Mockito.when(response.getHeaders()).thenReturn(map);
194         Base64 base64 = new Base64();
195         Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
196             base64.encodeAsString(("username:password").getBytes(StandardCharsets.UTF_8)))).thenReturn(response);
197
198         mrBaseClient.getResponse("/path", "username", "password", "HTTPAAF");
199         assertTrue(true);
200
201     }
202
203     @Test(expected = HttpException.class)
204     public void testGetResponse_error() throws JSONException, HttpException {
205
206         ResponseBuilder responseBuilder = Response.ok();
207         Mockito
208                 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
209                         "password"))
210                 .thenReturn(
211                         responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
212
213         mrBaseClient.getResponse("/path", null, null, "HTTPAUTH");
214     }
215
216     @Test
217     public void testAuthResponse() throws JSONException, HttpException {
218
219         Response response = Mockito.mock(Response.class);
220         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
221         map.add("transactionid", "transactionid");
222
223         Mockito.when(response.getStatus()).thenReturn(200);
224         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
225         Mockito.when(response.getHeaders()).thenReturn(map);
226
227         Mockito.when(
228                 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
229                 .thenReturn(response);
230
231         mrBaseClient.getAuthResponse("/path", "username", "password", "username", "password", "HTTPAUTH");
232         assertTrue(true);
233
234     }
235
236     @Test(expected = HttpException.class)
237     public void testAuthResponsee_error() throws JSONException, HttpException {
238
239         ResponseBuilder responseBuilder = Response.ok();
240         Mockito
241                 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
242                         "password"))
243                 .thenReturn(
244                         responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
245
246         mrBaseClient.getAuthResponse("/path", null, null, null, null, "HTTPAUTH");
247
248     }
249
250     @Test
251     public void testPostAuth() throws JSONException, HttpException {
252
253         Response response = Mockito.mock(Response.class);
254         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
255         map.add("transactionid", "transactionid");
256
257         Mockito.when(response.getStatus()).thenReturn(200);
258         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
259         Mockito.when(response.getHeaders()).thenReturn(map);
260
261         Mockito
262                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
263                         "password", ("{\"test\":\"test\"}").getBytes(), "application/json"))
264                 .thenReturn(response);
265
266         mrBaseClient.postAuth(new PostAuthDataObject().setPath("/path")
267                 .setData(("{\"test\":\"test\"}").getBytes())
268                 .setContentType("application/json")
269                 .setAuthKey("username")
270                 .setAuthDate("password")
271                 .setUsername("username")
272                 .setPassword("password")
273                 .setProtocolFlag("HTTPAUTH"));
274         assertTrue(true);
275
276     }
277
278     @Test(expected = HttpException.class)
279     public void testPostAuth_error() throws JSONException, HttpException {
280
281         ResponseBuilder responseBuilder = Response.ok();
282         Mockito
283                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
284                         "password", ("{\"test\":\"test\"}").getBytes(), "application/json"))
285                 .thenReturn(
286                         responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
287
288         mrBaseClient.postAuth(new PostAuthDataObject().setPath("/path")
289                 .setData(("{\"test\":\"test\"}").getBytes())
290                 .setContentType("application/json")
291                 .setAuthKey(null)
292                 .setAuthDate(null)
293                 .setUsername(null)
294                 .setPassword(null)
295                 .setProtocolFlag("HTTPAUTH"));
296     }
297
298     @Test
299     public void testGetNoAuthResponse() throws JSONException, HttpException {
300
301         Response response = Mockito.mock(Response.class);
302         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
303         map.add("transactionid", "transactionid");
304
305         Mockito.when(response.getStatus()).thenReturn(200);
306         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
307         Mockito.when(response.getHeaders()).thenReturn(map);
308
309         Mockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"))).thenReturn(response);
310
311         mrBaseClient.getNoAuthResponse("/path", "username", "password", "HTTPAUTH");
312         assertTrue(true);
313
314     }
315
316     @Test
317     public void testPost() throws JSONException, HttpException {
318
319         Response response = Mockito.mock(Response.class);
320         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
321         map.add("transactionid", "transactionid");
322
323         Mockito.when(response.getStatus()).thenReturn(200);
324         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
325         Mockito.when(response.getHeaders()).thenReturn(map);
326         Base64 base64 = new Base64();
327         Mockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
328             base64.encodeAsString(("username:password").getBytes(StandardCharsets.UTF_8)), ("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
329
330         mrBaseClient.post("/path", ("{\"test\":\"test\"}").getBytes(), "application/json", "username",
331                 "password", "HTTPAUTH");
332         verify(response, atLeast(1)).getStatus();
333         verify(response).readEntity(String.class);
334         verify(response).getHeaders();
335
336     }
337
338     @Test(expected = HttpException.class)
339     public void testPost_error() throws JSONException, HttpException {
340
341         ResponseBuilder responseBuilder = Response.ok();
342         Base64 base64 = new Base64();
343         Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
344             base64.encodeAsString(("username:password").getBytes(StandardCharsets.UTF_8)))).thenReturn(
345                         responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
346
347         mrBaseClient.post("/path", ("{\"test\":\"test\"}").getBytes(), "application/json", null, null,
348                 "HTTPAUTH");
349
350     }
351
352     @Test
353     public void testPostAuthwithResponse() throws JSONException, HttpException {
354
355         Response response = Mockito.mock(Response.class);
356         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
357         map.add("transactionid", "transactionid");
358
359         Mockito.when(response.getStatus()).thenReturn(200);
360         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
361         Mockito.when(response.getHeaders()).thenReturn(map);
362
363         Mockito
364                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
365                         "password", ("{\"test\":\"test\"}").getBytes(), "application/json"))
366                 .thenReturn(response);
367
368         mrBaseClient.postAuthwithResponse("/path", ("{\"test\":\"test\"}").getBytes(), "application/json",
369                 "username", "password", "username", "password", "HTTPAUTH");
370         assertTrue(true);
371
372     }
373
374     @Test(expected = HttpException.class)
375     public void testPostAuthwithResponse_error() throws JSONException, HttpException {
376
377         ResponseBuilder responseBuilder = Response.ok();
378         Mockito
379                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
380                         "password", ("{\"test\":\"test\"}").getBytes(), "application/json"))
381                 .thenReturn(
382                         responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
383
384         mrBaseClient.postAuthwithResponse("/path", ("{\"test\":\"test\"}").getBytes(), "application/json",
385                 null, null, null, null, "HTTPAUTH");
386
387     }
388
389     @Test
390     public void testPostWithResponse() throws JSONException, HttpException {
391
392         Response response = Mockito.mock(Response.class);
393         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
394         map.add("transactionid", "transactionid");
395
396         Mockito.when(response.getStatus()).thenReturn(200);
397         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
398         Mockito.when(response.getHeaders()).thenReturn(map);
399         Base64 base64 = new Base64();
400         Mockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
401             base64.encodeAsString(("username:password").getBytes(StandardCharsets.UTF_8)), ("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
402
403         mrBaseClient.postWithResponse("/path", ("{\"test\":\"test\"}").getBytes(), "application/json",
404                 "username", "password", "HTTPAUTH");
405         assertTrue(true);
406
407     }
408
409     @Test(expected = HttpException.class)
410     public void testPostWithResponse_error() throws JSONException, HttpException {
411
412         ResponseBuilder responseBuilder = Response.ok();
413         Base64 base64 = new Base64();
414         Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
415             base64.encodeAsString(("username:password").getBytes(StandardCharsets.UTF_8)))).thenReturn(
416             responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
417
418         mrBaseClient.postWithResponse("/path", ("{\"test\":\"test\"}").getBytes(), "application/json", null,
419                 null, "HTTPAUTH");
420
421     }
422
423     @Test
424     public void testGetAuth() throws JSONException, HttpException {
425
426         Response response = Mockito.mock(Response.class);
427         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
428         map.add("transactionid", "transactionid");
429
430         Mockito.when(response.getStatus()).thenReturn(200);
431         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
432         Mockito.when(response.getHeaders()).thenReturn(map);
433
434         Mockito.when(
435                 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
436                 .thenReturn(response);
437         mrBaseClient.getAuth("/path", "username", "password", "username", "password", "HTTPAUTH");
438         assertTrue(true);
439
440     }
441
442     @Test(expected = HttpException.class)
443     public void testGetAuth_error() throws JSONException, HttpException {
444
445         ResponseBuilder responseBuilder = Response.ok();
446         Mockito
447                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
448                         "password", ("{\"test\":\"test\"}").getBytes(), "application/json"))
449                 .thenReturn(
450                         responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
451
452         mrBaseClient.getAuth("/path", null, null, null, null, "HTTPAUTH");
453
454     }
455
456     @Test
457     public void testGetNoAuth() throws JSONException, HttpException {
458
459         Response response = Mockito.mock(Response.class);
460         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
461         map.add("transactionid", "transactionid");
462
463         Mockito.when(response.getStatus()).thenReturn(200);
464         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
465         Mockito.when(response.getHeaders()).thenReturn(map);
466
467         Mockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"))).thenReturn(response);
468         mrBaseClient.getNoAuth("/path");
469         assertTrue(true);
470
471     }
472
473
474     @Test
475     public void testGetHTTPErrorResponseMessage() {
476         assertEquals("testtest", mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"));
477
478     }
479
480     @Test
481     public void getGTTPErrorResponseCode() {
482         assertEquals("500", mrBaseClient.getHTTPErrorResponseCode("<title>500</title>"));
483     }
484
485
486     private ClientConfig getClientConfig() {
487         if (config == null) {
488             config = DmaapClientUtil.getClientConfig(null);
489         }
490         return config;
491
492     }
493
494 }