Upgrade to Java 11
[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 import static org.mockito.Mockito.atLeast;
27 import static org.mockito.Mockito.verify;
28
29 import java.net.MalformedURLException;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.HashSet;
33
34 import javax.ws.rs.core.MultivaluedMap;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.Response.ResponseBuilder;
37
38 import org.apache.http.HttpException;
39 import org.glassfish.jersey.client.ClientConfig;
40 import org.glassfish.jersey.internal.util.Base64;
41 import org.glassfish.jersey.internal.util.collection.StringKeyIgnoreCaseMultivaluedMap;
42 import org.json.JSONException;
43 import org.json.JSONObject;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Mockito;
48 import org.powermock.api.mockito.PowerMockito;
49 import org.powermock.core.classloader.annotations.PowerMockIgnore;
50 import org.powermock.core.classloader.annotations.PrepareForTest;
51 import org.powermock.modules.junit4.PowerMockRunner;
52
53
54 @RunWith(PowerMockRunner.class)
55 @PowerMockIgnore({"org.apache.http.conn.ssl.*", "jdk.internal.reflect.*"})
56 @PrepareForTest({ DmaapClientUtil.class })
57 public class MRBaseClientTest {
58
59         // @InjectMocks
60         private MRBaseClient mrBaseClient;
61         private Collection<String> hosts = new HashSet<>(Arrays.asList("localhost:8080"));
62         private String clientSignature = "topic" + "::" + "cg" + "::" + "cid";
63         private ClientConfig config=null;
64
65         @Before
66         public void setup() throws MalformedURLException {
67                 mrBaseClient = new MRBaseClient(hosts, clientSignature);
68                 PowerMockito.mockStatic(DmaapClientUtil.class);
69         }
70
71         @Test
72         public void testGet() throws JSONException, HttpException {
73
74                 Response response = Mockito.mock(Response.class);
75                 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
76                 map.add("transactionid", "transactionid");
77
78                 Mockito.when(response.getStatus()).thenReturn(200);
79                 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
80                 Mockito.when(response.getHeaders()).thenReturn(map);
81
82                 Mockito.when(
83                                 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username", "password"))
84                                 .thenReturn(response);
85
86                 JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
87                 assertEquals(200, result.getInt("status"));
88                 assertEquals("test", result.getString("test"));
89                 verify(response, atLeast(1)).getStatus();
90                 verify(response).readEntity(String.class);
91                 verify(response).getHeaders();
92         }
93
94         @Test
95         public void testGet_403() throws JSONException, HttpException {
96                 ResponseBuilder responseBuilder = Response.status(403);
97                 Mockito
98                                 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username",
99                                                 "password"))
100                                 .thenReturn(
101                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
102                 JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
103                 assertEquals(403, result.getInt("status"));
104         }
105
106         @Test
107         public void testGet_basicauth() throws JSONException, HttpException {
108
109                 Response response = Mockito.mock(Response.class);
110                 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
111                 map.add("transactionid", "transactionid");
112
113                 Mockito.when(response.getStatus()).thenReturn(200);
114                 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
115                 Mockito.when(response.getHeaders()).thenReturn(map);
116
117                 Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"),
118                                 Base64.encodeAsString("username:password"))).thenReturn(response);
119
120                 JSONObject result = mrBaseClient.get("/path", "username", "password", "HTTPAAF");
121                 assertEquals(200, result.getInt("status"));
122                 verify(response, atLeast(1)).getStatus();
123                 verify(response).readEntity(String.class);
124                 verify(response).getHeaders();
125
126         }
127
128         @Test(expected = HttpException.class)
129         public void testGet_error() throws JSONException, HttpException {
130
131                 ResponseBuilder responseBuilder = Response.ok();
132                 Mockito.when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username",
133                                                 "password"))
134                                 .thenReturn(
135                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
136
137                 mrBaseClient.get("/path", null, null, "HTTPAUTH");
138         }
139
140         @Test
141         public void testGet_wrongjson() throws JSONException, HttpException {
142
143                 Response response = Mockito.mock(Response.class);
144                 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
145                 map.add("transactionid", "transactionid");
146
147                 Mockito.when(response.getStatus()).thenReturn(200);
148                 Mockito.when(response.readEntity(String.class)).thenReturn("[[");
149                 Mockito.when(response.getHeaders()).thenReturn(map);
150
151                 Mockito.when(
152                                 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username", "password"))
153                                 .thenReturn(response);
154
155                 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
156                 verify(response, atLeast(1)).getStatus();
157                 verify(response).readEntity(String.class);
158                 verify(response).getHeaders();
159         }
160
161         @Test
162         public void testGetResponse() throws JSONException, HttpException {
163
164                 Response response = Mockito.mock(Response.class);
165                 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
166                 map.add("transactionid", "transactionid");
167
168                 Mockito.when(response.getStatus()).thenReturn(200);
169                 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
170                 Mockito.when(response.getHeaders()).thenReturn(map);
171
172                 Mockito.when(
173                                 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username", "password"))
174                                 .thenReturn(response);
175
176                 mrBaseClient.getResponse("/path", "username", "password", "HTTPAUTH");
177                 assertTrue(true);
178
179         }
180
181         @Test
182         public void testGetResponse_aaf() throws JSONException, HttpException {
183
184                 Response response = Mockito.mock(Response.class);
185                 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
186                 map.add("transactionid", "transactionid");
187
188                 Mockito.when(response.getStatus()).thenReturn(200);
189                 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
190                 Mockito.when(response.getHeaders()).thenReturn(map);
191
192                 Mockito.when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"),
193                                 Base64.encodeAsString("username:password"))).thenReturn(response);
194
195                 mrBaseClient.getResponse("/path", "username", "password", "HTTPAAF");
196                 assertTrue(true);
197
198         }
199
200         @Test(expected = HttpException.class)
201         public void testGetResponse_error() throws JSONException, HttpException {
202
203                 ResponseBuilder responseBuilder = Response.ok();
204                 Mockito
205                                 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username",
206                                                 "password"))
207                                 .thenReturn(
208                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
209
210                 mrBaseClient.getResponse("/path", null, null, "HTTPAUTH");
211         }
212
213         @Test
214         public void testAuthResponse() throws JSONException, HttpException {
215
216                 Response response = Mockito.mock(Response.class);
217                 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
218                 map.add("transactionid", "transactionid");
219
220                 Mockito.when(response.getStatus()).thenReturn(200);
221                 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
222                 Mockito.when(response.getHeaders()).thenReturn(map);
223
224                 Mockito.when(
225                                 DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username", "password"))
226                                 .thenReturn(response);
227
228                 mrBaseClient.getAuthResponse("/path", "username", "password", "username", "password", "HTTPAUTH");
229                 assertTrue(true);
230
231         }
232
233         @Test(expected = HttpException.class)
234         public void testAuthResponsee_error() throws JSONException, HttpException {
235
236                 ResponseBuilder responseBuilder = Response.ok();
237                 Mockito
238                                 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username",
239                                                 "password"))
240                                 .thenReturn(
241                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
242
243                 mrBaseClient.getAuthResponse("/path", null, null, null, null, "HTTPAUTH");
244
245         }
246
247         @Test
248         public void testPostAuth() throws JSONException, HttpException {
249
250                 Response response = Mockito.mock(Response.class);
251                 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
252                 map.add("transactionid", "transactionid");
253
254                 Mockito.when(response.getStatus()).thenReturn(200);
255                 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
256                 Mockito.when(response.getHeaders()).thenReturn(map);
257
258                 Mockito
259                                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username",
260                                                 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
261                                 .thenReturn(response);
262
263                   mrBaseClient.postAuth(new PostAuthDataObject().setPath("/path")
264                           .setData( new String("{\"test\":\"test\"}").getBytes())
265                           .setContentType("application/json")
266                           .setAuthKey("username")
267                       .setAuthDate("password")
268                       .setUsername("username") 
269                       .setPassword("password")
270                       .setProtocolFlag("HTTPAUTH"));
271                 assertTrue(true);
272
273         }
274
275         @Test(expected = HttpException.class)
276         public void testPostAuth_error() throws JSONException, HttpException {
277
278                 ResponseBuilder responseBuilder = Response.ok();
279                 Mockito
280                                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username",
281                                                 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
282                                 .thenReturn(
283                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
284                 
285                 mrBaseClient.postAuth(new PostAuthDataObject().setPath("/path")
286                 .setData( new String("{\"test\":\"test\"}").getBytes())
287                 .setContentType("application/json")
288                 .setAuthKey(null)
289                 .setAuthDate(null)
290                 .setUsername(null) 
291                 .setPassword(null)
292                 .setProtocolFlag("HTTPAUTH"));
293         }
294
295         @Test
296         public void testGetNoAuthResponse() throws JSONException, HttpException {
297
298                 Response response = Mockito.mock(Response.class);
299                 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
300                 map.add("transactionid", "transactionid");
301
302                 Mockito.when(response.getStatus()).thenReturn(200);
303                 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
304                 Mockito.when(response.getHeaders()).thenReturn(map);
305
306                 Mockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"))).thenReturn(response);
307
308                 mrBaseClient.getNoAuthResponse("/path", "username", "password", "HTTPAUTH");
309                 assertTrue(true);
310
311         }
312
313         @Test
314         public void testPost() throws JSONException, HttpException {
315
316                 Response response = Mockito.mock(Response.class);
317                 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
318                 map.add("transactionid", "transactionid");
319
320                 Mockito.when(response.getStatus()).thenReturn(200);
321                 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
322                 Mockito.when(response.getHeaders()).thenReturn(map);
323
324                 Mockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"),
325                                 Base64.encodeAsString("username:password"), new String("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
326
327                 mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username",
328                                 "password", "HTTPAUTH");
329                 verify(response, atLeast(1)).getStatus();
330                 verify(response).readEntity(String.class);
331                 verify(response).getHeaders();
332
333         }
334
335         @Test(expected = HttpException.class)
336         public void testPost_error() throws JSONException, HttpException {
337
338                 ResponseBuilder responseBuilder = Response.ok();
339                 Mockito
340                                 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"),
341                                                 Base64.encodeAsString("username:password")))
342                                 .thenReturn(
343                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
344
345                 mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null, null,
346                                 "HTTPAUTH");
347
348         }
349
350         @Test
351         public void testPostAuthwithResponse() throws JSONException, HttpException {
352
353                 Response response = Mockito.mock(Response.class);
354                 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
355                 map.add("transactionid", "transactionid");
356
357                 Mockito.when(response.getStatus()).thenReturn(200);
358                 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
359                 Mockito.when(response.getHeaders()).thenReturn(map);
360
361                 Mockito
362                                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username",
363                                                 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
364                                 .thenReturn(response);
365
366                 mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
367                                 "username", "password", "username", "password", "HTTPAUTH");
368                 assertTrue(true);
369
370         }
371
372         @Test(expected = HttpException.class)
373         public void testPostAuthwithResponse_error() throws JSONException, HttpException {
374
375                 ResponseBuilder responseBuilder = Response.ok();
376                 Mockito
377                                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"), "username",
378                                                 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
379                                 .thenReturn(
380                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
381
382                 mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
383                                 null, null, null, null, "HTTPAUTH");
384                 assertTrue(true);
385
386         }
387
388         @Test
389         public void testPostWithResponse() throws JSONException, HttpException {
390
391                 Response response = Mockito.mock(Response.class);
392                 MultivaluedMap<String, Object> map = new StringKeyIgnoreCaseMultivaluedMap<>();
393                 map.add("transactionid", "transactionid");
394
395                 Mockito.when(response.getStatus()).thenReturn(200);
396                 Mockito.when(response.readEntity(String.class)).thenReturn("{\"test\":\"test\"}");
397                 Mockito.when(response.getHeaders()).thenReturn(map);
398
399                 Mockito.when(DmaapClientUtil.postResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"),
400                                 Base64.encodeAsString("username:password"), new String("{\"test\":\"test\"}").getBytes(), "application/json")).thenReturn(response);
401
402                 mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
403                                 "username", "password", "HTTPAUTH");
404                 assertTrue(true);
405
406         }
407
408         @Test(expected = HttpException.class)
409         public void testPostWithResponse_error() throws JSONException, HttpException {
410
411                 ResponseBuilder responseBuilder = Response.ok();
412                 Mockito
413                                 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget(getClientConfig(),"/path"),
414                                                 Base64.encodeAsString("username:password")))
415                                 .thenReturn(
416                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
417
418                 mrBaseClient.postWithResponse("/path", new String("{\"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", new String("{\"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                 assertTrue(true);
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         
488         private ClientConfig getClientConfig(){
489                 if(config==null){
490                         config=DmaapClientUtil.getClientConfig(null);
491                 }
492                 return config;
493                 
494         }
495
496 }