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;
42 import java.util.ArrayList;
43 import java.util.List;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.mockito.MockitoAnnotations;
55 import org.onap.portalapp.portal.controller.MicroserviceController;
56 import org.onap.portalapp.portal.domain.MicroserviceData;
57 import org.onap.portalapp.portal.domain.WidgetCatalog;
58 import org.onap.portalapp.portal.domain.WidgetServiceHeaders;
59 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
60 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
61 import org.onap.portalapp.portal.framework.MockitoTestSuite;
62 import org.onap.portalapp.portal.service.ConsulHealthService;
63 import org.onap.portalapp.portal.service.ConsulHealthServiceImpl;
64 import org.onap.portalapp.portal.service.MicroserviceService;
65 import org.onap.portalapp.portal.service.MicroserviceServiceImpl;
66 import org.onap.portalapp.portal.utils.EcompPortalUtils;
67 import org.onap.portalsdk.core.util.SystemProperties;
68 import org.powermock.api.mockito.PowerMockito;
69 import org.powermock.core.classloader.annotations.PrepareForTest;
70 import org.powermock.modules.junit4.PowerMockRunner;
71 import org.springframework.core.ParameterizedTypeReference;
72 import org.springframework.http.HttpEntity;
73 import org.springframework.http.HttpMethod;
74 import org.springframework.http.HttpStatus;
75 import org.springframework.http.ResponseEntity;
76 import org.springframework.web.client.RestTemplate;
78 @RunWith(PowerMockRunner.class)
79 @PrepareForTest({WidgetServiceHeaders.class, EcompPortalUtils.class})
80 public class MicroserviceControllerTest extends MockitoTestSuite{
83 MicroserviceController microserviceController = new MicroserviceController();
86 ConsulHealthService consulHealthService = new ConsulHealthServiceImpl();
89 MicroserviceService microserviceService = new MicroserviceServiceImpl();
92 RestTemplate template = new RestTemplate();
95 MicroserviceData microserviceData = new MicroserviceData();
97 @SuppressWarnings("rawtypes")
99 ResponseEntity<List<WidgetCatalog>> ans = new ResponseEntity<>(HttpStatus.OK);
102 public void setup() {
103 MockitoAnnotations.initMocks(this);
107 EcompPortalUtils EcompPortalUtils = new EcompPortalUtils();
109 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
111 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
112 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
113 NullPointerException nullPointerException = new NullPointerException();
116 public void createMicroserviceIfServiceDataNullTest() throws Exception {
117 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<>();
118 expectedportalRestResponse.setMessage("FAILURE");
119 expectedportalRestResponse.setResponse("MicroserviceData cannot be null or empty");
120 expectedportalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
121 MicroserviceData microserviceData = null;
122 PortalRestResponse<String> actualportalRestResponse = microserviceController.createMicroservice(mockedRequest,
123 mockedResponse, microserviceData);
124 assertEquals(actualportalRestResponse, expectedportalRestResponse);
128 public void createMicroserviceTest() throws Exception {
129 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<>();
130 expectedportalRestResponse.setMessage("SUCCESS");
131 expectedportalRestResponse.setResponse("");
132 expectedportalRestResponse.setStatus(PortalRestStatusEnum.OK);
133 PortalRestResponse<String> actualportalRestResponse = microserviceController.createMicroservice(mockedRequest,
134 mockedResponse, microserviceData);
135 assertEquals(actualportalRestResponse, expectedportalRestResponse);
139 public void createMicroserviceXSSTest() throws Exception {
140 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<>();
141 expectedportalRestResponse.setMessage("ERROR");
142 expectedportalRestResponse.setResponse("MicroserviceData is not valid");
143 expectedportalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
144 MicroserviceData XSSMicroserviceData = new MicroserviceData();
145 XSSMicroserviceData.setActive("<script>alert(123);</script>");
146 XSSMicroserviceData.setName("<script>alert(/XSS”)</script>");
147 PortalRestResponse<String> actualportalRestResponse = microserviceController.createMicroservice(mockedRequest,
148 mockedResponse, XSSMicroserviceData);
149 assertEquals(expectedportalRestResponse, actualportalRestResponse);
153 public void createMicroserviceExceptionTest() throws Exception {
154 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<>();
155 expectedportalRestResponse.setMessage("FAILURE");
156 expectedportalRestResponse.setResponse(null);
157 expectedportalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
158 Mockito.when(microserviceService.saveMicroservice(microserviceData)).thenReturn((long) 1);
159 Mockito.when(microserviceData.getParameterList()).thenThrow(nullPointerException);
160 PortalRestResponse<String> actualportalRestResponse = microserviceController.createMicroservice(mockedRequest,
161 mockedResponse, microserviceData);
162 assertEquals(actualportalRestResponse, expectedportalRestResponse);
166 public void getMicroserviceTest() throws Exception {
167 Mockito.when(microserviceService.getMicroserviceData()).thenReturn(null);
168 List<MicroserviceData> list = microserviceController.getMicroservice(mockedRequest, mockedResponse);
169 assertEquals(list, null);
173 public void updateMicroserviceIfServiceISNullTest() {
174 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<>();
175 expectedportalRestResponse.setMessage("FAILURE");
176 expectedportalRestResponse.setResponse("MicroserviceData cannot be null or empty");
177 expectedportalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
178 MicroserviceData microserviceData = null;
179 PortalRestResponse<String> actualportalRestResponse = microserviceController.updateMicroservice(mockedRequest,
180 mockedResponse, 1, microserviceData);
181 assertEquals(actualportalRestResponse, expectedportalRestResponse);
185 public void updateMicroserviceTest() {
186 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<>();
187 expectedportalRestResponse.setMessage("SUCCESS");
188 expectedportalRestResponse.setResponse("");
189 expectedportalRestResponse.setStatus(PortalRestStatusEnum.OK);
190 PortalRestResponse<String> actualportalRestResponse = microserviceController.updateMicroservice(mockedRequest,
191 mockedResponse, 1, microserviceData);
192 assertEquals(actualportalRestResponse, expectedportalRestResponse);
196 public void updateMicroserviceXSSTest() {
197 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<>();
198 expectedportalRestResponse.setMessage("ERROR");
199 expectedportalRestResponse.setResponse("MicroserviceData is not valid");
200 expectedportalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
201 MicroserviceData XSSMicroserviceData = new MicroserviceData();
202 XSSMicroserviceData.setActive("<script>alert(123);</script>");
203 XSSMicroserviceData.setName("<script>alert(/XSS”)</script>");
204 PortalRestResponse<String> actualportalRestResponse = microserviceController.updateMicroservice(mockedRequest,
205 mockedResponse, 1, XSSMicroserviceData);
206 assertEquals(expectedportalRestResponse, actualportalRestResponse);
210 public void updateMicroserviceExceptionTest() {
211 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<>();
212 expectedportalRestResponse.setMessage("FAILURE");
213 expectedportalRestResponse.setResponse(null);
214 expectedportalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
215 Mockito.when(microserviceController.updateMicroservice(mockedRequest, mockedResponse, 1, microserviceData))
216 .thenThrow(nullPointerException);
217 PortalRestResponse<String> actualportalRestResponse = microserviceController.updateMicroservice(mockedRequest,
218 mockedResponse, 1, microserviceData);
219 assertEquals(actualportalRestResponse, expectedportalRestResponse);
223 public void deleteMicroserviceExceptionTest() {
224 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<>();
225 expectedportalRestResponse.setMessage("FAILURE");
226 PowerMockito.mockStatic(EcompPortalUtils.class);
227 expectedportalRestResponse.setResponse(
228 "I/O error on GET request for \"" + org.onap.portalapp.portal.utils.EcompPortalUtils.widgetMsProtocol()
229 + "://null/widget/microservices/widgetCatalog/service/1\":null; nested exception is java.net.UnknownHostException: null");
230 expectedportalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
231 PowerMockito.mockStatic(WidgetServiceHeaders.class);
232 PortalRestResponse<String> actuaPportalRestResponse = microserviceController.deleteMicroservice(mockedRequest,
234 assertEquals(actuaPportalRestResponse.getStatus(), expectedportalRestResponse.getStatus());
237 @SuppressWarnings("unchecked")
239 public void deleteMicroserviceTest() throws Exception {
240 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<>();
241 expectedportalRestResponse.setMessage("SOME WIDGETS ASSOICATE WITH THIS SERVICE");
242 expectedportalRestResponse.setResponse("'null' ,'null' ");
243 expectedportalRestResponse.setStatus(PortalRestStatusEnum.WARN);
244 List<WidgetCatalog> List = new ArrayList<>();
245 WidgetCatalog widgetCatalog = new WidgetCatalog();
246 widgetCatalog.setId(1);
247 WidgetCatalog widgetCatalog1 = new WidgetCatalog();
248 widgetCatalog.setId(2);
249 List.add(widgetCatalog);
250 List.add(widgetCatalog1);
251 PowerMockito.mockStatic(WidgetServiceHeaders.class);
252 PowerMockito.mockStatic(EcompPortalUtils.class);
253 String whatService = "widgets-service";
254 Mockito.when(consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))).thenReturn("Test");
255 Mockito.when(ans.getBody()).thenReturn(List);
256 ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
258 Mockito.when(template.exchange(
259 org.onap.portalapp.portal.utils.EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))
260 + "/widget/microservices/widgetCatalog/service/" + 1,
261 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), typeRef)).thenReturn(ans);
263 PortalRestResponse<String> actuaPportalRestResponse = microserviceController.deleteMicroservice(mockedRequest,
265 assertEquals(actuaPportalRestResponse, expectedportalRestResponse);
268 @SuppressWarnings("unchecked")
270 public void deleteMicroserviceWhenNoWidgetsAssociatedTest() throws Exception {
271 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<>();
272 expectedportalRestResponse.setMessage("SUCCESS");
273 expectedportalRestResponse.setResponse("");
274 expectedportalRestResponse.setStatus(PortalRestStatusEnum.OK);
275 List<WidgetCatalog> List = new ArrayList<>();
276 PowerMockito.mockStatic(WidgetServiceHeaders.class);
277 PowerMockito.mockStatic(EcompPortalUtils.class);
278 String whatService = "widgets-service";
279 Mockito.when(consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))).thenReturn("Test");
280 Mockito.when(ans.getBody()).thenReturn(List);
281 ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
283 Mockito.when(template.exchange(
284 org.onap.portalapp.portal.utils.EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))
285 + "/widget/microservices/widgetCatalog/service/" + 1,
286 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), typeRef)).thenReturn(ans);
287 PortalRestResponse<String> actuaPportalRestResponse = microserviceController.deleteMicroservice(mockedRequest,
289 assertEquals(actuaPportalRestResponse, expectedportalRestResponse);