[DMAAP-CLIENT] First sonar issues review part2
[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 org.apache.http.HttpException;
28 import org.glassfish.jersey.client.ClientConfig;
29 import org.glassfish.jersey.internal.util.Base64;
30 import org.glassfish.jersey.internal.util.collection.StringKeyIgnoreCaseMultivaluedMap;
31 import org.json.JSONException;
32 import org.json.JSONObject;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mockito;
37 import org.powermock.api.mockito.PowerMockito;
38 import org.powermock.core.classloader.annotations.PowerMockIgnore;
39 import org.powermock.core.classloader.annotations.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
41
42 import javax.ws.rs.core.MultivaluedMap;
43 import javax.ws.rs.core.Response;
44 import javax.ws.rs.core.Response.ResponseBuilder;
45 import java.net.MalformedURLException;
46 import java.util.Arrays;
47 import java.util.Collection;
48 import java.util.HashSet;
49
50 import static org.junit.Assert.assertEquals;
51 import static org.junit.Assert.assertTrue;
52 import static org.mockito.Mockito.atLeast;
53 import static org.mockito.Mockito.verify;
54
55
56 @RunWith(PowerMockRunner.class)
57 @PowerMockIgnore({"org.apache.http.conn.ssl.*", "jdk.internal.reflect.*"})
58 @PrepareForTest({DmaapClientUtil.class})
59 public class MRBaseClientTest {
60
61     // @InjectMocks
62     private MRBaseClient mrBaseClient;
63     private Collection<String> hosts = new HashSet<>(Arrays.asList("localhost:8080"));
64     private String clientSignature = "topic" + "::" + "cg" + "::" + "cid";
65     private ClientConfig config = null;
66
67     @Before
68     public void setup() throws MalformedURLException {
69         mrBaseClient = new MRBaseClient(hosts, clientSignature);
70         PowerMockito.mockStatic(DmaapClientUtil.class);
71     }
72
73     @Test
74     public void testGet() throws JSONException, HttpException {
75
76         Response response = Mockito.mock(Response.class);
77         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
78         map.add("transactionid", "transactionid");
79
80         Mockito.when(response.getStatus()).thenReturn(200);
81         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
82         Mockito.when(response.getHeaders()).thenReturn(map);
83
84         Mockito.when(
85                 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
86                 .thenReturn(response);
87
88         JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
89         assertEquals(200, result.getInt("status"));
90         assertEquals("test", result.getString("test"));
91         verify(response, atLeast(1)).getStatus();
92         verify(response).readEntity(String.class);
93         verify(response).getHeaders();
94     }
95
96     @Test
97     public void testGet_403() throws JSONException, HttpException {
98         ResponseBuilder responseBuilder = Response.status(403);
99         Mockito
100                 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
101                         "password"))
102                 .thenReturn(
103                         responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
104         JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
105         assertEquals(403, result.getInt("status"));
106     }
107
108     @Test
109     public void testGet_basicauth() throws JSONException, HttpException {
110
111         Response response = Mockito.mock(Response.class);
112         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
113         map.add("transactionid", "transactionid");
114
115         Mockito.when(response.getStatus()).thenReturn(200);
116         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
117         Mockito.when(response.getHeaders()).thenReturn(map);
118
119         Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
120                 Base64.encodeAsString("username:password"))).thenReturn(response);
121
122         JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAAF");
123         assertEquals(200, result.getInt("status"));
124         verify(response, atLeast(1)).getStatus();
125         verify(response).readEntity(String.class);
126         verify(response).getHeaders();
127
128     }
129
130     @Test(expected = HttpException.class)
131     public void testGet_error() throws JSONException, HttpException {
132
133         ResponseBuilder responseBuilder = Response.ok();
134         Mockito.when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
135                 "password"))
136                 .thenReturn(
137                         responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
138
139         mrBaseClient.get("/path", null, null, "HTTPAUTH");
140     }
141
142     @Test
143     public void testGet_wrongjson() throws JSONException, HttpException {
144
145         Response response = Mockito.mock(Response.class);
146         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
147         map.add("transactionid", "transactionid");
148
149         Mockito.when(response.getStatus()).thenReturn(200);
150         Mockito.when(response.readEntity(String.class)).thenReturn("[[");
151         Mockito.when(response.getHeaders()).thenReturn(map);
152
153         Mockito.when(
154                 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
155                 .thenReturn(response);
156
157         mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
158         verify(response, atLeast(1)).getStatus();
159         verify(response).readEntity(String.class);
160         verify(response).getHeaders();
161     }
162
163     @Test
164     public void testGetResponse() throws JSONException, HttpException {
165
166         Response response = Mockito.mock(Response.class);
167         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
168         map.add("transactionid", "transactionid");
169
170         Mockito.when(response.getStatus()).thenReturn(200);
171         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
172         Mockito.when(response.getHeaders()).thenReturn(map);
173
174         Mockito.when(
175                 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
176                 .thenReturn(response);
177
178         mrBaseClient.getResponse("/path", "username", "password", "HTTPAUTH");
179         assertTrue(true);
180
181     }
182
183     @Test
184     public void testGetResponse_aaf() throws JSONException, HttpException {
185
186         Response response = Mockito.mock(Response.class);
187         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
188         map.add("transactionid", "transactionid");
189
190         Mockito.when(response.getStatus()).thenReturn(200);
191         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
192         Mockito.when(response.getHeaders()).thenReturn(map);
193
194         Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
195                 Base64.encodeAsString("username:password"))).thenReturn(response);
196
197         mrBaseClient.getResponse("/path", "username", "password", "HTTPAAF");
198         assertTrue(true);
199
200     }
201
202     @Test(expected = HttpException.class)
203     public void testGetResponse_error() throws JSONException, HttpException {
204
205         ResponseBuilder responseBuilder = Response.ok();
206         Mockito
207                 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
208                         "password"))
209                 .thenReturn(
210                         responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
211
212         mrBaseClient.getResponse("/path", null, null, "HTTPAUTH");
213     }
214
215     @Test
216     public void testAuthResponse() throws JSONException, HttpException {
217
218         Response response = Mockito.mock(Response.class);
219         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
220         map.add("transactionid", "transactionid");
221
222         Mockito.when(response.getStatus()).thenReturn(200);
223         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
224         Mockito.when(response.getHeaders()).thenReturn(map);
225
226         Mockito.when(
227                 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
228                 .thenReturn(response);
229
230         mrBaseClient.getAuthResponse("/path", "username", "password", "username", "password", "HTTPAUTH");
231         assertTrue(true);
232
233     }
234
235     @Test(expected = HttpException.class)
236     public void testAuthResponsee_error() throws JSONException, HttpException {
237
238         ResponseBuilder responseBuilder = Response.ok();
239         Mockito
240                 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
241                         "password"))
242                 .thenReturn(
243                         responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
244
245         mrBaseClient.getAuthResponse("/path", null, null, null, null, "HTTPAUTH");
246
247     }
248
249     @Test
250     public void testPostAuth() throws JSONException, HttpException {
251
252         Response response = Mockito.mock(Response.class);
253         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
254         map.add("transactionid", "transactionid");
255
256         Mockito.when(response.getStatus()).thenReturn(200);
257         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
258         Mockito.when(response.getHeaders()).thenReturn(map);
259
260         Mockito
261                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
262                         "password", ("{\"test\":\"test\"}").getBytes(), "application/json"))
263                 .thenReturn(response);
264
265         mrBaseClient.postAuth(new PostAuthDataObject().setPath("/path")
266                 .setData(("{\"test\":\"test\"}").getBytes())
267                 .setContentType("application/json")
268                 .setAuthKey("username")
269                 .setAuthDate("password")
270                 .setUsername("username")
271                 .setPassword("password")
272                 .setProtocolFlag("HTTPAUTH"));
273         assertTrue(true);
274
275     }
276
277     @Test(expected = HttpException.class)
278     public void testPostAuth_error() throws JSONException, HttpException {
279
280         ResponseBuilder responseBuilder = Response.ok();
281         Mockito
282                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
283                         "password", ("{\"test\":\"test\"}").getBytes(), "application/json"))
284                 .thenReturn(
285                         responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
286
287         mrBaseClient.postAuth(new PostAuthDataObject().setPath("/path")
288                 .setData(("{\"test\":\"test\"}").getBytes())
289                 .setContentType("application/json")
290                 .setAuthKey(null)
291                 .setAuthDate(null)
292                 .setUsername(null)
293                 .setPassword(null)
294                 .setProtocolFlag("HTTPAUTH"));
295     }
296
297     @Test
298     public void testGetNoAuthResponse() throws JSONException, HttpException {
299
300         Response response = Mockito.mock(Response.class);
301         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
302         map.add("transactionid", "transactionid");
303
304         Mockito.when(response.getStatus()).thenReturn(200);
305         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
306         Mockito.when(response.getHeaders()).thenReturn(map);
307
308         Mockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"))).thenReturn(response);
309
310         mrBaseClient.getNoAuthResponse("/path", "username", "password", "HTTPAUTH");
311         assertTrue(true);
312
313     }
314
315     @Test
316     public void testPost() throws JSONException, HttpException {
317
318         Response response = Mockito.mock(Response.class);
319         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
320         map.add("transactionid", "transactionid");
321
322         Mockito.when(response.getStatus()).thenReturn(200);
323         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
324         Mockito.when(response.getHeaders()).thenReturn(map);
325
326         Mockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
327                 Base64.encodeAsString("username:password"), ("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
328
329         mrBaseClient.post("/path", ("{\"test\":\"test\"}").getBytes(), "application/json", "username",
330                 "password", "HTTPAUTH");
331         verify(response, atLeast(1)).getStatus();
332         verify(response).readEntity(String.class);
333         verify(response).getHeaders();
334
335     }
336
337     @Test(expected = HttpException.class)
338     public void testPost_error() throws JSONException, HttpException {
339
340         ResponseBuilder responseBuilder = Response.ok();
341         Mockito
342                 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
343                         Base64.encodeAsString("username:password")))
344                 .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
400         Mockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
401                 Base64.encodeAsString("username:password"), ("{\"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         Mockito
414                 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"),
415                         Base64.encodeAsString("username:password")))
416                 .thenReturn(
417                         responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
418
419         mrBaseClient.postWithResponse("/path", ("{\"test\":\"test\"}").getBytes(), "application/json", null,
420                 null, "HTTPAUTH");
421
422     }
423
424     @Test
425     public void testGetAuth() throws JSONException, HttpException {
426
427         Response response = Mockito.mock(Response.class);
428         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
429         map.add("transactionid", "transactionid");
430
431         Mockito.when(response.getStatus()).thenReturn(200);
432         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
433         Mockito.when(response.getHeaders()).thenReturn(map);
434
435         Mockito.when(
436                 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username", "password"))
437                 .thenReturn(response);
438         mrBaseClient.getAuth("/path", "username", "password", "username", "password", "HTTPAUTH");
439         assertTrue(true);
440
441     }
442
443     @Test(expected = HttpException.class)
444     public void testGetAuth_error() throws JSONException, HttpException {
445
446         ResponseBuilder responseBuilder = Response.ok();
447         Mockito
448                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"), "username",
449                         "password", ("{\"test\":\"test\"}").getBytes(), "application/json"))
450                 .thenReturn(
451                         responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
452
453         mrBaseClient.getAuth("/path", null, null, null, null, "HTTPAUTH");
454
455     }
456
457     @Test
458     public void testGetNoAuth() throws JSONException, HttpException {
459
460         Response response = Mockito.mock(Response.class);
461         MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
462         map.add("transactionid", "transactionid");
463
464         Mockito.when(response.getStatus()).thenReturn(200);
465         Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
466         Mockito.when(response.getHeaders()).thenReturn(map);
467
468         Mockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget(getClientConfig(), "/path"))).thenReturn(response);
469         mrBaseClient.getNoAuth("/path");
470         assertTrue(true);
471
472     }
473
474
475     @Test
476     public void testGetHTTPErrorResponseMessage() {
477         assertEquals("testtest", mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"));
478
479     }
480
481     @Test
482     public void getGTTPErrorResponseCode() {
483         assertEquals("500", mrBaseClient.getHTTPErrorResponseCode("<title>500</title>"));
484     }
485
486
487     private ClientConfig getClientConfig() {
488         if (config == null) {
489             config = DmaapClientUtil.getClientConfig(null);
490         }
491         return config;
492
493     }
494
495 }