2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.widget.test.controller;
41 import static org.hamcrest.CoreMatchers.is;
42 import static org.junit.Assert.assertEquals;
43 import static org.mockito.Matchers.any;
44 import static org.mockito.Mockito.times;
45 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
46 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
47 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
48 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
50 import java.util.ArrayList;
51 import java.util.List;
53 import org.apache.commons.codec.binary.Base64;
54 import org.junit.Before;
55 import org.junit.Test;
56 import org.junit.runner.RunWith;
57 import org.mockito.ArgumentCaptor;
58 import org.mockito.InjectMocks;
59 import org.mockito.Mock;
60 import org.mockito.Mockito;
61 import org.mockito.MockitoAnnotations;
62 import org.mockito.runners.MockitoJUnitRunner;
63 import org.onap.portalapp.widget.controller.WidgetsCatalogController;
64 import org.onap.portalapp.widget.domain.ValidationRespond;
65 import org.onap.portalapp.widget.domain.WidgetCatalog;
66 import org.onap.portalapp.widget.service.StorageService;
67 import org.onap.portalapp.widget.service.WidgetCatalogService;
68 import org.springframework.http.MediaType;
69 import org.springframework.test.util.ReflectionTestUtils;
70 import org.springframework.test.web.servlet.MockMvc;
71 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
72 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
73 import org.springframework.web.multipart.MultipartFile;
75 @RunWith(MockitoJUnitRunner.class)
76 public class WidgetsCatalogControllerTest {
78 private MockMvc mockMvc;
81 private WidgetCatalogService widgetService;
84 private StorageService storageService;
87 private WidgetsCatalogController controller;
91 MockitoAnnotations.initMocks(this);
92 mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
96 public void getWidgetCatalog_ValidAuthorization_NoError() throws Exception {
97 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
98 WidgetCatalog widget = new WidgetCatalog();
100 widget.setName("junit");
102 Mockito.when(widgetService.getWidgetCatalog()).thenReturn(list);
104 String security_user = "user";
105 String security_pass = "password";
107 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
108 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
110 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes()));
111 mockMvc.perform(get("/microservices/widgetCatalog/").header("Authorization", basic_auth))
112 .andExpect(status().isOk())
113 .andExpect(jsonPath("$[0].id", is(1)))
114 .andExpect(jsonPath("$[0].name", is("junit")));
118 public void getWidgetCatalog_InValidAuthorization_Unauthorized() throws Exception {
120 String security_user = "user";
121 String security_pass = "password";
122 String wrong_pass = "wrong";
124 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
125 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
127 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + wrong_pass).getBytes()));
128 mockMvc.perform(get("/microservices/widgetCatalog/").header("Authorization", basic_auth))
129 .andExpect(status().isUnauthorized());
133 public void getWidgetCatalog_NoAuthorization_BadRequest() throws Exception {
134 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
135 WidgetCatalog widget = new WidgetCatalog();
137 Mockito.lenient().when(widgetService.getWidgetCatalog()).thenReturn(list);
139 mockMvc.perform(get("/microservices/widgetCatalog/"))
140 .andExpect(status().isBadRequest());
145 public void getUserWidgetCatalog_ValidAuthorization_NoError() throws Exception {
146 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
147 WidgetCatalog widget = new WidgetCatalog();
149 widget.setName("junit");
151 Mockito.lenient().when(widgetService.getUserWidgetCatalog("test")).thenReturn(list);
153 String security_user = "user";
154 String security_pass = "password";
156 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
157 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
159 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes()));
160 mockMvc.perform(get("/microservices/widgetCatalog/test").header("Authorization", basic_auth))
161 .andExpect(status().isOk())
162 .andExpect(jsonPath("$[0].id", is(1)))
163 .andExpect(jsonPath("$[0].name", is("junit")));
167 public void getUserWidgetCatalog_Authorization_Error() throws Exception {
168 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
169 WidgetCatalog widget = new WidgetCatalog();
171 widget.setName("junit");
173 Mockito.lenient().when(widgetService.getUserWidgetCatalog("test")).thenReturn(list);
175 String security_user = "user";
176 String security_pass = "password";
177 String wrong_pass = "wrong";
179 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
180 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
182 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + wrong_pass).getBytes()));
183 mockMvc.perform(get("/microservices/widgetCatalog/test").header("Authorization", basic_auth))
184 .andExpect(status().isUnauthorized());
191 public void saveWidgetCatalog_ValidAuthorization_NoError() throws Exception {
192 ValidationRespond respond = new ValidationRespond(true, null);
193 Mockito.lenient().when(storageService.checkZipFile(any(MultipartFile.class))).thenReturn(respond);
195 String security_user = "user";
196 String security_pass = "password";
198 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
199 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
201 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes()));
202 mockMvc.perform(MockMvcRequestBuilders.fileUpload("/microservices/widgetCatalog/").file("file", null)
203 .param("widget", "{}")
204 .header("Authorization", basic_auth)
205 .contentType(MediaType.MULTIPART_FORM_DATA))
206 .andExpect(jsonPath("$.valid", is(true)));
208 Mockito.verify(widgetService, times(1)).saveWidgetCatalog(any(WidgetCatalog.class));
212 public void saveWidgetCatalog_Authorization_Error() throws Exception {
213 ValidationRespond respond = new ValidationRespond(true, null);
214 Mockito.lenient().when(storageService.checkZipFile(any(MultipartFile.class))).thenReturn(respond);
216 String security_user = "user";
217 String security_pass = "password";
218 String wrong_pass = "wrong";
220 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
221 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
223 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + wrong_pass).getBytes()));
224 mockMvc.perform(MockMvcRequestBuilders.fileUpload("/microservices/widgetCatalog/").file("file", null)
225 .param("widget", "{}")
226 .header("Authorization", basic_auth)
227 .contentType(MediaType.MULTIPART_FORM_DATA))
228 .andExpect(status().isUnauthorized());
230 //Mockito.verify(widgetService, times(1)).saveWidgetCatalog(any(WidgetCatalog.class));
236 public void updateWidgetCatalog_ValidAuthorization_NoError() throws Exception {
237 String security_user = "user";
238 String security_pass = "password";
239 Long widgetId = new Long(1);
240 ArgumentCaptor<Long> widgetServiceArg = ArgumentCaptor.forClass(Long.class);
242 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
243 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
245 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes()));
246 mockMvc.perform(put("/microservices/widgetCatalog/" + widgetId).contentType(MediaType.APPLICATION_JSON).content("{}").header("Authorization", basic_auth));
248 Mockito.verify(widgetService, times(1)).updateWidgetCatalog(widgetServiceArg.capture(), any(WidgetCatalog.class));
249 assertEquals(widgetServiceArg.getValue(), widgetId);
253 public void updateWidgetCatalog_Authorization_Error() throws Exception {
254 String security_user = "user";
255 String security_pass = "password";
256 String wrong_pass = "wrong";
257 Long widgetId = new Long(1);
258 ArgumentCaptor<Long> widgetServiceArg = ArgumentCaptor.forClass(Long.class);
260 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
261 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
263 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + wrong_pass).getBytes()));
264 mockMvc.perform(put("/microservices/widgetCatalog/" + widgetId).contentType(MediaType.APPLICATION_JSON).content("{}").header("Authorization", basic_auth)).andExpect(status().isUnauthorized());
271 public void updateWidgetCatalogwithFiles_ValidAuthorization_NoError() throws Exception {
272 ValidationRespond respond = new ValidationRespond(true, null);
273 Mockito.lenient().when(storageService.checkZipFile(any(MultipartFile.class))).thenReturn(respond);
275 String security_user = "user";
276 String security_pass = "password";
277 Long widgetId = new Long(1);
278 ArgumentCaptor<Long> widgetServiceArg = ArgumentCaptor.forClass(Long.class);
279 ArgumentCaptor<Long> storageServiceArg = ArgumentCaptor.forClass(Long.class);
281 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
282 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
284 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes()));
285 mockMvc.perform(MockMvcRequestBuilders.fileUpload("/microservices/widgetCatalog/" + widgetId).file("file", null)
286 .param("widget", "{}")
287 .header("Authorization", basic_auth)
288 .contentType(MediaType.MULTIPART_FORM_DATA))
289 .andExpect(jsonPath("$.valid", is(true)));
291 Mockito.verify(widgetService, times(1)).updateWidgetCatalog(widgetServiceArg.capture(), any(WidgetCatalog.class));
292 assertEquals(widgetServiceArg.getValue(), widgetId);
297 public void updateWidgetCatalogwithFiles_Authorization_error()throws Exception {
299 String security_user = "user";
300 String security_pass = "password";
301 String wrong_pass = "wrong";
302 Long widgetId = new Long(1);
304 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
305 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
307 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + wrong_pass).getBytes()));
308 mockMvc.perform(MockMvcRequestBuilders.fileUpload("/microservices/widgetCatalog/" + widgetId).file("file", null)
309 .param("widget", "{}")
310 .header("Authorization", basic_auth)
311 .contentType(MediaType.MULTIPART_FORM_DATA)
312 ).andExpect(status().isUnauthorized());
313 //("/microservices/widgetCatalog/" + widgetId).contentType(MediaType.APPLICATION_JSON).content("{}").header("Authorization", basic_auth)).andExpect(status().isUnauthorized());
318 public void deleteOnboardingWidget_ValidAuthorization_NoError() throws Exception {
320 String security_user = "user";
321 String security_pass = "password";
322 Long widgetId = new Long(1);
324 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
325 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
327 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes()));
328 mockMvc.perform(MockMvcRequestBuilders.delete("/microservices/widgetCatalog/" + widgetId)
329 .header("Authorization", basic_auth));
330 ArgumentCaptor<Long> widgetServiceArg = ArgumentCaptor.forClass(Long.class);
331 ArgumentCaptor<Long> storageServiceArg = ArgumentCaptor.forClass(Long.class);
333 Mockito.verify(widgetService, times(1)).deleteWidgetCatalog(widgetServiceArg.capture());
334 assertEquals(widgetServiceArg.getValue(), widgetId);
335 Mockito.verify(storageService, times(1)).deleteWidgetFile(storageServiceArg.capture());
336 assertEquals(storageServiceArg.getValue(), widgetId);
340 public void deleteOnboardingWidget_Authorization_Error() throws Exception {
342 String security_user = "user";
343 String security_pass = "password";
344 String wrong_pass = "wrong";
345 Long widgetId = new Long(1);
347 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
348 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
350 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + wrong_pass).getBytes()));
351 mockMvc.perform(MockMvcRequestBuilders.delete("/microservices/widgetCatalog/" + widgetId)
352 .header("Authorization", basic_auth)
353 ).andExpect(status().isUnauthorized());
357 public void getServiceIdByWidget_Authorization_error()throws Exception {
359 String security_user = "user";
360 String security_pass = "password";
361 String wrong_pass = "wrong";
362 Long widgetId = new Long(1);
364 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
365 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
367 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + wrong_pass).getBytes()));
368 mockMvc.perform(MockMvcRequestBuilders.get("/microservices/widgetCatalog/parameters/" + widgetId)
369 .header("Authorization", basic_auth)
370 ).andExpect(status().isUnauthorized());
375 public void getServiceIdByWidget_ValidAuthorization_NoError()throws Exception {
377 String security_user = "user";
378 String security_pass = "password";
379 Long widgetId = new Long(1);
381 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
382 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
384 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes()));
385 mockMvc.perform(MockMvcRequestBuilders.get("/microservices/widgetCatalog/parameters/" + widgetId)
386 .header("Authorization", basic_auth));
387 ArgumentCaptor<Long> widgetServiceArg = ArgumentCaptor.forClass(Long.class);
388 ArgumentCaptor<Long> storageServiceArg = ArgumentCaptor.forClass(Long.class);
390 Mockito.verify(widgetService, times(1)).getServiceIdByWidget(widgetServiceArg.capture());
391 assertEquals(widgetServiceArg.getValue(), widgetId);
396 public void getWidgetByServiceIdt_Authorization_error()throws Exception {
398 String security_user = "user";
399 String security_pass = "password";
400 String wrong_pass = "wrong";
401 Long serviceId = new Long(1);
403 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
404 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
406 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + wrong_pass).getBytes()));
407 mockMvc.perform(MockMvcRequestBuilders.get("/microservices/widgetCatalog/service/" + serviceId)
408 .header("Authorization", basic_auth)
409 ).andExpect(status().isUnauthorized());
414 public void getWidgetByServiceIdt_ValidAuthorization_Noerror()throws Exception {
416 Long serviceId = new Long(1);
418 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
419 WidgetCatalog widget = new WidgetCatalog();
421 widget.setName("junit");
423 Mockito.lenient().when(widgetService.getWidgetsByServiceId(serviceId)).thenReturn(list);
425 String security_user = "user";
426 String security_pass = "password";
428 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
429 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
431 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes()));
432 mockMvc.perform(get("/microservices/widgetCatalog/service/"+serviceId).header("Authorization", basic_auth))
433 .andExpect(status().isOk())
434 .andExpect(jsonPath("$[0].id", is(1)))
435 .andExpect(jsonPath("$[0].name", is("junit")));
440 public void getWidgetZipFile_Authorization_error()throws Exception {
442 String security_user = "user";
443 String security_pass = "password";
444 String wrong_pass = "wrong";
445 Long widgetId = new Long(1);
446 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
447 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
449 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + wrong_pass).getBytes()));
450 mockMvc.perform(MockMvcRequestBuilders.get("/microservices/download/" + widgetId)
451 .header("Authorization", basic_auth)
452 ).andExpect(status().isUnauthorized());
458 public void getWidgetZipFile_ValidAuthorization_Noerror()throws Exception {
460 String security_user = "user";
461 String security_pass = "password";
462 String wrong_pass = "wrong";
463 Long widgetId = new Long(1);
464 byte[] bytes="Test".getBytes();
465 Mockito.lenient().when(storageService.getWidgetCatalogContent(widgetId)).thenReturn(bytes);
467 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
468 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
470 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes()));
471 mockMvc.perform(MockMvcRequestBuilders.get("/microservices/download/" + widgetId)
472 .header("Authorization", basic_auth)
473 ).andExpect(status().isOk());