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