bd7eba3a6ae937b816fb8bcb52607436a63dd681
[portal.git] / ecomp-portal-widget-ms / widget-ms / src / test / java / org / openecomp / portalapp / widget / test / controller / WidgetsCatalogControllerTest.java
1 package org.openecomp.portalapp.widget.test.controller;
2
3
4 import static org.hamcrest.CoreMatchers.is;
5 import static org.junit.Assert.assertEquals;
6 import static org.mockito.Matchers.any;
7 import static org.mockito.Mockito.times;
8 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
9 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
10 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
11 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.apache.commons.codec.binary.Base64;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.ArgumentCaptor;
21 import org.mockito.InjectMocks;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.MockitoAnnotations;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.openecomp.portalapp.widget.controller.WidgetsCatalogController;
27 import org.openecomp.portalapp.widget.domain.ValidationRespond;
28 import org.openecomp.portalapp.widget.domain.WidgetCatalog;
29 import org.openecomp.portalapp.widget.service.StorageService;
30 import org.openecomp.portalapp.widget.service.WidgetCatalogService;
31 import org.springframework.http.MediaType;
32 import org.springframework.test.util.ReflectionTestUtils;
33 import org.springframework.test.web.servlet.MockMvc;
34 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
35 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
36 import org.springframework.web.multipart.MultipartFile;
37
38 @RunWith(MockitoJUnitRunner.class)
39 public class WidgetsCatalogControllerTest {
40
41         private MockMvc mockMvc;
42         
43         @Mock
44         private WidgetCatalogService widgetService;
45         
46         @Mock
47         private StorageService storageService;
48         
49         @InjectMocks
50         private WidgetsCatalogController controller;
51         
52         @Before
53         public void setUp() {
54                 MockitoAnnotations.initMocks(this);
55                 mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
56         }
57         
58         @Test
59         public void getWidgetCatalog_ValidAuthorization_NoError() throws Exception {    
60                 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
61                 WidgetCatalog widget = new WidgetCatalog();
62                 widget.setId(1);
63                 widget.setName("junit");
64                 list.add(widget);
65                 Mockito.when(widgetService.getWidgetCatalog()).thenReturn(list);
66                 
67                 String security_user = "user";
68                 String security_pass = "password";
69                 
70                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
71                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
72                 
73                 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes()));
74                 mockMvc.perform(get("/microservices/widgetCatalog/").header("Authorization", basic_auth))
75                 .andExpect(status().isOk())
76                 .andExpect(jsonPath("$[0].id", is(1)))
77                 .andExpect(jsonPath("$[0].name", is("junit")));
78         }
79           
80         @Test
81         public void getWidgetCatalog_InValidAuthorization_Unauthorized() throws Exception {     
82
83                 String security_user = "user";
84                 String security_pass = "password";
85                 String wrong_pass = "wrong";
86                 
87                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
88                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
89                 
90                 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + wrong_pass).getBytes()));
91                 mockMvc.perform(get("/microservices/widgetCatalog/").header("Authorization", basic_auth))
92                 .andExpect(status().isUnauthorized());
93         }
94         
95         @Test
96         public void getWidgetCatalog_NoAuthorization_BadRequest() throws Exception {    
97                 List<WidgetCatalog> list = new ArrayList<WidgetCatalog>();
98                 WidgetCatalog widget = new WidgetCatalog();
99                 list.add(widget);
100                 Mockito.when(widgetService.getWidgetCatalog()).thenReturn(list);
101                 
102                 mockMvc.perform(get("/microservices/widgetCatalog/"))
103                 .andExpect(status().isBadRequest());
104         }
105         
106         
107         @Test
108         public void saveWidgetCatalog_ValidAuthorization_NoError() throws Exception {   
109                 ValidationRespond respond = new ValidationRespond(true, null);
110                 Mockito.when(storageService.checkZipFile(any(MultipartFile.class))).thenReturn(respond);
111                 
112                 String security_user = "user";
113                 String security_pass = "password";
114                 
115                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
116                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
117                 
118                 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes()));
119                 mockMvc.perform(MockMvcRequestBuilders.fileUpload("/microservices/widgetCatalog/").file("file", null)
120                                 .param("widget", "{}")
121                                 .header("Authorization", basic_auth)
122                                 .contentType(MediaType.MULTIPART_FORM_DATA))
123                 .andExpect(jsonPath("$.valid", is(true)));
124                 
125                 Mockito.verify(widgetService, times(1)).saveWidgetCatalog(any(WidgetCatalog.class));
126         }
127         
128         
129         @Test
130         public void updateWidgetCatalog_ValidAuthorization_NoError() throws Exception { 
131                 String security_user = "user"; 
132                 String security_pass = "password";
133                 Long widgetId = new Long(1);
134                 ArgumentCaptor<Long> widgetServiceArg = ArgumentCaptor.forClass(Long.class);
135                 
136                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
137                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
138                 
139                 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes()));
140                 mockMvc.perform(put("/microservices/widgetCatalog/" + widgetId).contentType(MediaType.APPLICATION_JSON).content("{}").header("Authorization", basic_auth));
141                 
142                 Mockito.verify(widgetService, times(1)).updateWidgetCatalog(widgetServiceArg.capture(), any(WidgetCatalog.class));
143                 assertEquals(widgetServiceArg.getValue(), widgetId);
144         }
145         
146         
147         @Test
148         public void updateWidgetCatalogwithFiles_ValidAuthorization_NoError() throws Exception {
149                 ValidationRespond respond = new ValidationRespond(true, null);
150                 Mockito.when(storageService.checkZipFile(any(MultipartFile.class))).thenReturn(respond);
151                 
152                 String security_user = "user";
153                 String security_pass = "password";
154                 Long widgetId = new Long(1);
155                 ArgumentCaptor<Long> widgetServiceArg = ArgumentCaptor.forClass(Long.class);
156                 ArgumentCaptor<Long> storageServiceArg = ArgumentCaptor.forClass(Long.class);
157                 
158                 ReflectionTestUtils.setField(controller, "security_user", security_user, String.class);
159                 ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class);
160                 
161                 String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes()));
162                 mockMvc.perform(MockMvcRequestBuilders.fileUpload("/microservices/widgetCatalog/" + widgetId).file("file", null)
163                                 .param("widget", "{}")
164                                 .header("Authorization", basic_auth)
165                                 .contentType(MediaType.MULTIPART_FORM_DATA))
166                 .andExpect(jsonPath("$.valid", is(true)));
167                 
168                 Mockito.verify(widgetService, times(1)).updateWidgetCatalog(widgetServiceArg.capture(), any(WidgetCatalog.class));
169                 assertEquals(widgetServiceArg.getValue(), widgetId);
170         }
171         
172         @Test
173         public void deleteOnboardingWidget_ValidAuthorization_NoError() throws Exception {
174                 
175                 String security_user = "user";
176                 String security_pass = "password";
177                 Long widgetId = new Long(1);
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 + ":" + security_pass).getBytes()));
183                 mockMvc.perform(MockMvcRequestBuilders.delete("/microservices/widgetCatalog/" + widgetId)
184                                 .header("Authorization", basic_auth));
185                 ArgumentCaptor<Long> widgetServiceArg = ArgumentCaptor.forClass(Long.class);
186                 ArgumentCaptor<Long> storageServiceArg = ArgumentCaptor.forClass(Long.class);
187                 
188                 Mockito.verify(widgetService, times(1)).deleteWidgetCatalog(widgetServiceArg.capture());
189                 assertEquals(widgetServiceArg.getValue(), widgetId);
190                 Mockito.verify(storageService, times(1)).deleteWidgetFile(storageServiceArg.capture());
191                 assertEquals(storageServiceArg.getValue(), widgetId);
192         }
193         
194         
195 }