Null check for ClientResponse in PolicyUril.java
[portal.git] / ecomp-portal-widget-ms / widget-ms / src / test / java / org / openecomp / portalapp / widget / test / controller / WidgetsCatalogControllerTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.widget.test.controller;
39
40
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;
49
50 import java.util.ArrayList;
51 import java.util.List;
52
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;
74
75 @RunWith(MockitoJUnitRunner.class)
76 public class WidgetsCatalogControllerTest {
77
78         private MockMvc mockMvc;
79         
80         @Mock
81         private WidgetCatalogService widgetService;
82         
83         @Mock
84         private StorageService storageService;
85         
86         @InjectMocks
87         private WidgetsCatalogController controller;
88         
89         @Before
90         public void setUp() {
91                 MockitoAnnotations.initMocks(this);
92                 mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
93         }
94         
95         @Test
96         public void getWidgetCatalog_ValidAuthorization_NoError() throws Exception {    
97                 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
98                 WidgetCatalog widget = new WidgetCatalog();
99                 widget.setId(1);
100                 widget.setName("junit");
101                 list.add(widget);
102                 Mockito.when(widgetService.getWidgetCatalog()).thenReturn(list);
103                 
104                 String security_user = "user";
105                 String security_pass = "password";
106                 
107                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
108                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
109                 
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")));
115         }
116           
117         @Test
118         public void getWidgetCatalog_InValidAuthorization_Unauthorized() throws Exception {     
119
120                 String security_user = "user";
121                 String security_pass = "password";
122                 String wrong_pass = "wrong";
123                 
124                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
125                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
126                 
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());
130         }
131         
132         @Test
133         public void getWidgetCatalog_NoAuthorization_BadRequest() throws Exception {    
134                 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
135                 WidgetCatalog widget = new WidgetCatalog();
136                 list.add(widget);
137                 Mockito.when(widgetService.getWidgetCatalog()).thenReturn(list);
138                 
139                 mockMvc.perform(get("/microservices/widgetCatalog/"))
140                 .andExpect(status().isBadRequest());
141         }
142         
143         
144         @Test
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);
148                 
149                 String security_user = "user";
150                 String security_pass = "password";
151                 
152                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
153                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
154                 
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)));
161                 
162                 Mockito.verify(widgetService, times(1)).saveWidgetCatalog(any(WidgetCatalog.class));
163         }
164         
165         
166         @Test
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);
172                 
173                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
174                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
175                 
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));
178                 
179                 Mockito.verify(widgetService, times(1)).updateWidgetCatalog(widgetServiceArg.capture(), any(WidgetCatalog.class));
180                 assertEquals(widgetServiceArg.getValue(), widgetId);
181         }
182         
183         
184         @Test
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);
188                 
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);
194                 
195                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
196                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
197                 
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)));
204                 
205                 Mockito.verify(widgetService, times(1)).updateWidgetCatalog(widgetServiceArg.capture(), any(WidgetCatalog.class));
206                 assertEquals(widgetServiceArg.getValue(), widgetId);
207         }
208         
209         @Test
210         public void deleteOnboardingWidget_ValidAuthorization_NoError() throws Exception {
211                 
212                 String security_user = "user";
213                 String security_pass = "password";
214                 Long widgetId = new Long(1);
215                 
216                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
217                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
218                 
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);
224                 
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);
229         }
230         
231         
232 }