2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.openecomp.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.openecomp.portalapp.portal.domain.MicroserviceData;
56 import org.openecomp.portalapp.portal.domain.WidgetCatalog;
57 import org.openecomp.portalapp.portal.domain.WidgetServiceHeaders;
58 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
59 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
60 import org.openecomp.portalapp.portal.framework.MockitoTestSuite;
61 import org.openecomp.portalapp.portal.service.ConsulHealthService;
62 import org.openecomp.portalapp.portal.service.ConsulHealthServiceImpl;
63 import org.openecomp.portalapp.portal.service.MicroserviceService;
64 import org.openecomp.portalapp.portal.service.MicroserviceServiceImpl;
65 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
66 import org.openecomp.portalsdk.core.util.SystemProperties;
67 import org.powermock.api.mockito.PowerMockito;
68 import org.powermock.core.classloader.annotations.PrepareForTest;
69 import org.powermock.modules.junit4.PowerMockRunner;
70 import org.springframework.core.ParameterizedTypeReference;
71 import org.springframework.http.HttpEntity;
72 import org.springframework.http.HttpMethod;
73 import org.springframework.http.HttpStatus;
74 import org.springframework.http.ResponseEntity;
75 import org.springframework.web.client.RestTemplate;
77 @RunWith(PowerMockRunner.class)
78 @PrepareForTest({WidgetServiceHeaders.class, EcompPortalUtils.class})
79 public class MicroserviceControllerTest extends MockitoTestSuite{
82 MicroserviceController microserviceController = new MicroserviceController();
85 ConsulHealthService consulHealthService = new ConsulHealthServiceImpl();
88 MicroserviceService microserviceService = new MicroserviceServiceImpl();
91 RestTemplate template = new RestTemplate();
94 MicroserviceData microserviceData = new MicroserviceData();
96 @SuppressWarnings("rawtypes")
98 ResponseEntity<List<WidgetCatalog>> ans = new ResponseEntity<List<WidgetCatalog>>(HttpStatus.OK);
101 public void setup() {
102 MockitoAnnotations.initMocks(this);
106 EcompPortalUtils EcompPortalUtils = new EcompPortalUtils();
108 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
110 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
111 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
112 NullPointerException nullPointerException = new NullPointerException();
115 public void createMicroserviceIfServiceDataNullTest() throws Exception {
116 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
117 expectedportalRestResponse.setMessage("FAILURE");
118 expectedportalRestResponse.setResponse("MicroserviceData cannot be null or empty");
119 PortalRestStatusEnum portalRestStatusEnum = null;
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<String>();
130 expectedportalRestResponse.setMessage("SUCCESS");
131 expectedportalRestResponse.setResponse("");
132 PortalRestStatusEnum portalRestStatusEnum = null;
133 expectedportalRestResponse.setStatus(portalRestStatusEnum.OK);
134 PortalRestResponse<String> actualportalRestResponse = microserviceController.createMicroservice(mockedRequest,
135 mockedResponse, microserviceData);
136 assertEquals(actualportalRestResponse, expectedportalRestResponse);
140 public void createMicroserviceExceptionTest() throws Exception {
141 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
142 expectedportalRestResponse.setMessage("FAILURE");
143 expectedportalRestResponse.setResponse(null);
144 PortalRestStatusEnum portalRestStatusEnum = null;
145 expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR);
146 Mockito.when(microserviceService.saveMicroservice(microserviceData)).thenReturn((long) 1);
147 Mockito.when(microserviceData.getParameterList()).thenThrow(nullPointerException);
148 PortalRestResponse<String> actualportalRestResponse = microserviceController.createMicroservice(mockedRequest,
149 mockedResponse, microserviceData);
150 assertEquals(actualportalRestResponse, expectedportalRestResponse);
154 public void getMicroserviceTest() throws Exception {
155 Mockito.when(microserviceService.getMicroserviceData()).thenReturn(null);
156 List<MicroserviceData> list = microserviceController.getMicroservice(mockedRequest, mockedResponse);
157 assertEquals(list, null);
161 public void updateMicroserviceIfServiceISNullTest() throws Exception {
162 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
163 expectedportalRestResponse.setMessage("FAILURE");
164 expectedportalRestResponse.setResponse("MicroserviceData cannot be null or empty");
165 PortalRestStatusEnum portalRestStatusEnum = null;
166 expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR);
167 MicroserviceData microserviceData = null;
168 PortalRestResponse<String> actualportalRestResponse = microserviceController.updateMicroservice(mockedRequest,
169 mockedResponse, 1, microserviceData);
170 assertEquals(actualportalRestResponse, expectedportalRestResponse);
174 public void updateMicroserviceTest() throws Exception {
175 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
176 expectedportalRestResponse.setMessage("SUCCESS");
177 expectedportalRestResponse.setResponse("");
178 PortalRestStatusEnum portalRestStatusEnum = null;
179 expectedportalRestResponse.setStatus(portalRestStatusEnum.OK);
180 PortalRestResponse<String> actualportalRestResponse = microserviceController.updateMicroservice(mockedRequest,
181 mockedResponse, 1, microserviceData);
182 assertEquals(actualportalRestResponse, expectedportalRestResponse);
186 public void updateMicroserviceExceptionTest() throws Exception {
187 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
188 expectedportalRestResponse.setMessage("FAILURE");
189 expectedportalRestResponse.setResponse(null);
190 PortalRestStatusEnum portalRestStatusEnum = null;
191 expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR);
192 Mockito.when(microserviceController.updateMicroservice(mockedRequest, mockedResponse, 1, microserviceData))
193 .thenThrow(nullPointerException);
194 PortalRestResponse<String> actualportalRestResponse = microserviceController.updateMicroservice(mockedRequest,
195 mockedResponse, 1, microserviceData);
196 assertEquals(actualportalRestResponse, expectedportalRestResponse);
200 public void deleteMicroserviceExceptionTest() throws Exception {
201 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
202 expectedportalRestResponse.setMessage("FAILURE");
203 PowerMockito.mockStatic(EcompPortalUtils.class);
204 expectedportalRestResponse.setResponse(
205 "I/O error on GET request for \"" + EcompPortalUtils.widgetMsProtocol() + "://null/widget/microservices/widgetCatalog/service/1\":null; nested exception is java.net.UnknownHostException: null");
206 PortalRestStatusEnum portalRestStatusEnum = null;
207 expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR);
208 PowerMockito.mockStatic(WidgetServiceHeaders.class);
209 PortalRestResponse<String> actuaPportalRestResponse = microserviceController.deleteMicroservice(mockedRequest,
211 assertEquals(actuaPportalRestResponse.getStatus(), expectedportalRestResponse.getStatus());
214 @SuppressWarnings("unchecked")
216 public void deleteMicroserviceTest() throws Exception {
217 String HTTPS = "https://";
218 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
219 expectedportalRestResponse.setMessage("SOME WIDGETS ASSOICATE WITH THIS SERVICE");
220 expectedportalRestResponse.setResponse("'null' ,'null' ");
221 PortalRestStatusEnum portalRestStatusEnum = null;
222 expectedportalRestResponse.setStatus(portalRestStatusEnum.WARN);
223 List<WidgetCatalog> List = new ArrayList<WidgetCatalog>();
224 WidgetCatalog widgetCatalog = new WidgetCatalog();
225 widgetCatalog.setId(1);
226 WidgetCatalog widgetCatalog1 = new WidgetCatalog();
227 widgetCatalog.setId(2);
228 List.add(widgetCatalog);
229 List.add(widgetCatalog1);
230 PowerMockito.mockStatic(WidgetServiceHeaders.class);
231 PowerMockito.mockStatic(EcompPortalUtils.class);
232 String whatService = "widgets-service";
233 Mockito.when(consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))).thenReturn("Test");
234 Mockito.when(ans.getBody()).thenReturn(List);
235 ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
237 Mockito.when(template.exchange(
238 EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))
239 + "/widget/microservices/widgetCatalog/service/" + 1,
240 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), typeRef)).thenReturn(ans);
242 PortalRestResponse<String> actuaPportalRestResponse = microserviceController.deleteMicroservice(mockedRequest,
244 assertEquals(actuaPportalRestResponse, expectedportalRestResponse);
247 @SuppressWarnings("unchecked")
249 public void deleteMicroserviceWhenNoWidgetsAssociatedTest() throws Exception {
250 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
251 expectedportalRestResponse.setMessage("SUCCESS");
252 expectedportalRestResponse.setResponse("");
253 PortalRestStatusEnum portalRestStatusEnum = null;
254 expectedportalRestResponse.setStatus(portalRestStatusEnum.OK);
255 List<WidgetCatalog> List = new ArrayList<WidgetCatalog>();
256 PowerMockito.mockStatic(WidgetServiceHeaders.class);
257 PowerMockito.mockStatic(EcompPortalUtils.class);
258 String whatService = "widgets-service";
259 Mockito.when(consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))).thenReturn("Test");
260 Mockito.when(ans.getBody()).thenReturn(List);
261 ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
263 Mockito.when(template.exchange(
264 EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))
265 + "/widget/microservices/widgetCatalog/service/" + 1,
266 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), typeRef)).thenReturn(ans);
267 PortalRestResponse<String> actuaPportalRestResponse = microserviceController.deleteMicroservice(mockedRequest,
269 assertEquals(actuaPportalRestResponse, expectedportalRestResponse);