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.PrepareForTest;
48 import org.powermock.modules.junit4.PowerMockRunner;
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpServletResponse;
53 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
55 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
56 import org.onap.dmaap.dmf.mr.service.ApiKeysService;
57 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
58 import com.att.nsa.configs.ConfigDbException;
59 import com.att.nsa.security.db.NsaApiDb.KeyExistsException;
61 @RunWith(PowerMockRunner.class)
62 @PrepareForTest({ ServiceUtil.class })
63 public class ApiKeysRestServiceTest {
66 private ApiKeysRestService service;
69 ApiKeysService apiKeyService;
72 DMaaPContext dmaapContext;
75 HttpServletRequest httpServReq;
77 private HttpServletResponse response;
79 private ConfigurationReader configReader;
82 public void setUp() throws Exception {
83 MockitoAnnotations.initMocks(this);
87 public void tearDown() throws Exception {
91 public void testGetAllApiKeys() {
94 service.getAllApiKeys();
95 } catch (CambriaApiException e) {
96 // TODO Auto-generated catch block
98 } catch (NullPointerException e) {
105 public void testGetAllApiKeys_error() throws ConfigDbException, IOException {
106 PowerMockito.mockStatic(ServiceUtil.class);
107 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);
108 PowerMockito.doThrow(new IOException("error")).when(apiKeyService).getAllApiKeys(dmaapContext);
110 service.getAllApiKeys();
111 } catch (CambriaApiException e) {
112 // TODO Auto-generated catch block
114 } catch (NullPointerException e) {
121 public void testGetApiKey() {
124 service.getApiKey("apikeyName");
125 } catch (CambriaApiException e) {
126 // TODO Auto-generated catch block
128 } catch (NullPointerException e) {
135 public void testGetApiKey_error() throws ConfigDbException, IOException {
136 PowerMockito.mockStatic(ServiceUtil.class);
137 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);
138 PowerMockito.doThrow(new IOException("error")).when(apiKeyService).getApiKey(dmaapContext, "apikeyName");
141 service.getApiKey("apikeyName");
142 } catch (CambriaApiException e) {
143 // TODO Auto-generated catch block
145 } catch (NullPointerException e) {
152 public void testCreateApiKey() {
155 service.createApiKey(new ApiKeyBean("hs647a@att.com", "test apikey"));
156 } catch (CambriaApiException e) {
157 // TODO Auto-generated catch block
159 } catch (NullPointerException e) {
166 public void testCreateApiKey_error()
167 throws CambriaApiException, JSONException, KeyExistsException, ConfigDbException, IOException {
169 ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
170 PowerMockito.mockStatic(ServiceUtil.class);
171 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);
172 PowerMockito.doThrow(new IOException("error")).when(apiKeyService).createApiKey(dmaapContext, bean);
175 service.createApiKey(bean);
176 } catch (CambriaApiException e) {
178 } catch (NullPointerException e) {
185 public void testUpdateApiKey() {
188 service.updateApiKey("apikeyName", new ApiKeyBean("hs647a@att.com", "test apikey"));
189 } catch (CambriaApiException e) {
190 // TODO Auto-generated catch block
192 } catch (NullPointerException e) {
199 public void testUpdateApiKey_error() throws CambriaApiException, JSONException, KeyExistsException,
200 ConfigDbException, IOException, AccessDeniedException {
202 ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
203 PowerMockito.mockStatic(ServiceUtil.class);
204 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);
205 PowerMockito.doThrow(new IOException("error")).when(apiKeyService).updateApiKey(dmaapContext, "apikeyName",
208 service.updateApiKey("apikeyName", bean);
209 } catch (CambriaApiException e) {
210 // TODO Auto-generated catch block
212 } catch (NullPointerException e) {
219 public void testDeleteApiKey() {
222 service.deleteApiKey("apikeyName");
223 } catch (CambriaApiException e) {
224 // TODO Auto-generated catch block
226 } catch (NullPointerException e) {