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