Tests coverage up and some minor bug fixes
[portal.git] / portal-BE / src / test / java / org / onap / portal / controller / WidgetsControllerTest.java
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *
10  * Unless otherwise specified, all software contained herein is licensed
11  * under the Apache License, Version 2.0 (the "License");
12  * you may not use this software except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Unless otherwise specified, all documentation contained herein is licensed
24  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25  * you may not use this documentation except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
30  * Unless required by applicable law or agreed to in writing, documentation
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * ============LICENSE_END============================================
37  *
38  *
39  */
40
41 package org.onap.portal.controller;
42
43 import static junit.framework.TestCase.assertEquals;
44 import static junit.framework.TestCase.assertNull;
45 import static org.mockito.Mockito.when;
46
47 import java.time.LocalDateTime;
48 import java.util.ArrayList;
49 import java.util.List;
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpServletResponse;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.onap.portal.dao.fn.FnLanguageDao;
55 import org.onap.portal.dao.fn.FnUserDao;
56 import org.onap.portal.domain.db.fn.FnLanguage;
57 import org.onap.portal.domain.db.fn.FnUser;
58 import org.onap.portal.domain.db.fn.FnWidget;
59 import org.onap.portal.domain.dto.transport.FieldsValidator;
60 import org.onap.portal.domain.dto.transport.OnboardingWidget;
61 import org.onap.portal.framework.MockitoTestSuite;
62 import org.onap.portal.service.WidgetService;
63 import org.springframework.beans.factory.annotation.Autowired;
64 import org.springframework.boot.test.context.SpringBootTest;
65 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
66 import org.springframework.security.core.userdetails.UsernameNotFoundException;
67 import org.springframework.test.context.TestPropertySource;
68 import org.springframework.test.context.junit4.SpringRunner;
69 import org.springframework.transaction.annotation.Transactional;
70
71 @RunWith(SpringRunner.class)
72 @SpringBootTest
73 @TestPropertySource(locations = "classpath:test.properties")
74 @Transactional
75 public class WidgetsControllerTest {
76
77        private UsernamePasswordAuthenticationToken principal = new UsernamePasswordAuthenticationToken("demo",
78                "demo123");
79
80        MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
81
82        HttpServletRequest request = mockitoTestSuite.getMockedRequest();
83        HttpServletResponse response = mockitoTestSuite.getMockedResponse();
84
85        @Autowired
86        private WidgetsController widgetsController;
87        @Autowired
88        private FnUserDao fnUserDao;
89        @Autowired
90        private FnLanguageDao fnLanguageDao;
91        @Autowired
92        private WidgetService widgetService;
93
94        private FnLanguage language = getFnLanguage();
95        private FnUser questUser = getQuestUser();
96        private FnUser notQuestUser = getNotQuestUser();
97
98        @Test(expected = UsernameNotFoundException.class)
99        public void getOnboardingWidgetsNullUserTest() {
100               UsernamePasswordAuthenticationToken nullPrincipal = new UsernamePasswordAuthenticationToken("nulluser",
101                       "demo123");
102               widgetsController.getOnboardingWidgets(nullPrincipal, request, response);
103        }
104
105        @Test
106        public void getOnboardingWidgetsQuestUserTest() {
107               UsernamePasswordAuthenticationToken questPrincipal = new UsernamePasswordAuthenticationToken("questUser",
108                       "demo123");
109               fnUserDao.save(questUser);
110               List<OnboardingWidget> onboardingWidgets = widgetsController
111                       .getOnboardingWidgets(questPrincipal, request, response);
112               assertNull(onboardingWidgets);
113
114               //Clean up
115               fnUserDao.delete(questUser);
116               fnLanguageDao.delete(language);
117        }
118
119        @Test
120        public void getOnboardingWidgetsUserTest() {
121               UsernamePasswordAuthenticationToken notQuestprincipal = new UsernamePasswordAuthenticationToken("notQuestUser",
122                       "demo123");
123               fnUserDao.save(notQuestUser);
124               List<OnboardingWidget> expected = new ArrayList<>();
125               when(request.getHeader("X-Widgets-Type")).thenReturn("managed");
126
127               List<OnboardingWidget> actual = widgetsController
128                       .getOnboardingWidgets(notQuestprincipal, request, response);
129
130               assertEquals(expected, actual);
131               fnUserDao.delete(notQuestUser);
132        }
133
134        @Test
135        public void getOnboardingWidgetsWrongHeaderTest() {
136               UsernamePasswordAuthenticationToken notQuestprincipal = new UsernamePasswordAuthenticationToken("notQuestUser",
137                       "demo123");
138               fnUserDao.save(notQuestUser);
139               when(request.getHeader("X-Widgets-Type")).thenReturn("test");
140               List<OnboardingWidget> actual = widgetsController
141                       .getOnboardingWidgets(notQuestprincipal, request, response);
142
143               assertNull(actual);
144               fnUserDao.delete(notQuestUser);
145        }
146
147        @Test
148        public void putOnboardingWidgetSameWidget() {
149               //Given
150               UsernamePasswordAuthenticationToken notQuestprincipal = new UsernamePasswordAuthenticationToken("cs0008",
151                       "demo123");
152               fnUserDao.save(notQuestUser);
153               when(request.getHeader("X-Widgets-Type")).thenReturn("managed");
154
155               OnboardingWidget onboardingWidget = OnboardingWidget.builder()
156                       .id(123L)
157                       .name("Application")
158                       .appId(1421L)
159                       .appName("Application name")
160                       .width(123)
161                       .height(45)
162                       .url("testurl")
163                       .build();
164
165
166               FnWidget fnWidget = FnWidget.builder()
167                       .name("Application")
168                       .appId(453L)
169                       .width(123)
170                       .height(45)
171                       .url("testurl")
172                       .build();
173
174               widgetService.saveOne(fnWidget);
175
176               FieldsValidator expected = new FieldsValidator();
177               //When
178               FieldsValidator actual = widgetsController.putOnboardingWidget(principal, fnWidget.getWidgetId(), onboardingWidget, response);
179               //Then
180               assertEquals(expected.getErrorCode(), actual.getErrorCode());
181               assertEquals(expected.getHttpStatusCode(), actual.getHttpStatusCode());
182               assertEquals(expected.getFields(), actual.getFields());
183        }
184
185        @Test
186        public void putOnboardingWidgetAOP() {
187               //Given
188               UsernamePasswordAuthenticationToken notQuestprincipal = new UsernamePasswordAuthenticationToken("cs0008",
189                       "demo123");
190               fnUserDao.save(notQuestUser);
191               when(request.getHeader("X-Widgets-Type")).thenReturn("managed");
192
193               OnboardingWidget onboardingWidget = OnboardingWidget.builder()
194                       .id(123L)
195                       .name("")
196                       .appId(1L)
197                       .appName("")
198                       .width(123)
199                       .height(45)
200                       .url("testurl")
201                       .build();
202
203
204               FnWidget fnWidget = FnWidget.builder()
205                       .name("Application")
206                       .appId(1421L)
207                       .width(123)
208                       .height(45)
209                       .url("testurl")
210                       .build();
211
212               widgetService.saveOne(fnWidget);
213
214               FieldsValidator expected = new FieldsValidator();
215               expected.setHttpStatusCode(406L);
216               expected.addProblematicFieldName("appName can't be blank, appId value must be higher than 1");
217               //When
218               FieldsValidator actual = widgetsController.putOnboardingWidget(principal, fnWidget.getWidgetId(), onboardingWidget, response);
219               //Then
220               assertEquals(expected.getHttpStatusCode(), actual.getHttpStatusCode());
221               assertEquals(expected.getFields().size(), actual.getFields().size());
222        }
223
224        @Test
225        public void putOnboardingWidgetAOPXSSTest() {
226               //Given
227               UsernamePasswordAuthenticationToken notQuestprincipal = new UsernamePasswordAuthenticationToken("cs0008",
228                       "demo123");
229               fnUserDao.save(notQuestUser);
230               when(request.getHeader("X-Widgets-Type")).thenReturn("managed");
231
232               OnboardingWidget onboardingWidget = OnboardingWidget.builder()
233                       .id(123L)
234                       .name("<script>alert(“XSS”);</script>\n")
235                       .appId(34L)
236                       .appName("<ScRipT>alert(\"XSS\");</ScRipT>")
237                       .width(123)
238                       .height(45)
239                       .url("testurl")
240                       .build();
241
242               FieldsValidator expected = new FieldsValidator();
243               expected.setHttpStatusCode(406L);
244               expected.addProblematicFieldName("appName may have unsafe html content, name may have unsafe html content");
245               //When
246               FieldsValidator actual = widgetsController.putOnboardingWidget(principal, 15L, onboardingWidget, response);
247               //Then
248               assertEquals(expected.getHttpStatusCode(), actual.getHttpStatusCode());
249               assertEquals(expected.getFields().size(), actual.getFields().size());
250        }
251
252        @Test
253        public void postOnboardingWidget() {
254        }
255
256        @Test
257        public void deleteOnboardingWidget() {
258        }
259
260        @Test
261        public void putWidgetCatalogSelection() {
262        }
263
264        private FnUser getQuestUser(){
265               return FnUser.builder()
266                       .loginId("questUser")
267                       .loginPwd("demo123")
268                       .lastLoginDate(LocalDateTime.now())
269                       .activeYn(true)
270                       .createdDate(LocalDateTime.now())
271                       .modifiedDate(LocalDateTime.now())
272                       .isInternalYn(true)
273                       .languageId(language)
274                       .guest(true)
275                       .build();
276        }
277
278        private FnUser getNotQuestUser(){
279               return FnUser.builder()
280                       .loginId("notQuestUser")
281                       .loginPwd("demo123")
282                       .lastLoginDate(LocalDateTime.now())
283                       .activeYn(true)
284                       .createdDate(LocalDateTime.now())
285                       .modifiedDate(LocalDateTime.now())
286                       .isInternalYn(true)
287                       .languageId(language)
288                       .guest(false)
289                       .build();
290        }
291
292        private FnLanguage getFnLanguage(){
293               return FnLanguage.builder().languageName("Polish").languageAlias("Pl").build();
294        }
295 }