[DMAAP-MR] - Uplift to Java 11
[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 import static org.junit.Assert.*;
24
25 import java.io.IOException;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33
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;
38
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;
45
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;
50
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
53
54 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
55
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;
61
62 @RunWith(PowerMockRunner.class)
63 @PowerMockIgnore("jdk.internal.reflect.*")
64 @PrepareForTest({ ServiceUtil.class })
65 public class ApiKeysRestServiceTest {
66
67         @InjectMocks
68         private ApiKeysRestService service;
69
70         @Mock
71         ApiKeysService apiKeyService;
72
73         @Mock
74         DMaaPContext dmaapContext;
75
76         @Mock
77         HttpServletRequest httpServReq;
78         @Mock
79         private HttpServletResponse response;
80         @Mock
81         private ConfigurationReader configReader;
82
83         @Before
84         public void setUp() throws Exception {
85                 MockitoAnnotations.initMocks(this);
86         }
87
88         @After
89         public void tearDown() throws Exception {
90         }
91
92         @Test
93         public void testGetAllApiKeys() {
94
95                 try {
96                         service.getAllApiKeys();
97                 } catch (CambriaApiException e) {
98                         // TODO Auto-generated catch block
99                         e.printStackTrace();
100                 } catch (NullPointerException e) {
101                         assertTrue(true);
102                 }
103
104         }
105
106         @Test
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);
111                 try {
112                         service.getAllApiKeys();
113                 } catch (CambriaApiException e) {
114                         // TODO Auto-generated catch block
115                         assertTrue(true);
116                 } catch (NullPointerException e) {
117                         assertTrue(true);
118                 }
119
120         }
121
122         @Test
123         public void testGetApiKey() {
124
125                 try {
126                         service.getApiKey("apikeyName");
127                 } catch (CambriaApiException e) {
128                         // TODO Auto-generated catch block
129                         e.printStackTrace();
130                 } catch (NullPointerException e) {
131                         assertTrue(true);
132                 }
133
134         }
135
136         @Test
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");
141
142                 try {
143                         service.getApiKey("apikeyName");
144                 } catch (CambriaApiException e) {
145                         // TODO Auto-generated catch block
146                         assertTrue(true);
147                 } catch (NullPointerException e) {
148                         assertTrue(true);
149                 }
150
151         }
152
153         @Test
154         public void testCreateApiKey() {
155
156                 try {
157                         service.createApiKey(new ApiKeyBean("hs647a@att.com", "test apikey"));
158                 } catch (CambriaApiException e) {
159                         // TODO Auto-generated catch block
160                         e.printStackTrace();
161                 } catch (NullPointerException e) {
162                         assertTrue(true);
163                 }
164
165         }
166
167         @Test
168         public void testCreateApiKey_error()
169                         throws CambriaApiException, JSONException, KeyExistsException, ConfigDbException, IOException {
170
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);
175
176                 try {
177                         service.createApiKey(bean);
178                 } catch (CambriaApiException e) {
179                         assertTrue(true);
180                 } catch (NullPointerException e) {
181                         assertTrue(true);
182                 }
183
184         }
185
186         @Test
187         public void testUpdateApiKey() {
188
189                 try {
190                         service.updateApiKey("apikeyName", new ApiKeyBean("hs647a@att.com", "test apikey"));
191                 } catch (CambriaApiException e) {
192                         // TODO Auto-generated catch block
193                         e.printStackTrace();
194                 } catch (NullPointerException e) {
195                         assertTrue(true);
196                 }
197
198         }
199
200         @Test
201         public void testUpdateApiKey_error() throws CambriaApiException, JSONException, KeyExistsException,
202                         ConfigDbException, IOException, AccessDeniedException {
203
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",
208                                 bean);
209                 try {
210                         service.updateApiKey("apikeyName", bean);
211                 } catch (CambriaApiException e) {
212                         // TODO Auto-generated catch block
213                         e.printStackTrace();
214                 } catch (NullPointerException e) {
215                         assertTrue(true);
216                 }
217
218         }
219
220         @Test
221         public void testDeleteApiKey() {
222
223                 try {
224                         service.deleteApiKey("apikeyName");
225                 } catch (CambriaApiException e) {
226                         // TODO Auto-generated catch block
227                         e.printStackTrace();
228                 } catch (NullPointerException e) {
229                         assertTrue(true);
230                 }
231
232         }
233
234 }