update the package name
[dmaap/messagerouter/msgrtr.git] / src / test / java / org / onap / dmaap / mr / cambria / service / impl / MMServiceImplTest.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.junit.Assert.*;
24
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;
31 import java.util.Map;
32 import java.util.Set;
33
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;
56
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.PrepareForTest;
67 import org.powermock.modules.junit4.PowerMockRunner;
68 import org.springframework.mock.web.MockHttpServletRequest;
69 import org.springframework.mock.web.MockHttpServletResponse;
70
71 @RunWith(PowerMockRunner.class)
72 @PrepareForTest({ DMaaPAuthenticatorImpl.class, DMaaPResponseBuilder.class, PropertiesMapBean.class,
73                 AJSCPropertiesMap.class })
74 public class MMServiceImplTest {
75
76         @InjectMocks
77         MMServiceImpl service;
78
79         @Mock
80         DMaaPContext dmaapContext;
81         @Mock
82         ConsumerFactory factory;
83         @Mock
84         private DMaaPErrorMessages errorMessages;
85         @Mock
86         ConfigurationReader configReader;
87         @Mock
88         Blacklist Blacklist;
89         @Mock
90         Emailer emailer;
91         @Mock
92         DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
93         @Mock
94         Topic metatopic;
95
96         @Before
97         public void setUp() throws Exception {
98
99                 MockitoAnnotations.initMocks(this);
100                 PowerMockito.mockStatic(DMaaPAuthenticatorImpl.class);
101                 NsaSimpleApiKey user = new NsaSimpleApiKey("admin", "password");
102
103                 PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);
104                 PowerMockito.when(configReader.getfConsumerFactory()).thenReturn(factory);
105                 PowerMockito.when(configReader.getfIpBlackList()).thenReturn(Blacklist);
106
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);
111
112                 MockHttpServletRequest request = new MockHttpServletRequest();
113                 MockHttpServletResponse response = new MockHttpServletResponse();
114                 PowerMockito.when(dmaapContext.getRequest()).thenReturn(request);
115                 PowerMockito.when(dmaapContext.getResponse()).thenReturn(response);
116
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);
123
124         }
125
126         @After
127         public void tearDown() throws Exception {
128         }
129
130         @Test
131         public void testSubscribe_Blacklistip() {
132
133                 try {
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
138                         e.printStackTrace();
139                 } catch (NullPointerException e) {
140                         // TODO Auto-generated catch block
141                         // e.printStackTrace();
142                 } catch (CambriaApiException e) {
143                         assertTrue(true);
144                 } catch (ConfigDbException e) {
145                         // TODO Auto-generated catch block
146                         e.printStackTrace();
147                 } catch (TopicExistsException e) {
148                         // TODO Auto-generated catch block
149                         e.printStackTrace();
150                 } catch (AccessDeniedException e) {
151                         // TODO Auto-generated catch block
152                         e.printStackTrace();
153                 } catch (UnavailableException e) {
154                         // TODO Auto-generated catch block
155                         e.printStackTrace();
156                 } catch (IOException e) {
157                         // TODO Auto-generated catch block
158                         e.printStackTrace();
159                 }
160
161         }
162
163         @Test
164         public void testSubscribe_NullTopic() {
165
166                 try {
167                         PowerMockito.when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);
168                         service.subscribe(dmaapContext, "testTopic", "CG1", "23");
169                 } catch (org.json.JSONException e) {
170                         // TODO Auto-generated catch block
171                         e.printStackTrace();
172                 } catch (NullPointerException e) {
173                         // TODO Auto-generated catch block
174                         // e.printStackTrace();
175                         assertTrue(true);
176                 } catch (CambriaApiException e) {
177                         assertTrue(true);
178                 } catch (ConfigDbException e) {
179                         // TODO Auto-generated catch block
180                         e.printStackTrace();
181                 } catch (TopicExistsException e) {
182                         // TODO Auto-generated catch block
183                         e.printStackTrace();
184                 } catch (AccessDeniedException e) {
185                         // TODO Auto-generated catch block
186                         e.printStackTrace();
187                 } catch (UnavailableException e) {
188                         // TODO Auto-generated catch block
189                         e.printStackTrace();
190                 } catch (IOException e) {
191                         // TODO Auto-generated catch block
192                         e.printStackTrace();
193                 }
194
195         }
196         
197         @Test(expected = CambriaApiException.class)
198         public void testSubscribe_NullTopic_Error() throws ConfigDbException, TopicExistsException, AccessDeniedException, UnavailableException,
199         CambriaApiException, IOException {
200
201                     PowerMockito.when(configReader.getfMetrics()).thenThrow(new ConcurrentModificationException("Error occurred"));
202                         PowerMockito.when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(metatopic);
203                         service.subscribe(dmaapContext, "testTopic", "CG1", "23");
204         }
205
206         @Test
207         public void testPushEvents_wttransaction() {
208
209                 String source = "source of my InputStream";
210
211                 try {
212                         InputStream iStream = new ByteArrayInputStream(source.getBytes("UTF-8"));
213                         service.pushEvents(dmaapContext, "msgrtr.apinode.metrics.dmaap", iStream, "3", "12:00:00");
214
215                 } catch (org.json.JSONException e) {
216                         // TODO Auto-generated catch block
217                         e.printStackTrace();
218                 } catch (NullPointerException e) {
219                         // TODO Auto-generated catch block
220                         // e.printStackTrace();
221                         assertTrue(true);
222                 } catch (CambriaApiException e) {
223                         assertTrue(true);
224                 } catch (ConfigDbException e) {
225                         // TODO Auto-generated catch block
226                         e.printStackTrace();
227                 } catch (TopicExistsException e) {
228                         // TODO Auto-generated catch block
229                         e.printStackTrace();
230                 } catch (AccessDeniedException e) {
231                         // TODO Auto-generated catch block
232                         e.printStackTrace();
233                 } catch (IOException e) {
234                         // TODO Auto-generated catch block
235                         e.printStackTrace();
236                 } catch (missingReqdSetting e) {
237                         // TODO Auto-generated catch block
238                         e.printStackTrace();
239                 }
240         }
241         
242         @Test(expected = CambriaApiException.class)
243         public void testPushEvents_wttransaction_error() throws Exception {
244
245                 String source = "source of my InputStream";
246
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");
253
254         }
255
256         @Test
257         public void testPushEvents() {
258
259                 String source = "source of my InputStream";
260
261                 try {
262                         InputStream iStream = new ByteArrayInputStream(source.getBytes("UTF-8"));
263                         service.pushEvents(dmaapContext, "testTopic", iStream, "3", "12:00:00");
264
265                 } catch (org.json.JSONException e) {
266                         // TODO Auto-generated catch block
267                         e.printStackTrace();
268                 } catch (NullPointerException e) {
269                         // TODO Auto-generated catch block
270                         // e.printStackTrace();
271                         assertTrue(true);
272                 } catch (CambriaApiException e) {
273                         assertTrue(true);
274                 } catch (ConfigDbException e) {
275                         // TODO Auto-generated catch block
276                         e.printStackTrace();
277                 } catch (TopicExistsException e) {
278                         // TODO Auto-generated catch block
279                         e.printStackTrace();
280                 } catch (AccessDeniedException e) {
281                         // TODO Auto-generated catch block
282                         e.printStackTrace();
283                 } catch (IOException e) {
284                         // TODO Auto-generated catch block
285                         e.printStackTrace();
286                 } catch (missingReqdSetting e) {
287                         // TODO Auto-generated catch block
288                         e.printStackTrace();
289                 }
290
291         }
292
293         @Test
294         public void testPushEvents_blacklistip() {
295
296                 String source = "source of my InputStream";
297
298                 try {
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");
302
303                 } catch (org.json.JSONException e) {
304                         // TODO Auto-generated catch block
305                         e.printStackTrace();
306                 } catch (NullPointerException e) {
307                         // TODO Auto-generated catch block
308                         // e.printStackTrace();
309                         assertTrue(true);
310                 } catch (CambriaApiException e) {
311                         assertTrue(true);
312                 } catch (ConfigDbException e) {
313                         // TODO Auto-generated catch block
314                         e.printStackTrace();
315                 } catch (TopicExistsException e) {
316                         // TODO Auto-generated catch block
317                         e.printStackTrace();
318                 } catch (AccessDeniedException e) {
319                         // TODO Auto-generated catch block
320                         e.printStackTrace();
321                 } catch (IOException e) {
322                         // TODO Auto-generated catch block
323                         e.printStackTrace();
324                 } catch (missingReqdSetting e) {
325                         // TODO Auto-generated catch block
326                         e.printStackTrace();
327                 }
328
329         }
330
331         NsaApiDb<NsaSimpleApiKey> fApiKeyDb = new NsaApiDb<NsaSimpleApiKey>() {
332
333                 Set<String> keys = new HashSet<>(Arrays.asList("testkey", "admin"));
334
335                 @Override
336                 public NsaSimpleApiKey createApiKey(String arg0, String arg1)
337                                 throws com.att.nsa.security.db.NsaApiDb.KeyExistsException, ConfigDbException {
338                         // TODO Auto-generated method stub
339                         return new NsaSimpleApiKey(arg0, arg1);
340                 }
341
342                 @Override
343                 public boolean deleteApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
344                         // TODO Auto-generated method stub
345                         return false;
346                 }
347
348                 @Override
349                 public boolean deleteApiKey(String arg0) throws ConfigDbException {
350                         // TODO Auto-generated method stub
351                         return false;
352                 }
353
354                 @Override
355                 public Map<String, NsaSimpleApiKey> loadAllKeyRecords() throws ConfigDbException {
356                         // TODO Auto-generated method stub
357                         return null;
358                 }
359
360                 @Override
361                 public Set<String> loadAllKeys() throws ConfigDbException {
362                         // TODO Auto-generated method stub
363
364                         return keys;
365                 }
366
367                 @Override
368                 public NsaSimpleApiKey loadApiKey(String arg0) throws ConfigDbException {
369                         if (!keys.contains(arg0)) {
370                                 return null;
371                         }
372                         return new NsaSimpleApiKey(arg0, "password");
373                 }
374
375                 @Override
376                 public void saveApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
377                         // TODO Auto-generated method stub
378
379                 }
380         };
381
382 }