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