2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package com.att.nsa.dmaap.service;
\r
23 import static org.junit.Assert.*;
\r
25 import java.io.IOException;
\r
26 import java.lang.reflect.InvocationTargetException;
\r
27 import java.lang.reflect.Method;
\r
29 import org.junit.After;
\r
30 import org.junit.Before;
\r
31 import org.junit.Test;
\r
32 import org.junit.runner.RunWith;
\r
34 import org.onap.dmaap.dmf.mr.CambriaApiException;
\r
35 import org.onap.dmaap.dmf.mr.beans.ApiKeyBean;
\r
36 import com.att.nsa.configs.ConfigDbException;
\r
37 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
\r
39 import static org.junit.Assert.assertTrue;
\r
40 import static org.mockito.Mockito.when;
\r
41 import org.mockito.InjectMocks;
\r
42 import org.mockito.Mock;
\r
43 import org.mockito.MockitoAnnotations;
\r
44 import org.json.JSONException;
\r
46 import org.powermock.api.mockito.PowerMockito;
\r
47 import org.powermock.core.classloader.annotations.PrepareForTest;
\r
48 import org.powermock.modules.junit4.PowerMockRunner;
\r
50 import javax.servlet.http.HttpServletRequest;
\r
51 import javax.servlet.http.HttpServletResponse;
\r
53 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
\r
55 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
\r
56 import org.onap.dmaap.dmf.mr.service.ApiKeysService;
\r
57 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
\r
58 import com.att.nsa.configs.ConfigDbException;
\r
59 import com.att.nsa.security.db.NsaApiDb.KeyExistsException;
\r
61 @RunWith(PowerMockRunner.class)
\r
62 @PrepareForTest({ ServiceUtil.class })
\r
63 public class ApiKeysRestServiceTest {
\r
66 private ApiKeysRestService service;
\r
69 ApiKeysService apiKeyService;
\r
72 DMaaPContext dmaapContext;
\r
75 HttpServletRequest httpServReq;
\r
77 private HttpServletResponse response;
\r
79 private ConfigurationReader configReader;
\r
82 public void setUp() throws Exception {
\r
83 MockitoAnnotations.initMocks(this);
\r
87 public void tearDown() throws Exception {
\r
91 public void testGetAllApiKeys() {
\r
94 service.getAllApiKeys();
\r
95 } catch (CambriaApiException e) {
\r
96 // TODO Auto-generated catch block
\r
97 e.printStackTrace();
\r
98 } catch (NullPointerException e) {
\r
105 public void testGetAllApiKeys_error() throws ConfigDbException, IOException {
\r
106 PowerMockito.mockStatic(ServiceUtil.class);
\r
107 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);
\r
108 PowerMockito.doThrow(new IOException("error")).when(apiKeyService).getAllApiKeys(dmaapContext);
\r
110 service.getAllApiKeys();
\r
111 } catch (CambriaApiException e) {
\r
112 // TODO Auto-generated catch block
\r
114 } catch (NullPointerException e) {
\r
121 public void testGetApiKey() {
\r
124 service.getApiKey("apikeyName");
\r
125 } catch (CambriaApiException e) {
\r
126 // TODO Auto-generated catch block
\r
127 e.printStackTrace();
\r
128 } catch (NullPointerException e) {
\r
135 public void testGetApiKey_error() throws ConfigDbException, IOException {
\r
136 PowerMockito.mockStatic(ServiceUtil.class);
\r
137 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);
\r
138 PowerMockito.doThrow(new IOException("error")).when(apiKeyService).getApiKey(dmaapContext, "apikeyName");
\r
141 service.getApiKey("apikeyName");
\r
142 } catch (CambriaApiException e) {
\r
143 // TODO Auto-generated catch block
\r
145 } catch (NullPointerException e) {
\r
152 public void testCreateApiKey() {
\r
155 service.createApiKey(new ApiKeyBean("hs647a@att.com", "test apikey"));
\r
156 } catch (CambriaApiException e) {
\r
157 // TODO Auto-generated catch block
\r
158 e.printStackTrace();
\r
159 } catch (NullPointerException e) {
\r
166 public void testCreateApiKey_error()
\r
167 throws CambriaApiException, JSONException, KeyExistsException, ConfigDbException, IOException {
\r
169 ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
\r
170 PowerMockito.mockStatic(ServiceUtil.class);
\r
171 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);
\r
172 PowerMockito.doThrow(new IOException("error")).when(apiKeyService).createApiKey(dmaapContext, bean);
\r
175 service.createApiKey(bean);
\r
176 } catch (CambriaApiException e) {
\r
178 } catch (NullPointerException e) {
\r
185 public void testUpdateApiKey() {
\r
188 service.updateApiKey("apikeyName", new ApiKeyBean("hs647a@att.com", "test apikey"));
\r
189 } catch (CambriaApiException e) {
\r
190 // TODO Auto-generated catch block
\r
191 e.printStackTrace();
\r
192 } catch (NullPointerException e) {
\r
199 public void testUpdateApiKey_error() throws CambriaApiException, JSONException, KeyExistsException,
\r
200 ConfigDbException, IOException, AccessDeniedException {
\r
202 ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
\r
203 PowerMockito.mockStatic(ServiceUtil.class);
\r
204 PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext);
\r
205 PowerMockito.doThrow(new IOException("error")).when(apiKeyService).updateApiKey(dmaapContext, "apikeyName",
\r
208 service.updateApiKey("apikeyName", bean);
\r
209 } catch (CambriaApiException e) {
\r
210 // TODO Auto-generated catch block
\r
211 e.printStackTrace();
\r
212 } catch (NullPointerException e) {
\r
219 public void testDeleteApiKey() {
\r
222 service.deleteApiKey("apikeyName");
\r
223 } catch (CambriaApiException e) {
\r
224 // TODO Auto-generated catch block
\r
225 e.printStackTrace();
\r
226 } catch (NullPointerException e) {
\r