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.cambria.service.impl;
\r
23 import static org.junit.Assert.*;
\r
25 import java.io.ByteArrayInputStream;
\r
26 import java.io.IOException;
\r
27 import java.io.InputStream;
\r
28 import java.util.Arrays;
\r
29 import java.util.ConcurrentModificationException;
\r
30 import java.util.HashSet;
\r
31 import java.util.Map;
\r
32 import java.util.Set;
\r
34 import com.att.ajsc.beans.PropertiesMapBean;
\r
35 import com.att.ajsc.filemonitor.AJSCPropertiesMap;
\r
36 import com.att.dmf.mr.CambriaApiException;
\r
37 import com.att.dmf.mr.backends.ConsumerFactory;
\r
38 import com.att.dmf.mr.backends.ConsumerFactory.UnavailableException;
\r
39 import com.att.dmf.mr.beans.DMaaPContext;
\r
40 import com.att.dmf.mr.beans.DMaaPKafkaMetaBroker;
\r
41 import com.att.dmf.mr.constants.CambriaConstants;
\r
42 import com.att.dmf.mr.exception.DMaaPErrorMessages;
\r
43 import com.att.dmf.mr.metabroker.Topic;
\r
44 import com.att.dmf.mr.metabroker.Broker.TopicExistsException;
\r
45 import com.att.dmf.mr.security.DMaaPAuthenticatorImpl;
\r
46 import com.att.dmf.mr.service.impl.MMServiceImpl;
\r
47 import com.att.dmf.mr.utils.ConfigurationReader;
\r
48 import com.att.dmf.mr.utils.DMaaPResponseBuilder;
\r
49 import com.att.dmf.mr.utils.Emailer;
\r
50 import com.att.nsa.configs.ConfigDbException;
\r
51 import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
\r
52 import com.att.nsa.limits.Blacklist;
\r
53 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
\r
54 import com.att.nsa.security.db.NsaApiDb;
\r
55 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
\r
57 import org.junit.After;
\r
58 import org.junit.Before;
\r
59 import org.junit.Test;
\r
60 import org.junit.runner.RunWith;
\r
61 import static org.mockito.Matchers.anyString;
\r
62 import org.mockito.InjectMocks;
\r
63 import org.mockito.Mock;
\r
64 import org.mockito.MockitoAnnotations;
\r
65 import org.powermock.api.mockito.PowerMockito;
\r
66 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
67 import org.powermock.modules.junit4.PowerMockRunner;
\r
68 import org.springframework.mock.web.MockHttpServletRequest;
\r
69 import org.springframework.mock.web.MockHttpServletResponse;
\r
71 @RunWith(PowerMockRunner.class)
\r
72 @PrepareForTest({ DMaaPAuthenticatorImpl.class, DMaaPResponseBuilder.class, PropertiesMapBean.class,
\r
73 AJSCPropertiesMap.class })
\r
74 public class MMServiceImplTest {
\r
77 MMServiceImpl service;
\r
80 DMaaPContext dmaapContext;
\r
82 ConsumerFactory factory;
\r
84 private DMaaPErrorMessages errorMessages;
\r
86 ConfigurationReader configReader;
\r
88 Blacklist Blacklist;
\r
92 DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
\r
97 public void setUp() throws Exception {
\r
99 MockitoAnnotations.initMocks(this);
\r
100 PowerMockito.mockStatic(DMaaPAuthenticatorImpl.class);
\r
101 NsaSimpleApiKey user = new NsaSimpleApiKey("admin", "password");
\r
103 PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);
\r
104 PowerMockito.when(configReader.getfConsumerFactory()).thenReturn(factory);
\r
105 PowerMockito.when(configReader.getfIpBlackList()).thenReturn(Blacklist);
\r
107 PowerMockito.when(configReader.getfApiKeyDb()).thenReturn(fApiKeyDb);
\r
108 PowerMockito.when(configReader.getSystemEmailer()).thenReturn(emailer);
\r
109 PowerMockito.when(DMaaPAuthenticatorImpl.getAuthenticatedUser(dmaapContext)).thenReturn(user);
\r
110 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
\r
112 MockHttpServletRequest request = new MockHttpServletRequest();
\r
113 MockHttpServletResponse response = new MockHttpServletResponse();
\r
114 PowerMockito.when(dmaapContext.getRequest()).thenReturn(request);
\r
115 PowerMockito.when(dmaapContext.getResponse()).thenReturn(response);
\r
117 PowerMockito.mockStatic(AJSCPropertiesMap.class);
\r
118 PowerMockito.mockStatic(PropertiesMapBean.class);
\r
119 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "timeout")).thenReturn("1000");
\r
120 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "pretty")).thenReturn("true");
\r
121 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "meta")).thenReturn("true");
\r
122 PowerMockito.when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
\r
127 public void tearDown() throws Exception {
\r
131 public void testSubscribe_Blacklistip() {
\r
134 PowerMockito.when(Blacklist.contains("127.0.0.1")).thenReturn(true);
\r
135 service.subscribe(dmaapContext, "testTopic", "CG1", "23");
\r
136 } catch (org.json.JSONException e) {
\r
137 // TODO Auto-generated catch block
\r
138 e.printStackTrace();
\r
139 } catch (NullPointerException e) {
\r
140 // TODO Auto-generated catch block
\r
141 // e.printStackTrace();
\r
142 } catch (CambriaApiException e) {
\r
144 } catch (ConfigDbException e) {
\r
145 // TODO Auto-generated catch block
\r
146 e.printStackTrace();
\r
147 } catch (TopicExistsException e) {
\r
148 // TODO Auto-generated catch block
\r
149 e.printStackTrace();
\r
150 } catch (AccessDeniedException e) {
\r
151 // TODO Auto-generated catch block
\r
152 e.printStackTrace();
\r
153 } catch (UnavailableException e) {
\r
154 // TODO Auto-generated catch block
\r
155 e.printStackTrace();
\r
156 } catch (IOException e) {
\r
157 // TODO Auto-generated catch block
\r
158 e.printStackTrace();
\r
164 public void testSubscribe_NullTopic() {
\r
167 PowerMockito.when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);
\r
168 service.subscribe(dmaapContext, "testTopic", "CG1", "23");
\r
169 } catch (org.json.JSONException e) {
\r
170 // TODO Auto-generated catch block
\r
171 e.printStackTrace();
\r
172 } catch (NullPointerException e) {
\r
173 // TODO Auto-generated catch block
\r
174 // e.printStackTrace();
\r
176 } catch (CambriaApiException e) {
\r
178 } catch (ConfigDbException e) {
\r
179 // TODO Auto-generated catch block
\r
180 e.printStackTrace();
\r
181 } catch (TopicExistsException e) {
\r
182 // TODO Auto-generated catch block
\r
183 e.printStackTrace();
\r
184 } catch (AccessDeniedException e) {
\r
185 // TODO Auto-generated catch block
\r
186 e.printStackTrace();
\r
187 } catch (UnavailableException e) {
\r
188 // TODO Auto-generated catch block
\r
189 e.printStackTrace();
\r
190 } catch (IOException e) {
\r
191 // TODO Auto-generated catch block
\r
192 e.printStackTrace();
\r
197 @Test(expected = CambriaApiException.class)
\r
198 public void testSubscribe_NullTopic_Error() throws ConfigDbException, TopicExistsException, AccessDeniedException, UnavailableException,
\r
199 CambriaApiException, IOException {
\r
201 PowerMockito.when(configReader.getfMetrics()).thenThrow(new ConcurrentModificationException("Error occurred"));
\r
202 PowerMockito.when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(metatopic);
\r
203 service.subscribe(dmaapContext, "testTopic", "CG1", "23");
\r
207 public void testPushEvents_wttransaction() {
\r
209 String source = "source of my InputStream";
\r
212 InputStream iStream = new ByteArrayInputStream(source.getBytes("UTF-8"));
\r
213 service.pushEvents(dmaapContext, "msgrtr.apinode.metrics.dmaap", iStream, "3", "12:00:00");
\r
215 } catch (org.json.JSONException e) {
\r
216 // TODO Auto-generated catch block
\r
217 e.printStackTrace();
\r
218 } catch (NullPointerException e) {
\r
219 // TODO Auto-generated catch block
\r
220 // e.printStackTrace();
\r
222 } catch (CambriaApiException e) {
\r
224 } catch (ConfigDbException e) {
\r
225 // TODO Auto-generated catch block
\r
226 e.printStackTrace();
\r
227 } catch (TopicExistsException e) {
\r
228 // TODO Auto-generated catch block
\r
229 e.printStackTrace();
\r
230 } catch (AccessDeniedException e) {
\r
231 // TODO Auto-generated catch block
\r
232 e.printStackTrace();
\r
233 } catch (IOException e) {
\r
234 // TODO Auto-generated catch block
\r
235 e.printStackTrace();
\r
236 } catch (missingReqdSetting e) {
\r
237 // TODO Auto-generated catch block
\r
238 e.printStackTrace();
\r
242 @Test(expected = CambriaApiException.class)
\r
243 public void testPushEvents_wttransaction_error() throws Exception {
\r
245 String source = "source of my InputStream";
\r
247 InputStream iStream = new ByteArrayInputStream(source.getBytes("UTF-8"));
\r
248 PowerMockito.mockStatic(AJSCPropertiesMap.class);
\r
249 PowerMockito.mockStatic(PropertiesMapBean.class);
\r
250 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "event.batch.length")).thenReturn("-5");
\r
251 PowerMockito.when(configReader.getfPublisher()).thenThrow(new ConcurrentModificationException("Error occurred"));
\r
252 service.pushEvents(dmaapContext, "msgrtr.apinode.metrics.dmaap1", iStream, "3", "12:00:00");
\r
257 public void testPushEvents() {
\r
259 String source = "source of my InputStream";
\r
262 InputStream iStream = new ByteArrayInputStream(source.getBytes("UTF-8"));
\r
263 service.pushEvents(dmaapContext, "testTopic", iStream, "3", "12:00:00");
\r
265 } catch (org.json.JSONException e) {
\r
266 // TODO Auto-generated catch block
\r
267 e.printStackTrace();
\r
268 } catch (NullPointerException e) {
\r
269 // TODO Auto-generated catch block
\r
270 // e.printStackTrace();
\r
272 } catch (CambriaApiException e) {
\r
274 } catch (ConfigDbException e) {
\r
275 // TODO Auto-generated catch block
\r
276 e.printStackTrace();
\r
277 } catch (TopicExistsException e) {
\r
278 // TODO Auto-generated catch block
\r
279 e.printStackTrace();
\r
280 } catch (AccessDeniedException e) {
\r
281 // TODO Auto-generated catch block
\r
282 e.printStackTrace();
\r
283 } catch (IOException e) {
\r
284 // TODO Auto-generated catch block
\r
285 e.printStackTrace();
\r
286 } catch (missingReqdSetting e) {
\r
287 // TODO Auto-generated catch block
\r
288 e.printStackTrace();
\r
294 public void testPushEvents_blacklistip() {
\r
296 String source = "source of my InputStream";
\r
299 PowerMockito.when(Blacklist.contains("127.0.0.1")).thenReturn(true);
\r
300 InputStream iStream = new ByteArrayInputStream(source.getBytes("UTF-8"));
\r
301 service.pushEvents(dmaapContext, "testTopic", iStream, "3", "12:00:00");
\r
303 } catch (org.json.JSONException e) {
\r
304 // TODO Auto-generated catch block
\r
305 e.printStackTrace();
\r
306 } catch (NullPointerException e) {
\r
307 // TODO Auto-generated catch block
\r
308 // e.printStackTrace();
\r
310 } catch (CambriaApiException e) {
\r
312 } catch (ConfigDbException e) {
\r
313 // TODO Auto-generated catch block
\r
314 e.printStackTrace();
\r
315 } catch (TopicExistsException e) {
\r
316 // TODO Auto-generated catch block
\r
317 e.printStackTrace();
\r
318 } catch (AccessDeniedException e) {
\r
319 // TODO Auto-generated catch block
\r
320 e.printStackTrace();
\r
321 } catch (IOException e) {
\r
322 // TODO Auto-generated catch block
\r
323 e.printStackTrace();
\r
324 } catch (missingReqdSetting e) {
\r
325 // TODO Auto-generated catch block
\r
326 e.printStackTrace();
\r
331 NsaApiDb<NsaSimpleApiKey> fApiKeyDb = new NsaApiDb<NsaSimpleApiKey>() {
\r
333 Set<String> keys = new HashSet<>(Arrays.asList("testkey", "admin"));
\r
336 public NsaSimpleApiKey createApiKey(String arg0, String arg1)
\r
337 throws com.att.nsa.security.db.NsaApiDb.KeyExistsException, ConfigDbException {
\r
338 // TODO Auto-generated method stub
\r
339 return new NsaSimpleApiKey(arg0, arg1);
\r
343 public boolean deleteApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
\r
344 // TODO Auto-generated method stub
\r
349 public boolean deleteApiKey(String arg0) throws ConfigDbException {
\r
350 // TODO Auto-generated method stub
\r
355 public Map<String, NsaSimpleApiKey> loadAllKeyRecords() throws ConfigDbException {
\r
356 // TODO Auto-generated method stub
\r
361 public Set<String> loadAllKeys() throws ConfigDbException {
\r
362 // TODO Auto-generated method stub
\r
368 public NsaSimpleApiKey loadApiKey(String arg0) throws ConfigDbException {
\r
369 if (!keys.contains(arg0)) {
\r
372 return new NsaSimpleApiKey(arg0, "password");
\r
376 public void saveApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
\r
377 // TODO Auto-generated method stub
\r