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.mockito.Mockito.when;
24 import static org.mockito.Matchers.anyString;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
28 import java.io.ByteArrayInputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.lang.reflect.Constructor;
33 import java.lang.reflect.InvocationTargetException;
34 import java.lang.reflect.Method;
35 import java.util.ConcurrentModificationException;
37 import java.util.Properties;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.powermock.api.mockito.PowerMockito;
46 import org.powermock.core.classloader.annotations.PrepareForTest;
47 import org.powermock.modules.junit4.PowerMockRunner;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.mock.web.MockHttpServletRequest;
50 import org.springframework.mock.web.MockHttpServletResponse;
52 import com.att.ajsc.beans.PropertiesMapBean;
53 import com.att.ajsc.filemonitor.AJSCPropertiesMap;
54 import org.onap.dmaap.dmf.mr.CambriaApiException;
55 import org.onap.dmaap.dmf.mr.security.DMaaPAAFAuthenticator;
56 import org.onap.dmaap.dmf.mr.security.DMaaPAuthenticator;
57 import org.onap.dmaap.dmf.mr.security.DMaaPAuthenticatorImpl;
58 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
59 import org.onap.dmaap.dmf.mr.backends.ConsumerFactory.UnavailableException;
60 import org.onap.dmaap.dmf.mr.beans.DMaaPCambriaLimiter;
61 import org.onap.dmaap.dmf.mr.backends.ConsumerFactory;
62 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
63 import org.onap.dmaap.dmf.mr.beans.DMaaPKafkaMetaBroker;
64 import org.onap.dmaap.dmf.mr.constants.CambriaConstants;
65 import org.onap.dmaap.dmf.mr.exception.DMaaPAccessDeniedException;
66 import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;
67 import org.onap.dmaap.dmf.mr.metabroker.Topic;
68 import org.onap.dmaap.dmf.mr.metabroker.Broker.TopicExistsException;
69 import org.onap.dmaap.dmf.mr.service.impl.EventsServiceImpl;
70 import org.onap.dmaap.dmf.mr.utils.PropertyReader;
71 import com.att.nsa.configs.ConfigDbException;
72 import com.att.nsa.drumlin.till.nv.rrNvReadable.invalidSettingValue;
73 import com.att.nsa.drumlin.till.nv.rrNvReadable.loadException;
74 import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
75 import com.att.nsa.limits.Blacklist;
76 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
77 import com.att.nsa.security.NsaApiKey;
78 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
80 import kafka.admin.AdminUtils;
82 @RunWith(PowerMockRunner.class)
83 @PrepareForTest({ DMaaPAuthenticatorImpl.class, AJSCPropertiesMap.class })
84 public class EventsServiceImplTest {
86 private InputStream iStream = null;
87 DMaaPContext dMaapContext = new DMaaPContext();
88 EventsServiceImpl service = new EventsServiceImpl();
89 DMaaPErrorMessages pErrorMessages = new DMaaPErrorMessages();
91 ConfigurationReader configurationReader;
95 DMaaPAuthenticator<NsaSimpleApiKey> dmaaPAuthenticator;
97 DMaaPAAFAuthenticator dmaapAAFauthenticator;
101 NsaSimpleApiKey nsaSimpleApiKey;
103 DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
107 ConsumerFactory factory;
110 public void setUp() throws Exception {
111 MockitoAnnotations.initMocks(this);
112 String source = "source of my InputStream";
113 iStream = new ByteArrayInputStream(source.getBytes("UTF-8"));
115 MockHttpServletRequest request = new MockHttpServletRequest();
116 MockHttpServletResponse response = new MockHttpServletResponse();
117 dMaapContext.setRequest(request);
118 dMaapContext.setResponse(response);
119 when(blacklist.contains(anyString())).thenReturn(false);
120 when(configurationReader.getfIpBlackList()).thenReturn(blacklist);
121 dMaapContext.setConfigReader(configurationReader);
123 service.setErrorMessages(pErrorMessages);
124 PowerMockito.mockStatic(AJSCPropertiesMap.class);
125 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "timeout")).thenReturn("100");
127 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "timeout");
131 @Test(expected = CambriaApiException.class)
132 public void testGetEvents() throws DMaaPAccessDeniedException, CambriaApiException, ConfigDbException,
133 TopicExistsException, AccessDeniedException, UnavailableException, IOException {
134 when(dmaaPAuthenticator.authenticate(dMaapContext)).thenReturn(nsaSimpleApiKey);
135 when(configurationReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
136 when(configurationReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
137 when(dmaapKafkaMetaBroker.getTopic("testTopic")).thenReturn(createdTopic);
138 PowerMockito.when(configurationReader.getfConsumerFactory()).thenReturn(factory);
139 service.getEvents(dMaapContext, "testTopic", "CG1", "23");
142 @Test(expected = CambriaApiException.class)
143 public void testGetEventsBlackListErr() throws DMaaPAccessDeniedException, CambriaApiException, ConfigDbException,
144 TopicExistsException, AccessDeniedException, UnavailableException, IOException {
145 when(blacklist.contains(anyString())).thenReturn(true);
146 when(configurationReader.getfIpBlackList()).thenReturn(blacklist);
147 dMaapContext.setConfigReader(configurationReader);
148 service.getEvents(dMaapContext, "testTopic", "CG1", "23");
151 @Test(expected = CambriaApiException.class)
152 public void testGetEventsNoTopicError() throws DMaaPAccessDeniedException, CambriaApiException, ConfigDbException,
153 TopicExistsException, AccessDeniedException, UnavailableException, IOException {
154 when(dmaaPAuthenticator.authenticate(dMaapContext)).thenReturn(nsaSimpleApiKey);
155 when(configurationReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
156 when(configurationReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
157 when(dmaapKafkaMetaBroker.getTopic("testTopic")).thenReturn(null);
158 PowerMockito.when(configurationReader.getfConsumerFactory()).thenReturn(factory);
159 service.getEvents(dMaapContext, "testTopic", "CG1", "23");
162 @Test(expected = CambriaApiException.class)
163 public void testGetEventsuserNull() throws DMaaPAccessDeniedException, CambriaApiException, ConfigDbException,
164 TopicExistsException, AccessDeniedException, UnavailableException, IOException {
165 when(dmaaPAuthenticator.authenticate(dMaapContext)).thenReturn(null);
166 when(configurationReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
167 when(configurationReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
168 when(dmaapKafkaMetaBroker.getTopic("testTopic")).thenReturn(createdTopic);
169 PowerMockito.when(configurationReader.getfConsumerFactory()).thenReturn(factory);
170 MockHttpServletRequest mockRequest = new MockHttpServletRequest();
171 mockRequest.addHeader("Authorization", "passed");
172 dMaapContext.setRequest(mockRequest);
173 dMaapContext.getRequest().getHeader("Authorization");
174 service.getEvents(dMaapContext, "testTopic", "CG1", "23");
177 @Test(expected = CambriaApiException.class)
178 public void testGetEventsExcp2() throws DMaaPAccessDeniedException, CambriaApiException, ConfigDbException,
179 TopicExistsException, AccessDeniedException, UnavailableException, IOException {
180 when(dmaaPAuthenticator.authenticate(dMaapContext)).thenReturn(nsaSimpleApiKey);
181 when(configurationReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
182 when(configurationReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
183 when(dmaapKafkaMetaBroker.getTopic("testTopic")).thenReturn(createdTopic);
184 PowerMockito.when(configurationReader.getfConsumerFactory()).thenReturn(factory);
185 when(configurationReader.getfRateLimiter()).thenThrow(new ConcurrentModificationException("Error occurred"));
186 service.getEvents(dMaapContext, "testTopic", "CG1", "23");
189 @Test(expected = CambriaApiException.class)
190 public void testPushEvents() throws DMaaPAccessDeniedException, CambriaApiException, ConfigDbException,
191 TopicExistsException, AccessDeniedException, UnavailableException, IOException, missingReqdSetting,
192 invalidSettingValue, loadException {
194 // AdminUtils.createTopic(configurationReader.getZk(), "testTopic", 10,
195 // 1, new Properties());
197 configurationReader.setfRateLimiter(new DMaaPCambriaLimiter(new PropertyReader()));
199 when(dmaaPAuthenticator.authenticate(dMaapContext)).thenReturn(nsaSimpleApiKey);
200 when(configurationReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
201 when(configurationReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
202 when(dmaapKafkaMetaBroker.getTopic("testTopic")).thenReturn(createdTopic);
203 PowerMockito.when(configurationReader.getfConsumerFactory()).thenReturn(factory);
205 service.pushEvents(dMaapContext, "testTopic", iStream, "3", "12:00:00");
207 service.getEvents(dMaapContext, "testTopic", "CG1", "23");
210 * String trueValue = "True";
211 * assertTrue(trueValue.equalsIgnoreCase("True"));
216 @Test(expected = CambriaApiException.class)
217 public void testPushEventsBlackListedIp() throws DMaaPAccessDeniedException, CambriaApiException, ConfigDbException,
218 TopicExistsException, AccessDeniedException, UnavailableException, IOException, missingReqdSetting,
219 invalidSettingValue, loadException {
221 // AdminUtils.createTopic(configurationReader.getZk(), "testTopic", 10,
222 // 1, new Properties());
223 when(blacklist.contains(anyString())).thenReturn(true);
224 when(configurationReader.getfIpBlackList()).thenReturn(blacklist);
225 configurationReader.setfRateLimiter(new DMaaPCambriaLimiter(new PropertyReader()));
226 when(dmaaPAuthenticator.authenticate(dMaapContext)).thenReturn(nsaSimpleApiKey);
227 when(configurationReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
228 when(configurationReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
229 when(dmaapKafkaMetaBroker.getTopic("testTopic")).thenReturn(createdTopic);
230 PowerMockito.when(configurationReader.getfConsumerFactory()).thenReturn(factory);
232 service.pushEvents(dMaapContext, "testTopic", iStream, "3", "12:00:00");
236 @Test(expected = NullPointerException.class)
237 public void testPushEventsNoUser() throws DMaaPAccessDeniedException, CambriaApiException, ConfigDbException,
238 TopicExistsException, AccessDeniedException, UnavailableException, IOException, missingReqdSetting,
239 invalidSettingValue, loadException {
241 configurationReader.setfRateLimiter(new DMaaPCambriaLimiter(new PropertyReader()));
243 when(dmaaPAuthenticator.authenticate(dMaapContext)).thenReturn(null);
244 when(configurationReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
245 when(configurationReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
246 when(dmaapKafkaMetaBroker.getTopic("testTopic")).thenReturn(createdTopic);
247 PowerMockito.when(configurationReader.getfConsumerFactory()).thenReturn(factory);
248 MockHttpServletRequest mockRequest = new MockHttpServletRequest();
249 mockRequest.addHeader("Authorization", "passed");
250 mockRequest.addHeader("Authorization", "passed");
251 dMaapContext.setRequest(mockRequest);
252 dMaapContext.getRequest().getHeader("Authorization");
253 service.pushEvents(dMaapContext, "testTopic", iStream, "3", "12:00:00");
257 @Test(expected = CambriaApiException.class)
258 public void testPushEventsWtTransaction() throws DMaaPAccessDeniedException, CambriaApiException, ConfigDbException,
259 TopicExistsException, AccessDeniedException, UnavailableException, IOException, missingReqdSetting,
260 invalidSettingValue, loadException {
262 configurationReader.setfRateLimiter(new DMaaPCambriaLimiter(new PropertyReader()));
264 when(dmaaPAuthenticator.authenticate(dMaapContext)).thenReturn(nsaSimpleApiKey);
265 when(configurationReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
266 when(configurationReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
267 when(dmaapKafkaMetaBroker.getTopic("testTopic")).thenReturn(createdTopic);
268 PowerMockito.when(configurationReader.getfConsumerFactory()).thenReturn(factory);
269 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "transidUEBtopicreqd")).thenReturn("true");
271 service.pushEvents(dMaapContext, "testTopic", iStream, "3", "12:00:00");
275 @Test(expected = CambriaApiException.class)
276 public void testPushEventsWtTransactionError() throws DMaaPAccessDeniedException, CambriaApiException, ConfigDbException,
277 TopicExistsException, AccessDeniedException, UnavailableException, IOException, missingReqdSetting,
278 invalidSettingValue, loadException {
280 configurationReader.setfRateLimiter(new DMaaPCambriaLimiter(new PropertyReader()));
282 when(dmaaPAuthenticator.authenticate(dMaapContext)).thenReturn(nsaSimpleApiKey);
283 when(configurationReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
284 when(configurationReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
285 when(dmaapKafkaMetaBroker.getTopic("testTopic")).thenReturn(createdTopic);
286 PowerMockito.when(configurationReader.getfConsumerFactory()).thenReturn(factory);
287 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "transidUEBtopicreqd")).thenReturn("true");
288 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "event.batch.length")).thenReturn("0");
289 when(configurationReader.getfPublisher()).thenThrow(new ConcurrentModificationException("Error occurred"));
291 service.pushEvents(dMaapContext, "testTopic", iStream, "3", "12:00:00");
296 public void testIsTransEnabled1() {
298 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,
299 "transidUEBtopicreqd")).thenReturn("true");
300 assertTrue(service.isTransEnabled());
304 public void testIsTransEnabled2() {
306 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,
307 "transidUEBtopicreqd")).thenReturn("false");
308 assertFalse(service.isTransEnabled());