MR Java 11 Uplift
[dmaap/messagerouter/msgrtr.git] / src / test / java / org / onap / dmaap / mr / cambria / service / impl / ApiKeysServiceImplTest.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
22  package org.onap.dmaap.mr.cambria.service.impl;
23
24 import static org.junit.Assert.*;
25
26 import java.io.IOException;
27 import java.util.Arrays;
28 import java.util.HashSet;
29 import java.util.Map;
30 import java.util.Set;
31
32 import org.onap.dmaap.dmf.mr.backends.ConsumerFactory;
33 import org.onap.dmaap.dmf.mr.beans.ApiKeyBean;
34 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
35 import org.onap.dmaap.dmf.mr.security.DMaaPAuthenticatorImpl;
36 import org.onap.dmaap.dmf.mr.service.impl.ApiKeysServiceImpl;
37 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
38 import org.onap.dmaap.dmf.mr.utils.DMaaPResponseBuilder;
39 import org.onap.dmaap.dmf.mr.utils.Emailer;
40 import com.att.nsa.configs.ConfigDbException;
41 import com.att.nsa.limits.Blacklist;
42 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
43 import com.att.nsa.security.db.NsaApiDb;
44 import com.att.nsa.security.db.NsaApiDb.KeyExistsException;
45 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
46
47 import org.junit.After;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.MockitoAnnotations;
54 import org.powermock.api.mockito.PowerMockito;
55 import org.powermock.core.classloader.annotations.PowerMockIgnore;
56 import org.powermock.core.classloader.annotations.PrepareForTest;
57 import org.powermock.modules.junit4.PowerMockRunner;
58
59 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"})
60 @RunWith(PowerMockRunner.class)
61 @PrepareForTest({ DMaaPAuthenticatorImpl.class, DMaaPResponseBuilder.class })
62 public class ApiKeysServiceImplTest {
63         
64         @InjectMocks
65         ApiKeysServiceImpl service;
66
67         @Mock
68         DMaaPContext dmaapContext;
69         @Mock
70         ConsumerFactory factory;
71
72         @Mock
73         ConfigurationReader configReader;
74         @Mock
75         Blacklist Blacklist;
76         @Mock
77         Emailer emailer;
78
79         @Before
80         public void setUp() throws Exception {
81
82                 MockitoAnnotations.initMocks(this);
83                 PowerMockito.mockStatic(DMaaPAuthenticatorImpl.class);
84                 NsaSimpleApiKey user = new NsaSimpleApiKey("admin", "password");
85
86                 PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);
87                 PowerMockito.when(configReader.getfConsumerFactory()).thenReturn(factory);
88                 PowerMockito.when(configReader.getfIpBlackList()).thenReturn(Blacklist);
89                 
90                 PowerMockito.when(configReader.getfApiKeyDb()).thenReturn(fApiKeyDb);
91                 PowerMockito.when(configReader.getSystemEmailer()).thenReturn(emailer);
92                 PowerMockito.when(DMaaPAuthenticatorImpl.getAuthenticatedUser(dmaapContext)).thenReturn(user);
93                 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
94         
95         }
96
97         @After
98         public void tearDown() throws Exception {
99         }
100
101         
102         @Test
103         public void testGetAllApiKeys() {
104                 
105                  service = new ApiKeysServiceImpl();
106                 try {
107                         service.getAllApiKeys(dmaapContext);
108                 } catch (NullPointerException e) {
109                         // TODO Auto-generated catch block
110                         //e.printStackTrace();
111                         
112                 } catch (ConfigDbException e) {
113                         // TODO Auto-generated catch block
114                         e.printStackTrace();
115                 } catch (IOException e) {
116                         // TODO Auto-generated catch block
117                         e.printStackTrace();
118                 }
119                 assertTrue(true);
120          
121         }
122         
123         @Test
124         public void testGetApiKey() {
125                 
126                 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
127                 try {
128                         service.getApiKey(dmaapContext, "testkey");
129                 } catch (NullPointerException e) {
130                         // TODO Auto-generated catch block
131                         //e.printStackTrace();
132                         assertTrue(true);
133                 } catch (ConfigDbException e) {
134                         // TODO Auto-generated catch block
135                         e.printStackTrace();
136                 } catch (IOException e) {
137                         // TODO Auto-generated catch block
138                 }
139                 assertTrue(true);
140          
141         }
142         
143         @Test
144         public void testGetApiKey_error() {
145                 
146                 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
147                 try {
148                         service.getApiKey(dmaapContext, "k35Hdw6Sde");
149                 } catch (NullPointerException e) {
150                         // TODO Auto-generated catch block
151                         //e.printStackTrace();
152                 } catch (ConfigDbException e) {
153                         // TODO Auto-generated catch block
154                         e.printStackTrace();
155                 } catch (IOException e) {
156                         // TODO Auto-generated catch block
157                         assertTrue(true);
158                 }
159          
160         }
161         
162         @Test
163         public void testCreateApiKey() {
164                 
165                 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
166                 try {
167                         service.createApiKey(dmaapContext, new ApiKeyBean("test@onap.com", "testing apikey bean"));
168                 } catch (NullPointerException e) {
169                         // TODO Auto-generated catch block
170                         //e.printStackTrace();
171                 } catch (ConfigDbException e) {
172                         // TODO Auto-generated catch block
173                         e.printStackTrace();
174                 } catch (IOException e) {
175                         // TODO Auto-generated catch block
176                         e.printStackTrace();
177                 } catch (KeyExistsException e) {
178                         // TODO Auto-generated catch block
179                         e.printStackTrace();
180                 } catch(NoClassDefFoundError e) {
181                         
182                 }
183                  assertTrue(true);
184         }
185         
186         @Test
187         public void testUpdateApiKey() {
188                 
189                 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
190                 try {
191                         
192                         service.updateApiKey(dmaapContext, "admin", new ApiKeyBean("test@onapt.com", "testing apikey bean"));
193                 } catch (NullPointerException e) {
194                         // TODO Auto-generated catch block
195                         //e.printStackTrace();
196                 } catch (ConfigDbException e) {
197                         // TODO Auto-generated catch block
198                         e.printStackTrace();
199                 } catch (IOException e) {
200                         // TODO Auto-generated catch block
201                         e.printStackTrace();
202                 } catch (AccessDeniedException e) {
203                         // TODO Auto-generated catch block
204                         e.printStackTrace();
205                 }
206                  assertTrue(true);
207          
208         }
209         @Test
210         public void testUpdateApiKey_error() {
211                 
212                 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
213                 try {
214                         
215                         service.updateApiKey(dmaapContext, null, new ApiKeyBean("test@onapt.com", "testing apikey bean"));
216                 } catch (NullPointerException e) {
217                         // TODO Auto-generated catch block
218                         //e.printStackTrace();
219                         assertTrue(true);
220                 } catch (ConfigDbException e) {
221                         // TODO Auto-generated catch block
222                         e.printStackTrace();
223                 } catch (IOException e) {
224                         // TODO Auto-generated catch block
225                          assertTrue(true);
226                 } catch (AccessDeniedException e) {
227                         // TODO Auto-generated catch block
228                         e.printStackTrace();
229                 }
230          
231         }
232         
233         @Test
234         public void testDeleteApiKey() {
235                 
236                 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
237                 try {
238                         
239                         service.deleteApiKey(dmaapContext, null);
240                 } catch (NullPointerException e) {
241                         // TODO Auto-generated catch block
242                         //e.printStackTrace();
243                         assertTrue(true);
244                 } catch (ConfigDbException e) {
245                         // TODO Auto-generated catch block
246                         e.printStackTrace();
247                 } catch (IOException e) {
248                         // TODO Auto-generated catch block
249                         e.printStackTrace();
250                 } catch (AccessDeniedException e) {
251                         // TODO Auto-generated catch block
252                         e.printStackTrace();
253                 }
254          
255         }
256         
257         @Test
258         public void testDeleteApiKey_error() {
259                 
260                 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
261                 try {
262                         
263                         service.deleteApiKey(dmaapContext, "admin");
264                 } catch (NullPointerException e) {
265                         // TODO Auto-generated catch block
266                         //e.printStackTrace();
267                         assertTrue(true);
268                 } catch (ConfigDbException e) {
269                         // TODO Auto-generated catch block
270                         e.printStackTrace();
271                 } catch (IOException e) {
272                         // TODO Auto-generated catch block
273                         e.printStackTrace();
274                 } catch (AccessDeniedException e) {
275                         // TODO Auto-generated catch block
276                         e.printStackTrace();
277                 }
278          
279         }
280         
281         NsaApiDb<NsaSimpleApiKey> fApiKeyDb= new NsaApiDb<NsaSimpleApiKey>() {
282                 
283                 
284                 Set<String> keys = new HashSet<>(Arrays.asList("testkey","admin"));
285                 
286                 
287                 @Override
288                 public NsaSimpleApiKey createApiKey(String arg0, String arg1)
289                                 throws com.att.nsa.security.db.NsaApiDb.KeyExistsException, ConfigDbException {
290                         // TODO Auto-generated method stub
291                         return new NsaSimpleApiKey(arg0, arg1);
292                 }
293
294                 @Override
295                 public boolean deleteApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
296                         // TODO Auto-generated method stub
297                         return false;
298                 }
299
300                 @Override
301                 public boolean deleteApiKey(String arg0) throws ConfigDbException {
302                         // TODO Auto-generated method stub
303                         return false;
304                 }
305
306                 @Override
307                 public Map<String, NsaSimpleApiKey> loadAllKeyRecords() throws ConfigDbException {
308                         // TODO Auto-generated method stub
309                         return null;
310                 }
311
312                 @Override
313                 public Set<String> loadAllKeys() throws ConfigDbException {
314                         // TODO Auto-generated method stub
315                         
316                         return keys ;
317                 }
318
319                 @Override
320                 public NsaSimpleApiKey loadApiKey(String arg0) throws ConfigDbException {
321                         if(!keys.contains(arg0)){
322                                 return null;
323                         }
324                         return new NsaSimpleApiKey(arg0, "password");
325                 }
326
327                 @Override
328                 public void saveApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
329                         // TODO Auto-generated method stub
330                         
331                 }
332         };
333 }