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.*;
25 import java.io.ByteArrayInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.Arrays;
29 import java.util.ConcurrentModificationException;
30 import java.util.HashSet;
34 import com.att.ajsc.beans.PropertiesMapBean;
35 import com.att.ajsc.filemonitor.AJSCPropertiesMap;
36 import org.onap.dmaap.dmf.mr.CambriaApiException;
37 import org.onap.dmaap.dmf.mr.backends.ConsumerFactory;
38 import org.onap.dmaap.dmf.mr.backends.ConsumerFactory.UnavailableException;
39 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
40 import org.onap.dmaap.dmf.mr.beans.DMaaPKafkaMetaBroker;
41 import org.onap.dmaap.dmf.mr.constants.CambriaConstants;
42 import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;
43 import org.onap.dmaap.dmf.mr.metabroker.Topic;
44 import org.onap.dmaap.dmf.mr.metabroker.Broker.TopicExistsException;
45 import org.onap.dmaap.dmf.mr.security.DMaaPAuthenticatorImpl;
46 import org.onap.dmaap.dmf.mr.service.impl.MMServiceImpl;
47 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
48 import org.onap.dmaap.dmf.mr.utils.DMaaPResponseBuilder;
49 import org.onap.dmaap.dmf.mr.utils.Emailer;
50 import com.att.nsa.configs.ConfigDbException;
51 import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
52 import com.att.nsa.limits.Blacklist;
53 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
54 import com.att.nsa.security.db.NsaApiDb;
55 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
57 import org.junit.After;
58 import org.junit.Before;
59 import org.junit.Test;
60 import org.junit.runner.RunWith;
61 import static org.mockito.Matchers.anyString;
62 import org.mockito.InjectMocks;
63 import org.mockito.Mock;
64 import org.mockito.MockitoAnnotations;
65 import org.powermock.api.mockito.PowerMockito;
66 import org.powermock.core.classloader.annotations.PowerMockIgnore;
67 import org.powermock.core.classloader.annotations.PrepareForTest;
68 import org.powermock.modules.junit4.PowerMockRunner;
69 import org.springframework.mock.web.MockHttpServletRequest;
70 import org.springframework.mock.web.MockHttpServletResponse;
72 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"})
73 @RunWith(PowerMockRunner.class)
74 @PrepareForTest({ DMaaPAuthenticatorImpl.class, DMaaPResponseBuilder.class, PropertiesMapBean.class,
75 AJSCPropertiesMap.class })
76 public class MMServiceImplTest {
79 MMServiceImpl service;
82 DMaaPContext dmaapContext;
84 ConsumerFactory factory;
86 private DMaaPErrorMessages errorMessages;
88 ConfigurationReader configReader;
94 DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
99 public void setUp() throws Exception {
101 MockitoAnnotations.initMocks(this);
102 PowerMockito.mockStatic(DMaaPAuthenticatorImpl.class);
103 NsaSimpleApiKey user = new NsaSimpleApiKey("admin", "password");
105 PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);
106 PowerMockito.when(configReader.getfConsumerFactory()).thenReturn(factory);
107 PowerMockito.when(configReader.getfIpBlackList()).thenReturn(Blacklist);
109 PowerMockito.when(configReader.getfApiKeyDb()).thenReturn(fApiKeyDb);
110 PowerMockito.when(configReader.getSystemEmailer()).thenReturn(emailer);
111 PowerMockito.when(DMaaPAuthenticatorImpl.getAuthenticatedUser(dmaapContext)).thenReturn(user);
112 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
114 MockHttpServletRequest request = new MockHttpServletRequest();
115 MockHttpServletResponse response = new MockHttpServletResponse();
116 PowerMockito.when(dmaapContext.getRequest()).thenReturn(request);
117 PowerMockito.when(dmaapContext.getResponse()).thenReturn(response);
119 PowerMockito.mockStatic(AJSCPropertiesMap.class);
120 PowerMockito.mockStatic(PropertiesMapBean.class);
121 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "timeout")).thenReturn("1000");
122 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "pretty")).thenReturn("true");
123 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "meta")).thenReturn("true");
124 PowerMockito.when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
129 public void tearDown() throws Exception {
133 public void testSubscribe_Blacklistip() {
136 PowerMockito.when(Blacklist.contains("127.0.0.1")).thenReturn(true);
137 service.subscribe(dmaapContext, "testTopic", "CG1", "23");
138 } catch (org.json.JSONException e) {
139 // TODO Auto-generated catch block
141 } catch (NullPointerException e) {
142 // TODO Auto-generated catch block
143 // e.printStackTrace();
144 } catch (CambriaApiException e) {
146 } catch (ConfigDbException e) {
147 // TODO Auto-generated catch block
149 } catch (TopicExistsException e) {
150 // TODO Auto-generated catch block
152 } catch (AccessDeniedException e) {
153 // TODO Auto-generated catch block
155 } catch (UnavailableException e) {
156 // TODO Auto-generated catch block
158 } catch (IOException e) {
159 // TODO Auto-generated catch block
166 public void testSubscribe_NullTopic() {
169 PowerMockito.when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);
170 service.subscribe(dmaapContext, "testTopic", "CG1", "23");
171 } catch (org.json.JSONException e) {
172 // TODO Auto-generated catch block
174 } catch (NullPointerException e) {
175 // TODO Auto-generated catch block
176 // e.printStackTrace();
178 } catch (CambriaApiException e) {
180 } catch (ConfigDbException e) {
181 // TODO Auto-generated catch block
183 } catch (TopicExistsException e) {
184 // TODO Auto-generated catch block
186 } catch (AccessDeniedException e) {
187 // TODO Auto-generated catch block
189 } catch (UnavailableException e) {
190 // TODO Auto-generated catch block
192 } catch (IOException e) {
193 // TODO Auto-generated catch block
199 @Test(expected = CambriaApiException.class)
200 public void testSubscribe_NullTopic_Error() throws ConfigDbException, TopicExistsException, AccessDeniedException, UnavailableException,
201 CambriaApiException, IOException {
203 PowerMockito.when(configReader.getfMetrics()).thenThrow(new ConcurrentModificationException("Error occurred"));
204 PowerMockito.when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(metatopic);
205 service.subscribe(dmaapContext, "testTopic", "CG1", "23");
209 public void testPushEvents_wttransaction() {
211 String source = "source of my InputStream";
214 InputStream iStream = new ByteArrayInputStream(source.getBytes("UTF-8"));
215 service.pushEvents(dmaapContext, "msgrtr.apinode.metrics.dmaap", iStream, "3", "12:00:00");
217 } catch (org.json.JSONException e) {
218 // TODO Auto-generated catch block
220 } catch (NullPointerException e) {
221 // TODO Auto-generated catch block
222 // e.printStackTrace();
224 } catch (CambriaApiException e) {
226 } catch (ConfigDbException e) {
227 // TODO Auto-generated catch block
229 } catch (TopicExistsException e) {
230 // TODO Auto-generated catch block
232 } catch (AccessDeniedException e) {
233 // TODO Auto-generated catch block
235 } catch (IOException e) {
236 // TODO Auto-generated catch block
238 } catch (missingReqdSetting e) {
239 // TODO Auto-generated catch block
244 @Test(expected = CambriaApiException.class)
245 public void testPushEvents_wttransaction_error() throws Exception {
247 String source = "source of my InputStream";
249 InputStream iStream = new ByteArrayInputStream(source.getBytes("UTF-8"));
250 PowerMockito.mockStatic(AJSCPropertiesMap.class);
251 PowerMockito.mockStatic(PropertiesMapBean.class);
252 PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "event.batch.length")).thenReturn("-5");
253 PowerMockito.when(configReader.getfPublisher()).thenThrow(new ConcurrentModificationException("Error occurred"));
254 service.pushEvents(dmaapContext, "msgrtr.apinode.metrics.dmaap1", iStream, "3", "12:00:00");
259 public void testPushEvents() {
261 String source = "source of my InputStream";
264 InputStream iStream = new ByteArrayInputStream(source.getBytes("UTF-8"));
265 service.pushEvents(dmaapContext, "testTopic", iStream, "3", "12:00:00");
267 } catch (org.json.JSONException e) {
268 // TODO Auto-generated catch block
270 } catch (NullPointerException e) {
271 // TODO Auto-generated catch block
272 // e.printStackTrace();
274 } catch (CambriaApiException e) {
276 } catch (ConfigDbException e) {
277 // TODO Auto-generated catch block
279 } catch (TopicExistsException e) {
280 // TODO Auto-generated catch block
282 } catch (AccessDeniedException e) {
283 // TODO Auto-generated catch block
285 } catch (IOException e) {
286 // TODO Auto-generated catch block
288 } catch (missingReqdSetting e) {
289 // TODO Auto-generated catch block
296 public void testPushEvents_blacklistip() {
298 String source = "source of my InputStream";
301 PowerMockito.when(Blacklist.contains("127.0.0.1")).thenReturn(true);
302 InputStream iStream = new ByteArrayInputStream(source.getBytes("UTF-8"));
303 service.pushEvents(dmaapContext, "testTopic", iStream, "3", "12:00:00");
305 } catch (org.json.JSONException e) {
306 // TODO Auto-generated catch block
308 } catch (NullPointerException e) {
309 // TODO Auto-generated catch block
310 // e.printStackTrace();
312 } catch (CambriaApiException e) {
314 } catch (ConfigDbException e) {
315 // TODO Auto-generated catch block
317 } catch (TopicExistsException e) {
318 // TODO Auto-generated catch block
320 } catch (AccessDeniedException e) {
321 // TODO Auto-generated catch block
323 } catch (IOException e) {
324 // TODO Auto-generated catch block
326 } catch (missingReqdSetting e) {
327 // TODO Auto-generated catch block
333 NsaApiDb<NsaSimpleApiKey> fApiKeyDb = new NsaApiDb<NsaSimpleApiKey>() {
335 Set<String> keys = new HashSet<>(Arrays.asList("testkey", "admin"));
338 public NsaSimpleApiKey createApiKey(String arg0, String arg1)
339 throws com.att.nsa.security.db.NsaApiDb.KeyExistsException, ConfigDbException {
340 // TODO Auto-generated method stub
341 return new NsaSimpleApiKey(arg0, arg1);
345 public boolean deleteApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
346 // TODO Auto-generated method stub
351 public boolean deleteApiKey(String arg0) throws ConfigDbException {
352 // TODO Auto-generated method stub
357 public Map<String, NsaSimpleApiKey> loadAllKeyRecords() throws ConfigDbException {
358 // TODO Auto-generated method stub
363 public Set<String> loadAllKeys() throws ConfigDbException {
364 // TODO Auto-generated method stub
370 public NsaSimpleApiKey loadApiKey(String arg0) throws ConfigDbException {
371 if (!keys.contains(arg0)) {
374 return new NsaSimpleApiKey(arg0, "password");
378 public void saveApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
379 // TODO Auto-generated method stub