removing unused imports from src and test classes
[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("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username",
255                                 "password", "username", "password", "HTTPAUTH");
256                 assertTrue(true);
257
258         }
259
260         @Test(expected = HttpException.class)
261         public void testPostAuth_error() throws JSONException, HttpException {
262
263                 ResponseBuilder responseBuilder = Response.ok();
264                 PowerMockito
265                                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
266                                                 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
267                                 .thenReturn(
268                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
269
270                 mrBaseClient.postAuth("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null, null,
271                                 null, null, "HTTPAUTH");
272                 assertTrue(true);
273
274         }
275
276         @Test
277         public void testGetNoAuthResponse() throws JSONException, HttpException {
278
279                 Response response = Mockito.mock(Response.class);
280                 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
281                 map.add("transactionid", "transactionid");
282
283                 PowerMockito.when(response.getStatus()).thenReturn(200);
284                 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
285                 PowerMockito.when(response.getHeaders()).thenReturn(map);
286
287                 PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(response);
288
289                 mrBaseClient.getNoAuthResponse("/path", "username", "password", "HTTPAUTH");
290                 assertTrue(true);
291
292         }
293
294         @Test
295         public void testPost() throws JSONException, HttpException {
296
297                 Response response = Mockito.mock(Response.class);
298                 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
299                 map.add("transactionid", "transactionid");
300
301                 PowerMockito.when(response.getStatus()).thenReturn(200);
302                 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
303                 PowerMockito.when(response.getHeaders()).thenReturn(map);
304
305                 PowerMockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
306                                 Base64.encodeAsString("username:password"), new String("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
307
308                 mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username",
309                                 "password", "HTTPAUTH");
310                 assertTrue(true);
311
312         }
313
314         @Test(expected = HttpException.class)
315         public void testPost_error() throws JSONException, HttpException {
316
317                 ResponseBuilder responseBuilder = Response.ok();
318                 PowerMockito
319                                 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
320                                                 Base64.encodeAsString("username:password")))
321                                 .thenReturn(
322                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
323
324                 mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null, null,
325                                 "HTTPAUTH");
326
327         }
328
329         @Test
330         public void testPostAuthwithResponse() throws JSONException, HttpException {
331
332                 Response response = Mockito.mock(Response.class);
333                 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
334                 map.add("transactionid", "transactionid");
335
336                 PowerMockito.when(response.getStatus()).thenReturn(200);
337                 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
338                 PowerMockito.when(response.getHeaders()).thenReturn(map);
339
340                 PowerMockito
341                                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
342                                                 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
343                                 .thenReturn(response);
344
345                 mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
346                                 "username", "password", "username", "password", "HTTPAUTH");
347                 assertTrue(true);
348
349         }
350
351         @Test(expected = HttpException.class)
352         public void testPostAuthwithResponse_error() throws JSONException, HttpException {
353
354                 ResponseBuilder responseBuilder = Response.ok();
355                 PowerMockito
356                                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
357                                                 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
358                                 .thenReturn(
359                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
360
361                 mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
362                                 null, null, null, null, "HTTPAUTH");
363                 assertTrue(true);
364
365         }
366
367         @Test
368         public void testPostWithResponse() throws JSONException, HttpException {
369
370                 Response response = Mockito.mock(Response.class);
371                 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
372                 map.add("transactionid", "transactionid");
373
374                 PowerMockito.when(response.getStatus()).thenReturn(200);
375                 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
376                 PowerMockito.when(response.getHeaders()).thenReturn(map);
377
378                 PowerMockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
379                                 Base64.encodeAsString("username:password"), new String("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
380
381                 mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
382                                 "username", "password", "HTTPAUTH");
383                 assertTrue(true);
384
385         }
386
387         @Test(expected = HttpException.class)
388         public void testPostWithResponse_error() throws JSONException, HttpException {
389
390                 ResponseBuilder responseBuilder = Response.ok();
391                 PowerMockito
392                                 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
393                                                 Base64.encodeAsString("username:password")))
394                                 .thenReturn(
395                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
396
397                 mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null,
398                                 null, "HTTPAUTH");
399
400         }
401
402         @Test
403         public void testGetAuth() throws JSONException, HttpException {
404
405                 Response response = Mockito.mock(Response.class);
406                 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
407                 map.add("transactionid", "transactionid");
408
409                 PowerMockito.when(response.getStatus()).thenReturn(200);
410                 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
411                 PowerMockito.when(response.getHeaders()).thenReturn(map);
412
413                 PowerMockito.when(
414                                 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username", "password"))
415                                 .thenReturn(response);
416                 mrBaseClient.getAuth("/path", "username", "password", "username", "password", "HTTPAUTH");
417                 assertTrue(true);
418
419         }
420
421         @Test(expected = HttpException.class)
422         public void testGetAuth_error() throws JSONException, HttpException {
423
424                 ResponseBuilder responseBuilder = Response.ok();
425                 PowerMockito
426                                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
427                                                 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
428                                 .thenReturn(
429                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
430
431                 mrBaseClient.getAuth("/path", null, null, null, null, "HTTPAUTH");
432                 assertTrue(true);
433
434         }
435
436         @Test
437         public void testGetNoAuth() throws JSONException, HttpException {
438
439                 Response response = Mockito.mock(Response.class);
440                 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
441                 map.add("transactionid", "transactionid");
442
443                 PowerMockito.when(response.getStatus()).thenReturn(200);
444                 PowerMockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
445                 PowerMockito.when(response.getHeaders()).thenReturn(map);
446
447                 PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(response);
448                 mrBaseClient.getNoAuth("/path");
449                 assertTrue(true);
450
451         }
452
453
454         @Test
455         public void testGetHTTPErrorResponseMessage() {
456
457                 assertEquals(mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"), "testtest");
458
459         }
460
461         @Test
462         public void getGTTPErrorResponseCode() {
463
464                 assertEquals(mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"), "testtest");
465
466         }
467
468 }