2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.openecomp.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.openecomp.portalapp.widget.controller.WidgetsCatalogController;
64 import org.openecomp.portalapp.widget.domain.ValidationRespond;
65 import org.openecomp.portalapp.widget.domain.WidgetCatalog;
66 import org.openecomp.portalapp.widget.service.StorageService;
67 import org.openecomp.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.when(widgetService.getWidgetCatalog()).thenReturn(list);
139 mockMvc.perform(get("/microservices/widgetCatalog/"))
140 .andExpect(status().isBadRequest());
145 public void saveWidgetCatalog_ValidAuthorization_NoError() throws Exception {
146 ValidationRespond respond = new ValidationRespond(true, null);
147 Mockito.when(storageService.checkZipFile(any(MultipartFile.class))).thenReturn(respond);
149 String security_user = "user";
150 String security_pass = "password";
152 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
153 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
155 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes()));
156 mockMvc.perform(MockMvcRequestBuilders.fileUpload("/microservices/widgetCatalog/").file("file", null)
157 .param("widget", "{}")
158 .header("Authorization", basic_auth)
159 .contentType(MediaType.MULTIPART_FORM_DATA))
160 .andExpect(jsonPath("$.valid", is(true)));
162 Mockito.verify(widgetService, times(1)).saveWidgetCatalog(any(WidgetCatalog.class));
167 public void updateWidgetCatalog_ValidAuthorization_NoError() throws Exception {
168 String security_user = "user";
169 String security_pass = "password";
170 Long widgetId = new Long(1);
171 ArgumentCaptor<Long> widgetServiceArg = ArgumentCaptor.forClass(Long.class);
173 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
174 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
176 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes()));
177 mockMvc.perform(put("/microservices/widgetCatalog/" + widgetId).contentType(MediaType.APPLICATION_JSON).content("{}").header("Authorization", basic_auth));
179 Mockito.verify(widgetService, times(1)).updateWidgetCatalog(widgetServiceArg.capture(), any(WidgetCatalog.class));
180 assertEquals(widgetServiceArg.getValue(), widgetId);
185 public void updateWidgetCatalogwithFiles_ValidAuthorization_NoError() throws Exception {
186 ValidationRespond respond = new ValidationRespond(true, null);
187 Mockito.when(storageService.checkZipFile(any(MultipartFile.class))).thenReturn(respond);
189 String security_user = "user";
190 String security_pass = "password";
191 Long widgetId = new Long(1);
192 ArgumentCaptor<Long> widgetServiceArg = ArgumentCaptor.forClass(Long.class);
193 ArgumentCaptor<Long> storageServiceArg = ArgumentCaptor.forClass(Long.class);
195 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
196 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
198 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes()));
199 mockMvc.perform(MockMvcRequestBuilders.fileUpload("/microservices/widgetCatalog/" + widgetId).file("file", null)
200 .param("widget", "{}")
201 .header("Authorization", basic_auth)
202 .contentType(MediaType.MULTIPART_FORM_DATA))
203 .andExpect(jsonPath("$.valid", is(true)));
205 Mockito.verify(widgetService, times(1)).updateWidgetCatalog(widgetServiceArg.capture(), any(WidgetCatalog.class));
206 assertEquals(widgetServiceArg.getValue(), widgetId);
210 public void deleteOnboardingWidget_ValidAuthorization_NoError() throws Exception {
212 String security_user = "user";
213 String security_pass = "password";
214 Long widgetId = new Long(1);
216 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
217 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
219 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes()));
220 mockMvc.perform(MockMvcRequestBuilders.delete("/microservices/widgetCatalog/" + widgetId)
221 .header("Authorization", basic_auth));
222 ArgumentCaptor<Long> widgetServiceArg = ArgumentCaptor.forClass(Long.class);
223 ArgumentCaptor<Long> storageServiceArg = ArgumentCaptor.forClass(Long.class);
225 Mockito.verify(widgetService, times(1)).deleteWidgetCatalog(widgetServiceArg.capture());
226 assertEquals(widgetServiceArg.getValue(), widgetId);
227 Mockito.verify(storageService, times(1)).deleteWidgetFile(storageServiceArg.capture());
228 assertEquals(storageServiceArg.getValue(), widgetId);