DMAAP-MR Unit test improvements
[dmaap/messagerouter/messageservice.git] / src / test / java / org / onap / dmaap / service / ApiKeysRestServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21  package org.onap.dmaap.service;
22
23
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Mockito.doThrow;
27
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;
43
44 @RunWith(MockitoJUnitRunner.class)
45 public class ApiKeysRestServiceTest {
46         @InjectMocks
47         private ApiKeysRestService service;
48
49         @Mock
50         ApiKeysService apiKeyService;
51
52         @Test
53         public void testGetAllApiKeys() {
54                 try {
55                         service.getAllApiKeys();
56                 } catch (CambriaApiException e) {
57                         // TODO Auto-generated catch block
58                         e.printStackTrace();
59                 } catch (NullPointerException e) {
60                         assertTrue(true);
61                 }
62
63         }
64
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");
70         }
71
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");
77         }
78
79         @Test
80         public void testGetApiKey() {
81                 try {
82                         service.getApiKey("apikeyName");
83                 } catch (CambriaApiException e) {
84                         // TODO Auto-generated catch block
85                         e.printStackTrace();
86                 } catch (NullPointerException e) {
87                         assertTrue(true);
88                 }
89         }
90
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));
95
96                 service.getApiKey(apikeyName);
97                 fail("Was expecting an exception to be thrown");
98         }
99
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));
104
105                 service.getApiKey(apikeyName);
106                 fail("Was expecting an exception to be thrown");
107         }
108
109         @Test
110         public void testCreateApiKey() {
111                 try {
112                         service.createApiKey(new ApiKeyBean("hs647a@att.com", "test apikey"));
113                 } catch (CambriaApiException e) {
114                         // TODO Auto-generated catch block
115                         e.printStackTrace();
116                 } catch (NullPointerException e) {
117                         assertTrue(true);
118                 }
119         }
120
121         @Test(expected=CambriaApiException.class)
122         public void testCreateApiKey_ConfigDbException()
123                         throws CambriaApiException, JSONException, KeyExistsException, ConfigDbException, IOException {
124
125                 ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
126
127                 doThrow(new ConfigDbException("error")).when(apiKeyService).createApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(ApiKeyBean.class));
128
129                 service.createApiKey(bean);
130                 fail("Was expecting an exception to be thrown");
131         }
132
133         @Test(expected=CambriaApiException.class)
134         public void testCreateApiKey_IOException()
135                 throws CambriaApiException, JSONException, KeyExistsException, ConfigDbException, IOException {
136
137                 ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
138
139                 doThrow(new IOException("error")).when(apiKeyService).createApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(ApiKeyBean.class));
140
141                 service.createApiKey(bean);
142                 fail("Was expecting an exception to be thrown");
143         }
144
145         @Test(expected=CambriaApiException.class)
146         public void testCreateApiKey_KeyExistsException()
147                 throws CambriaApiException, JSONException, KeyExistsException, ConfigDbException, IOException {
148
149                 ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey");
150
151                 doThrow(new KeyExistsException("error")).when(apiKeyService).createApiKey(ArgumentMatchers.any(DMaaPContext.class), ArgumentMatchers.any(ApiKeyBean.class));
152
153                 service.createApiKey(bean);
154                 fail("Was expecting an exception to be thrown");
155         }
156
157         @Test
158         public void testUpdateApiKey() {
159                 try {
160                         service.updateApiKey("apikeyName", new ApiKeyBean("hs647a@att.com", "test apikey"));
161                 } catch (CambriaApiException e) {
162                         // TODO Auto-generated catch block
163                         e.printStackTrace();
164                 } catch (NullPointerException e) {
165                         assertTrue(true);
166                 }
167         }
168
169         @Test(expected=CambriaApiException.class)
170         public void testUpdateApiKey_ConfigDbException() throws CambriaApiException, JSONException,
171                         ConfigDbException, IOException, AccessDeniedException {
172
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));
175
176                 service.updateApiKey("apikeyName", bean);
177                 fail("Was expecting an exception to be thrown");
178         }
179
180         @Test(expected=CambriaApiException.class)
181         public void testUpdateApiKey_IOException() throws CambriaApiException, JSONException,
182                 ConfigDbException, IOException, AccessDeniedException {
183
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));
186
187                 service.updateApiKey("apikeyName", bean);
188                 fail("Was expecting an exception to be thrown");
189         }
190
191         @Test(expected=CambriaApiException.class)
192         public void testUpdateApiKey_AccessDeniedException() throws CambriaApiException, JSONException,
193                 ConfigDbException, IOException, AccessDeniedException {
194
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));
197
198                 service.updateApiKey("apikeyName", bean);
199                 fail("Was expecting an exception to be thrown");
200         }
201
202         @Test
203         public void testDeleteApiKey() {
204
205                 try {
206                         service.deleteApiKey("apikeyName");
207                 } catch (CambriaApiException e) {
208                         // TODO Auto-generated catch block
209                         e.printStackTrace();
210                 } catch (NullPointerException e) {
211                         assertTrue(true);
212                 }
213         }
214
215         @Test(expected=CambriaApiException.class)
216         public void testDeleteApiKey_AccessDeniedException() throws CambriaApiException, JSONException,
217                 ConfigDbException, IOException, AccessDeniedException {
218
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));
221
222                 service.deleteApiKey("apikeyName");
223                 fail("Was expecting an exception to be thrown");
224         }
225
226         @Test(expected=CambriaApiException.class)
227         public void testDeleteApiKey_IOException() throws CambriaApiException, JSONException,
228                 ConfigDbException, IOException, AccessDeniedException {
229
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));
232
233                 service.deleteApiKey("apikeyName");
234                 fail("Was expecting an exception to be thrown");
235         }
236
237         @Test(expected=CambriaApiException.class)
238         public void testDeleteApiKey_ConfigDbException() throws CambriaApiException, JSONException,
239                 ConfigDbException, IOException, AccessDeniedException {
240
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));
243
244                 service.deleteApiKey("apikeyName");
245                 fail("Was expecting an exception to be thrown");
246         }
247 }