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