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.PowerMockIgnore;
56 import org.powermock.core.classloader.annotations.PrepareForTest;
57 import org.powermock.modules.junit4.PowerMockRunner;
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 {
65 ApiKeysServiceImpl service;
68 DMaaPContext dmaapContext;
70 ConsumerFactory factory;
73 ConfigurationReader configReader;
80 public void setUp() throws Exception {
82 MockitoAnnotations.initMocks(this);
83 PowerMockito.mockStatic(DMaaPAuthenticatorImpl.class);
84 NsaSimpleApiKey user = new NsaSimpleApiKey("admin", "password");
86 PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);
87 PowerMockito.when(configReader.getfConsumerFactory()).thenReturn(factory);
88 PowerMockito.when(configReader.getfIpBlackList()).thenReturn(Blacklist);
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);
98 public void tearDown() throws Exception {
103 public void testGetAllApiKeys() {
105 service = new ApiKeysServiceImpl();
107 service.getAllApiKeys(dmaapContext);
108 } catch (NullPointerException e) {
109 // TODO Auto-generated catch block
110 //e.printStackTrace();
112 } catch (ConfigDbException e) {
113 // TODO Auto-generated catch block
115 } catch (IOException e) {
116 // TODO Auto-generated catch block
124 public void testGetApiKey() {
126 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
128 service.getApiKey(dmaapContext, "testkey");
129 } catch (NullPointerException e) {
130 // TODO Auto-generated catch block
131 //e.printStackTrace();
133 } catch (ConfigDbException e) {
134 // TODO Auto-generated catch block
136 } catch (IOException e) {
137 // TODO Auto-generated catch block
144 public void testGetApiKey_error() {
146 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
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
155 } catch (IOException e) {
156 // TODO Auto-generated catch block
163 public void testCreateApiKey() {
165 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
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
174 } catch (IOException e) {
175 // TODO Auto-generated catch block
177 } catch (KeyExistsException e) {
178 // TODO Auto-generated catch block
180 } catch(NoClassDefFoundError e) {
187 public void testUpdateApiKey() {
189 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
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
199 } catch (IOException e) {
200 // TODO Auto-generated catch block
202 } catch (AccessDeniedException e) {
203 // TODO Auto-generated catch block
210 public void testUpdateApiKey_error() {
212 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
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();
220 } catch (ConfigDbException e) {
221 // TODO Auto-generated catch block
223 } catch (IOException e) {
224 // TODO Auto-generated catch block
226 } catch (AccessDeniedException e) {
227 // TODO Auto-generated catch block
234 public void testDeleteApiKey() {
236 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
239 service.deleteApiKey(dmaapContext, null);
240 } catch (NullPointerException e) {
241 // TODO Auto-generated catch block
242 //e.printStackTrace();
244 } catch (ConfigDbException e) {
245 // TODO Auto-generated catch block
247 } catch (IOException e) {
248 // TODO Auto-generated catch block
250 } catch (AccessDeniedException e) {
251 // TODO Auto-generated catch block
258 public void testDeleteApiKey_error() {
260 ApiKeysServiceImpl service = new ApiKeysServiceImpl();
263 service.deleteApiKey(dmaapContext, "admin");
264 } catch (NullPointerException e) {
265 // TODO Auto-generated catch block
266 //e.printStackTrace();
268 } catch (ConfigDbException e) {
269 // TODO Auto-generated catch block
271 } catch (IOException e) {
272 // TODO Auto-generated catch block
274 } catch (AccessDeniedException e) {
275 // TODO Auto-generated catch block
281 NsaApiDb<NsaSimpleApiKey> fApiKeyDb= new NsaApiDb<NsaSimpleApiKey>() {
284 Set<String> keys = new HashSet<>(Arrays.asList("testkey","admin"));
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);
295 public boolean deleteApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
296 // TODO Auto-generated method stub
301 public boolean deleteApiKey(String arg0) throws ConfigDbException {
302 // TODO Auto-generated method stub
307 public Map<String, NsaSimpleApiKey> loadAllKeyRecords() throws ConfigDbException {
308 // TODO Auto-generated method stub
313 public Set<String> loadAllKeys() throws ConfigDbException {
314 // TODO Auto-generated method stub
320 public NsaSimpleApiKey loadApiKey(String arg0) throws ConfigDbException {
321 if(!keys.contains(arg0)){
324 return new NsaSimpleApiKey(arg0, "password");
328 public void saveApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
329 // TODO Auto-generated method stub