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