2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.dmaap.mr.cambria.service.impl;
24 import static org.junit.Assert.*;
26 import java.io.IOException;
27 import java.util.Arrays;
28 import java.util.HashSet;
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;
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;
58 @RunWith(PowerMockRunner.class)
59 @PrepareForTest({ DMaaPAuthenticatorImpl.class, DMaaPResponseBuilder.class })
60 public class ApiKeysServiceImplTest {
63 ApiKeysServiceImpl service;
66 DMaaPContext dmaapContext;
68 ConsumerFactory factory;
71 ConfigurationReader configReader;
78 public void setUp() throws Exception {
80 MockitoAnnotations.initMocks(this);
81 PowerMockito.mockStatic(DMaaPAuthenticatorImpl.class);
82 NsaSimpleApiKey user = new NsaSimpleApiKey("admin", "password");
84 PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);
85 PowerMockito.when(configReader.getfConsumerFactory()).thenReturn(factory);
86 PowerMockito.when(configReader.getfIpBlackList()).thenReturn(Blacklist);
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);
96 public void tearDown() throws Exception {
101 public void testGetAllApiKeys() {
103 service = new ApiKeysServiceImpl();
105 service.getAllApiKeys(dmaapContext);
106 } catch (NullPointerException e) {
107 // TODO Auto-generated catch block
108 //e.printStackTrace();
110 } catch (ConfigDbException e) {
111 // TODO Auto-generated catch block
113 } catch (IOException e) {
114 // TODO Auto-generated catch block
122 public void testGetApiKey() {
124 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
126 service.getApiKey(dmaapContext, "testkey");
127 } catch (NullPointerException e) {
128 // TODO Auto-generated catch block
129 //e.printStackTrace();
131 } catch (ConfigDbException e) {
132 // TODO Auto-generated catch block
134 } catch (IOException e) {
135 // TODO Auto-generated catch block
142 public void testGetApiKey_error() {
144 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
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
153 } catch (IOException e) {
154 // TODO Auto-generated catch block
161 public void testCreateApiKey() {
163 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
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
172 } catch (IOException e) {
173 // TODO Auto-generated catch block
175 } catch (KeyExistsException e) {
176 // TODO Auto-generated catch block
178 } catch(NoClassDefFoundError e) {
185 public void testUpdateApiKey() {
187 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
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
197 } catch (IOException e) {
198 // TODO Auto-generated catch block
200 } catch (AccessDeniedException e) {
201 // TODO Auto-generated catch block
208 public void testUpdateApiKey_error() {
210 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
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();
218 } catch (ConfigDbException e) {
219 // TODO Auto-generated catch block
221 } catch (IOException e) {
222 // TODO Auto-generated catch block
224 } catch (AccessDeniedException e) {
225 // TODO Auto-generated catch block
232 public void testDeleteApiKey() {
234 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
237 service.deleteApiKey(dmaapContext, null);
238 } catch (NullPointerException e) {
239 // TODO Auto-generated catch block
240 //e.printStackTrace();
242 } catch (ConfigDbException e) {
243 // TODO Auto-generated catch block
245 } catch (IOException e) {
246 // TODO Auto-generated catch block
248 } catch (AccessDeniedException e) {
249 // TODO Auto-generated catch block
256 public void testDeleteApiKey_error() {
258 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
261 service.deleteApiKey(dmaapContext, "admin");
262 } catch (NullPointerException e) {
263 // TODO Auto-generated catch block
264 //e.printStackTrace();
266 } catch (ConfigDbException e) {
267 // TODO Auto-generated catch block
269 } catch (IOException e) {
270 // TODO Auto-generated catch block
272 } catch (AccessDeniedException e) {
273 // TODO Auto-generated catch block
279 NsaApiDb<NsaSimpleApiKey> fApiKeyDb= new NsaApiDb<NsaSimpleApiKey>() {
282 Set<String> keys = new HashSet<>(Arrays.asList("testkey","admin"));
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);
293 public boolean deleteApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
294 // TODO Auto-generated method stub
299 public boolean deleteApiKey(String arg0) throws ConfigDbException {
300 // TODO Auto-generated method stub
305 public Map<String, NsaSimpleApiKey> loadAllKeyRecords() throws ConfigDbException {
306 // TODO Auto-generated method stub
311 public Set<String> loadAllKeys() throws ConfigDbException {
312 // TODO Auto-generated method stub
318 public NsaSimpleApiKey loadApiKey(String arg0) throws ConfigDbException {
319 if(!keys.contains(arg0)){
322 return new NsaSimpleApiKey(arg0, "password");
326 public void saveApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
327 // TODO Auto-generated method stub