Fixed health check issue
[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.domain.db.ep.EpMicroserviceParameter;
55 import org.onap.portal.domain.db.ep.EpWidgetCatalog;
56 import org.onap.portal.domain.db.ep.EpWidgetCatalogParameter;
57 import org.onap.portal.domain.db.fn.FnLanguage;
58 import org.onap.portal.domain.db.fn.FnUser;
59 import org.onap.portal.domain.dto.ecomp.WidgetCatalog;
60 import org.onap.portal.service.microserviceParameter.EpMicroserviceParameterService;
61 import org.onap.portal.service.widgetCatalogParameter.EpWidgetCatalogParameterService;
62 import org.onap.portal.service.widgetCatalog.EpWidgetCatalogService;
63 import org.onap.portal.service.language.FnLanguageService;
64 import org.onap.portal.service.user.FnUserService;
65 import org.springframework.beans.factory.annotation.Autowired;
66 import org.springframework.boot.test.context.SpringBootTest;
67 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
68 import org.springframework.test.context.TestPropertySource;
69 import org.springframework.test.context.junit4.SpringRunner;
70
71 @RunWith(SpringRunner.class)
72 @SpringBootTest
73 @Transactional
74 @TestPropertySource(locations = "classpath:test.properties")
75 public class WidgetsCatalogControllerTest {
76        private final UsernamePasswordAuthenticationToken principal = new UsernamePasswordAuthenticationToken("demo",
77                "demo123");
78        @Autowired
79        private WidgetsCatalogController widgetsCatalogController;
80        @Autowired
81        private FnUserService fnUserService;
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(language);
146               FnUser user = buildFnUser();
147               language.setFnUsers(new HashSet<>(Collections.singleton(user)));
148               user.setLanguageId(language);
149               fnUserService.saveFnUser(user);
150               EpWidgetCatalogParameter data =  EpWidgetCatalogParameter.builder()
151                       .widgetId(widget).userId(user).paramId(parameter).userValue("TestData").build();
152               //When
153               epWidgetCatalogParameterService.saveUserParameter(data);
154               List<EpWidgetCatalogParameter> actual = widgetsCatalogController.getUserParameterById(parameter.getId());
155               //Then
156               assertEquals(1, actual.size());
157               //Clean
158        }
159
160        @Test
161        public void deleteUserParameterById() {
162               //Given
163               EpWidgetCatalog widget = EpWidgetCatalog.builder()
164                       .wdgName("Name")
165                       .wdgFileLoc("loc")
166                       .allUserFlag(true)
167                       .build();
168               epWidgetCatalogService.save(widget);
169               EpMicroserviceParameter parameter = new EpMicroserviceParameter();
170               epMicroserviceParameterService.save(parameter);
171               FnUser user = buildFnUser();
172               FnLanguage language = fnLanguageService.getByLanguageAlias("EN");
173               user.setLanguageId(language);
174               fnUserService.saveFnUser(user);
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(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(language);
232               FnUser user = buildFnUser();
233               language.setFnUsers(new HashSet<>(Collections.singleton(user)));
234               user.setLanguageId(language);
235               fnUserService.saveFnUser(user);
236
237               EpWidgetCatalogParameter old =  EpWidgetCatalogParameter.builder()
238                       .widgetId(widget).userId(user).paramId(parameter).userValue("TestData").build();
239
240               //When
241               widgetsCatalogController.saveWidgetParameter(principal, old);
242
243               EpWidgetCatalogParameter newWidgetParameter =  EpWidgetCatalogParameter.builder()
244                       .widgetId(widget).userId(user).paramId(parameter).userValue("TestData2").build();
245
246               widgetsCatalogController.saveWidgetParameter(principal, newWidgetParameter);
247
248               EpWidgetCatalogParameter oldOne = epWidgetCatalogParameterService.getById(old.getId());
249
250               //Then
251               assertEquals("TestData2", oldOne.getUserValue());
252
253        }
254
255        @Test
256        public void getUploadFlag() {
257               String expected = "true";
258               String actual = widgetsCatalogController.getUploadFlag();
259
260               assertEquals(expected, actual);
261        }
262
263        private FnUser buildFnUser(){
264               return FnUser.builder()
265                       .lastLoginDate(LocalDateTime.now())
266                       .activeYn(true)
267                       .modifiedDate(LocalDateTime.now())
268                       .createdDate(LocalDateTime.now())
269                       .isInternalYn(true)
270                       .isSystemUser(true)
271                       .guest(false)
272                       .build();
273        }
274 }