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=========================================================
21 package org.onap.dmaap.service;
23 import static org.junit.Assert.*;
25 import java.io.IOException;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
34 import org.onap.dmaap.dmf.mr.CambriaApiException;
35 import org.onap.dmaap.dmf.mr.beans.ApiKeyBean;
36 import com.att.nsa.configs.ConfigDbException;
37 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
39 import static org.junit.Assert.assertTrue;
40 import static org.mockito.Mockito.when;
41 import org.mockito.InjectMocks;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.json.JSONException;
46 import org.powermock.api.mockito.PowerMockito;
47 import org.powermock.core.classloader.annotations.PowerMockIgnore;
48 import org.powermock.core.classloader.annotations.PrepareForTest;
49 import org.powermock.modules.junit4.PowerMockRunner;
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
54 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
56 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
57 import org.onap.dmaap.dmf.mr.service.ApiKeysService;
58 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
59 import com.att.nsa.configs.ConfigDbException;
60 import com.att.nsa.security.db.NsaApiDb.KeyExistsException;
62 @RunWith(PowerMockRunner.class)
63 @PowerMockIgnore("jdk.internal.reflect.*")
64 @PrepareForTest({ ServiceUtil.class })
65 public class ApiKeysRestServiceTest {
68 private ApiKeysRestService service;
71 ApiKeysService apiKeyService;
74 DMaaPContext dmaapContext;
77 HttpServletRequest httpServReq;
79 private HttpServletResponse response;
81 private ConfigurationReader configReader;
84 public void setUp() throws Exception {
85 MockitoAnnotations.initMocks(this);
89 public void tearDown() throws Exception {
93 public void testGetAllApiKeys() {
96 service.getAllApiKeys();
97 } catch (CambriaApiException e) {
98 // TODO Auto-generated catch block
100 } catch (NullPointerException e) {
107 public void testGetAllApiKeys_error() throws ConfigDbException, IOException {
108 PowerMockito.mockStatic(ServiceUtil.class);
109 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);
110 PowerMockito.doThrow(new IOException("error")).when(apiKeyService).getAllApiKeys(dmaapContext);
112 service.getAllApiKeys();
113 } catch (CambriaApiException e) {
114 // TODO Auto-generated catch block
116 } catch (NullPointerException e) {
123 public void testGetApiKey() {
126 service.getApiKey("apikeyName");
127 } catch (CambriaApiException e) {
128 // TODO Auto-generated catch block
130 } catch (NullPointerException e) {
137 public void testGetApiKey_error() throws ConfigDbException, IOException {
138 PowerMockito.mockStatic(ServiceUtil.class);
139 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);
140 PowerMockito.doThrow(new IOException("error")).when(apiKeyService).getApiKey(dmaapContext, "apikeyName");
143 service.getApiKey("apikeyName");
144 } catch (CambriaApiException e) {
145 // TODO Auto-generated catch block
147 } catch (NullPointerException e) {
154 public void testCreateApiKey() {
157 service.createApiKey(new ApiKeyBean("hs647a@att.com", "test apikey"));
158 } catch (CambriaApiException e) {
159 // TODO Auto-generated catch block
161 } catch (NullPointerException e) {
168 public void testCreateApiKey_error()
169 throws CambriaApiException, JSONException, KeyExistsException, ConfigDbException, IOException {
171 ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
172 PowerMockito.mockStatic(ServiceUtil.class);
173 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);
174 PowerMockito.doThrow(new IOException("error")).when(apiKeyService).createApiKey(dmaapContext, bean);
177 service.createApiKey(bean);
178 } catch (CambriaApiException e) {
180 } catch (NullPointerException e) {
187 public void testUpdateApiKey() {
190 service.updateApiKey("apikeyName", new ApiKeyBean("hs647a@att.com", "test apikey"));
191 } catch (CambriaApiException e) {
192 // TODO Auto-generated catch block
194 } catch (NullPointerException e) {
201 public void testUpdateApiKey_error() throws CambriaApiException, JSONException, KeyExistsException,
202 ConfigDbException, IOException, AccessDeniedException {
204 ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
205 PowerMockito.mockStatic(ServiceUtil.class);
206 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);
207 PowerMockito.doThrow(new IOException("error")).when(apiKeyService).updateApiKey(dmaapContext, "apikeyName",
210 service.updateApiKey("apikeyName", bean);
211 } catch (CambriaApiException e) {
212 // TODO Auto-generated catch block
214 } catch (NullPointerException e) {
221 public void testDeleteApiKey() {
224 service.deleteApiKey("apikeyName");
225 } catch (CambriaApiException e) {
226 // TODO Auto-generated catch block
228 } catch (NullPointerException e) {