4832d842beeb2e438f56afbad345d250928d5d30
[dmaap/messagerouter/messageservice.git] / src / test / java / org / onap / dmaap / service / MMRestServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 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  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21  package org.onap.dmaap.service;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 import static org.mockito.Matchers.anyString;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.FileInputStream;
29
30 //import static org.mockito.Matchers.anyString;
31 //import static org.mockito.Mockito.when;
32
33 import java.io.IOException;
34 import java.io.InputStream;
35
36 import javax.servlet.ServletOutputStream;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39
40 import org.apache.commons.io.IOUtils;
41 import org.json.JSONArray;
42 import org.json.JSONException;
43 import org.json.JSONObject;
44 import org.junit.After;
45 import org.junit.Assert;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.InjectMocks;
50 import org.mockito.Mock;
51 import org.mockito.MockitoAnnotations;
52 import org.powermock.api.mockito.PowerMockito;
53 import org.powermock.core.classloader.annotations.PrepareForTest;
54 import org.powermock.modules.junit4.PowerMockRunner;
55
56 import com.att.ajsc.beans.PropertiesMapBean;
57 import com.att.ajsc.filemonitor.AJSCPropertiesMap;
58 import org.onap.dmaap.dmf.mr.CambriaApiException;
59 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
60 import org.onap.dmaap.dmf.mr.beans.DMaaPKafkaMetaBroker;
61 import org.onap.dmaap.dmf.mr.constants.CambriaConstants;
62 import org.onap.dmaap.dmf.mr.exception.DMaaPAccessDeniedException;
63 import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;
64 import org.onap.dmaap.dmf.mr.metabroker.Broker.TopicExistsException;
65 import org.onap.dmaap.dmf.mr.metabroker.Topic;
66 import org.onap.dmaap.dmf.mr.security.DMaaPAAFAuthenticator;
67 import org.onap.dmaap.dmf.mr.security.DMaaPAAFAuthenticatorImpl;
68 import org.onap.dmaap.dmf.mr.security.DMaaPAuthenticator;
69 import org.onap.dmaap.dmf.mr.service.MMService;
70 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
71 import com.att.nsa.configs.ConfigDbException;
72 import org.onap.dmaap.mmagent.CreateMirrorMaker;
73 import org.onap.dmaap.mmagent.MirrorMaker;
74 import org.onap.dmaap.mmagent.UpdateMirrorMaker;
75 import com.att.nsa.security.NsaAcl;
76 import com.att.nsa.security.NsaApiKey;
77 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
78 import com.google.gson.Gson;
79
80 //@RunWith(MockitoJUnitRunner.class)
81 @RunWith(PowerMockRunner.class)
82 @PrepareForTest({ PropertiesMapBean.class, AJSCPropertiesMap.class })
83 public class MMRestServiceTest {
84
85         @InjectMocks
86         MMRestService mmRestService;
87
88         @Mock
89         private MMService mmservice;
90
91         @Mock
92         CreateMirrorMaker cMirroMaker;
93
94         @Mock
95         UpdateMirrorMaker uMirroMaker;
96
97         private TopicRestService service = new TopicRestService();
98         @Mock
99         private DMaaPErrorMessages errorMessages;
100
101         @Mock
102         DMaaPContext dmaapContext;
103
104         @Mock
105         ConfigurationReader configReader;
106
107         @Mock
108         ServletOutputStream oStream;
109
110         @Mock
111         DMaaPAuthenticator<NsaSimpleApiKey> dmaaPAuthenticator;
112
113         @Mock
114         MirrorMaker mMaker;
115
116         @Mock
117         DMaaPAAFAuthenticator dmaapAAFauthenticator;
118
119         @Mock
120         DMaaPAAFAuthenticatorImpl impl;
121
122         @Mock
123         NsaApiKey user;
124
125         @Mock
126         NsaSimpleApiKey nsaSimpleApiKey;
127
128         @Mock
129         HttpServletRequest httpServReq;
130
131         @Mock
132         HttpServletResponse httpServRes;
133
134         @Mock
135         InputStream iStream;
136
137         @Mock
138         DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
139
140         @Mock
141         Topic createdTopic;
142
143         @Mock
144         NsaAcl nsaAcl;
145
146         @Mock
147         JSONObject jsonObj;
148
149         @Mock
150         JSONArray jsonArray;
151
152         @Before
153         public void setUp() throws Exception {
154
155                 MockitoAnnotations.initMocks(this);
156         }
157
158         @After
159         public void tearDown() throws Exception {
160         }
161
162         @Test
163         public void testCallCreateMirrorMaker() throws Exception {
164                 prepareForTestCommon();
165
166                 // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: {
167                 // name:\"test\", consumer:\"test\", producer:\"test\",
168                 // whitelist:\"test\",status:\"test\" }}";
169                 String sampleJson = "{ messageID:\"test\", createMirrorMaker: {   name:\"test\",   consumer:\"test\",  producer:\"test\"}}";
170                 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
171                 mmRestService.callCreateMirrorMaker(inputSteam);
172                 assertTrue(true);
173
174         }
175         @Test
176         public void testCallCreateMirrorMaker_error4() throws Exception {
177                 prepareForTestCommon();
178
179                 // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: {
180                 // name:\"test\", consumer:\"test\", producer:\"test\",
181                 // whitelist:\"test\",status:\"test\" }}";
182                 String sampleJson = "{ messageID:\"test\", createMirrorMaker: {   name:\"test@#\",   consumer:\"test\",  producer:\"test\"}}";
183                 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
184                 mmRestService.callCreateMirrorMaker(inputSteam);
185                 assertTrue(true);
186
187         }
188         @Test
189         public void testCallCreateMirrorMaker_3() throws Exception {
190                 prepareForTestCommon();
191
192                 // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: {
193                 // name:\"test\", consumer:\"test\", producer:\"test\",
194                 // whitelist:\"test\",status:\"test\" }}";
195                 String sampleJson = "{ messageID:\"test\", createMirrorMaker: {   name:\"\",   consumer:\"test\",  producer:\"test\"}}";
196                 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
197                 mmRestService.callCreateMirrorMaker(inputSteam);
198                 assertTrue(true);
199
200         }
201         @Test
202         public void testCallCreateMirrorMaker_error2() throws Exception {
203                 prepareForTestCommon();
204
205                 // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: {
206                 // name:\"test\", consumer:\"test\", producer:\"test\",
207                 // whitelist:\"test\",status:\"test\" }}";
208                 String sampleJson = "{ messageID:\"test\", createMirrorMaker: {   name:\"test\",   consumer:\"test\",  producer:\"test\",whitelist:\"test\"}}";
209                 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
210                 mmRestService.callCreateMirrorMaker(inputSteam);
211                 assertTrue(true);
212
213         }
214         
215         @Test
216         public void testCallCreateMirrorMaker_error1() throws Exception {
217                 prepareForTestCommon();
218
219                 // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: {
220                 // name:\"test\", consumer:\"test\", producer:\"test\",
221                 // whitelist:\"test\",status:\"test\" }}";
222                 String sampleJson = "{ messageID:\"test\"}";
223                 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
224                 mmRestService.callCreateMirrorMaker(inputSteam);
225                 assertTrue(true);
226
227         }
228
229         @Test
230         public void testCallListAllMirrorMaker() throws Exception {
231                 prepareForTestCommon();
232
233                 String sampleJson = "{ messageID:\"test\", createMirrorMaker: {   name:\"test\",   consumer:\"test\",  producer:\"test\",  whitelist:\"test\",status:\"test\" }}";
234                 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
235                 mmRestService.callListAllMirrorMaker(inputSteam);
236                 assertTrue(true);
237         }
238
239         @Test
240         public void testCallUpdateMirrorMaker() throws Exception {
241                 prepareForTestCommon();
242
243                 String sampleJson = "{ messageID:\"test\", updateMirrorMaker: {   name:\"test\",   consumer:\"test\",  producer:\"test\"}}";
244                 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
245                 mmRestService.callUpdateMirrorMaker(inputSteam);
246                 assertTrue(true);
247         }
248         
249         @Test
250         public void testCallUpdateMirrorMaker_error1() throws Exception {
251                 prepareForTestCommon();
252
253                 String sampleJson = "{ messageID:\"test@1\", updateMirrorMaker: {   name:\"test\",   consumer:\"test\",  producer:\"test\"}}";
254                 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
255                 mmRestService.callUpdateMirrorMaker(inputSteam);
256                 assertTrue(true);
257         }
258         @Test
259         public void testCallUpdateMirrorMaker_error2() throws Exception {
260                 prepareForTestCommon();
261
262                 String sampleJson = "{ messageID:\"test\", updateMirrorMaker: {   name:\"\",   consumer:\"test\",  producer:\"test\"}}";
263                 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
264                 mmRestService.callUpdateMirrorMaker(inputSteam);
265                 assertTrue(true);
266         }
267         @Test
268         public void testCallUpdateMirrorMaker_error3() throws Exception{
269                 prepareForTestCommon();
270
271                 String sampleJson = "{ messageID:\"test\", updateMirrorMaker: {   name:\"test\",   consumer:\"test\",  producer:\"test\",  whitelist:\"test\",status:\"test\"}}";
272                 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
273                 mmRestService.callUpdateMirrorMaker(inputSteam);
274                 assertTrue(true);
275         }
276         @Test
277         public void testCallUpdateMirrorMaker_error4() throws Exception {
278                 prepareForTestCommon();
279
280                 String sampleJson = "{ messageID:\"test\"}}";
281                 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
282                 mmRestService.callUpdateMirrorMaker(inputSteam);
283                 assertTrue(true);
284         }
285
286         @Test
287         public void testCallDeleteMirrorMaker() throws Exception {
288                 prepareForTestCommon();
289
290                 String sampleJson = "{ messageID:\"test\", deleteMirrorMaker: {   name:\"test\",   consumer:\"test\",  producer:\"test\",  whitelist:\"test\",status:\"test\" }}";
291                 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
292                 mmRestService.callDeleteMirrorMaker(inputSteam);
293                 assertTrue(true);
294         }
295
296         @Test
297         public void testListWhiteList() throws Exception {
298                 prepareForTestCommon();
299
300                 String sampleJson = "{ name:\"test\", namespace:\"test\"}}";
301                 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
302                 mmRestService.listWhiteList(inputSteam);
303                 assertTrue(true);
304         }
305
306         @Test
307         public void testCreateWhiteList() throws Exception {
308                 prepareForTestCommon();
309                 String sampleJson = "{ name:\"test\", namespace:\"test\",   whitelistTopicName:\"test\"}}";
310                 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
311
312                 mmRestService.createWhiteList(inputSteam);
313                 assertTrue(true);
314         }
315
316         @Test
317         public void testDeleteWhiteList() throws Exception {
318                 prepareForTestCommon();
319
320                 String sampleJson = "{ name:\"test\", namespace:\"test\",   whitelistTopicName:\"test\"}}";
321                 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
322                 mmRestService.deleteWhiteList(inputSteam);
323                 assertTrue(true);
324         }
325
326         private void prepareForTestCommon() throws Exception {
327                 Assert.assertNotNull(mmRestService);
328                 PowerMockito.when(dmaapContext.getRequest()).thenReturn(httpServReq);
329                 PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true);
330                 PowerMockito.when(httpServReq.isUserInRole("admin")).thenReturn(true);
331
332                 PowerMockito.mockStatic(AJSCPropertiesMap.class);
333
334                 assertTrue(true);
335
336                 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeradmin.aaf"))
337                                 .thenReturn("admin");
338                 PowerMockito
339                                 .when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf.create"))
340                                 .thenReturn("aafcreate");
341
342                 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf"))
343                                 .thenReturn("admin");
344
345                 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.timeout"))
346                                 .thenReturn("100");
347
348                 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.topic"))
349                                 .thenReturn("mirrormaker.topic");
350
351                 PowerMockito
352                                 .when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumergroup"))
353                                 .thenReturn("mirrormaker.consumergroup");
354
355                 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumerid"))
356                                 .thenReturn("mirrormaker.consumerid");
357
358                 PowerMockito.when(dmaapContext.getRequest()).thenReturn(httpServReq);
359
360                 PowerMockito.when(httpServReq.isUserInRole("admin")).thenReturn(true);
361
362                 // PowerMockito.when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
363                 PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin.aaf")).thenReturn(true);
364                 PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true);
365                 PowerMockito.when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
366                 PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true);
367                 PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "aafcreatetest|create"))
368                                 .thenReturn(true);
369
370                 PowerMockito.when(cMirroMaker.getCreateMirrorMaker()).thenReturn(mMaker);
371
372                 PowerMockito.when(mMaker.getName()).thenReturn("mirroMakerName");
373                 PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);
374                 PowerMockito.when(dmaapContext.getRequest()).thenReturn(httpServReq);
375                 PowerMockito.when(httpServReq.getHeader("Authorization")).thenReturn("Authorization");
376
377                 PowerMockito.when(dmaapContext.getResponse()).thenReturn(httpServRes);
378                 PowerMockito.when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
379                 PowerMockito.when(httpServReq.getMethod()).thenReturn("HEAD");
380
381                 PowerMockito.when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);
382         }
383
384 }