X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ecomp-portal-BE-common%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fportalapp%2Fportal%2Fcontroller%2FMicroserviceControllerTest.java;fp=ecomp-portal-BE-common%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fportalapp%2Fportal%2Fcontroller%2FMicroserviceControllerTest.java;h=e6e9f7596226e41e3335ab6fa2c75cffa7549a96;hb=21a8761f684745bb300e075c7e98ad897ace9eed;hp=0000000000000000000000000000000000000000;hpb=3af8af1310d5a27cb58be29505573f0bbdc1717c;p=portal.git diff --git a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/controller/MicroserviceControllerTest.java b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/controller/MicroserviceControllerTest.java new file mode 100644 index 00000000..e6e9f759 --- /dev/null +++ b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/controller/MicroserviceControllerTest.java @@ -0,0 +1,272 @@ +/*- + * ============LICENSE_START========================================== + * ONAP Portal + * =================================================================== + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalapp.portal.controller; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.portal.controller.MicroserviceController; +import org.onap.portalapp.portal.domain.MicroserviceData; +import org.onap.portalapp.portal.domain.WidgetCatalog; +import org.onap.portalapp.portal.domain.WidgetServiceHeaders; +import org.onap.portalapp.portal.ecomp.model.PortalRestResponse; +import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum; +import org.onap.portalapp.portal.framework.MockitoTestSuite; +import org.onap.portalapp.portal.service.ConsulHealthService; +import org.onap.portalapp.portal.service.ConsulHealthServiceImpl; +import org.onap.portalapp.portal.service.MicroserviceService; +import org.onap.portalapp.portal.service.MicroserviceServiceImpl; +import org.onap.portalapp.portal.utils.EcompPortalUtils; +import org.onap.portalsdk.core.util.SystemProperties; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({WidgetServiceHeaders.class, EcompPortalUtils.class}) +public class MicroserviceControllerTest extends MockitoTestSuite{ + + @InjectMocks + MicroserviceController microserviceController = new MicroserviceController(); + + @Mock + ConsulHealthService consulHealthService = new ConsulHealthServiceImpl(); + + @Mock + MicroserviceService microserviceService = new MicroserviceServiceImpl(); + + @Mock + RestTemplate template = new RestTemplate(); + + @Mock + MicroserviceData microserviceData = new MicroserviceData(); + + @SuppressWarnings("rawtypes") + @Mock + ResponseEntity> ans = new ResponseEntity>(HttpStatus.OK); + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @Mock + EcompPortalUtils EcompPortalUtils = new EcompPortalUtils(); + + MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); + + HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); + HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); + NullPointerException nullPointerException = new NullPointerException(); + + @Test + public void createMicroserviceIfServiceDataNullTest() throws Exception { + PortalRestResponse expectedportalRestResponse = new PortalRestResponse(); + expectedportalRestResponse.setMessage("FAILURE"); + expectedportalRestResponse.setResponse("MicroserviceData cannot be null or empty"); + PortalRestStatusEnum portalRestStatusEnum = null; + expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR); + MicroserviceData microserviceData = null; + PortalRestResponse actualportalRestResponse = microserviceController.createMicroservice(mockedRequest, + mockedResponse, microserviceData); + assertEquals(actualportalRestResponse, expectedportalRestResponse); + } + + @Test + public void createMicroserviceTest() throws Exception { + PortalRestResponse expectedportalRestResponse = new PortalRestResponse(); + expectedportalRestResponse.setMessage("SUCCESS"); + expectedportalRestResponse.setResponse(""); + PortalRestStatusEnum portalRestStatusEnum = null; + expectedportalRestResponse.setStatus(portalRestStatusEnum.OK); + PortalRestResponse actualportalRestResponse = microserviceController.createMicroservice(mockedRequest, + mockedResponse, microserviceData); + assertEquals(actualportalRestResponse, expectedportalRestResponse); + } + + @Test + public void createMicroserviceExceptionTest() throws Exception { + PortalRestResponse expectedportalRestResponse = new PortalRestResponse(); + expectedportalRestResponse.setMessage("FAILURE"); + expectedportalRestResponse.setResponse(null); + PortalRestStatusEnum portalRestStatusEnum = null; + expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR); + Mockito.when(microserviceService.saveMicroservice(microserviceData)).thenReturn((long) 1); + Mockito.when(microserviceData.getParameterList()).thenThrow(nullPointerException); + PortalRestResponse actualportalRestResponse = microserviceController.createMicroservice(mockedRequest, + mockedResponse, microserviceData); + assertEquals(actualportalRestResponse, expectedportalRestResponse); + } + + @Test + public void getMicroserviceTest() throws Exception { + Mockito.when(microserviceService.getMicroserviceData()).thenReturn(null); + List list = microserviceController.getMicroservice(mockedRequest, mockedResponse); + assertEquals(list, null); + } + + @Test + public void updateMicroserviceIfServiceISNullTest() throws Exception { + PortalRestResponse expectedportalRestResponse = new PortalRestResponse(); + expectedportalRestResponse.setMessage("FAILURE"); + expectedportalRestResponse.setResponse("MicroserviceData cannot be null or empty"); + PortalRestStatusEnum portalRestStatusEnum = null; + expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR); + MicroserviceData microserviceData = null; + PortalRestResponse actualportalRestResponse = microserviceController.updateMicroservice(mockedRequest, + mockedResponse, 1, microserviceData); + assertEquals(actualportalRestResponse, expectedportalRestResponse); + } + + @Test + public void updateMicroserviceTest() throws Exception { + PortalRestResponse expectedportalRestResponse = new PortalRestResponse(); + expectedportalRestResponse.setMessage("SUCCESS"); + expectedportalRestResponse.setResponse(""); + PortalRestStatusEnum portalRestStatusEnum = null; + expectedportalRestResponse.setStatus(portalRestStatusEnum.OK); + PortalRestResponse actualportalRestResponse = microserviceController.updateMicroservice(mockedRequest, + mockedResponse, 1, microserviceData); + assertEquals(actualportalRestResponse, expectedportalRestResponse); + } + + @Test + public void updateMicroserviceExceptionTest() throws Exception { + PortalRestResponse expectedportalRestResponse = new PortalRestResponse(); + expectedportalRestResponse.setMessage("FAILURE"); + expectedportalRestResponse.setResponse(null); + PortalRestStatusEnum portalRestStatusEnum = null; + expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR); + Mockito.when(microserviceController.updateMicroservice(mockedRequest, mockedResponse, 1, microserviceData)) + .thenThrow(nullPointerException); + PortalRestResponse actualportalRestResponse = microserviceController.updateMicroservice(mockedRequest, + mockedResponse, 1, microserviceData); + assertEquals(actualportalRestResponse, expectedportalRestResponse); + } + + @Test + public void deleteMicroserviceExceptionTest() throws Exception { + PortalRestResponse expectedportalRestResponse = new PortalRestResponse(); + expectedportalRestResponse.setMessage("FAILURE"); + PowerMockito.mockStatic(EcompPortalUtils.class); + expectedportalRestResponse.setResponse( + "I/O error on GET request for \"" + EcompPortalUtils.widgetMsProtocol() + "://null/widget/microservices/widgetCatalog/service/1\":null; nested exception is java.net.UnknownHostException: null"); + PortalRestStatusEnum portalRestStatusEnum = null; + expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR); + PowerMockito.mockStatic(WidgetServiceHeaders.class); + PortalRestResponse actuaPportalRestResponse = microserviceController.deleteMicroservice(mockedRequest, + mockedResponse, 1); + assertEquals(actuaPportalRestResponse.getStatus(), expectedportalRestResponse.getStatus()); + } + + @SuppressWarnings("unchecked") + @Test + public void deleteMicroserviceTest() throws Exception { + String HTTPS = "https://"; + PortalRestResponse expectedportalRestResponse = new PortalRestResponse(); + expectedportalRestResponse.setMessage("SOME WIDGETS ASSOICATE WITH THIS SERVICE"); + expectedportalRestResponse.setResponse("'null' ,'null' "); + PortalRestStatusEnum portalRestStatusEnum = null; + expectedportalRestResponse.setStatus(portalRestStatusEnum.WARN); + List List = new ArrayList(); + WidgetCatalog widgetCatalog = new WidgetCatalog(); + widgetCatalog.setId(1); + WidgetCatalog widgetCatalog1 = new WidgetCatalog(); + widgetCatalog.setId(2); + List.add(widgetCatalog); + List.add(widgetCatalog1); + PowerMockito.mockStatic(WidgetServiceHeaders.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + String whatService = "widgets-service"; + Mockito.when(consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))).thenReturn("Test"); + Mockito.when(ans.getBody()).thenReturn(List); + ParameterizedTypeReference> typeRef = new ParameterizedTypeReference>() { + }; + Mockito.when(template.exchange( + EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port")) + + "/widget/microservices/widgetCatalog/service/" + 1, + HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), typeRef)).thenReturn(ans); + + PortalRestResponse actuaPportalRestResponse = microserviceController.deleteMicroservice(mockedRequest, + mockedResponse, 1); + assertEquals(actuaPportalRestResponse, expectedportalRestResponse); + } + + @SuppressWarnings("unchecked") + @Test + public void deleteMicroserviceWhenNoWidgetsAssociatedTest() throws Exception { + PortalRestResponse expectedportalRestResponse = new PortalRestResponse(); + expectedportalRestResponse.setMessage("SUCCESS"); + expectedportalRestResponse.setResponse(""); + PortalRestStatusEnum portalRestStatusEnum = null; + expectedportalRestResponse.setStatus(portalRestStatusEnum.OK); + List List = new ArrayList(); + PowerMockito.mockStatic(WidgetServiceHeaders.class); + PowerMockito.mockStatic(EcompPortalUtils.class); + String whatService = "widgets-service"; + Mockito.when(consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))).thenReturn("Test"); + Mockito.when(ans.getBody()).thenReturn(List); + ParameterizedTypeReference> typeRef = new ParameterizedTypeReference>() { + }; + Mockito.when(template.exchange( + EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port")) + + "/widget/microservices/widgetCatalog/service/" + 1, + HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), typeRef)).thenReturn(ans); + PortalRestResponse actuaPportalRestResponse = microserviceController.deleteMicroservice(mockedRequest, + mockedResponse, 1); + assertEquals(actuaPportalRestResponse, expectedportalRestResponse); + } +}