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