2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.service;
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Matchers.anyString;
27 import com.att.ajsc.beans.PropertiesMapBean;
28 import com.att.ajsc.filemonitor.AJSCPropertiesMap;
29 import com.att.nsa.configs.ConfigDbException;
30 import com.att.nsa.security.NsaAcl;
31 import com.att.nsa.security.NsaApiKey;
32 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
33 import java.io.ByteArrayInputStream;
34 import java.io.InputStream;
35 import javax.servlet.ServletOutputStream;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38 import org.json.JSONArray;
39 import org.json.JSONException;
40 import org.json.JSONObject;
41 import org.junit.After;
42 import org.junit.Assert;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.InjectMocks;
47 import org.mockito.Mock;
48 import org.mockito.MockitoAnnotations;
49 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
50 import org.onap.dmaap.dmf.mr.beans.DMaaPKafkaMetaBroker;
51 import org.onap.dmaap.dmf.mr.constants.CambriaConstants;
52 import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;
53 import org.onap.dmaap.dmf.mr.metabroker.Topic;
54 import org.onap.dmaap.dmf.mr.security.DMaaPAAFAuthenticator;
55 import org.onap.dmaap.dmf.mr.security.DMaaPAAFAuthenticatorImpl;
56 import org.onap.dmaap.dmf.mr.security.DMaaPAuthenticator;
57 import org.onap.dmaap.dmf.mr.service.MMService;
58 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
59 import org.onap.dmaap.mmagent.CreateMirrorMaker;
60 import org.onap.dmaap.mmagent.MirrorMaker;
61 import org.onap.dmaap.mmagent.UpdateMirrorMaker;
62 import org.powermock.api.mockito.PowerMockito;
63 import org.powermock.core.classloader.annotations.PowerMockIgnore;
64 import org.powermock.core.classloader.annotations.PrepareForTest;
65 import org.powermock.modules.junit4.PowerMockRunner;
67 //@RunWith(MockitoJUnitRunner.class)
68 @RunWith(PowerMockRunner.class)
69 @PowerMockIgnore("jdk.internal.reflect.*")
70 @PrepareForTest({ PropertiesMapBean.class, AJSCPropertiesMap.class })
71 public class MMRestServiceTest {
74 MMRestService mmRestService;
77 private MMService mmservice;
80 CreateMirrorMaker cMirroMaker;
83 UpdateMirrorMaker uMirroMaker;
85 private TopicRestService service = new TopicRestService();
87 private DMaaPErrorMessages errorMessages;
90 DMaaPContext dmaapContext;
93 ConfigurationReader configReader;
96 ServletOutputStream oStream;
99 DMaaPAuthenticator<NsaSimpleApiKey> dmaaPAuthenticator;
105 DMaaPAAFAuthenticator dmaapAAFauthenticator;
108 DMaaPAAFAuthenticatorImpl impl;
114 NsaSimpleApiKey nsaSimpleApiKey;
117 HttpServletRequest httpServReq;
120 HttpServletResponse httpServRes;
126 DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
141 public void setUp() {
143 MockitoAnnotations.initMocks(this);
147 public void tearDown() {
151 public void testCallCreateMirrorMaker() {
152 prepareForTestCommon();
154 // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: {
155 // name:\"test\", consumer:\"test\", producer:\"test\",
156 // whitelist:\"test\",status:\"test\" }}";
157 String sampleJson = "{ messageID:\"test\", createMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\"}}";
158 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
160 mmRestService.callCreateMirrorMaker(inputSteam);
161 } catch (Exception e) {
168 public void testCallCreateMirrorMaker_error4() {
170 prepareForTestCommon();
171 } catch (Exception e) {
175 // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: {
176 // name:\"test\", consumer:\"test\", producer:\"test\",
177 // whitelist:\"test\",status:\"test\" }}";
178 String sampleJson = "{ messageID:\"test\", createMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\"}}";
179 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
181 mmRestService.callCreateMirrorMaker(inputSteam);
182 } catch (Exception e) {
189 public void testCallCreateMirrorMaker_3() {
190 prepareForTestCommon();
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());
201 public void testCallCreateMirrorMaker_error2() {
202 prepareForTestCommon();
204 // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: {
205 // name:\"test\", consumer:\"test\", producer:\"test\",
206 // whitelist:\"test\",status:\"test\" }}";
207 String sampleJson = "{ messageID:\"test\", createMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\",whitelist:\"test\"}}";
208 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
210 mmRestService.callCreateMirrorMaker(inputSteam);
211 } catch (Exception e) {
219 public void testCallCreateMirrorMaker_error1() {
220 prepareForTestCommon();
222 // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: {
223 // name:\"test\", consumer:\"test\", producer:\"test\",
224 // whitelist:\"test\",status:\"test\" }}";
225 String sampleJson = "{ messageID:\"test\"}";
226 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
228 mmRestService.callCreateMirrorMaker(inputSteam);
229 } catch (Exception e) {
237 public void testCallCreateMirrorMakerCreateAafPermissionError() {
238 prepareForTestCommon();
240 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeradmin.aaf"))
242 String sampleJson = "{ messageID:\"test\"}";
243 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
245 mmRestService.callCreateMirrorMaker(inputSteam);
246 } catch (Exception e) {
254 public void testCallListAllMirrorMaker() {
255 prepareForTestCommon();
257 String sampleJson = "{ messageID:\"test\", createMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\", whitelist:\"test\",status:\"test\" }}";
258 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
260 mmRestService.callListAllMirrorMaker(inputSteam);
261 } catch (Exception e) {
268 public void testCallListAllMirrorMakerPermissionError() {
269 prepareForTestCommon();
270 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeradmin.aaf"))
272 String sampleJson = "{ messageID:\"test\", createMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\", whitelist:\"test\",status:\"test\" }}";
273 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
275 mmRestService.callListAllMirrorMaker(inputSteam);
276 } catch (Exception e) {
283 public void testCallUpdateMirrorMaker() {
284 prepareForTestCommon();
286 String sampleJson = "{ messageID:\"test\", updateMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\"}}";
287 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
289 mmRestService.callUpdateMirrorMaker(inputSteam);
290 } catch (Exception e) {
297 public void testCallUpdateMirrorMaker_error1() {
298 prepareForTestCommon();
300 String sampleJson = "{ messageID:\"test@1\", updateMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\"}}";
301 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
303 mmRestService.callUpdateMirrorMaker(inputSteam);
304 } catch (Exception e) {
310 public void testCallUpdateMirrorMaker_error2() {
311 prepareForTestCommon();
313 String sampleJson = "{ messageID:\"test\", updateMirrorMaker: { name:\"\", consumer:\"test\", producer:\"test\"}}";
314 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
316 mmRestService.callUpdateMirrorMaker(inputSteam);
317 } catch (Exception e) {
323 public void testCallUpdateMirrorMaker_error3() {
324 prepareForTestCommon();
326 String sampleJson = "{ messageID:\"test\", updateMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\", whitelist:\"test\",status:\"test\"}}";
327 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
329 mmRestService.callUpdateMirrorMaker(inputSteam);
330 } catch (Exception e) {
336 public void testCallUpdateMirrorMaker_error4() {
337 prepareForTestCommon();
339 String sampleJson = "{ messageID:\"test\"}}";
340 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
342 mmRestService.callUpdateMirrorMaker(inputSteam);
343 } catch (Exception e) {
350 public void testCallUpdateMirrorMakerAafPermissionError() {
351 prepareForTestCommon();
353 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeradmin.aaf"))
356 String sampleJson = "{ messageID:\"test\"}}";
357 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
359 mmRestService.callUpdateMirrorMaker(inputSteam);
360 } catch (Exception e) {
367 public void testCallDeleteMirrorMaker() {
368 prepareForTestCommon();
370 String sampleJson = "{ messageID:\"test\", deleteMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\", whitelist:\"test\",status:\"test\" }}";
371 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
373 mmRestService.callDeleteMirrorMaker(inputSteam);
374 } catch (JSONException e) {
376 } catch (Exception e) {
384 public void testCallDeleteMirrorMakerAafPermissionError() {
385 prepareForTestCommon();
386 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeradmin.aaf"))
388 String sampleJson = "{ messageID:\"test\", deleteMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\", whitelist:\"test\",status:\"test\" }}";
389 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
391 mmRestService.callDeleteMirrorMaker(inputSteam);
392 } catch (JSONException e) {
394 } catch (Exception e) {
402 public void testListWhiteList() {
403 prepareForTestCommon();
405 String sampleJson = "{ name:\"test\", namespace:\"test\"}}";
406 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
407 String msgSubscribe = "[{ messageID:\"test123\", listMirrorMaker:[ {name: \"test\"}]}]";
410 PowerMockito.when(mmservice.subscribe(any(), anyString(), anyString(), anyString())).thenReturn(msgSubscribe);
411 mmRestService.listWhiteList(inputSteam);
412 } catch (Exception e) {
419 public void testListWhiteListAafPermissionError() {
420 prepareForTestCommon();
422 String sampleJson = "{ name:\"test\", namespace:\"test\"}}";
423 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
424 String msgSubscribe = "[{ messageID:\"test123\", listMirrorMaker:[ {name: \"test\"}]}]";
427 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf"))
429 mmRestService.listWhiteList(inputSteam);
430 } catch (Exception e) {
437 public void testListWhiteListCreatePermissionError() {
438 prepareForTestCommon();
440 String sampleJson = "{ name:\"test\", namespace:\"test\"}}";
441 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
442 String msgSubscribe = "[{ messageID:\"test123\", listMirrorMaker:[ {name: \"test\"}]}]";
445 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf.create"))
447 mmRestService.listWhiteList(inputSteam);
448 } catch (Exception e) {
454 public void testListWhiteListJSONError() {
455 prepareForTestCommon();
457 String sampleJson = "{ namespace:\"test\"}}";
458 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
459 String msgSubscribe = "[{ messageID:\"test123\", listMirrorMaker:[ {name: \"test\"}]}]";
462 mmRestService.listWhiteList(inputSteam);
463 } catch (Exception e) {
473 public void testCreateWhiteList() {
474 prepareForTestCommon();
476 String sampleJson = "{ name:\"test\", namespace:\"test\", whitelistTopicName:\"test\"}}";
477 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
480 mmRestService.createWhiteList(inputSteam);
481 } catch (Exception e) {
488 public void testCreateWhiteListCreatePermissionError() {
489 prepareForTestCommon();
492 .when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf.create"))
495 String sampleJson = "{ name:\"test\", namespace:\"test\", whitelistTopicName:\"test\"}}";
496 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
499 mmRestService.createWhiteList(inputSteam);
500 } catch (Exception e) {
507 public void testCreateWhiteListAafPermissionError() {
508 prepareForTestCommon();
510 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf"))
513 String sampleJson = "{ name:\"test\", namespace:\"test\", whitelistTopicName:\"test\"}}";
514 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
517 mmRestService.createWhiteList(inputSteam);
518 } catch (Exception e) {
525 public void testCreateWhiteListJSONError() {
526 prepareForTestCommon();
528 String sampleJson = "{ namespace:\"test\", whitelistTopicName:\"test\"}}";
529 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
532 mmRestService.createWhiteList(inputSteam);
533 } catch (Exception e) {
540 public void testDeleteWhiteList() {
541 prepareForTestCommon();
543 String sampleJson = "{ name:\"test\", namespace:\"test\", whitelistTopicName:\"test\"}}";
544 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
546 mmRestService.deleteWhiteList(inputSteam);
547 } catch (Exception e) {
554 public void testDeleteWhiteListMirrorMakerPermissionError() {
555 prepareForTestCommon();
557 .when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf.create"))
559 String sampleJson = "{ name:\"test\", namespace:\"test\", whitelistTopicName:\"test\"}}";
560 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
562 mmRestService.deleteWhiteList(inputSteam);
563 } catch (Exception e) {
571 public void testDeleteWhiteListMirrorMakerAafPermissionError() {
572 prepareForTestCommon();
574 .when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf"))
576 String sampleJson = "{ name:\"test\", namespace:\"test\", whitelistTopicName:\"test\"}}";
577 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
579 mmRestService.deleteWhiteList(inputSteam);
580 } catch (Exception e) {
588 public void testDeleteWhiteListJsonError() {
589 prepareForTestCommon();
591 String sampleJson = "{ namespace:\"test\", whitelistTopicName:\"test\"}}";
592 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
594 mmRestService.deleteWhiteList(inputSteam);
595 } catch (Exception e) {
602 public void testDeleteWhiteListJsonFormattingError() {
603 prepareForTestCommon();
605 String sampleJson = "{ : namespace:\"test\", whitelistTopicName:\"test\"}}";
606 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
608 mmRestService.deleteWhiteList(inputSteam);
609 } catch (Exception e) {
616 public void testCallPubSubForWhitelist() {
617 prepareForTestCommon();
619 String sampleJson = "{ name:\"test\", namespace:\"test\", whitelistTopicName:\"test\"}}";
620 String msgSubscribe = "[{ messageID:\"test123\", listMirrorMaker:[ {name: \"test\"}]}]";
621 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
623 PowerMockito.when(mmservice.subscribe(any(), anyString(), anyString(), anyString())).thenReturn(msgSubscribe);
624 mmRestService.callPubSubForWhitelist("test123", dmaapContext, inputSteam, new JSONObject (sampleJson)) ;
625 } catch (Exception e) {
631 public void testCallPubSub() {
632 prepareForTestCommon();
634 String sampleJson = "{ name:\"test\", namespace:\"test\", whitelistTopicName:\"test\"}}";
635 String msgSubscribe = "[{ messageID:\"test123\", listMirrorMaker:[ {name: \"test\"}]}]";
636 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
638 PowerMockito.when(mmservice.subscribe(any(), anyString(), anyString(), anyString())).thenReturn(msgSubscribe);
639 mmRestService.callPubSub("test123", dmaapContext, inputSteam, "test", false) ;
640 } catch (Exception e) {
646 public void testCallPubSubForWhitelistNoMsgFromSubscribe() {
647 prepareForTestCommon();
649 String sampleJson = "{ name:\"test\", namespace:\"test\", whitelistTopicName:\"test\"}}";
650 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
652 PowerMockito.when(mmservice.subscribe(any(), anyString(), anyString(), anyString())).thenReturn(null);
653 mmRestService.callPubSubForWhitelist("test123", dmaapContext, inputSteam, new JSONObject (sampleJson)) ;
654 } catch (Exception e) {
660 public void testGetListMirrorMaker() {
661 prepareForTestCommon();
663 String sampleJson = "[{ messageID:\"test123\", listMirrorMaker:[\"test\"]}]";
665 mmRestService.getListMirrorMaker(sampleJson, "test123");
666 } catch (Exception e) {
671 private void prepareForTestCommon() {
672 Assert.assertNotNull(mmRestService);
674 PowerMockito.when(dmaapContext.getRequest()).thenReturn(httpServReq);
675 PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true);
676 PowerMockito.when(httpServReq.isUserInRole("admin")).thenReturn(true);
678 PowerMockito.mockStatic(AJSCPropertiesMap.class);
682 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeradmin.aaf"))
683 .thenReturn("admin");
685 .when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf.create"))
686 .thenReturn("aafcreate");
688 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf"))
689 .thenReturn("admin");
691 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.timeout"))
694 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.topic"))
695 .thenReturn("mirrormaker.topic");
698 .when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumergroup"))
699 .thenReturn("mirrormaker.consumergroup");
701 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumerid"))
702 .thenReturn("mirrormaker.consumerid");
704 PowerMockito.when(dmaapContext.getRequest()).thenReturn(httpServReq);
706 PowerMockito.when(httpServReq.isUserInRole("admin")).thenReturn(true);
708 // PowerMockito.when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
709 PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin.aaf")).thenReturn(true);
710 PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true);
711 PowerMockito.when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
712 PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true);
713 PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "aafcreatetest|create"))
716 PowerMockito.when(cMirroMaker.getCreateMirrorMaker()).thenReturn(mMaker);
718 PowerMockito.when(mMaker.getName()).thenReturn("mirroMakerName");
719 PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);
720 PowerMockito.when(dmaapContext.getRequest()).thenReturn(httpServReq);
721 PowerMockito.when(httpServReq.getHeader("Authorization")).thenReturn("Authorization");
723 PowerMockito.when(dmaapContext.getResponse()).thenReturn(httpServRes);
724 PowerMockito.when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
725 PowerMockito.when(httpServReq.getMethod()).thenReturn("HEAD");
728 PowerMockito.when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);
729 } catch (ConfigDbException e) {