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