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