From b9c324e98a42ba7af4c7c8e5ac4b5482e7509b8c Mon Sep 17 00:00:00 2001 From: Bogumil Zebek Date: Mon, 16 Dec 2019 09:34:21 +0100 Subject: Improve code coverage Issue-ID: DCAEGEN2-1992 Signed-off-by: Zebek Bogumil Change-Id: I3e57f34fb8d3fcec66a5c937aa352032dc84b1e8 --- .../impl/DcaeServicesGroupByApiServiceImpl.java | 15 ++- .../io/swagger/api/FakeUriInfoTestDataFactory.java | 135 +++++++++++++++++++++ src/test/java/io/swagger/api/Util.java | 131 -------------------- .../impl/DcaeServicesApiServiceImplH2Tests.java | 7 +- .../api/impl/DcaeServicesApiServiceImplTests.java | 21 ++-- .../api/impl/DcaeServicesLinkResolverTest.java | 99 +++++++++++++++ .../api/impl/RunningDcaeServicesProviderTest.java | 68 +++++++++++ 7 files changed, 326 insertions(+), 150 deletions(-) create mode 100644 src/test/java/io/swagger/api/FakeUriInfoTestDataFactory.java delete mode 100644 src/test/java/io/swagger/api/Util.java create mode 100644 src/test/java/io/swagger/api/impl/DcaeServicesLinkResolverTest.java create mode 100644 src/test/java/io/swagger/api/impl/RunningDcaeServicesProviderTest.java diff --git a/src/main/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImpl.java b/src/main/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImpl.java index f879bff..fff2bd4 100644 --- a/src/main/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImpl.java +++ b/src/main/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImpl.java @@ -27,6 +27,7 @@ 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 org.skife.jdbi.v2.Query; import javax.ws.rs.core.Link; import javax.ws.rs.core.Response; @@ -149,14 +150,18 @@ public class DcaeServicesGroupByApiServiceImpl extends DcaeServicesGroupbyApiSer 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(); + return executeQuery(queryString, jdbiHandle); } } + List> executeQuery(String queryString, Handle jdbiHandle) { + // NOTE: This is hardcoded because service status is only used internally. + final Query> query = jdbiHandle.createQuery(queryString); + final Query> bind = query + .bind(SERVICE_STATUS_PLACE_HOLDER, DCAEServiceObject.DCAEServiceStatus.RUNNING); + return bind.list(); + } + static String createQuery(String columnName) { StringBuilder sb = new StringBuilder(); sb.append(String.format("select %s, count(1) as num ", columnName)); diff --git a/src/test/java/io/swagger/api/FakeUriInfoTestDataFactory.java b/src/test/java/io/swagger/api/FakeUriInfoTestDataFactory.java new file mode 100644 index 0000000..edfce01 --- /dev/null +++ b/src/test/java/io/swagger/api/FakeUriInfoTestDataFactory.java @@ -0,0 +1,135 @@ +package io.swagger.api;/*- + * ============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 java.net.URI; +import java.util.List; + +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.PathSegment; +import javax.ws.rs.core.UriBuilder; +import javax.ws.rs.core.UriInfo; + +/** + * Created by mhwang on 9/25/17. + */ +public class FakeUriInfoTestDataFactory { + + public static UriInfo givenFakeUriInfo(){ + return new FakeUriInfo(); + } + + private static class FakeUriInfo implements UriInfo { + + @Override + public String getPath() { + return null; + } + + @Override + public String getPath(boolean bunused) { + return null; + } + + @Override + public List getPathSegments() { + return null; + } + + @Override + public List getPathSegments(boolean bunused) { + return null; + } + + @Override + public URI getRequestUri() { + return null; + } + + @Override + public UriBuilder getRequestUriBuilder() { + return null; + } + + @Override + public URI getAbsolutePath() { + return null; + } + + @Override + public UriBuilder getAbsolutePathBuilder() { + return null; + } + + @Override + public URI getBaseUri() { + return null; + } + + @Override + public UriBuilder getBaseUriBuilder() { + return UriBuilder.fromUri("http://some-fake-base-uri"); + } + + @Override + public MultivaluedMap getPathParameters() { + return null; + } + + @Override + public MultivaluedMap getPathParameters(boolean bunused) { + return null; + } + + @Override + public MultivaluedMap getQueryParameters() { + return null; + } + + @Override + public MultivaluedMap getQueryParameters(boolean bunused) { + return null; + } + + @Override + public List getMatchedURIs() { + return null; + } + + @Override + public List getMatchedURIs(boolean bunused) { + return null; + } + + @Override + public List getMatchedResources() { + return null; + } + + @Override + public URI resolve(URI uri) { + return null; + } + + @Override + public URI relativize(URI uri) { + return null; + } + } +} diff --git a/src/test/java/io/swagger/api/Util.java b/src/test/java/io/swagger/api/Util.java deleted file mode 100644 index 6e5a554..0000000 --- a/src/test/java/io/swagger/api/Util.java +++ /dev/null @@ -1,131 +0,0 @@ -package io.swagger.api;/*- - * ============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 java.net.URI; -import java.util.List; - -import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.core.PathSegment; -import javax.ws.rs.core.UriBuilder; -import javax.ws.rs.core.UriInfo; - -/** - * Created by mhwang on 9/25/17. - */ -public class Util { - - public static class FakeUriInfo implements UriInfo { - - @Override - public String getPath() { - return null; - } - - @Override - public String getPath(boolean bunused) { - return null; - } - - @Override - public List getPathSegments() { - return null; - } - - @Override - public List getPathSegments(boolean bunused) { - return null; - } - - @Override - public URI getRequestUri() { - return null; - } - - @Override - public UriBuilder getRequestUriBuilder() { - return null; - } - - @Override - public URI getAbsolutePath() { - return null; - } - - @Override - public UriBuilder getAbsolutePathBuilder() { - return null; - } - - @Override - public URI getBaseUri() { - return null; - } - - @Override - public UriBuilder getBaseUriBuilder() { - return UriBuilder.fromUri("http://some-fake-base-uri"); - } - - @Override - public MultivaluedMap getPathParameters() { - return null; - } - - @Override - public MultivaluedMap getPathParameters(boolean bunused) { - return null; - } - - @Override - public MultivaluedMap getQueryParameters() { - return null; - } - - @Override - public MultivaluedMap getQueryParameters(boolean bunused) { - return null; - } - - @Override - public List getMatchedURIs() { - return null; - } - - @Override - public List getMatchedURIs(boolean bunused) { - return null; - } - - @Override - public List getMatchedResources() { - return null; - } - - @Override - public URI resolve(URI uri) { - return null; - } - - @Override - public URI relativize(URI uri) { - return null; - } - } -} diff --git a/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplH2Tests.java b/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplH2Tests.java index cbdff7d..32cc664 100644 --- a/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplH2Tests.java +++ b/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplH2Tests.java @@ -28,6 +28,7 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.SecurityContext; import javax.ws.rs.core.UriInfo; +import org.eclipse.jetty.http.HttpStatus; import org.joda.time.DateTime; import org.junit.Before; import org.junit.Test; @@ -51,7 +52,7 @@ import io.dropwizard.db.DataSourceFactory; import io.dropwizard.jackson.Jackson; import io.dropwizard.jdbi.DBIFactory; import io.dropwizard.setup.Environment; -import io.swagger.api.Util; +import io.swagger.api.FakeUriInfoTestDataFactory; import io.swagger.model.DCAEServiceRequest; /** @@ -126,11 +127,11 @@ public class DcaeServicesApiServiceImplH2Tests { Boolean shareable = Boolean.TRUE; DateTime created = null; Integer offset = 0; - UriInfo uriInfo = new Util.FakeUriInfo(); + UriInfo uriInfo = FakeUriInfoTestDataFactory.givenFakeUriInfo(); SecurityContext securityContext = null; Response response = api.dcaeServicesGet(typeId, vnfId, vnfType, vnfLocation, componentType, shareable, created, offset, uriInfo, securityContext); - assertEquals(response.getStatus(), 200); + assertEquals(HttpStatus.OK_200, response.getStatus()); } } diff --git a/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplTests.java b/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplTests.java index 0f5a0a8..9599ca5 100644 --- a/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplTests.java +++ b/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplTests.java @@ -31,6 +31,7 @@ import java.util.List; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; +import org.eclipse.jetty.http.HttpStatus; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -48,7 +49,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import io.swagger.api.NotFoundException; -import io.swagger.api.Util; +import io.swagger.api.FakeUriInfoTestDataFactory; import io.swagger.model.DCAEService; import io.swagger.model.DCAEServiceRequest; @@ -102,8 +103,8 @@ public class DcaeServicesApiServiceImplTests { serviceRequest.setVnfId("vnf-id-def"); String serviceId = "service-id-123"; DCAEServiceObject serviceObject = new DCAEServiceObject(serviceId, serviceRequest); - List components = new ArrayList(); - UriInfo uriInfo = new Util.FakeUriInfo(); + List components = new ArrayList<>(); + UriInfo uriInfo = FakeUriInfoTestDataFactory.givenFakeUriInfo(); try { DCAEService service = (DCAEService) createDCAEService.invoke(api, serviceObject, components, uriInfo); @@ -121,15 +122,15 @@ public class DcaeServicesApiServiceImplTests { serviceRequest.setVnfId("vnf-id-def"); DCAEServiceObject serviceObject = new DCAEServiceObject(serviceId, serviceRequest); when(mockServicesDao.getByServiceId(DCAEServiceObject.DCAEServiceStatus.RUNNING, serviceId)).thenReturn(serviceObject); - when(mockComponentsDao.getByServiceId(serviceId)).thenReturn(new ArrayList()); + when(mockComponentsDao.getByServiceId(serviceId)).thenReturn(new ArrayList<>()); DatabusControllerClient dbcc = mock(DatabusControllerClient.class); DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc); - UriInfo uriInfo = new Util.FakeUriInfo(); + UriInfo uriInfo = FakeUriInfoTestDataFactory.givenFakeUriInfo(); try { Response response = api.dcaeServicesServiceIdGet(serviceId, uriInfo, null); - assertEquals(response.getStatus(), 200); + assertEquals(HttpStatus.OK_200, response.getStatus()); } catch (NotFoundException e) { fail("Service should have been found"); } @@ -183,11 +184,11 @@ public class DcaeServicesApiServiceImplTests { DatabusControllerClient dbcc = mock(DatabusControllerClient.class); DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc); - UriInfo uriInfo = new Util.FakeUriInfo(); + UriInfo uriInfo = FakeUriInfoTestDataFactory.givenFakeUriInfo(); String serviceId = "service-id-123"; Response response = api.dcaeServicesServiceIdPut(serviceId, serviceRequest, uriInfo, null); - assertEquals(response.getStatus(), 422); + assertEquals(HttpStatus.UNPROCESSABLE_ENTITY_422, response.getStatus()); } @Test @@ -201,11 +202,10 @@ public class DcaeServicesApiServiceImplTests { DatabusControllerClient dbcc = mock(DatabusControllerClient.class); DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc); - UriInfo uriInfo = new Util.FakeUriInfo(); try { Response response = api.dcaeServicesServiceIdDelete(serviceId, null); - assertEquals(response.getStatus(), 200); + assertEquals(HttpStatus.OK_200, response.getStatus()); } catch (NotFoundException e) { fail("Should have NOT thrown a NotFoundException"); } catch (Exception e) { @@ -221,7 +221,6 @@ public class DcaeServicesApiServiceImplTests { DatabusControllerClient dbcc = mock(DatabusControllerClient.class); DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc); - UriInfo uriInfo = new Util.FakeUriInfo(); try { api.dcaeServicesServiceIdDelete(serviceId, null); diff --git a/src/test/java/io/swagger/api/impl/DcaeServicesLinkResolverTest.java b/src/test/java/io/swagger/api/impl/DcaeServicesLinkResolverTest.java new file mode 100644 index 0000000..3d19536 --- /dev/null +++ b/src/test/java/io/swagger/api/impl/DcaeServicesLinkResolverTest.java @@ -0,0 +1,99 @@ +/*- + * ============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 io.swagger.api.FakeUriInfoTestDataFactory; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import javax.ws.rs.core.Link; +import javax.ws.rs.core.UriInfo; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DcaeServicesLinkResolverTest { + private DcaeServicesGroupByApiServiceImpl.DcaeServicesLinkResolver dcaeServicesLinkResolver; + private UriInfo uriInfo; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp(){ + dcaeServicesLinkResolver = new DcaeServicesGroupByApiServiceImpl.DcaeServicesLinkResolver(); + uriInfo = FakeUriInfoTestDataFactory.givenFakeUriInfo(); + } + + @Test + public void shouldReturnDcaeServiceLinkForTypeParameter(){ + // when + final Link link = dcaeServicesLinkResolver.resolveLink( + DcaeServicesGroupByApiServiceImpl.PROPERTY_NAME_TYPE, + uriInfo, + "test" + ); + + // then + assertThat(link.getRel()).isEqualTo("dcae_services"); + assertThat(link.getUri().toString()).isEqualTo("http://some-fake-base-uri/dcae-services/?typeId=test"); + } + + @Test + public void shouldReturnDcaeServiceLinkForVnfLocationParameter(){ + // when + final Link link = dcaeServicesLinkResolver.resolveLink( + DcaeServicesGroupByApiServiceImpl.PROPERTY_NAME_VNF_LOCATION, + uriInfo, + "test" + ); + + // then + assertThat(link.getRel()).isEqualTo("dcae_services"); + assertThat(link.getUri().toString()).isEqualTo("http://some-fake-base-uri/dcae-services/?vnfLocation=test"); + } + + @Test + public void shouldReturnDcaeServiceLinkForVnfTypeParameter(){ + // when + final Link link = dcaeServicesLinkResolver.resolveLink( + DcaeServicesGroupByApiServiceImpl.PROPERTY_NAME_VNF_TYPE, + uriInfo, + "test" + ); + + // then + assertThat(link.getRel()).isEqualTo("dcae_services"); + assertThat(link.getUri().toString()).isEqualTo("http://some-fake-base-uri/dcae-services/?vnfType=test"); + } + + + @Test + public void shouldReportAnErrorForUnknownParameter(){ + // arrange + thrown.expect(UnsupportedOperationException.class); + thrown.expectMessage("Unsupported 'unknown' property name!"); + + // when + dcaeServicesLinkResolver.resolveLink("unknown", uriInfo, "test"); + + } +} diff --git a/src/test/java/io/swagger/api/impl/RunningDcaeServicesProviderTest.java b/src/test/java/io/swagger/api/impl/RunningDcaeServicesProviderTest.java new file mode 100644 index 0000000..18e2da9 --- /dev/null +++ b/src/test/java/io/swagger/api/impl/RunningDcaeServicesProviderTest.java @@ -0,0 +1,68 @@ +/*- + * ============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 org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.dcae.inventory.daos.InventoryDataAccessManager; +import org.skife.jdbi.v2.Handle; + +import java.util.ArrayList; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class RunningDcaeServicesProviderTest { + + @Mock + private InventoryDataAccessManager inventoryDataAccessManager; + @Mock + private Handle handle; + + private DcaeServicesGroupByApiServiceImpl.RunningDcaeServicesProvider runningDcaeServicesProvider; + + + @Before + public void setUp() { + runningDcaeServicesProvider = Mockito.spy(new DcaeServicesGroupByApiServiceImpl.RunningDcaeServicesProvider(inventoryDataAccessManager)); + } + + @Test + public void shouldInvokeQueryForRunningDcaeServices() { + // given + when(inventoryDataAccessManager.getHandle()).thenReturn(handle); + Mockito.doReturn(new ArrayList<>()).when(runningDcaeServicesProvider).executeQuery(any(), any()); + + // when + runningDcaeServicesProvider.fetch("testColumnName"); + + // then + verify(runningDcaeServicesProvider, times(1)).executeQuery(any(), any()); + + } +} -- cgit 1.2.3-korg