2 * ============LICENSE_START=======================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
22 package com.att.nsa.cambria.service.impl;
\r
24 import static org.junit.Assert.*;
\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
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
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
58 @RunWith(PowerMockRunner.class)
\r
59 @PrepareForTest({ DMaaPAuthenticatorImpl.class, DMaaPResponseBuilder.class })
\r
60 public class ApiKeysServiceImplTest {
\r
63 ApiKeysServiceImpl service;
\r
66 DMaaPContext dmaapContext;
\r
68 ConsumerFactory factory;
\r
71 ConfigurationReader configReader;
\r
73 Blacklist Blacklist;
\r
78 public void setUp() throws Exception {
\r
80 MockitoAnnotations.initMocks(this);
\r
81 PowerMockito.mockStatic(DMaaPAuthenticatorImpl.class);
\r
82 NsaSimpleApiKey user = new NsaSimpleApiKey("admin", "password");
\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
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
96 public void tearDown() throws Exception {
\r
101 public void testGetAllApiKeys() {
\r
103 service = new ApiKeysServiceImpl();
\r
105 service.getAllApiKeys(dmaapContext);
\r
106 } catch (NullPointerException e) {
\r
107 // TODO Auto-generated catch block
\r
108 //e.printStackTrace();
\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
122 public void testGetApiKey() {
\r
124 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
\r
126 service.getApiKey(dmaapContext, "testkey");
\r
127 } catch (NullPointerException e) {
\r
128 // TODO Auto-generated catch block
\r
129 //e.printStackTrace();
\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
142 public void testGetApiKey_error() {
\r
144 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
\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
161 public void testCreateApiKey() {
\r
163 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
\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
185 public void testUpdateApiKey() {
\r
187 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
\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
208 public void testUpdateApiKey_error() {
\r
210 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
\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
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
224 } catch (AccessDeniedException e) {
\r
225 // TODO Auto-generated catch block
\r
226 e.printStackTrace();
\r
232 public void testDeleteApiKey() {
\r
234 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
\r
237 service.deleteApiKey(dmaapContext, null);
\r
238 } catch (NullPointerException e) {
\r
239 // TODO Auto-generated catch block
\r
240 //e.printStackTrace();
\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
256 public void testDeleteApiKey_error() {
\r
258 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
\r
261 service.deleteApiKey(dmaapContext, "admin");
\r
262 } catch (NullPointerException e) {
\r
263 // TODO Auto-generated catch block
\r
264 //e.printStackTrace();
\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
279 NsaApiDb<NsaSimpleApiKey> fApiKeyDb= new NsaApiDb<NsaSimpleApiKey>() {
\r
282 Set<String> keys = new HashSet<>(Arrays.asList("testkey","admin"));
\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
293 public boolean deleteApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
\r
294 // TODO Auto-generated method stub
\r
299 public boolean deleteApiKey(String arg0) throws ConfigDbException {
\r
300 // TODO Auto-generated method stub
\r
305 public Map<String, NsaSimpleApiKey> loadAllKeyRecords() throws ConfigDbException {
\r
306 // TODO Auto-generated method stub
\r
311 public Set<String> loadAllKeys() throws ConfigDbException {
\r
312 // TODO Auto-generated method stub
\r
318 public NsaSimpleApiKey loadApiKey(String arg0) throws ConfigDbException {
\r
319 if(!keys.contains(arg0)){
\r
322 return new NsaSimpleApiKey(arg0, "password");
\r
326 public void saveApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
\r
327 // TODO Auto-generated method stub
\r