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.*;
\r
25 //import static org.mockito.Matchers.anyString;
\r
26 //import static org.mockito.Mockito.when;
\r
28 import java.io.IOException;
\r
29 import java.io.InputStream;
\r
31 import javax.servlet.ServletOutputStream;
\r
32 import javax.servlet.http.HttpServletRequest;
\r
33 import javax.servlet.http.HttpServletResponse;
\r
35 import org.junit.After;
\r
36 import org.json.JSONArray;
\r
37 import org.json.JSONException;
\r
38 import org.json.JSONObject;
\r
39 import org.junit.Assert;
\r
40 import org.junit.Before;
\r
41 import org.junit.Test;
\r
43 import org.junit.runner.RunWith;
\r
44 /*import org.mockito.InjectMocks;
\r
45 import org.mockito.Mock;
\r
46 import org.mockito.MockitoAnnotations;
\r
47 import org.powermock.api.mockito.PowerMockito;
\r
48 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
49 import org.powermock.modules.junit4.PowerMockRunner;*/
\r
51 import com.att.ajsc.beans.PropertiesMapBean;
\r
52 import com.att.ajsc.filemonitor.AJSCPropertiesMap;
\r
53 import com.att.nsa.cambria.CambriaApiException;
\r
54 import com.att.nsa.cambria.beans.DMaaPContext;
\r
55 import com.att.nsa.cambria.beans.DMaaPKafkaMetaBroker;
\r
56 import com.att.nsa.cambria.constants.CambriaConstants;
\r
57 import com.att.nsa.cambria.exception.DMaaPAccessDeniedException;
\r
58 import com.att.nsa.cambria.exception.DMaaPErrorMessages;
\r
59 import com.att.nsa.cambria.metabroker.Broker.TopicExistsException;
\r
60 import com.att.nsa.cambria.metabroker.Topic;
\r
61 import com.att.nsa.cambria.security.DMaaPAAFAuthenticator;
\r
62 import com.att.nsa.cambria.security.DMaaPAAFAuthenticatorImpl;
\r
63 import com.att.nsa.cambria.security.DMaaPAuthenticator;
\r
64 import com.att.nsa.cambria.service.MMService;
\r
65 import com.att.nsa.cambria.utils.ConfigurationReader;
\r
66 import com.att.nsa.configs.ConfigDbException;
\r
67 import com.att.nsa.dmaap.mmagent.CreateMirrorMaker;
\r
68 import com.att.nsa.dmaap.mmagent.MirrorMaker;
\r
69 import com.att.nsa.dmaap.mmagent.UpdateMirrorMaker;
\r
70 import com.att.nsa.security.NsaAcl;
\r
71 import com.att.nsa.security.NsaApiKey;
\r
72 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
\r
74 //@RunWith(MockitoJUnitRunner.class)
\r
75 /*@RunWith(PowerMockRunner.class)
\r
76 @PrepareForTest({ PropertiesMapBean.class, AJSCPropertiesMap.class })*/
\r
77 public class MMRestServiceTest {/*
\r
80 MMRestService mmRestService;
\r
83 private MMService mmservice;
\r
86 CreateMirrorMaker cMirroMaker;
\r
89 UpdateMirrorMaker uMirroMaker;
\r
91 private TopicRestService service = new TopicRestService();
\r
93 private DMaaPErrorMessages errorMessages;
\r
96 DMaaPContext dmaapContext;
\r
99 ConfigurationReader configReader;
\r
102 ServletOutputStream oStream;
\r
105 DMaaPAuthenticator<NsaSimpleApiKey> dmaaPAuthenticator;
\r
108 MirrorMaker mMaker;
\r
111 DMaaPAAFAuthenticator dmaapAAFauthenticator;
\r
114 DMaaPAAFAuthenticatorImpl impl;
\r
120 NsaSimpleApiKey nsaSimpleApiKey;
\r
123 HttpServletRequest httpServReq;
\r
126 HttpServletResponse httpServRes;
\r
129 InputStream iStream;
\r
132 DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
\r
135 Topic createdTopic;
\r
141 JSONObject jsonObj;
\r
144 JSONArray jsonArray;
\r
147 public void setUp() throws Exception {
\r
149 MockitoAnnotations.initMocks(this);
\r
153 public void tearDown() throws Exception {
\r
158 public void testCallCreateMirrorMaker() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
\r
159 TopicExistsException, JSONException, ConfigDbException {
\r
161 Assert.assertNotNull(mmRestService);
\r
162 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
163 when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true);
\r
164 when(httpServReq.isUserInRole("admin")).thenReturn(true);
\r
166 PowerMockito.mockStatic(AJSCPropertiesMap.class);
\r
169 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeradmin.aaf"))
\r
170 .thenReturn("admin");
\r
172 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.timeout"))
\r
173 .thenReturn("100");
\r
175 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.topic"))
\r
176 .thenReturn("mirrormaker.topic");
\r
178 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumergroup"))
\r
179 .thenReturn("mirrormaker.consumergroup");
\r
181 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumerid"))
\r
182 .thenReturn("mirrormaker.consumerid");
\r
184 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
186 when(httpServReq.isUserInRole("admin")).thenReturn(true);
\r
189 // when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
\r
190 when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin.aaf")).thenReturn(true);
\r
191 when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true);
\r
192 when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
\r
193 when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true);
\r
195 when(cMirroMaker.getCreateMirrorMaker()).thenReturn(mMaker);
\r
197 when(mMaker.getName()).thenReturn("mirroMakerName");
\r
198 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
199 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
200 when(httpServReq.getHeader("Authorization")).thenReturn("Authorization");
\r
202 when(dmaapContext.getResponse()).thenReturn(httpServRes);
\r
203 when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
\r
204 when(httpServReq.getMethod()).thenReturn("HEAD");
\r
206 when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);
\r
208 mmRestService.callCreateMirrorMaker(iStream);
\r
212 public void testCallListAllMirrorMaker() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
\r
213 TopicExistsException, JSONException, ConfigDbException {
\r
215 Assert.assertNotNull(mmRestService);
\r
216 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
217 when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true);
\r
218 when(httpServReq.isUserInRole("admin")).thenReturn(true);
\r
220 PowerMockito.mockStatic(AJSCPropertiesMap.class);
\r
223 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeradmin.aaf"))
\r
224 .thenReturn("admin");
\r
226 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.timeout"))
\r
227 .thenReturn("100");
\r
229 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.topic"))
\r
230 .thenReturn("mirrormaker.topic");
\r
232 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumergroup"))
\r
233 .thenReturn("mirrormaker.consumergroup");
\r
235 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumerid"))
\r
236 .thenReturn("mirrormaker.consumerid");
\r
238 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
240 when(httpServReq.isUserInRole("admin")).thenReturn(true);
\r
243 // when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
\r
244 when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin.aaf")).thenReturn(true);
\r
245 when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true);
\r
246 when(httpServReq.getHeader("Authorization")).thenReturn("Admin");
\r
247 when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true);
\r
249 when(cMirroMaker.getCreateMirrorMaker()).thenReturn(mMaker);
\r
251 when(mMaker.getName()).thenReturn("mirroMakerName");
\r
252 when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
253 when(dmaapContext.getRequest()).thenReturn(httpServReq);
\r
254 when(httpServReq.getHeader("Authorization")).thenReturn("Authorization");
\r
256 when(dmaapContext.getResponse()).thenReturn(httpServRes);
\r
257 when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
\r
258 when(httpServReq.getMethod()).thenReturn("HEAD");
\r
260 when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);
\r
262 mmRestService.callListAllMirrorMaker(iStream);
\r