From cfdf0dbbcd208a669c9919f23f1c30b2ec16b4d8 Mon Sep 17 00:00:00 2001 From: Bogumil Zebek Date: Fri, 6 Dec 2019 08:37:39 +0100 Subject: Improve code coverage Issue-ID: DCAEGEN2-1980 Signed-off-by: Zebek Bogumil Change-Id: I062a4a7f6508a6a3b2c212793bdad019ab46f7e2 --- pom.xml | 6 + .../DcaeServicesGroupbyApiServiceFactory.java | 7 +- .../impl/DcaeServicesGroupByApiServiceImpl.java | 168 +++++++++++++++++++++ .../impl/DcaeServicesGroupbyApiServiceImpl.java | 103 ------------- .../dcae/inventory/daos/InventoryDAOManager.java | 9 +- .../inventory/daos/InventoryDataAccessManager.java | 34 +++++ .../DcaeServicesGroupByApiServiceImplTest.java | 126 ++++++++++++++++ .../DcaeServicesGroupbyApiServiceImplTests.java | 88 ----------- 8 files changed, 346 insertions(+), 195 deletions(-) create mode 100644 src/main/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImpl.java delete mode 100644 src/main/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImpl.java create mode 100644 src/main/java/org/onap/dcae/inventory/daos/InventoryDataAccessManager.java create mode 100644 src/test/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImplTest.java delete mode 100644 src/test/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImplTests.java diff --git a/pom.xml b/pom.xml index 594a846..e511b29 100644 --- a/pom.xml +++ b/pom.xml @@ -217,6 +217,12 @@ ECOMP is a trademark and service mark of AT&T Intellectual Property. 2.0.2-beta test + + org.assertj + assertj-core + 3.8.0 + test + org.powermock powermock-module-junit4 diff --git a/src/main/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactory.java b/src/main/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactory.java index 509ac2a..a43be5a 100644 --- a/src/main/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactory.java +++ b/src/main/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactory.java @@ -21,12 +21,15 @@ package io.swagger.api.factories; import io.swagger.api.DcaeServicesGroupbyApiService; -import io.swagger.api.impl.DcaeServicesGroupbyApiServiceImpl; +import io.swagger.api.impl.DcaeServicesGroupByApiServiceImpl; +import org.onap.dcae.inventory.daos.InventoryDAOManager; @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaJerseyServerCodegen", date = "2016-04-18T20:16:22.119Z") public final class DcaeServicesGroupbyApiServiceFactory { - private static final DcaeServicesGroupbyApiService service = new DcaeServicesGroupbyApiServiceImpl(); + private static final DcaeServicesGroupbyApiService service = new DcaeServicesGroupByApiServiceImpl( + new DcaeServicesGroupByApiServiceImpl.RunningDcaeServicesProvider(InventoryDAOManager.getInstance()), + new DcaeServicesGroupByApiServiceImpl.DcaeServicesLinkResolver()); //Utility classes, which are a collection of static members, are not meant to be instantiated. private DcaeServicesGroupbyApiServiceFactory(){ diff --git a/src/main/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImpl.java b/src/main/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImpl.java new file mode 100644 index 0000000..f879bff --- /dev/null +++ b/src/main/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImpl.java @@ -0,0 +1,168 @@ +/*- + * ============LICENSE_START======================================================= + * dcae-inventory + * ================================================================================ + * Copyright (C) 2019 AT&T and Nokia Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file 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. + * ============LICENSE_END========================================================= + */ + +package io.swagger.api.impl; + +import io.swagger.api.DcaeServicesApi; +import io.swagger.api.DcaeServicesGroupbyApiService; +import org.onap.dcae.inventory.daos.InventoryDataAccessManager; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject; +import io.swagger.model.DCAEServiceGroupByResults; +import io.swagger.model.DCAEServiceGroupByResultsPropertyValues; +import org.skife.jdbi.v2.Handle; + +import javax.ws.rs.core.Link; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.SecurityContext; +import javax.ws.rs.core.UriInfo; +import java.util.List; +import java.util.Map; + +@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaJerseyServerCodegen", date = "2016-04-18T20:16:22.119Z") +public class DcaeServicesGroupByApiServiceImpl extends DcaeServicesGroupbyApiService { + + public static final String PROPERTY_NAME_TYPE = "type"; + public static final String PROPERTY_NAME_VNF_LOCATION = "vnfLocation"; + public static final String PROPERTY_NAME_VNF_TYPE = "vnfType"; + private static final String UNKNOWN_PROPERTY_NAME = "n/a"; + + private final RunningDcaeServicesProvider runningDcaeServicesProvider; + private final DcaeServicesLinkResolver dcaeServicesLinkResolver; + + public DcaeServicesGroupByApiServiceImpl( + RunningDcaeServicesProvider runningDcaeServicesProvider, + DcaeServicesLinkResolver dcaeServicesLinkResolver) { + this.runningDcaeServicesProvider = runningDcaeServicesProvider; + this.dcaeServicesLinkResolver = dcaeServicesLinkResolver; + } + + @Override + public Response dcaeServicesGroupbyPropertyNameGet( + String propertyName, UriInfo uriInfo, SecurityContext securityContext) { + + String columnName = resolveColumnName(propertyName); + if(columnName.equals(UNKNOWN_PROPERTY_NAME)) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + + List> runningDcaeServices = runningDcaeServicesProvider.fetch(columnName); + DCAEServiceGroupByResults response = generateResponse(propertyName, uriInfo, columnName, runningDcaeServices); + + return Response.ok().entity(response).build(); + } + + DCAEServiceGroupByResults generateResponse( + String propertyName, UriInfo uriInfo, String columnName, List> runningDcaeServices) { + + DCAEServiceGroupByResults response = new DCAEServiceGroupByResults(); + response.setPropertyName(propertyName); + + for (Map result : runningDcaeServices) { + DCAEServiceGroupByResultsPropertyValues value = new DCAEServiceGroupByResultsPropertyValues(); + value.setCount(((Long) result.get("num")).intValue()); + + String propertyValue = (String) result.get(columnName); + value.setPropertyValue(propertyValue); + + Link dcaeServicesLink = dcaeServicesLinkResolver.resolveLink(propertyName, uriInfo, propertyValue); + value.setDcaeServiceQueryLink(dcaeServicesLink); + + response.getPropertyValues().add(value); + } + return response; + } + + String resolveColumnName(String propertyName) { + String columnName; + + switch (propertyName) { + case PROPERTY_NAME_TYPE: + columnName = "type_id"; + break; + case PROPERTY_NAME_VNF_LOCATION: + columnName = "vnf_location"; + break; + case PROPERTY_NAME_VNF_TYPE: + columnName = "vnf_type"; + break; + default: + columnName = UNKNOWN_PROPERTY_NAME; + } + return columnName; + } + + public static class DcaeServicesLinkResolver { + + private static final String RELATION = "dcae_services"; + + Link resolveLink(String propertyName, UriInfo uriInfo, String propertyValue) { + Link dcaeServices = null; + switch (propertyName) { + case PROPERTY_NAME_TYPE: + dcaeServices = DcaeServicesApi.buildLinkForGetByTypeId(uriInfo, RELATION, + propertyValue); + break; + case PROPERTY_NAME_VNF_LOCATION: + dcaeServices = DcaeServicesApi.buildLinkForGetByVnfLocation(uriInfo, RELATION, + propertyValue); + break; + case PROPERTY_NAME_VNF_TYPE: + dcaeServices = DcaeServicesApi.buildLinkForGetByVnfType(uriInfo, RELATION, + propertyValue); + break; + default: + throw new UnsupportedOperationException(String.format("Unsupported '%s' property name!", propertyName)); + } + return dcaeServices; + } + } + + + public static class RunningDcaeServicesProvider { + + private static final String SERVICE_STATUS_PLACE_HOLDER = "serviceStatus"; + + public RunningDcaeServicesProvider(InventoryDataAccessManager inventoryDataAccessManager) { + this.inventoryDataAccessManager = inventoryDataAccessManager; + } + + private final InventoryDataAccessManager inventoryDataAccessManager; + + public List> fetch(String columnName) { + + String queryString = createQuery(columnName); + try (Handle jdbiHandle = inventoryDataAccessManager.getHandle()) { + + // NOTE: This is hardcoded because service status is only used internally. + return jdbiHandle.createQuery(queryString) + .bind(SERVICE_STATUS_PLACE_HOLDER, DCAEServiceObject.DCAEServiceStatus.RUNNING) + .list(); + } + } + + static String createQuery(String columnName) { + StringBuilder sb = new StringBuilder(); + sb.append(String.format("select %s, count(1) as num ", columnName)); + sb.append(String.format(" from dcae_services where status = :%s",SERVICE_STATUS_PLACE_HOLDER)); + sb.append(String.format(" group by %s order by count(1) desc", columnName)); + return sb.toString(); + } + } +} diff --git a/src/main/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImpl.java b/src/main/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImpl.java deleted file mode 100644 index 34fba7d..0000000 --- a/src/main/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImpl.java +++ /dev/null @@ -1,103 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * dcae-inventory - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file 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. - * ============LICENSE_END========================================================= - */ - -package io.swagger.api.impl; - -import org.onap.dcae.inventory.daos.InventoryDAOManager; -import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject; -import io.swagger.api.*; -import io.swagger.model.DCAEServiceGroupByResults; -import io.swagger.model.DCAEServiceGroupByResultsPropertyValues; -import org.skife.jdbi.v2.Handle; - -import javax.ws.rs.core.Response; -import javax.ws.rs.core.SecurityContext; -import javax.ws.rs.core.UriInfo; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaJerseyServerCodegen", date = "2016-04-18T20:16:22.119Z") -public class DcaeServicesGroupbyApiServiceImpl extends DcaeServicesGroupbyApiService { - - @Override - public Response dcaeServicesGroupbyPropertyNameGet(String propertyName, UriInfo uriInfo, SecurityContext securityContext) { - String columnName; - - switch (propertyName) { - case "type": - columnName = "type_id"; - break; - case "vnfLocation": - columnName = "vnf_location"; - break; - case "vnfType": - columnName = "vnf_type"; - break; - default: - return Response.status(Response.Status.BAD_REQUEST).build(); - } - - List> results = new ArrayList<>(); - - try (Handle jdbiHandle = InventoryDAOManager.getInstance().getHandle()) { - StringBuilder sb = new StringBuilder(); - sb.append(String.format("select %s, count(1) as num ", columnName)); - sb.append(" from dcae_services where status = :serviceStatus"); - sb.append(String.format(" group by %s order by count(1) desc", columnName)); - String queryString = sb.toString(); - - // NOTE: This is hardcoded because service status is only used internally. - results = jdbiHandle.createQuery(queryString).bind("serviceStatus", DCAEServiceObject.DCAEServiceStatus.RUNNING) - .list(); - } - - DCAEServiceGroupByResults response = new DCAEServiceGroupByResults(); - response.setPropertyName(propertyName); - - for (Map result : results) { - DCAEServiceGroupByResultsPropertyValues value = new DCAEServiceGroupByResultsPropertyValues(); - value.setCount(((Long) result.get("num")).intValue()); - String propertyValue = (String) result.get(columnName); - value.setPropertyValue(propertyValue); - - switch (propertyName) { - case "type": - value.setDcaeServiceQueryLink(DcaeServicesApi.buildLinkForGetByTypeId(uriInfo, "dcae_services", - propertyValue)); - break; - case "vnfLocation": - value.setDcaeServiceQueryLink(DcaeServicesApi.buildLinkForGetByVnfLocation(uriInfo, "dcae_services", - propertyValue)); - break; - case "vnfType": - value.setDcaeServiceQueryLink(DcaeServicesApi.buildLinkForGetByVnfType(uriInfo, "dcae_services", - propertyValue)); - break; - default: - return Response.status(Response.Status.BAD_REQUEST).build(); - } - - response.getPropertyValues().add(value); - } - - return Response.ok().entity(response).build(); - } -} diff --git a/src/main/java/org/onap/dcae/inventory/daos/InventoryDAOManager.java b/src/main/java/org/onap/dcae/inventory/daos/InventoryDAOManager.java index 5bdecfe..7039700 100644 --- a/src/main/java/org/onap/dcae/inventory/daos/InventoryDAOManager.java +++ b/src/main/java/org/onap/dcae/inventory/daos/InventoryDAOManager.java @@ -40,11 +40,11 @@ import java.util.List; * * Created by mhwang on 4/19/16. */ -public final class InventoryDAOManager { +public final class InventoryDAOManager implements InventoryDataAccessManager { private static InventoryDAOManager instance; - public static InventoryDAOManager getInstance() { + public static synchronized InventoryDAOManager getInstance() { if (instance == null) { instance = new InventoryDAOManager(); } @@ -177,10 +177,12 @@ public final class InventoryDAOManager { return jdbi.onDemand(klass); } + @Override public DCAEServicesDAO getDCAEServicesDAO() { return (DCAEServicesDAO) this.getDAO(DCAEServicesDAO.class); } + @Override public DCAEServiceComponentsDAO getDCAEServiceComponentsDAO() { return (DCAEServiceComponentsDAO) this.getDAO(DCAEServiceComponentsDAO.class); } @@ -189,10 +191,12 @@ public final class InventoryDAOManager { return (DCAEServicesComponentsMapsDAO) this.getDAO(DCAEServicesComponentsMapsDAO.class); } + @Override public DCAEServiceTransactionDAO getDCAEServiceTransactionDAO() { return jdbi.onDemand(DCAEServiceTransactionDAO.class); } + @Override public DCAEServiceTypesDAO getDCAEServiceTypesDAO() { return (DCAEServiceTypesDAO) this.getDAO(DCAEServiceTypesDAO.class); } @@ -202,6 +206,7 @@ public final class InventoryDAOManager { * * @return */ + @Override public Handle getHandle() { return this.jdbi.open(); } diff --git a/src/main/java/org/onap/dcae/inventory/daos/InventoryDataAccessManager.java b/src/main/java/org/onap/dcae/inventory/daos/InventoryDataAccessManager.java new file mode 100644 index 0000000..ef4375b --- /dev/null +++ b/src/main/java/org/onap/dcae/inventory/daos/InventoryDataAccessManager.java @@ -0,0 +1,34 @@ +/*- + * ============LICENSE_START======================================================= + * dcae-inventory + * ================================================================================ + * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file 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. + * ============LICENSE_END========================================================= + */ +package org.onap.dcae.inventory.daos; + +import org.skife.jdbi.v2.Handle; + +public interface InventoryDataAccessManager { + DCAEServicesDAO getDCAEServicesDAO(); + + DCAEServiceComponentsDAO getDCAEServiceComponentsDAO(); + + DCAEServiceTransactionDAO getDCAEServiceTransactionDAO(); + + DCAEServiceTypesDAO getDCAEServiceTypesDAO(); + + Handle getHandle(); +} diff --git a/src/test/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImplTest.java b/src/test/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImplTest.java new file mode 100644 index 0000000..66f864b --- /dev/null +++ b/src/test/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImplTest.java @@ -0,0 +1,126 @@ +/*- + * ============LICENSE_START======================================================= + * dcae-inventory + * ================================================================================ + * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file 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. + * ============LICENSE_END========================================================= + */ +package io.swagger.api.impl; + + +import com.google.common.collect.Lists; +import io.swagger.model.DCAEServiceGroupByResults; +import io.swagger.model.DCAEServiceGroupByResultsPropertyValues; +import org.assertj.core.util.Maps; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import javax.ws.rs.core.Link; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.SecurityContext; +import javax.ws.rs.core.UriInfo; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class DcaeServicesGroupByApiServiceImplTest { + + + @Mock + private DcaeServicesGroupByApiServiceImpl.RunningDcaeServicesProvider runningDcaeServicesProvider; + @Mock + private DcaeServicesGroupByApiServiceImpl.DcaeServicesLinkResolver dcaeServicesLinkResolver; + @Mock + private UriInfo uriInfo; + @Mock + private SecurityContext securityContext; + private DcaeServicesGroupByApiServiceImpl dcaeServicesGroupbyApiService; + + @Before + public void setUp() { + this.dcaeServicesGroupbyApiService = new DcaeServicesGroupByApiServiceImpl( + runningDcaeServicesProvider, dcaeServicesLinkResolver); + } + + @Test + public void shouldReportBadRequestResponseWhenColumnNameDoesNotExist() { + final Response response = dcaeServicesGroupbyApiService.dcaeServicesGroupbyPropertyNameGet( + "columnNameDoesNotExist", + uriInfo, + securityContext + ); + + assertThat(response.getStatus()).isEqualTo(Response.Status.BAD_REQUEST.getStatusCode()); + } + + @Test + public void shouldMapPropertyNameToProperColumnName() { + assertThat(dcaeServicesGroupbyApiService.resolveColumnName("type")).isEqualTo("type_id"); + assertThat(dcaeServicesGroupbyApiService.resolveColumnName("vnfLocation")).isEqualTo("vnf_location"); + assertThat(dcaeServicesGroupbyApiService.resolveColumnName("vnfType")).isEqualTo("vnf_type"); + assertThat(dcaeServicesGroupbyApiService.resolveColumnName("unknownPropertyName")).isEqualTo("n/a"); + } + + @Test + public void shouldCreateQueryTemplate() { + assertThat( + DcaeServicesGroupByApiServiceImpl.RunningDcaeServicesProvider.createQuery("type") + ).isEqualTo("select type, count(1) as num from dcae_services where status = :serviceStatus group by type order by count(1) desc"); + } + + @Test + public void shouldReturnOkResponseWhenThereIsNoRunningDcaeServices(){ + final Response response = dcaeServicesGroupbyApiService.dcaeServicesGroupbyPropertyNameGet("type", uriInfo, securityContext); + assertThat(response.getStatus()).isEqualTo(Response.Status.OK.getStatusCode()); + } + @Test + public void shouldReturnOkResponseWhenThereIsOneRunningDcaeService(){ + + // given + final Map dataMap = Maps.newHashMap("num", 1L); + dataMap.put("type_id", "some_id_value"); + + List> data = Lists.newArrayList(dataMap); + when(runningDcaeServicesProvider.fetch(eq("type_id"))).thenReturn(data); + + Link link = mock(Link.class); + when(dcaeServicesLinkResolver.resolveLink(eq("type"),eq(uriInfo),eq("some_id_value"))).thenReturn(link); + + // when + final Response response = dcaeServicesGroupbyApiService.dcaeServicesGroupbyPropertyNameGet("type", uriInfo, securityContext); + + // then + assertThat(response.getStatus()).isEqualTo(Response.Status.OK.getStatusCode()); + + final DCAEServiceGroupByResults entity = (DCAEServiceGroupByResults) response.getEntity(); + assertThatEntityResponseContainsInformationAboutRunningDcaeServices(entity); + } + + void assertThatEntityResponseContainsInformationAboutRunningDcaeServices(DCAEServiceGroupByResults entity) { + assertThat(entity.getPropertyName()).isEqualTo("type"); + assertThat(entity.getPropertyValues().size()).isEqualTo(1); + final DCAEServiceGroupByResultsPropertyValues propertyValues = entity.getPropertyValues().get(0); + assertThat(propertyValues.getCount()).isEqualTo(1); + assertThat(propertyValues.getPropertyValue()).isEqualTo("some_id_value"); + } +} diff --git a/src/test/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImplTests.java b/src/test/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImplTests.java deleted file mode 100644 index c2d0944..0000000 --- a/src/test/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImplTests.java +++ /dev/null @@ -1,88 +0,0 @@ -package io.swagger.api.impl;/*- - * ============LICENSE_START======================================================= - * dcae-inventory - * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file 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. - * ============LICENSE_END========================================================= - */ - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriInfo; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.onap.dcae.inventory.daos.InventoryDAOManager; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import io.swagger.api.Util; - -/** - * Created by mhwang on 9/27/17. - */ -@PrepareForTest({InventoryDAOManager.class}) -@RunWith(PowerMockRunner.class) -public class DcaeServicesGroupbyApiServiceImplTests { - - @Before - public void setupClass() { - // PowerMockito does bytecode magic to mock static methods and use final classes - PowerMockito.mockStatic(InventoryDAOManager.class); - InventoryDAOManager mockDaoManager = mock(InventoryDAOManager.class); - - when(InventoryDAOManager.getInstance()).thenReturn(mockDaoManager); - } - - @Test - public void testBadRequest() { - DcaeServicesGroupbyApiServiceImpl api = new DcaeServicesGroupbyApiServiceImpl(); - - String propertyName = "non-existent"; - UriInfo uriInfo = new Util.FakeUriInfo(); - Response response = api.dcaeServicesGroupbyPropertyNameGet(propertyName, uriInfo, null); - assertEquals(response.getStatus(), 400); - } - - /* - Commented this unit test because could not get past Nullpointer in the line trying to mock the explicit "bind" - function call. Mockito does not handle mocking overloaded functions well so it goes into the actual method where - an member variable called foreman is null. - @Test - public void testNoResults() { - DcaeServicesGroupbyApiServiceImpl api = new DcaeServicesGroupbyApiServiceImpl(); - - String propertyName = "type"; - UriInfo uriInfo = new io.swagger.api.Util.FakeUriInfo(); - - Handle mockHandle = mock(Handle.class); - when(InventoryDAOManager.getInstance().getHandle()).thenReturn(mockHandle); - Query mockQueryGeneric = mock(Query.class); - when(mockHandle.createQuery(any())).thenReturn(mockQueryGeneric); - // LOOK HERE! - when(mockQueryGeneric.bind(any(), DCAEServiceObject.DCAEServiceStatus.RUNNING)).thenReturn(mockQueryGeneric); - when(mockQueryGeneric.list()).thenReturn(null); - - Response response = api.dcaeServicesGroupbyPropertyNameGet(propertyName, uriInfo, null); - assertEquals(response.getStatus(), 400); - } - */ - -} -- cgit 1.2.3-korg