fix for security vulnerabilities
[dmaap/messagerouter/msgrtr.git] / src / test / java / org / onap / dmaap / mr / cambria / service / impl / EventsServiceImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21  package org.onap.dmaap.mr.cambria.service.impl;
22
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;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.File;
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;
36 import java.util.Map;
37 import java.util.Properties;
38
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;
51
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;
79
80 import kafka.admin.AdminUtils;
81
82 @RunWith(PowerMockRunner.class)
83 @PrepareForTest({ DMaaPAuthenticatorImpl.class, AJSCPropertiesMap.class })
84 public class EventsServiceImplTest {
85
86         private InputStream iStream = null;
87         DMaaPContext dMaapContext = new DMaaPContext();
88         EventsServiceImpl service = new EventsServiceImpl();
89         DMaaPErrorMessages pErrorMessages = new DMaaPErrorMessages();
90         @Mock
91         ConfigurationReader configurationReader;
92         @Mock
93         Blacklist blacklist;
94         @Mock
95         DMaaPAuthenticator<NsaSimpleApiKey> dmaaPAuthenticator;
96         @Mock
97         DMaaPAAFAuthenticator dmaapAAFauthenticator;
98         @Mock
99         NsaApiKey user;
100         @Mock
101         NsaSimpleApiKey nsaSimpleApiKey;
102         @Mock
103         DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
104         @Mock
105         Topic createdTopic;
106         @Mock
107         ConsumerFactory factory;
108
109         @Before
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"));
114
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);
122
123                 service.setErrorMessages(pErrorMessages);
124                 PowerMockito.mockStatic(AJSCPropertiesMap.class);
125                 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "timeout")).thenReturn("100");
126
127                 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "timeout");
128
129         }
130
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");
140         }
141
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");
149         }
150
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");
160         }
161
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");
175         }
176
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");
187         }
188
189         @Test(expected = CambriaApiException.class)
190         public void testPushEvents() throws DMaaPAccessDeniedException, CambriaApiException, ConfigDbException,
191                         TopicExistsException, AccessDeniedException, UnavailableException, IOException, missingReqdSetting,
192                         invalidSettingValue, loadException {
193
194                 // AdminUtils.createTopic(configurationReader.getZk(), "testTopic", 10,
195                 // 1, new Properties());
196
197                 configurationReader.setfRateLimiter(new DMaaPCambriaLimiter(new PropertyReader()));
198
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);
204
205                 service.pushEvents(dMaapContext, "testTopic", iStream, "3", "12:00:00");
206
207                 service.getEvents(dMaapContext, "testTopic", "CG1", "23");
208
209                 /*
210                  * String trueValue = "True";
211                  * assertTrue(trueValue.equalsIgnoreCase("True"));
212                  */
213
214         }
215
216         @Test(expected = CambriaApiException.class)
217         public void testPushEventsBlackListedIp() throws DMaaPAccessDeniedException, CambriaApiException, ConfigDbException,
218                         TopicExistsException, AccessDeniedException, UnavailableException, IOException, missingReqdSetting,
219                         invalidSettingValue, loadException {
220
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);
231
232                 service.pushEvents(dMaapContext, "testTopic", iStream, "3", "12:00:00");
233
234         }
235
236         @Test(expected = NullPointerException.class)
237         public void testPushEventsNoUser() throws DMaaPAccessDeniedException, CambriaApiException, ConfigDbException,
238                         TopicExistsException, AccessDeniedException, UnavailableException, IOException, missingReqdSetting,
239                         invalidSettingValue, loadException {
240
241                 configurationReader.setfRateLimiter(new DMaaPCambriaLimiter(new PropertyReader()));
242
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");
254
255         }
256
257         @Test(expected = CambriaApiException.class)
258         public void testPushEventsWtTransaction() throws DMaaPAccessDeniedException, CambriaApiException, ConfigDbException,
259                         TopicExistsException, AccessDeniedException, UnavailableException, IOException, missingReqdSetting,
260                         invalidSettingValue, loadException {
261
262                 configurationReader.setfRateLimiter(new DMaaPCambriaLimiter(new PropertyReader()));
263
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");
270
271                 service.pushEvents(dMaapContext, "testTopic", iStream, "3", "12:00:00");
272
273         }
274         
275         @Test(expected = CambriaApiException.class)
276         public void testPushEventsWtTransactionError() throws DMaaPAccessDeniedException, CambriaApiException, ConfigDbException,
277                         TopicExistsException, AccessDeniedException, UnavailableException, IOException, missingReqdSetting,
278                         invalidSettingValue, loadException {
279
280                 configurationReader.setfRateLimiter(new DMaaPCambriaLimiter(new PropertyReader()));
281
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"));
290
291                 service.pushEvents(dMaapContext, "testTopic", iStream, "3", "12:00:00");
292
293         }
294         
295         @Test
296         public void testIsTransEnabled1() {
297
298                 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,
299                                 "transidUEBtopicreqd")).thenReturn("true");
300                   assertTrue(service.isTransEnabled());
301
302         }
303         @Test
304         public void testIsTransEnabled2() {
305
306                 when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,
307                                 "transidUEBtopicreqd")).thenReturn("false");
308                   assertFalse(service.isTransEnabled());
309
310         }
311
312 }