Sonar critical issues
[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.Response;
33 import javax.ws.rs.core.Response.ResponseBuilder;
34
35 import org.apache.http.HttpException;
36 import org.glassfish.jersey.internal.util.Base64;
37 import org.json.JSONException;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.InjectMocks;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
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                 ResponseBuilder responseBuilder = Response.ok();
69                 PowerMockito
70                                 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
71                                                 "password"))
72                                 .thenReturn(
73                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
74
75                 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
76                 assertTrue(true);
77
78         }
79
80         @Test
81         public void testGet_403() throws JSONException, HttpException {
82                 ResponseBuilder responseBuilder = Response.status(403);
83                 PowerMockito
84                                 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
85                                                 "password"))
86                                 .thenReturn(
87                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
88                 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
89                 assertTrue(true);
90
91         }
92
93         @Test
94         public void testGet_basicauth() throws JSONException, HttpException {
95
96                 ResponseBuilder responseBuilder = Response.ok();
97                 PowerMockito
98                                 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
99                                                 Base64.encodeAsString("username:password")))
100                                 .thenReturn(
101                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
102
103                 mrBaseClient.get("/path", "username", "password", "HTTPAAF");
104                 assertTrue(true);
105
106         }
107
108         @Test(expected = HttpException.class)
109         public void testGet_error() throws JSONException, HttpException {
110
111                 ResponseBuilder responseBuilder = Response.ok();
112                 PowerMockito
113                                 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
114                                                 "password"))
115                                 .thenReturn(
116                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
117
118                 mrBaseClient.get("/path", null, null, "HTTPAUTH");
119                 assertTrue(true);
120
121         }
122
123         @Test
124         public void testGet_wrongjson() throws JSONException, HttpException {
125
126                 ResponseBuilder responseBuilder = Response.ok();
127                 PowerMockito
128                                 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
129                                                 "password"))
130                                 .thenReturn(responseBuilder.header("transactionid", "transactionid").entity("[[").build());
131
132                 mrBaseClient.get("/path", "username", "password", "HTTPAUTH");
133                 assertTrue(true);
134         }
135
136         @Test
137         public void testGetResponse() throws JSONException, HttpException {
138
139                 ResponseBuilder responseBuilder = Response.ok();
140                 PowerMockito
141                                 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
142                                                 "password"))
143                                 .thenReturn(
144                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
145
146                 mrBaseClient.getResponse("/path", "username", "password", "HTTPAUTH");
147                 assertTrue(true);
148
149         }
150
151         @Test
152         public void testGetResponse_aaf() throws JSONException, HttpException {
153
154                 ResponseBuilder responseBuilder = Response.ok();
155                 PowerMockito
156                                 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
157                                                 Base64.encodeAsString("username:password")))
158                                 .thenReturn(
159                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
160
161                 mrBaseClient.getResponse("/path", "username", "password", "HTTPAAF");
162                 assertTrue(true);
163
164         }
165
166         @Test(expected = HttpException.class)
167         public void testGetResponse_error() throws JSONException, HttpException {
168
169                 ResponseBuilder responseBuilder = Response.ok();
170                 PowerMockito
171                                 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
172                                                 "password"))
173                                 .thenReturn(
174                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
175
176                 mrBaseClient.getResponse("/path", null, null, "HTTPAUTH");
177
178         }
179
180         @Test
181         public void testAuthResponse() throws JSONException, HttpException {
182
183                 ResponseBuilder responseBuilder = Response.ok();
184                 PowerMockito
185                                 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
186                                                 "password"))
187                                 .thenReturn(
188                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
189
190                 mrBaseClient.getAuthResponse("/path", "username", "password", "username", "password", "HTTPAUTH");
191                 assertTrue(true);
192
193         }
194
195         @Test(expected = HttpException.class)
196         public void testAuthResponsee_error() throws JSONException, HttpException {
197
198                 ResponseBuilder responseBuilder = Response.ok();
199                 PowerMockito
200                                 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
201                                                 "password"))
202                                 .thenReturn(
203                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
204
205                 mrBaseClient.getAuthResponse("/path", null, null, null, null, "HTTPAUTH");
206
207         }
208
209         @Test
210         public void testPostAuth() throws JSONException, HttpException {
211
212                 ResponseBuilder responseBuilder = Response.ok();
213                 PowerMockito
214                                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
215                                                 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
216                                 .thenReturn(
217                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
218
219                 mrBaseClient.postAuth("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username",
220                                 "password", "username", "password", "HTTPAUTH");
221                 assertTrue(true);
222
223         }
224
225         @Test(expected = HttpException.class)
226         public void testPostAuth_error() throws JSONException, HttpException {
227
228                 ResponseBuilder responseBuilder = Response.ok();
229                 PowerMockito
230                                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
231                                                 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
232                                 .thenReturn(
233                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
234
235                 mrBaseClient.postAuth("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null, null,
236                                 null, null, "HTTPAUTH");
237                 assertTrue(true);
238
239         }
240
241         @Test
242         public void testGetNoAuthResponse() throws JSONException, HttpException {
243
244                 ResponseBuilder responseBuilder = Response.ok();
245                 PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(
246                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
247
248                 mrBaseClient.getNoAuthResponse("/path", "username", "password", "HTTPAUTH");
249                 assertTrue(true);
250
251         }
252
253         @Test
254         public void testPost() throws JSONException, HttpException {
255
256                 ResponseBuilder responseBuilder = Response.ok();
257                 PowerMockito
258                                 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
259                                                 Base64.encodeAsString("username:password")))
260                                 .thenReturn(
261                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
262
263                 mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", "username",
264                                 "password", "HTTPAUTH");
265                 assertTrue(true);
266
267         }
268
269         @Test(expected = HttpException.class)
270         public void testPost_error() throws JSONException, HttpException {
271
272                 ResponseBuilder responseBuilder = Response.ok();
273                 PowerMockito
274                                 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
275                                                 Base64.encodeAsString("username:password")))
276                                 .thenReturn(
277                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
278
279                 mrBaseClient.post("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null, null,
280                                 "HTTPAUTH");
281
282         }
283
284         @Test
285         public void testPostAuthwithResponse() throws JSONException, HttpException {
286
287                 ResponseBuilder responseBuilder = Response.ok();
288                 PowerMockito
289                                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
290                                                 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
291                                 .thenReturn(
292                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
293
294                 mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
295                                 "username", "password", "username", "password", "HTTPAUTH");
296                 assertTrue(true);
297
298         }
299
300         @Test(expected = HttpException.class)
301         public void testPostAuthwithResponse_error() throws JSONException, HttpException {
302
303                 ResponseBuilder responseBuilder = Response.ok();
304                 PowerMockito
305                                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
306                                                 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
307                                 .thenReturn(
308                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
309
310                 mrBaseClient.postAuthwithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
311                                 null, null, null, null, "HTTPAUTH");
312                 assertTrue(true);
313
314         }
315
316         @Test
317         public void testPostWithResponse() throws JSONException, HttpException {
318
319                 ResponseBuilder responseBuilder = Response.ok();
320                 PowerMockito
321                                 .when(DmaapClientUtil.getResponsewtBasicAuth(DmaapClientUtil.getTarget("/path"),
322                                                 Base64.encodeAsString("username:password")))
323                                 .thenReturn(
324                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
325
326                 mrBaseClient.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json",
327                                 "username", "password", "HTTPAUTH");
328                 assertTrue(true);
329
330         }
331
332         @Test(expected = HttpException.class)
333         public void testPostWithResponse_error() throws JSONException, HttpException {
334
335                 ResponseBuilder responseBuilder = Response.ok();
336                 PowerMockito
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.postWithResponse("/path", new String("{\"test\":\"test\"}").getBytes(), "application/json", null,
343                                 null, "HTTPAUTH");
344
345         }
346
347         @Test
348         public void testGetAuth() throws JSONException, HttpException {
349
350                 ResponseBuilder responseBuilder = Response.ok();
351                 PowerMockito
352                                 .when(DmaapClientUtil.getResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
353                                                 "password"))
354                                 .thenReturn(
355                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
356                 mrBaseClient.getAuth("/path", "username", "password", "username", "password", "HTTPAUTH");
357                 assertTrue(true);
358
359         }
360
361         @Test(expected = HttpException.class)
362         public void testGetAuth_error() throws JSONException, HttpException {
363
364                 ResponseBuilder responseBuilder = Response.ok();
365                 PowerMockito
366                                 .when(DmaapClientUtil.postResponsewtCambriaAuth(DmaapClientUtil.getTarget("/path"), "username",
367                                                 "password", new String("{\"test\":\"test\"}").getBytes(), "application/json"))
368                                 .thenReturn(
369                                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
370
371                 mrBaseClient.getAuth("/path", null, null, null, null, "HTTPAUTH");
372                 assertTrue(true);
373
374         }
375
376         @Test
377         public void testGetNoAuth() throws JSONException, HttpException {
378
379                 ResponseBuilder responseBuilder = Response.ok();
380                 PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(
381                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
382                 mrBaseClient.getNoAuth("/path", "username", "password", "HTTPAUTH");
383                 assertTrue(true);
384
385         }
386
387         @Test(expected = HttpException.class)
388         public void testGetNoAuth_error() throws JSONException, HttpException {
389
390                 ResponseBuilder responseBuilder = Response.ok();
391                 PowerMockito.when(DmaapClientUtil.getResponsewtNoAuth(DmaapClientUtil.getTarget("/path"))).thenReturn(
392                                 responseBuilder.header("transactionid", "transactionid").entity("{\"test\":\"test\"}").build());
393                 mrBaseClient.getNoAuth("/path", null, null, "HTTPAUTH");
394                 assertTrue(true);
395
396         }
397
398         @Test
399         public void testGetHTTPErrorResponseMessage() {
400
401                 assertEquals(mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"), "testtest");
402
403         }
404
405         @Test
406         public void getGTTPErrorResponseCode() {
407
408                 assertEquals(mrBaseClient.getHTTPErrorResponseMessage("<body>testtest</body>"), "testtest");
409
410         }
411
412 }