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;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Mockito.doThrow;
28 import com.att.nsa.configs.ConfigDbException;
29 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
30 import com.att.nsa.security.db.NsaApiDb.KeyExistsException;
31 import java.io.IOException;
32 import org.json.JSONException;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.ArgumentMatchers;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mock;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.onap.dmaap.dmf.mr.CambriaApiException;
40 import org.onap.dmaap.dmf.mr.beans.ApiKeyBean;
41 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
42 import org.onap.dmaap.dmf.mr.service.ApiKeysService;
44 @RunWith(MockitoJUnitRunner.class)
45 public class ApiKeysRestServiceTest {
47 private ApiKeysRestService service;
50 ApiKeysService apiKeyService;
53 public void testGetAllApiKeys() {
55 service.getAllApiKeys();
56 } catch (CambriaApiException e) {
57 // TODO Auto-generated catch block
59 } catch (NullPointerException e) {
65 @Test(expected=CambriaApiException.class)
66 public void testGetAllApiKeys_IOException() throws ConfigDbException, IOException, CambriaApiException {
67 doThrow(new IOException("error")).when(apiKeyService).getAllApiKeys(ArgumentMatchers.any(DMaaPContext.class));
68 service.getAllApiKeys();
69 fail("Was expecting an exception to be thrown");
72 @Test(expected=CambriaApiException.class)
73 public void testGetAllApiKeys_ConfigDBException() throws ConfigDbException, IOException, CambriaApiException {
74 doThrow(new ConfigDbException("error")).when(apiKeyService).getAllApiKeys(ArgumentMatchers.any(DMaaPContext.class));
75 service.getAllApiKeys();
76 fail("Was expecting an exception to be thrown");
80 public void testGetApiKey() {
82 service.getApiKey("apikeyName");
83 } catch (CambriaApiException e) {
84 // TODO Auto-generated catch block
86 } catch (NullPointerException e) {
91 @Test(expected=CambriaApiException.class)
92 public void testGetApiKey_IOException() throws ConfigDbException, IOException, CambriaApiException {
93 String apikeyName = "apikeyName";
94 doThrow(new IOException("error")).when(apiKeyService).getApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
96 service.getApiKey(apikeyName);
97 fail("Was expecting an exception to be thrown");
100 @Test(expected=CambriaApiException.class)
101 public void testGetApiKey_ConfigDBException() throws ConfigDbException, IOException, CambriaApiException {
102 String apikeyName = "apikeyName";
103 doThrow(new ConfigDbException("error")).when(apiKeyService).getApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
105 service.getApiKey(apikeyName);
106 fail("Was expecting an exception to be thrown");
110 public void testCreateApiKey() {
112 service.createApiKey(new ApiKeyBean("hs647a@att.com", "test apikey"));
113 } catch (CambriaApiException e) {
114 // TODO Auto-generated catch block
116 } catch (NullPointerException e) {
121 @Test(expected=CambriaApiException.class)
122 public void testCreateApiKey_ConfigDbException()
123 throws CambriaApiException, JSONException, KeyExistsException, ConfigDbException, IOException {
125 ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
127 doThrow(new ConfigDbException("error")).when(apiKeyService).createApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(ApiKeyBean.class));
129 service.createApiKey(bean);
130 fail("Was expecting an exception to be thrown");
133 @Test(expected=CambriaApiException.class)
134 public void testCreateApiKey_IOException()
135 throws CambriaApiException, JSONException, KeyExistsException, ConfigDbException, IOException {
137 ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
139 doThrow(new IOException("error")).when(apiKeyService).createApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(ApiKeyBean.class));
141 service.createApiKey(bean);
142 fail("Was expecting an exception to be thrown");
145 @Test(expected=CambriaApiException.class)
146 public void testCreateApiKey_KeyExistsException()
147 throws CambriaApiException, JSONException, KeyExistsException, ConfigDbException, IOException {
149 ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
151 doThrow(new KeyExistsException("error")).when(apiKeyService).createApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(ApiKeyBean.class));
153 service.createApiKey(bean);
154 fail("Was expecting an exception to be thrown");
158 public void testUpdateApiKey() {
160 service.updateApiKey("apikeyName", new ApiKeyBean("hs647a@att.com", "test apikey"));
161 } catch (CambriaApiException e) {
162 // TODO Auto-generated catch block
164 } catch (NullPointerException e) {
169 @Test(expected=CambriaApiException.class)
170 public void testUpdateApiKey_ConfigDbException() throws CambriaApiException, JSONException,
171 ConfigDbException, IOException, AccessDeniedException {
173 ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
174 doThrow(new ConfigDbException("error")).when(apiKeyService).updateApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(ApiKeyBean.class));
176 service.updateApiKey("apikeyName", bean);
177 fail("Was expecting an exception to be thrown");
180 @Test(expected=CambriaApiException.class)
181 public void testUpdateApiKey_IOException() throws CambriaApiException, JSONException,
182 ConfigDbException, IOException, AccessDeniedException {
184 ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
185 doThrow(new IOException("error")).when(apiKeyService).updateApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(ApiKeyBean.class));
187 service.updateApiKey("apikeyName", bean);
188 fail("Was expecting an exception to be thrown");
191 @Test(expected=CambriaApiException.class)
192 public void testUpdateApiKey_AccessDeniedException() throws CambriaApiException, JSONException,
193 ConfigDbException, IOException, AccessDeniedException {
195 ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
196 doThrow(new AccessDeniedException("error")).when(apiKeyService).updateApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class), ArgumentMatchers.any(ApiKeyBean.class));
198 service.updateApiKey("apikeyName", bean);
199 fail("Was expecting an exception to be thrown");
203 public void testDeleteApiKey() {
206 service.deleteApiKey("apikeyName");
207 } catch (CambriaApiException e) {
208 // TODO Auto-generated catch block
210 } catch (NullPointerException e) {
215 @Test(expected=CambriaApiException.class)
216 public void testDeleteApiKey_AccessDeniedException() throws CambriaApiException, JSONException,
217 ConfigDbException, IOException, AccessDeniedException {
219 ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
220 doThrow(new AccessDeniedException("error")).when(apiKeyService).deleteApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
222 service.deleteApiKey("apikeyName");
223 fail("Was expecting an exception to be thrown");
226 @Test(expected=CambriaApiException.class)
227 public void testDeleteApiKey_IOException() throws CambriaApiException, JSONException,
228 ConfigDbException, IOException, AccessDeniedException {
230 ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
231 doThrow(new IOException("error")).when(apiKeyService).deleteApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
233 service.deleteApiKey("apikeyName");
234 fail("Was expecting an exception to be thrown");
237 @Test(expected=CambriaApiException.class)
238 public void testDeleteApiKey_ConfigDbException() throws CambriaApiException, JSONException,
239 ConfigDbException, IOException, AccessDeniedException {
241 ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
242 doThrow(new ConfigDbException("error")).when(apiKeyService).deleteApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(String.class));
244 service.deleteApiKey("apikeyName");
245 fail("Was expecting an exception to be thrown");