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