2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.controller;
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNull;
43 import java.util.ArrayList;
44 import java.util.List;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 import org.mockito.InjectMocks;
53 import org.mockito.Matchers;
54 import org.mockito.Mock;
55 import org.mockito.Mockito;
56 import org.mockito.MockitoAnnotations;
57 import org.onap.portalapp.portal.controller.WidgetsCatalogController;
58 import org.onap.portalapp.portal.core.MockEPUser;
59 import org.onap.portalapp.portal.domain.EPUser;
60 import org.onap.portalapp.portal.domain.MicroserviceParameter;
61 import org.onap.portalapp.portal.domain.WidgetCatalog;
62 import org.onap.portalapp.portal.domain.WidgetCatalogParameter;
63 import org.onap.portalapp.portal.domain.WidgetParameterResult;
64 import org.onap.portalapp.portal.domain.WidgetServiceHeaders;
65 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
66 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
67 import org.onap.portalapp.portal.framework.MockitoTestSuite;
68 import org.onap.portalapp.portal.service.WidgetMService;
69 import org.onap.portalapp.portal.service.WidgetMServiceImpl;
70 import org.onap.portalapp.portal.service.MicroserviceService;
71 import org.onap.portalapp.portal.service.MicroserviceServiceImpl;
72 import org.onap.portalapp.portal.service.WidgetParameterService;
73 import org.onap.portalapp.portal.service.WidgetParameterServiceImpl;
74 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
75 import org.onap.portalapp.portal.utils.EcompPortalUtils;
76 import org.onap.portalapp.util.EPUserUtils;
77 import org.onap.portalsdk.core.onboarding.exception.CipherUtilException;
78 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
79 import org.onap.portalsdk.core.util.SystemProperties;
80 import org.powermock.api.mockito.PowerMockito;
81 import org.powermock.core.classloader.annotations.PrepareForTest;
82 import org.powermock.modules.junit4.PowerMockRunner;
83 import org.springframework.http.HttpEntity;
84 import org.springframework.http.HttpMethod;
85 import org.springframework.http.HttpStatus;
86 import org.springframework.http.ResponseEntity;
87 import org.springframework.mock.web.MockHttpServletRequest;
88 import org.springframework.mock.web.MockHttpServletResponse;
89 import org.springframework.mock.web.MockMultipartFile;
90 import org.springframework.mock.web.MockMultipartHttpServletRequest;
91 import org.springframework.web.client.RestClientException;
92 import org.springframework.web.client.RestTemplate;
94 @SuppressWarnings("rawtypes")
95 @RunWith(PowerMockRunner.class)
96 @PrepareForTest({ EPUserUtils.class, CipherUtil.class, EcompPortalUtils.class, SystemProperties.class,
97 EPCommonSystemProperties.class, EPUserUtils.class })
98 public class WidgetsCatalogControllerTest {
101 WidgetMService widgetMService = new WidgetMServiceImpl();
104 MicroserviceService microserviceService = new MicroserviceServiceImpl();
107 WidgetParameterService widgetParameterService = new WidgetParameterServiceImpl();
110 WidgetsCatalogController widgetsCatalogController = new WidgetsCatalogController();
113 public void setup() {
114 MockitoAnnotations.initMocks(this);
118 WidgetServiceHeaders widgetServiceHeaders;
121 RestTemplate template = new RestTemplate();
123 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
125 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
126 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
128 NullPointerException nullPointerException = new NullPointerException();
130 MockEPUser mockUser = new MockEPUser();
133 public void getUserWidgetCatalogTest() throws RestClientException, Exception {
134 PowerMockito.mockStatic(EcompPortalUtils.class);
135 PowerMockito.mockStatic(SystemProperties.class);
136 PowerMockito.mockStatic(EPCommonSystemProperties.class);
137 PowerMockito.mockStatic(CipherUtil.class);
138 Mockito.when(EcompPortalUtils.widgetMsProtocol()).thenReturn("test1");
139 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WIDGET_MS_PROTOCOL)).thenReturn("https");
140 Mockito.when(SystemProperties.getProperty("microservices.widget.local.port")).thenReturn("test");
141 Mockito.when(EcompPortalUtils.getPropertyOrDefault("microservices.widget.username", "widget_user"))
143 Mockito.when(CipherUtil
144 .decryptPKC(EcompPortalUtils.getPropertyOrDefault("microservices.widget.password", "widget_password")))
146 List<WidgetCatalog> widgetsList = new ArrayList<>();
147 WidgetCatalog widgetCatalog = new WidgetCatalog();
148 widgetCatalog.setId(1l);
149 widgetCatalog.setName("test");
150 widgetsList.add(widgetCatalog);
151 ResponseEntity<List> ans = new ResponseEntity<>(widgetsList, HttpStatus.OK);
152 Mockito.when(widgetMService.getServiceLocation("widgets-service", "test")).thenReturn("test.com");
153 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), Matchers.<HttpEntity<?>>any(),
154 Matchers.eq(List.class))).thenReturn(ans);
155 List<WidgetCatalog> expectedWidgets = widgetsCatalogController.getUserWidgetCatalog("guestT");
156 assertEquals(expectedWidgets, widgetsList);
160 public void getUserWidgetCatalogExceptionTest() throws RestClientException, Exception {
161 PowerMockito.mockStatic(EcompPortalUtils.class);
162 PowerMockito.mockStatic(SystemProperties.class);
163 PowerMockito.mockStatic(EPCommonSystemProperties.class);
164 PowerMockito.mockStatic(CipherUtil.class);
165 Mockito.when(EcompPortalUtils.widgetMsProtocol()).thenReturn("test1");
166 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WIDGET_MS_PROTOCOL)).thenReturn("https");
167 Mockito.when(SystemProperties.getProperty("microservices.widget.local.port")).thenReturn("test");
168 Mockito.when(EcompPortalUtils.getPropertyOrDefault("microservices.widget.username", "widget_user"))
170 Mockito.when(CipherUtil
171 .decryptPKC(EcompPortalUtils.getPropertyOrDefault("microservices.widget.password", "widget_password")))
173 Mockito.when(widgetMService.getServiceLocation("widgets-service", "test")).thenReturn("test.com");
174 Mockito.doThrow(new NullPointerException()).when(template).exchange(Matchers.anyString(),
175 Matchers.eq(HttpMethod.GET), Matchers.<HttpEntity<?>>any(), Matchers.eq(List.class));
176 List<WidgetCatalog> expectedWidgets = widgetsCatalogController.getUserWidgetCatalog("guestT");
177 assertNull(expectedWidgets);
181 public void getWidgetCatalogTest() throws CipherUtilException {
182 PowerMockito.mockStatic(EcompPortalUtils.class);
183 PowerMockito.mockStatic(SystemProperties.class);
184 PowerMockito.mockStatic(EPCommonSystemProperties.class);
185 PowerMockito.mockStatic(CipherUtil.class);
186 Mockito.when(EcompPortalUtils.widgetMsProtocol()).thenReturn("test1");
187 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WIDGET_MS_PROTOCOL)).thenReturn("https");
188 Mockito.when(SystemProperties.getProperty("microservices.widget.local.port")).thenReturn("test");
189 Mockito.when(EcompPortalUtils.getPropertyOrDefault("microservices.widget.username", "widget_user"))
191 Mockito.when(CipherUtil
192 .decryptPKC(EcompPortalUtils.getPropertyOrDefault("microservices.widget.password", "widget_password")))
194 List<WidgetCatalog> widgetsList = new ArrayList<>();
195 WidgetCatalog widgetCatalog = new WidgetCatalog();
196 widgetCatalog.setId(1l);
197 widgetCatalog.setName("test");
198 widgetsList.add(widgetCatalog);
199 ResponseEntity<List> ans = new ResponseEntity<>(widgetsList, HttpStatus.OK);
200 Mockito.when(widgetMService.getServiceLocation("widgets-service", "test")).thenReturn("test.com");
201 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), Matchers.<HttpEntity<?>>any(),
202 Matchers.eq(List.class))).thenReturn(ans);
203 List<WidgetCatalog> expectedWidgets = widgetsCatalogController.getWidgetCatalog();
204 assertEquals(expectedWidgets, widgetsList);
208 public void getWidgetCatalogExceptionTest() throws Exception {
209 PowerMockito.mockStatic(EcompPortalUtils.class);
210 PowerMockito.mockStatic(SystemProperties.class);
211 PowerMockito.mockStatic(EPCommonSystemProperties.class);
212 PowerMockito.mockStatic(CipherUtil.class);
213 Mockito.when(EcompPortalUtils.widgetMsProtocol()).thenReturn("test1");
214 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WIDGET_MS_PROTOCOL)).thenReturn("https");
215 Mockito.when(SystemProperties.getProperty("microservices.widget.local.port")).thenReturn("test");
216 Mockito.when(EcompPortalUtils.getPropertyOrDefault("microservices.widget.username", "widget_user"))
218 Mockito.when(CipherUtil
219 .decryptPKC(EcompPortalUtils.getPropertyOrDefault("microservices.widget.password", "widget_password")))
221 Mockito.when(widgetMService.getServiceLocation("widgets-service", "test")).thenReturn("test.com");
222 Mockito.doThrow(new NullPointerException()).when(template).exchange(Matchers.anyString(),
223 Matchers.eq(HttpMethod.GET), Matchers.<HttpEntity<?>>any(), Matchers.eq(List.class));
224 List<WidgetCatalog> expectedWidgets = widgetsCatalogController.getUserWidgetCatalog("guestT");
225 assertNull(expectedWidgets);
229 public void updateWidgetCatalogTest() throws Exception {
230 PowerMockito.mockStatic(EcompPortalUtils.class);
231 PowerMockito.mockStatic(SystemProperties.class);
232 PowerMockito.mockStatic(EPCommonSystemProperties.class);
233 PowerMockito.mockStatic(CipherUtil.class);
234 Mockito.when(EcompPortalUtils.widgetMsProtocol()).thenReturn("test1");
235 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WIDGET_MS_PROTOCOL)).thenReturn("https");
236 Mockito.when(SystemProperties.getProperty("microservices.widget.local.port")).thenReturn("test");
237 Mockito.when(EcompPortalUtils.getPropertyOrDefault("microservices.widget.username", "widget_user"))
239 Mockito.when(CipherUtil
240 .decryptPKC(EcompPortalUtils.getPropertyOrDefault("microservices.widget.password", "widget_password")))
242 Mockito.when(widgetMService.getServiceLocation("widgets-service", "test")).thenReturn("test.com");
243 ResponseEntity<List> ans = new ResponseEntity<>(HttpStatus.OK);
244 Mockito.when(widgetMService.getServiceLocation("widgets-service", "test")).thenReturn("test.com");
245 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.PUT), Matchers.<HttpEntity<?>>any(),
246 Matchers.eq(List.class))).thenReturn(ans);
247 WidgetCatalog widget = new WidgetCatalog();
249 widgetsCatalogController.updateWidgetCatalog(widget, 1);
253 public void deleteOnboardingWidgetTest() throws Exception {
254 PowerMockito.mockStatic(EcompPortalUtils.class);
255 PowerMockito.mockStatic(SystemProperties.class);
256 PowerMockito.mockStatic(EPCommonSystemProperties.class);
257 PowerMockito.mockStatic(CipherUtil.class);
258 Mockito.when(EcompPortalUtils.widgetMsProtocol()).thenReturn("test1");
259 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WIDGET_MS_PROTOCOL)).thenReturn("https");
260 Mockito.when(SystemProperties.getProperty("microservices.widget.local.port")).thenReturn("test");
261 Mockito.when(EcompPortalUtils.getPropertyOrDefault("microservices.widget.username", "widget_user"))
263 Mockito.when(CipherUtil
264 .decryptPKC(EcompPortalUtils.getPropertyOrDefault("microservices.widget.password", "widget_password")))
266 Mockito.when(widgetMService.getServiceLocation("widgets-service", "test")).thenReturn("test.com");
267 ResponseEntity<List> ans = new ResponseEntity<>(HttpStatus.OK);
268 Mockito.when(widgetMService.getServiceLocation("widgets-service", "test")).thenReturn("test.com");
269 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
270 Matchers.<HttpEntity<?>>any(), Matchers.eq(List.class))).thenReturn(ans);
271 widgetsCatalogController.deleteOnboardingWidget(1l);
274 @SuppressWarnings("unchecked")
276 public void updateWidgetCatalogWithFilesTest() throws Exception {
277 PowerMockito.mockStatic(EcompPortalUtils.class);
278 PowerMockito.mockStatic(SystemProperties.class);
279 PowerMockito.mockStatic(EPCommonSystemProperties.class);
280 PowerMockito.mockStatic(CipherUtil.class);
281 Mockito.when(EcompPortalUtils.widgetMsProtocol()).thenReturn("test1");
282 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WIDGET_MS_PROTOCOL)).thenReturn("https");
283 Mockito.when(SystemProperties.getProperty("microservices.widget.local.port")).thenReturn("test");
284 Mockito.when(EcompPortalUtils.getPropertyOrDefault("microservices.widget.username", "widget_user"))
286 Mockito.when(CipherUtil
287 .decryptPKC(EcompPortalUtils.getPropertyOrDefault("microservices.widget.password", "widget_password")))
289 Mockito.when(widgetMService.getServiceLocation("widgets-service", "test")).thenReturn("test.com");
290 String ans = "success";
291 Mockito.when(widgetMService.getServiceLocation("widgets-service", "test")).thenReturn("test.com");
292 Mockito.when(template.postForObject(Mockito.any(String.class), Mockito.any(HttpEntity.class),
293 Mockito.any(Class.class))).thenReturn(ans);
294 MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
295 byte[] mockData = "test".getBytes();
296 String originalFilename = "Test_File.zip";
297 MockMultipartFile mockMultipartFile = new MockMultipartFile("file", originalFilename, "application/zip",
299 request.addFile(mockMultipartFile);
300 String actual = widgetsCatalogController.updateWidgetCatalogWithFiles(request, 1l);
301 assertEquals(ans, actual);
304 @SuppressWarnings("unchecked")
306 public void createWidgetCatalogTest() throws Exception {
307 PowerMockito.mockStatic(EcompPortalUtils.class);
308 PowerMockito.mockStatic(SystemProperties.class);
309 PowerMockito.mockStatic(EPCommonSystemProperties.class);
310 PowerMockito.mockStatic(CipherUtil.class);
311 Mockito.when(EcompPortalUtils.widgetMsProtocol()).thenReturn("test1");
312 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WIDGET_MS_PROTOCOL)).thenReturn("https");
313 Mockito.when(SystemProperties.getProperty("microservices.widget.local.port")).thenReturn("test");
314 Mockito.when(EcompPortalUtils.getPropertyOrDefault("microservices.widget.username", "widget_user"))
316 Mockito.when(CipherUtil
317 .decryptPKC(EcompPortalUtils.getPropertyOrDefault("microservices.widget.password", "widget_password")))
319 Mockito.when(widgetMService.getServiceLocation("widgets-service", "test")).thenReturn("test.com");
320 String ans = "success";
321 Mockito.when(widgetMService.getServiceLocation("widgets-service", "test")).thenReturn("test.com");
322 Mockito.when(template.postForObject(Mockito.any(String.class), Mockito.any(HttpEntity.class),
323 Mockito.any(Class.class))).thenReturn(ans);
324 MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
325 byte[] mockData = "test".getBytes();
326 String originalFilename = "Test_File.zip";
327 MockMultipartFile mockMultipartFile = new MockMultipartFile("file", originalFilename, "application/zip",
329 request.addFile(mockMultipartFile);
330 String actual = widgetsCatalogController.createWidgetCatalog(request);
331 assertEquals(ans, actual);
334 @SuppressWarnings("unchecked")
336 public void getWidgetFrameworkTest() throws Exception {
337 PowerMockito.mockStatic(EcompPortalUtils.class);
338 PowerMockito.mockStatic(SystemProperties.class);
339 PowerMockito.mockStatic(EPCommonSystemProperties.class);
340 PowerMockito.mockStatic(CipherUtil.class);
341 Mockito.when(EcompPortalUtils.widgetMsProtocol()).thenReturn("test1");
342 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WIDGET_MS_PROTOCOL)).thenReturn("https");
343 Mockito.when(SystemProperties.getProperty("microservices.widget.local.port")).thenReturn("test");
344 Mockito.when(EcompPortalUtils.getPropertyOrDefault("microservices.widget.username", "widget_user"))
346 Mockito.when(CipherUtil
347 .decryptPKC(EcompPortalUtils.getPropertyOrDefault("microservices.widget.password", "widget_password")))
349 Mockito.when(widgetMService.getServiceLocation("widgets-service", "test")).thenReturn("test.com");
350 Mockito.when(template.getForObject(Mockito.anyString(), Mockito.any(Class.class), Mockito.any(String.class)))
351 .thenReturn("test123");
352 String result = widgetsCatalogController.getWidgetFramework(1l);
356 @SuppressWarnings("unchecked")
358 public void getWidgetControllerTest() throws Exception {
359 PowerMockito.mockStatic(EcompPortalUtils.class);
360 PowerMockito.mockStatic(SystemProperties.class);
361 PowerMockito.mockStatic(EPCommonSystemProperties.class);
362 PowerMockito.mockStatic(CipherUtil.class);
363 Mockito.when(EcompPortalUtils.widgetMsProtocol()).thenReturn("test1");
364 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WIDGET_MS_PROTOCOL)).thenReturn("https");
365 Mockito.when(SystemProperties.getProperty("microservices.widget.local.port")).thenReturn("test");
366 Mockito.when(EcompPortalUtils.getPropertyOrDefault("microservices.widget.username", "widget_user"))
368 Mockito.when(CipherUtil
369 .decryptPKC(EcompPortalUtils.getPropertyOrDefault("microservices.widget.password", "widget_password")))
371 Mockito.when(widgetMService.getServiceLocation("widgets-service", "test")).thenReturn("test.com");
372 Mockito.when(template.getForObject(Mockito.anyString(), Mockito.any(Class.class), Mockito.any(String.class)))
373 .thenReturn("test123");
374 String result = widgetsCatalogController.getWidgetController(1);
378 @SuppressWarnings("unchecked")
380 public void getWidgetCSSTest() throws Exception {
381 PowerMockito.mockStatic(EcompPortalUtils.class);
382 PowerMockito.mockStatic(SystemProperties.class);
383 PowerMockito.mockStatic(EPCommonSystemProperties.class);
384 PowerMockito.mockStatic(CipherUtil.class);
385 Mockito.when(EcompPortalUtils.widgetMsProtocol()).thenReturn("test1");
386 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WIDGET_MS_PROTOCOL)).thenReturn("https");
387 Mockito.when(SystemProperties.getProperty("microservices.widget.local.port")).thenReturn("test");
388 Mockito.when(EcompPortalUtils.getPropertyOrDefault("microservices.widget.username", "widget_user"))
390 Mockito.when(CipherUtil
391 .decryptPKC(EcompPortalUtils.getPropertyOrDefault("microservices.widget.password", "widget_password")))
393 Mockito.when(widgetMService.getServiceLocation("widgets-service", "test")).thenReturn("test.com");
394 Mockito.when(template.getForObject(Mockito.anyString(), Mockito.any(Class.class), Mockito.any(String.class)))
395 .thenReturn("test123");
396 String result = widgetsCatalogController.getWidgetCSS(1);
401 public void getWidgetParameterResultTest() throws Exception {
402 PowerMockito.mockStatic(EcompPortalUtils.class);
403 PowerMockito.mockStatic(SystemProperties.class);
404 PowerMockito.mockStatic(EPCommonSystemProperties.class);
405 PowerMockito.mockStatic(CipherUtil.class);
406 PowerMockito.mockStatic(EPUserUtils.class);
407 EPUser user = mockUser.mockEPUser();
408 MockHttpServletRequest request = new MockHttpServletRequest();
409 Mockito.when(EPUserUtils.getUserSession(request)).thenReturn(user);
410 Mockito.when(EcompPortalUtils.widgetMsProtocol()).thenReturn("test1");
411 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WIDGET_MS_PROTOCOL)).thenReturn("https");
412 Mockito.when(SystemProperties.getProperty("microservices.widget.local.port")).thenReturn("test");
413 Mockito.when(EcompPortalUtils.getPropertyOrDefault("microservices.widget.username", "widget_user"))
415 Mockito.when(CipherUtil
416 .decryptPKC(EcompPortalUtils.getPropertyOrDefault("microservices.widget.password", "widget_password")))
418 Mockito.when(widgetMService.getServiceLocation("widgets-service", "test")).thenReturn("test.com");
419 ResponseEntity<Long> ans = new ResponseEntity<>(1l, HttpStatus.OK);
420 Mockito.when(widgetMService.getServiceLocation("widgets-service", "test")).thenReturn("test.com");
421 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), Matchers.<HttpEntity<?>>any(),
422 Matchers.eq(Long.class))).thenReturn(ans);
423 List<MicroserviceParameter> defaultParam = new ArrayList<>();
424 MicroserviceParameter microserviceParameter = new MicroserviceParameter();
425 microserviceParameter.setId(1l);
426 microserviceParameter.setPara_key("test");
427 MicroserviceParameter microserviceParameter2 = new MicroserviceParameter();
428 microserviceParameter2.setId(2l);
429 microserviceParameter2.setPara_key("test2");
430 defaultParam.add(microserviceParameter);
431 defaultParam.add(microserviceParameter2);
432 Mockito.when(microserviceService.getParametersById(1)).thenReturn(defaultParam);
433 Mockito.when(widgetParameterService.getUserParamById(1l, user.getId(), 1l)).thenReturn(null);
434 WidgetCatalogParameter userValue = new WidgetCatalogParameter();
435 userValue.setUser_value("test123");
436 Mockito.when(widgetParameterService.getUserParamById(1l, user.getId(), 2l)).thenReturn(userValue);
437 PortalRestResponse<List<WidgetParameterResult>> actual = widgetsCatalogController
438 .getWidgetParameterResult(request, 1);
439 PortalRestResponse<List<WidgetParameterResult>> expected = new PortalRestResponse<List<WidgetParameterResult>>(
440 PortalRestStatusEnum.OK, "SUCCESS", new ArrayList<>());
441 assertEquals(expected.getStatus(), actual.getStatus());
444 @SuppressWarnings("unchecked")
446 public void doDownloadTest() throws Exception {
447 PowerMockito.mockStatic(EcompPortalUtils.class);
448 PowerMockito.mockStatic(SystemProperties.class);
449 PowerMockito.mockStatic(EPCommonSystemProperties.class);
450 PowerMockito.mockStatic(CipherUtil.class);
451 Mockito.when(EcompPortalUtils.widgetMsProtocol()).thenReturn("test1");
452 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.WIDGET_MS_PROTOCOL)).thenReturn("https");
453 Mockito.when(SystemProperties.getProperty("microservices.widget.local.port")).thenReturn("test");
454 Mockito.when(EcompPortalUtils.getPropertyOrDefault("microservices.widget.username", "widget_user"))
456 Mockito.when(CipherUtil
457 .decryptPKC(EcompPortalUtils.getPropertyOrDefault("microservices.widget.password", "widget_password")))
459 Mockito.when(widgetMService.getServiceLocation("widgets-service", "test")).thenReturn("test.com");
460 MockHttpServletRequest request = new MockHttpServletRequest();
461 MockHttpServletResponse response = new MockHttpServletResponse();
462 ResponseEntity<byte[]> mockData = new ResponseEntity("testfile.zip".getBytes(), HttpStatus.OK);
463 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET), Matchers.<HttpEntity<?>>any(),
464 Matchers.eq(byte[].class))).thenReturn(mockData);
465 widgetsCatalogController.doDownload(request, response, 1l);
469 public void saveWidgetParameterTest() {
470 PowerMockito.mockStatic(EPUserUtils.class);
471 EPUser user = mockUser.mockEPUser();
472 WidgetCatalogParameter widgetCatalogParameter = new WidgetCatalogParameter();
473 widgetCatalogParameter.setId(1l);
474 widgetCatalogParameter.setParamId(1l);
475 widgetCatalogParameter.setUserId(user.getId());
476 widgetCatalogParameter.setUser_value("test123");
477 MockHttpServletRequest request = new MockHttpServletRequest();
478 Mockito.when(EPUserUtils.getUserSession(request)).thenReturn(user);
479 Mockito.when(widgetParameterService.getUserParamById(widgetCatalogParameter.getWidgetId(),
480 widgetCatalogParameter.getUserId(), widgetCatalogParameter.getParamId())).thenReturn(widgetCatalogParameter);
481 PortalRestResponse<String> response = widgetsCatalogController.saveWidgetParameter(request,
482 widgetCatalogParameter);
483 PortalRestResponse<String> expected = new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
484 assertEquals(expected.getMessage(), response.getMessage());
488 public void saveWidgetParameterExceptionTest() {
489 PowerMockito.mockStatic(EPUserUtils.class);
490 EPUser user = mockUser.mockEPUser();
491 WidgetCatalogParameter widgetCatalogParameter = new WidgetCatalogParameter();
492 widgetCatalogParameter.setId(1l);
493 widgetCatalogParameter.setParamId(1l);
494 widgetCatalogParameter.setUserId(user.getId());
495 widgetCatalogParameter.setUser_value("test123");
496 MockHttpServletRequest request = new MockHttpServletRequest();
497 Mockito.when(EPUserUtils.getUserSession(request)).thenReturn(user);
498 Mockito.doThrow(new NullPointerException()).when(widgetParameterService).getUserParamById(widgetCatalogParameter.getWidgetId(),
499 widgetCatalogParameter.getUserId(), widgetCatalogParameter.getParamId());
500 PortalRestResponse<String> response = widgetsCatalogController.saveWidgetParameter(request,
501 widgetCatalogParameter);
502 PortalRestResponse<String> expected = new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", "");
503 assertEquals(expected.getMessage(), response.getMessage());