UserRolesController methods up
[portal.git] / portal-BE / src / test / java / org / onap / portal / controller / WidgetsCatalogControllerTest.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 org.junit.jupiter.api.Assertions.assertEquals;
44 import static org.junit.jupiter.api.Assertions.assertNotNull;
45 import static org.junit.jupiter.api.Assertions.assertNull;
46 import static org.junit.jupiter.api.Assertions.assertTrue;
47
48 import java.time.LocalDateTime;
49 import java.util.Collections;
50 import java.util.HashSet;
51 import java.util.List;
52 import javax.servlet.http.HttpServletRequest;
53 import javax.transaction.Transactional;
54 import org.junit.Test;
55 import org.junit.runner.RunWith;
56 import org.onap.portal.domain.db.ep.EpMicroserviceParameter;
57 import org.onap.portal.domain.db.ep.EpWidgetCatalog;
58 import org.onap.portal.domain.db.ep.EpWidgetCatalogParameter;
59 import org.onap.portal.domain.db.fn.FnLanguage;
60 import org.onap.portal.domain.db.fn.FnUser;
61 import org.onap.portal.domain.dto.ecomp.WidgetCatalog;
62 import org.onap.portal.framework.MockitoTestSuite;
63 import org.onap.portal.service.ep.EpMicroserviceParameterService;
64 import org.onap.portal.service.ep.EpWidgetCatalogParameterService;
65 import org.onap.portal.service.ep.EpWidgetCatalogService;
66 import org.onap.portal.service.fn.FnLanguageService;
67 import org.springframework.beans.factory.annotation.Autowired;
68 import org.springframework.boot.test.context.SpringBootTest;
69 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
70 import org.springframework.test.context.TestPropertySource;
71 import org.springframework.test.context.junit4.SpringRunner;
72
73 @RunWith(SpringRunner.class)
74 @SpringBootTest
75 @Transactional
76 @TestPropertySource(locations = "classpath:test.properties")
77 public class WidgetsCatalogControllerTest {
78        private UsernamePasswordAuthenticationToken principal = new UsernamePasswordAuthenticationToken("demo",
79                "demo123");
80        @Autowired
81        private WidgetsCatalogController widgetsCatalogController;
82        @Autowired
83        private FnLanguageService fnLanguageService;
84        @Autowired
85        private EpWidgetCatalogParameterService epWidgetCatalogParameterService;
86        @Autowired
87        private EpMicroserviceParameterService epMicroserviceParameterService;
88        @Autowired
89        private EpWidgetCatalogService epWidgetCatalogService;
90
91        @Test
92        public void getUserWidgetCatalog() {
93               List<WidgetCatalog> actual = widgetsCatalogController.getUserWidgetCatalog("demo");
94               assertNull(actual);
95        }
96
97        @Test
98        public void getWidgetCatalog() {
99        }
100
101        @Test
102        public void updateWidgetCatalog() {
103        }
104
105        @Test
106        public void deleteOnboardingWidget() {
107        }
108
109        @Test
110        public void updateWidgetCatalogWithFiles() {
111        }
112
113        @Test
114        public void createWidgetCatalog() {
115        }
116
117        @Test
118        public void getWidgetFramework() {
119        }
120
121        @Test
122        public void getWidgetController() {
123        }
124
125        @Test
126        public void getWidgetCSS() {
127        }
128
129        @Test
130        public void getWidgetParameterResult() {
131        }
132
133        @Test
134        public void getUserParameterById() {
135               //Given
136               EpWidgetCatalog widget = EpWidgetCatalog.builder()
137                       .wdgName("Name")
138                       .wdgFileLoc("loc")
139                       .allUserFlag(true)
140                       .build();
141               epWidgetCatalogService.save(widget);
142               EpMicroserviceParameter parameter = new EpMicroserviceParameter();
143               epMicroserviceParameterService.save(parameter);
144               FnLanguage language = FnLanguage.builder().languageAlias("TS").languageName("TEST").build();
145               fnLanguageService.save(principal, language);
146               FnUser user = buildFnUser();
147               language.setFnUsers(new HashSet<>(Collections.singleton(user)));
148               user.setLanguageId(language);
149               EpWidgetCatalogParameter data =  EpWidgetCatalogParameter.builder()
150                       .widgetId(widget).userId(user).paramId(parameter).userValue("TestData").build();
151               //When
152               epWidgetCatalogParameterService.saveUserParameter(data);
153               List<EpWidgetCatalogParameter> actual = widgetsCatalogController.getUserParameterById(parameter.getId());
154               //Then
155               assertEquals(1, actual.size());
156               //Clean
157        }
158
159        @Test
160        public void deleteUserParameterById() {
161               //Given
162               EpWidgetCatalog widget = EpWidgetCatalog.builder()
163                       .wdgName("Name")
164                       .wdgFileLoc("loc")
165                       .allUserFlag(true)
166                       .build();
167               epWidgetCatalogService.save(widget);
168               EpMicroserviceParameter parameter = new EpMicroserviceParameter();
169               epMicroserviceParameterService.save(parameter);
170               FnLanguage language = FnLanguage.builder().languageAlias("TS").languageName("TEST").build();
171               fnLanguageService.save(principal, language);
172               FnUser user = buildFnUser();
173               language.setFnUsers(new HashSet<>(Collections.singleton(user)));
174               user.setLanguageId(language);
175               EpWidgetCatalogParameter data =  EpWidgetCatalogParameter.builder()
176                       .widgetId(widget).userId(user).paramId(parameter).userValue("TestData").build();
177               //When
178               assertEquals(0, widgetsCatalogController.getUserParameterById(parameter.getId()).size());
179               epWidgetCatalogParameterService.saveUserParameter(data);
180               //Then assert
181               assertEquals(1, widgetsCatalogController.getUserParameterById(parameter.getId()).size());
182               assertTrue(widgetsCatalogController.deleteUserParameterById(parameter.getId()));
183               assertEquals(0, widgetsCatalogController.getUserParameterById(parameter.getId()).size());
184
185        }
186
187        @Test
188        public void doDownload() {
189        }
190
191        @Test
192        public void saveWidgetParameter() {
193               //Given
194               EpWidgetCatalog widget = EpWidgetCatalog.builder()
195                       .wdgName("Name")
196                       .wdgFileLoc("loc")
197                       .allUserFlag(true)
198                       .build();
199               epWidgetCatalogService.save(widget);
200               EpMicroserviceParameter parameter = new EpMicroserviceParameter();
201               epMicroserviceParameterService.save(parameter);
202               FnLanguage language = FnLanguage.builder().languageAlias("TS").languageName("TEST").build();
203               fnLanguageService.save(principal, language);
204               FnUser user = buildFnUser();
205               language.setFnUsers(new HashSet<>(Collections.singleton(user)));
206               user.setLanguageId(language);
207               EpWidgetCatalogParameter data =  EpWidgetCatalogParameter.builder()
208                       .widgetId(widget).userId(user).paramId(parameter).userValue("TestData").build();
209
210               //When
211               widgetsCatalogController.saveWidgetParameter(principal, data);
212               //Then
213               EpWidgetCatalogParameter actual = epWidgetCatalogParameterService.getById(data.getId());
214
215               assertEquals("TestData", actual.getUserValue());
216
217        }
218
219        @Test
220        public void saveWidgetParameterOldParamTest() {
221               //Given
222               EpWidgetCatalog widget = EpWidgetCatalog.builder()
223                       .wdgName("Name")
224                       .wdgFileLoc("loc")
225                       .allUserFlag(true)
226                       .build();
227               epWidgetCatalogService.save(widget);
228               EpMicroserviceParameter parameter = new EpMicroserviceParameter();
229               epMicroserviceParameterService.save(parameter);
230               FnLanguage language = FnLanguage.builder().languageAlias("TS").languageName("TEST").build();
231               fnLanguageService.save(principal, language);
232               FnUser user = buildFnUser();
233               language.setFnUsers(new HashSet<>(Collections.singleton(user)));
234               user.setLanguageId(language);
235               EpWidgetCatalogParameter old =  EpWidgetCatalogParameter.builder()
236                       .widgetId(widget).userId(user).paramId(parameter).userValue("TestData").build();
237
238               //When
239               widgetsCatalogController.saveWidgetParameter(principal, old);
240
241               EpWidgetCatalogParameter newWidgetParameter =  EpWidgetCatalogParameter.builder()
242                       .widgetId(widget).userId(user).paramId(parameter).userValue("TestData2").build();
243
244               widgetsCatalogController.saveWidgetParameter(principal, newWidgetParameter);
245
246               EpWidgetCatalogParameter oldOne = epWidgetCatalogParameterService.getById(old.getId());
247
248               //Then
249               assertEquals("TestData2", oldOne.getUserValue());
250
251        }
252
253        @Test
254        public void getUploadFlag() {
255               String expected = "";
256               String actual = widgetsCatalogController.getUploadFlag();
257
258               assertEquals(expected, actual);
259        }
260
261        private FnUser buildFnUser(){
262               return FnUser.builder()
263                       .lastLoginDate(LocalDateTime.now())
264                       .activeYn(true)
265                       .modifiedDate(LocalDateTime.now())
266                       .createdDate(LocalDateTime.now())
267                       .isInternalYn(true)
268                       .isSystemUser(true)
269                       .guest(false)
270                       .build();
271        }
272 }