2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package com.att.nsa.dmaap.service;
\r
23 import static org.junit.Assert.assertTrue;
\r
24 import static org.junit.Assert.fail;
\r
25 import static org.mockito.Matchers.anyString;
\r
27 import java.io.FileInputStream;
\r
29 //import static org.mockito.Matchers.anyString;
\r
30 //import static org.mockito.Mockito.when;
\r
32 import java.io.IOException;
\r
33 import java.io.InputStream;
\r
35 import javax.servlet.ServletOutputStream;
\r
36 import javax.servlet.http.HttpServletRequest;
\r
37 import javax.servlet.http.HttpServletResponse;
\r
39 import org.apache.commons.io.IOUtils;
\r
40 import org.json.JSONArray;
\r
41 import org.json.JSONException;
\r
42 import org.json.JSONObject;
\r
43 import org.junit.After;
\r
44 import org.junit.Assert;
\r
45 import org.junit.Before;
\r
46 import org.junit.Test;
\r
47 import org.junit.runner.RunWith;
\r
48 import org.mockito.InjectMocks;
\r
49 import org.mockito.Mock;
\r
50 import org.mockito.MockitoAnnotations;
\r
51 import org.powermock.api.mockito.PowerMockito;
\r
52 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
53 import org.powermock.modules.junit4.PowerMockRunner;
\r
55 import com.att.ajsc.beans.PropertiesMapBean;
\r
56 import com.att.ajsc.filemonitor.AJSCPropertiesMap;
\r
57 import com.att.nsa.cambria.CambriaApiException;
\r
58 import com.att.nsa.cambria.beans.DMaaPContext;
\r
59 import com.att.nsa.cambria.beans.DMaaPKafkaMetaBroker;
\r
60 import com.att.nsa.cambria.constants.CambriaConstants;
\r
61 import com.att.nsa.cambria.exception.DMaaPAccessDeniedException;
\r
62 import com.att.nsa.cambria.exception.DMaaPErrorMessages;
\r
63 import com.att.nsa.cambria.metabroker.Broker.TopicExistsException;
\r
64 import com.att.nsa.cambria.metabroker.Topic;
\r
65 import com.att.nsa.cambria.security.DMaaPAAFAuthenticator;
\r
66 import com.att.nsa.cambria.security.DMaaPAAFAuthenticatorImpl;
\r
67 import com.att.nsa.cambria.security.DMaaPAuthenticator;
\r
68 import com.att.nsa.cambria.service.MMService;
\r
69 import com.att.nsa.cambria.utils.ConfigurationReader;
\r
70 import com.att.nsa.configs.ConfigDbException;
\r
71 import com.att.nsa.dmaap.mmagent.CreateMirrorMaker;
\r
72 import com.att.nsa.dmaap.mmagent.MirrorMaker;
\r
73 import com.att.nsa.dmaap.mmagent.UpdateMirrorMaker;
\r
74 import com.att.nsa.security.NsaAcl;
\r
75 import com.att.nsa.security.NsaApiKey;
\r
76 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
\r
77 import com.google.gson.Gson;
\r
79 //@RunWith(MockitoJUnitRunner.class)
\r
80 @RunWith(PowerMockRunner.class)
\r
81 @PrepareForTest({ PropertiesMapBean.class, AJSCPropertiesMap.class })
\r
82 public class MMRestServiceTest {
\r
85 MMRestService mmRestService;
\r
88 private MMService mmservice;
\r
91 CreateMirrorMaker cMirroMaker;
\r
94 UpdateMirrorMaker uMirroMaker;
\r
96 private TopicRestService service = new TopicRestService();
\r
98 private DMaaPErrorMessages errorMessages;
\r
101 DMaaPContext dmaapContext;
\r
104 ConfigurationReader configReader;
\r
107 ServletOutputStream oStream;
\r
110 DMaaPAuthenticator<NsaSimpleApiKey> dmaaPAuthenticator;
\r
113 MirrorMaker mMaker;
\r
116 DMaaPAAFAuthenticator dmaapAAFauthenticator;
\r
119 DMaaPAAFAuthenticatorImpl impl;
\r
125 NsaSimpleApiKey nsaSimpleApiKey;
\r
128 HttpServletRequest httpServReq;
\r
131 HttpServletResponse httpServRes;
\r
134 InputStream iStream;
\r
137 DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
\r
140 Topic createdTopic;
\r
146 JSONObject jsonObj;
\r
149 JSONArray jsonArray;
\r
152 public void setUp() throws Exception {
\r
154 MockitoAnnotations.initMocks(this);
\r
158 public void tearDown() throws Exception {
\r
162 public void testCallCreateMirrorMaker() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
\r
163 TopicExistsException, JSONException, ConfigDbException {
\r
164 prepareForTestCommon();
\r
165 mmRestService.callCreateMirrorMaker(iStream);
\r
170 public void testCallListAllMirrorMaker() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
\r
171 TopicExistsException, JSONException, ConfigDbException {
\r
172 prepareForTestCommon();
\r
173 mmRestService.callListAllMirrorMaker(iStream);
\r
178 public void testCallUpdateMirrorMaker() throws ConfigDbException, CambriaApiException {
\r
179 prepareForTestCommon();
\r
180 mmRestService.callUpdateMirrorMaker(iStream);
\r
185 public void testCallDeleteMirrorMaker() throws ConfigDbException, CambriaApiException {
\r
186 prepareForTestCommon();
\r
187 mmRestService.callDeleteMirrorMaker(iStream);
\r
192 public void testListWhiteList() throws ConfigDbException {
\r
193 prepareForTestCommon();
\r
194 mmRestService.listWhiteList(iStream);
\r
199 public void testCreateWhiteList() throws ConfigDbException {
\r
200 prepareForTestCommon();
\r
201 mmRestService.createWhiteList(iStream);
\r
206 public void testDeleteWhiteList() throws ConfigDbException {
\r
207 prepareForTestCommon();
\r
208 mmRestService.deleteWhiteList(iStream);
\r
212 private void prepareForTestCommon() throws ConfigDbException {
\r
213 Assert.assertNotNull(mmRestService);
\r
214 PowerMockito.when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
215 PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true);
\r
216 PowerMockito.when(httpServReq.isUserInRole("admin")).thenReturn(true);
\r
218 PowerMockito.mockStatic(AJSCPropertiesMap.class);
\r
222 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeradmin.aaf"))
\r
223 .thenReturn("admin");
\r
225 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf"))
\r
226 .thenReturn("admin");
\r
228 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.timeout"))
\r
229 .thenReturn("100");
\r
231 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.topic"))
\r
232 .thenReturn("mirrormaker.topic");
\r
235 .when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumergroup"))
\r
236 .thenReturn("mirrormaker.consumergroup");
\r
238 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumerid"))
\r
239 .thenReturn("mirrormaker.consumerid");
\r
241 PowerMockito.when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
243 PowerMockito.when(httpServReq.isUserInRole("admin")).thenReturn(true);
\r
245 // PowerMockito.when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
\r
246 PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin.aaf")).thenReturn(true);
\r
247 PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true);
\r
248 PowerMockito.when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
\r
249 PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true);
\r
251 PowerMockito.when(cMirroMaker.getCreateMirrorMaker()).thenReturn(mMaker);
\r
253 PowerMockito.when(mMaker.getName()).thenReturn("mirroMakerName");
\r
254 PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
255 PowerMockito.when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
256 PowerMockito.when(httpServReq.getHeader("Authorization")).thenReturn("Authorization");
\r
258 PowerMockito.when(dmaapContext.getResponse()).thenReturn(httpServRes);
\r
259 PowerMockito.when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
\r
260 PowerMockito.when(httpServReq.getMethod()).thenReturn("HEAD");
\r
262 PowerMockito.when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);
\r