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.junit.Assert.fail;
25 import static org.mockito.Matchers.anyString;
27 import java.io.ByteArrayInputStream;
28 import java.io.FileInputStream;
30 //import static org.mockito.Matchers.anyString;
31 //import static org.mockito.Mockito.when;
33 import java.io.IOException;
34 import java.io.InputStream;
36 import javax.servlet.ServletOutputStream;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
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;
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;
80 //@RunWith(MockitoJUnitRunner.class)
81 @RunWith(PowerMockRunner.class)
82 @PrepareForTest({ PropertiesMapBean.class, AJSCPropertiesMap.class })
83 public class MMRestServiceTest {
86 MMRestService mmRestService;
89 private MMService mmservice;
92 CreateMirrorMaker cMirroMaker;
95 UpdateMirrorMaker uMirroMaker;
97 private TopicRestService service = new TopicRestService();
99 private DMaaPErrorMessages errorMessages;
102 DMaaPContext dmaapContext;
105 ConfigurationReader configReader;
108 ServletOutputStream oStream;
111 DMaaPAuthenticator<NsaSimpleApiKey> dmaaPAuthenticator;
117 DMaaPAAFAuthenticator dmaapAAFauthenticator;
120 DMaaPAAFAuthenticatorImpl impl;
126 NsaSimpleApiKey nsaSimpleApiKey;
129 HttpServletRequest httpServReq;
132 HttpServletResponse httpServRes;
138 DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
153 public void setUp() throws Exception {
155 MockitoAnnotations.initMocks(this);
159 public void tearDown() throws Exception {
163 public void testCallCreateMirrorMaker() throws Exception {
164 prepareForTestCommon();
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);
176 public void testCallCreateMirrorMaker_error4() throws Exception {
177 prepareForTestCommon();
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);
189 public void testCallCreateMirrorMaker_3() throws Exception {
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());
197 mmRestService.callCreateMirrorMaker(inputSteam);
202 public void testCallCreateMirrorMaker_error2() throws Exception {
203 prepareForTestCommon();
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);
216 public void testCallCreateMirrorMaker_error1() throws Exception {
217 prepareForTestCommon();
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);
230 public void testCallListAllMirrorMaker() throws Exception {
231 prepareForTestCommon();
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);
240 public void testCallUpdateMirrorMaker() throws Exception {
241 prepareForTestCommon();
243 String sampleJson = "{ messageID:\"test\", updateMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\"}}";
244 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
245 mmRestService.callUpdateMirrorMaker(inputSteam);
250 public void testCallUpdateMirrorMaker_error1() throws Exception {
251 prepareForTestCommon();
253 String sampleJson = "{ messageID:\"test@1\", updateMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\"}}";
254 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
255 mmRestService.callUpdateMirrorMaker(inputSteam);
259 public void testCallUpdateMirrorMaker_error2() throws Exception {
260 prepareForTestCommon();
262 String sampleJson = "{ messageID:\"test\", updateMirrorMaker: { name:\"\", consumer:\"test\", producer:\"test\"}}";
263 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
264 mmRestService.callUpdateMirrorMaker(inputSteam);
268 public void testCallUpdateMirrorMaker_error3() throws Exception{
269 prepareForTestCommon();
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);
277 public void testCallUpdateMirrorMaker_error4() throws Exception {
278 prepareForTestCommon();
280 String sampleJson = "{ messageID:\"test\"}}";
281 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
282 mmRestService.callUpdateMirrorMaker(inputSteam);
287 public void testCallDeleteMirrorMaker() throws Exception {
288 prepareForTestCommon();
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);
297 public void testListWhiteList() throws Exception {
298 prepareForTestCommon();
300 String sampleJson = "{ name:\"test\", namespace:\"test\"}}";
301 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
302 mmRestService.listWhiteList(inputSteam);
307 public void testCreateWhiteList() throws Exception {
308 prepareForTestCommon();
309 String sampleJson = "{ name:\"test\", namespace:\"test\", whitelistTopicName:\"test\"}}";
310 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
312 mmRestService.createWhiteList(inputSteam);
317 public void testDeleteWhiteList() throws Exception {
318 prepareForTestCommon();
320 String sampleJson = "{ name:\"test\", namespace:\"test\", whitelistTopicName:\"test\"}}";
321 InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
322 mmRestService.deleteWhiteList(inputSteam);
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);
332 PowerMockito.mockStatic(AJSCPropertiesMap.class);
336 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeradmin.aaf"))
337 .thenReturn("admin");
339 .when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf.create"))
340 .thenReturn("aafcreate");
342 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf"))
343 .thenReturn("admin");
345 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.timeout"))
348 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.topic"))
349 .thenReturn("mirrormaker.topic");
352 .when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumergroup"))
353 .thenReturn("mirrormaker.consumergroup");
355 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumerid"))
356 .thenReturn("mirrormaker.consumerid");
358 PowerMockito.when(dmaapContext.getRequest()).thenReturn(httpServReq);
360 PowerMockito.when(httpServReq.isUserInRole("admin")).thenReturn(true);
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"))
370 PowerMockito.when(cMirroMaker.getCreateMirrorMaker()).thenReturn(mMaker);
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");
377 PowerMockito.when(dmaapContext.getResponse()).thenReturn(httpServRes);
378 PowerMockito.when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
379 PowerMockito.when(httpServReq.getMethod()).thenReturn("HEAD");
381 PowerMockito.when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);