d3dc1cbf4f9c6d1ec0d4028a2bdb55799f871873
[portal.git] / ecomp-portal-widget-ms / widget-ms / src / test / java / org / onap / portalapp / widget / test / controller / WidgetsCatalogControllerTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017-2018 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  * 
37  */
38 package org.onap.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.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;
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 getUserWidgetCatalog_ValidAuthorization_NoError() throws Exception {        
146                 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
147                 WidgetCatalog widget = new WidgetCatalog();
148                 widget.setId(1);
149                 widget.setName("junit");
150                 list.add(widget);
151                 Mockito.when(widgetService.getUserWidgetCatalog("test")).thenReturn(list);
152                 
153                 String security_user = "user";
154                 String security_pass = "password";
155                 
156                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
157                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
158                 
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")));
164         }
165         
166         @Test
167         public void getUserWidgetCatalog_Authorization_Error() throws Exception {       
168                 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
169                 WidgetCatalog widget = new WidgetCatalog();
170                 widget.setId(1);
171                 widget.setName("junit");
172                 list.add(widget);
173                 Mockito.when(widgetService.getUserWidgetCatalog("test")).thenReturn(list);
174                 
175                 String security_user = "user";
176                 String security_pass = "password";
177                 String wrong_pass = "wrong";
178                 
179                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
180                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
181                 
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());
185                 
186         }
187         
188         
189         
190         @Test
191         public void saveWidgetCatalog_ValidAuthorization_NoError() throws Exception {   
192                 ValidationRespond respond = new ValidationRespond(true, null);
193                 Mockito.when(storageService.checkZipFile(any(MultipartFile.class))).thenReturn(respond);
194                 
195                 String security_user = "user";
196                 String security_pass = "password";
197                 
198                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
199                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
200                 
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)));
207                 
208                 Mockito.verify(widgetService, times(1)).saveWidgetCatalog(any(WidgetCatalog.class));
209         }
210         
211         @Test
212         public void saveWidgetCatalog_Authorization_Error() throws Exception {  
213                 ValidationRespond respond = new ValidationRespond(true, null);
214                 Mockito.when(storageService.checkZipFile(any(MultipartFile.class))).thenReturn(respond);
215                 
216                 String security_user = "user";
217                 String security_pass = "password";
218                 String wrong_pass = "wrong";
219                 
220                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
221                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
222                 
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());
229                 
230                 //Mockito.verify(widgetService, times(1)).saveWidgetCatalog(any(WidgetCatalog.class));
231         }       
232         
233         
234         
235         @Test
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);
241                 
242                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
243                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
244                 
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));
247                 
248                 Mockito.verify(widgetService, times(1)).updateWidgetCatalog(widgetServiceArg.capture(), any(WidgetCatalog.class));
249                 assertEquals(widgetServiceArg.getValue(), widgetId);
250         }
251         
252         @Test
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);
259                 
260                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
261                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
262                 
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());
265                 
266                 
267         }
268         
269         
270         @Test
271         public void updateWidgetCatalogwithFiles_ValidAuthorization_NoError() throws Exception {
272                 ValidationRespond respond = new ValidationRespond(true, null);
273                 Mockito.when(storageService.checkZipFile(any(MultipartFile.class))).thenReturn(respond);
274                 
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);
280                 
281                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
282                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
283                 
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)));
290                 
291                 Mockito.verify(widgetService, times(1)).updateWidgetCatalog(widgetServiceArg.capture(), any(WidgetCatalog.class));
292                 assertEquals(widgetServiceArg.getValue(), widgetId);
293         }
294         
295         
296         @Test
297         public void updateWidgetCatalogwithFiles_Authorization_error()throws Exception {
298                 
299                 String security_user = "user"; 
300                 String security_pass = "password";
301                 String wrong_pass = "wrong";
302                 Long widgetId = new Long(1);
303                         
304                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
305                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
306                 
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());
314         }
315         
316         
317         @Test
318         public void deleteOnboardingWidget_ValidAuthorization_NoError() throws Exception {
319                 
320                 String security_user = "user";
321                 String security_pass = "password";
322                 Long widgetId = new Long(1);
323                 
324                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
325                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
326                 
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);
332                 
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);
337         }
338         
339         @Test
340         public void deleteOnboardingWidget_Authorization_Error() throws Exception {
341                 
342                 String security_user = "user"; 
343                 String security_pass = "password";
344                 String wrong_pass = "wrong";
345                 Long widgetId = new Long(1);
346                         
347                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
348                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
349                 
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());
354         }
355         
356         @Test
357         public void getServiceIdByWidget_Authorization_error()throws Exception {
358                 
359                 String security_user = "user"; 
360                 String security_pass = "password";
361                 String wrong_pass = "wrong";
362                 Long widgetId = new Long(1);
363                         
364                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
365                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
366                 
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());
371                 
372         }
373         
374         @Test
375         public void getServiceIdByWidget_ValidAuthorization_NoError()throws Exception {
376                 
377                 String security_user = "user";
378                 String security_pass = "password";
379                 Long widgetId = new Long(1);
380                 
381                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
382                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
383                 
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);
389                 
390                 Mockito.verify(widgetService, times(1)).getServiceIdByWidget(widgetServiceArg.capture());
391                 assertEquals(widgetServiceArg.getValue(), widgetId);            
392         }
393         
394
395         @Test
396         public void getWidgetByServiceIdt_Authorization_error()throws Exception {
397                 
398                 String security_user = "user"; 
399                 String security_pass = "password";
400                 String wrong_pass = "wrong";
401                 Long serviceId = new Long(1);
402                         
403                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
404                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
405                 
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());
410                 
411         }
412         
413         @Test
414         public void getWidgetByServiceIdt_ValidAuthorization_Noerror()throws Exception {
415                 
416                 Long serviceId = new Long(1);
417                 
418                 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
419                 WidgetCatalog widget = new WidgetCatalog();
420                 widget.setId(1);
421                 widget.setName("junit");
422                 list.add(widget);
423                 Mockito.when(widgetService.getWidgetsByServiceId(serviceId)).thenReturn(list);
424                 
425                 String security_user = "user";
426                 String security_pass = "password";
427                 
428                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
429                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
430                 
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")));
436                 
437         }
438         
439         @Test
440         public void getWidgetZipFile_Authorization_error()throws Exception {
441                 
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);
448                 
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());
453                 
454         }
455         
456         
457         @Test
458         public void getWidgetZipFile_ValidAuthorization_Noerror()throws Exception {
459                 
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.when(storageService.getWidgetCatalogContent(widgetId)).thenReturn(bytes);       
466                         
467                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
468                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
469                 
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());
474                 
475         }
476         
477         
478         
479 }