From e65fc2fc02594a7c8640a6330b2373db1efb923e Mon Sep 17 00:00:00 2001 From: Michael Hwang Date: Thu, 22 Mar 2018 11:02:57 -0400 Subject: Add unit tests to reach 50% coverage Ripped out the dcae controller code which is no longer needed. Change-Id: I43906edb80bcd5e25445ec2a7175c6748b0ab2ae Signed-off-by: Michael Hwang Issue-ID: DCAEGEN2-257 --- src/test/java/io/swagger/api/BootstrapTest.java | 43 ++++ src/test/java/io/swagger/api/Util.java | 130 ++++++++++++ .../DcaeServiceTypesApiServiceFactoryTest.java | 37 ++++ .../DcaeServicesApiServiceFactoryTest.java | 37 ++++ .../DcaeServicesGroupbyApiServiceFactoryTest.java | 37 ++++ .../impl/DcaeServiceTypesApiServiceImplTests.java | 222 +++++++++++++++++++ .../impl/DcaeServicesApiServiceImplH2Tests.java | 132 ++++++++++++ .../api/impl/DcaeServicesApiServiceImplTests.java | 236 +++++++++++++++++++++ .../DcaeServicesGroupbyApiServiceImplTests.java | 88 ++++++++ 9 files changed, 962 insertions(+) create mode 100644 src/test/java/io/swagger/api/BootstrapTest.java create mode 100644 src/test/java/io/swagger/api/Util.java create mode 100644 src/test/java/io/swagger/api/factories/DcaeServiceTypesApiServiceFactoryTest.java create mode 100644 src/test/java/io/swagger/api/factories/DcaeServicesApiServiceFactoryTest.java create mode 100644 src/test/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactoryTest.java create mode 100644 src/test/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImplTests.java create mode 100644 src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplH2Tests.java create mode 100644 src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplTests.java create mode 100644 src/test/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImplTests.java (limited to 'src/test/java/io') diff --git a/src/test/java/io/swagger/api/BootstrapTest.java b/src/test/java/io/swagger/api/BootstrapTest.java new file mode 100644 index 0000000..7c86d8c --- /dev/null +++ b/src/test/java/io/swagger/api/BootstrapTest.java @@ -0,0 +1,43 @@ +/*- + * ============LICENSE_START======================================================= + * dcae-inventory + * ================================================================================ + * Copyright (C) 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========================================================= + */ + +package io.swagger.api; + +import org.junit.Test; + +import javax.servlet.ServletException; + +import static org.junit.Assert.fail; + +/** + * Created by mhwang on 3/22/18. + */ +public class BootstrapTest { + + @Test + public void testNoServletConfig() { + try { + (new Bootstrap()).init(null); + } catch(ServletException e) { + fail("This might be a valid failure. Should investigate."); + } + } + +} diff --git a/src/test/java/io/swagger/api/Util.java b/src/test/java/io/swagger/api/Util.java new file mode 100644 index 0000000..8f9e09f --- /dev/null +++ b/src/test/java/io/swagger/api/Util.java @@ -0,0 +1,130 @@ +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 javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.PathSegment; +import javax.ws.rs.core.UriBuilder; +import javax.ws.rs.core.UriInfo; +import java.net.URI; +import java.util.List; + +/** + * 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 b) { + return null; + } + + @Override + public List getPathSegments() { + return null; + } + + @Override + public List getPathSegments(boolean b) { + 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 b) { + return null; + } + + @Override + public MultivaluedMap getQueryParameters() { + return null; + } + + @Override + public MultivaluedMap getQueryParameters(boolean b) { + return null; + } + + @Override + public List getMatchedURIs() { + return null; + } + + @Override + public List getMatchedURIs(boolean b) { + 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/factories/DcaeServiceTypesApiServiceFactoryTest.java b/src/test/java/io/swagger/api/factories/DcaeServiceTypesApiServiceFactoryTest.java new file mode 100644 index 0000000..6b385df --- /dev/null +++ b/src/test/java/io/swagger/api/factories/DcaeServiceTypesApiServiceFactoryTest.java @@ -0,0 +1,37 @@ +/*- + * ============LICENSE_START======================================================= + * dcae-inventory + * ================================================================================ + * Copyright (C) 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========================================================= + */ + +package io.swagger.api.factories; + +import org.junit.Test; + +import static junit.framework.TestCase.assertNotNull; + +/** + * Created by mhwang on 3/22/18. + */ +public class DcaeServiceTypesApiServiceFactoryTest { + + @Test + public void testGetDcaeServiceTypesApi() { + assertNotNull(DcaeServiceTypesApiServiceFactory.getDcaeServiceTypesApi()); + } + +} diff --git a/src/test/java/io/swagger/api/factories/DcaeServicesApiServiceFactoryTest.java b/src/test/java/io/swagger/api/factories/DcaeServicesApiServiceFactoryTest.java new file mode 100644 index 0000000..d9c6808 --- /dev/null +++ b/src/test/java/io/swagger/api/factories/DcaeServicesApiServiceFactoryTest.java @@ -0,0 +1,37 @@ +/*- + * ============LICENSE_START======================================================= + * dcae-inventory + * ================================================================================ + * Copyright (C) 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========================================================= + */ + +package io.swagger.api.factories; + +import org.junit.Test; + +import static junit.framework.TestCase.assertNotNull; + +/** + * Created by mhwang on 3/22/18. + */ +public class DcaeServicesApiServiceFactoryTest { + + @Test + public void testGetDcaeServicesApi() { + assertNotNull(DcaeServicesApiServiceFactory.getDcaeServicesApi()); + } + +} diff --git a/src/test/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactoryTest.java b/src/test/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactoryTest.java new file mode 100644 index 0000000..033c8a6 --- /dev/null +++ b/src/test/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactoryTest.java @@ -0,0 +1,37 @@ +/*- + * ============LICENSE_START======================================================= + * dcae-inventory + * ================================================================================ + * Copyright (C) 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========================================================= + */ + +package io.swagger.api.factories; + +import org.junit.Test; + +import static junit.framework.TestCase.assertNotNull; + +/** + * Created by mhwang on 3/22/18. + */ +public class DcaeServicesGroupbyApiServiceFactoryTest { + + @Test + public void testGetDcaeServicesGroupbyApi() { + assertNotNull(DcaeServicesGroupbyApiServiceFactory.getDcaeServicesGroupbyApi()); + } + +} diff --git a/src/test/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImplTests.java b/src/test/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImplTests.java new file mode 100644 index 0000000..4a47787 --- /dev/null +++ b/src/test/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImplTests.java @@ -0,0 +1,222 @@ +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 org.onap.dcae.inventory.daos.DCAEServiceTypesDAO; +import org.onap.dcae.inventory.daos.DCAEServicesDAO; +import org.onap.dcae.inventory.daos.InventoryDAOManager; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceTypeObject; +import io.swagger.model.DCAEServiceType; +import io.swagger.model.DCAEServiceTypeRequest; +import org.joda.time.DateTime; +import org.junit.Before; +import org.junit.Test; + +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; +import javax.ws.rs.core.UriInfo; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.*; + +import org.junit.runner.RunWith; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; +import java.util.Objects; +import java.util.UUID; + + +/** + * Created by mhwang on 10/27/16. + */ +@PrepareForTest({InventoryDAOManager.class}) +@RunWith(PowerMockRunner.class) +public class DcaeServiceTypesApiServiceImplTests { + + private final static Logger LOG = LoggerFactory.getLogger(DcaeServiceTypesApiServiceImplTests.class); + + private final static String URL_PATH = "http://testing-is-good.com"; + + private DcaeServiceTypesApiServiceImpl api = null; + private UriInfo uriInfo = null; + private DCAEServiceTypesDAO mockTypesDAO = null; + private DCAEServicesDAO mockServicesDAO = null; + + @Before + public void setupClass() { + api = new DcaeServiceTypesApiServiceImpl(); + + uriInfo = mock(UriInfo.class); + mockTypesDAO = mock(DCAEServiceTypesDAO.class); + mockServicesDAO = mock(DCAEServicesDAO.class); + + // 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); + when(mockDAOManager.getDCAEServiceTypesDAO()).thenReturn(mockTypesDAO); + when(mockDAOManager.getDCAEServicesDAO()).thenReturn(mockServicesDAO); + + when(uriInfo.getBaseUriBuilder()).thenReturn(UriBuilder.fromPath(URL_PATH)); + } + + private static boolean matchTypeVsTypeObject(DCAEServiceType serviceType, DCAEServiceTypeObject serviceTypeObject, String prefixPath) { + return Objects.equals(serviceType.getTypeId(), serviceTypeObject.getTypeId()) + && Objects.equals(serviceType.getTypeName(), serviceTypeObject.getTypeName()) + && Objects.equals(serviceType.getTypeVersion(), serviceTypeObject.getTypeVersion()) + && Objects.equals(serviceType.getOwner(), serviceTypeObject.getOwner()) + && Objects.equals(serviceType.getBlueprintTemplate(), serviceTypeObject.getBlueprintTemplate()) + && Objects.equals(serviceType.getCreated(), serviceTypeObject.getCreated().toDate()) + && Objects.equals(serviceType.getVnfTypes(), serviceTypeObject.getVnfTypes()) + && Objects.equals(serviceType.getServiceIds(), serviceTypeObject.getServiceIds()) + && Objects.equals(serviceType.getServiceLocations(), serviceTypeObject.getServiceLocations()) + && Objects.equals(serviceType.getAsdcResourceId(), serviceTypeObject.getAsdcResourceId()) + && Objects.equals(serviceType.getAsdcServiceId(), serviceTypeObject.getAsdcServiceId()) + && serviceType.getSelfLink().getUri().toString().contains(prefixPath) + && serviceType.getSelfLink().getUri().toString().contains(serviceTypeObject.getTypeId()); + } + + @Test + public void testGetSuccess() { + DCAEServiceTypeObject minimalFixture = new DCAEServiceTypeObject(); + minimalFixture.setTypeId("abc:1"); + minimalFixture.setTypeName("abc"); + minimalFixture.setTypeVersion(1); + minimalFixture.setOwner("tester"); + minimalFixture.setBlueprintTemplate("{ blueprint template goes here }"); + minimalFixture.setCreated(DateTime.parse("2016-10-28T00:00")); + + DCAEServiceTypeObject fullFixture = new DCAEServiceTypeObject(); + fullFixture.setTypeId("def:1"); + fullFixture.setTypeName("def"); + fullFixture.setTypeVersion(1); + fullFixture.setOwner("tester"); + fullFixture.setBlueprintTemplate("{ blueprint template goes here }"); + fullFixture.setCreated(DateTime.parse("2016-10-28T00:00")); + fullFixture.setAsdcServiceId("4bb4e740-3920-442d-9ed3-89f15bdbff8a"); + fullFixture.setAsdcResourceId("3ea9dfae-a00d-4da8-8c87-02a34de8fc02"); + fullFixture.setVnfTypes(Arrays.asList(new String[] { "vnf-marble", "vnf-granite" })); + fullFixture.setServiceIds(Arrays.asList(new String[] { "service-alpha", "service-bravo" })); + fullFixture.setServiceLocations(Arrays.asList(new String[] { "New York", "Washington" })); + + for (DCAEServiceTypeObject fixture : new DCAEServiceTypeObject[] {minimalFixture, fullFixture}) { + String someTypeId = fixture.getTypeId(); + when(mockTypesDAO.getByTypeId(someTypeId)).thenReturn(fixture); + + try { + Response response = api.dcaeServiceTypesTypeIdGet(someTypeId, uriInfo, null); + DCAEServiceType serviceType = (DCAEServiceType) response.getEntity(); + assertTrue("GET - 200 test case failed", matchTypeVsTypeObject(serviceType, fixture, URL_PATH)); + } catch (Exception e) { + throw new RuntimeException("Unexpected exception: get 200", e); + } + } + } + + @Test + public void testGetNotFound() { + String someTypeId = "abc:1"; + when(mockTypesDAO.getByTypeId(someTypeId)).thenReturn(null); + + try { + Response response = api.dcaeServiceTypesTypeIdGet(someTypeId, uriInfo, null); + assertEquals("GET - 404 test case failed", 404, response.getStatus()); + } catch(Exception e) { + throw new RuntimeException("Unexpected exception: get 404", e); + } + } + + // TODO: Update this to check type id again. Must mock dao calls deeper. + private static boolean matchTypeVsTypeRequest(DCAEServiceType serviceType, DCAEServiceTypeRequest serviceTypeRequest, String prefixPath) { + return Objects.equals(serviceType.getTypeName(), serviceTypeRequest.getTypeName()) + && Objects.equals(serviceType.getTypeVersion(), serviceTypeRequest.getTypeVersion()) + && Objects.equals(serviceType.getOwner(), serviceTypeRequest.getOwner()) + && Objects.equals(serviceType.getBlueprintTemplate(), serviceTypeRequest.getBlueprintTemplate()) + && serviceType.getCreated() != null + && Objects.equals(serviceType.getVnfTypes(), serviceTypeRequest.getVnfTypes()) + && Objects.equals(serviceType.getServiceIds(), serviceTypeRequest.getServiceIds()) + && Objects.equals(serviceType.getServiceLocations(), serviceTypeRequest.getServiceLocations()) + && Objects.equals(serviceType.getAsdcResourceId(), serviceTypeRequest.getAsdcResourceId()) + && Objects.equals(serviceType.getAsdcServiceId(), serviceTypeRequest.getAsdcServiceId()) + && serviceType.getSelfLink().getUri().toString().contains(prefixPath); + } + + // TODO: Need to add tests for repeated POSTs == updates. + @Test + public void testPost() { + DCAEServiceTypeRequest minimalFixture = new DCAEServiceTypeRequest(); + minimalFixture.setTypeName("abc"); + minimalFixture.setTypeVersion(1); + minimalFixture.setOwner("tester"); + minimalFixture.setBlueprintTemplate("{ blueprint template goes here }"); + + DCAEServiceTypeRequest fullFixture = new DCAEServiceTypeRequest(); + fullFixture.setTypeName("def"); + fullFixture.setTypeVersion(1); + fullFixture.setOwner("tester"); + fullFixture.setBlueprintTemplate("{ blueprint template goes here }"); + fullFixture.setAsdcServiceId("4bb4e740-3920-442d-9ed3-89f15bdbff8a"); + fullFixture.setAsdcResourceId("3ea9dfae-a00d-4da8-8c87-02a34de8fc02"); + fullFixture.setVnfTypes(Arrays.asList(new String[] { "vnf-marble", "vnf-granite" })); + fullFixture.setServiceIds(Arrays.asList(new String[] { "service-alpha", "service-bravo" })); + fullFixture.setServiceLocations(Arrays.asList(new String[] { "New York", "Washington" })); + + for (DCAEServiceTypeRequest fixture : new DCAEServiceTypeRequest[] {minimalFixture, fullFixture}) { + try { + Response response = api.dcaeServiceTypesTypeIdPost(fixture, uriInfo, null); + DCAEServiceType serviceType = (DCAEServiceType) response.getEntity(); + assertTrue("POST - 200 test case failed", matchTypeVsTypeRequest(serviceType, fixture, URL_PATH)); + } catch (Exception e) { + throw new RuntimeException("Unexpected exception: post new 200", e); + } + } + } + + @Test + public void testPostConflict() { + DCAEServiceTypeRequest minimalFixture = new DCAEServiceTypeRequest(); + minimalFixture.setTypeName("abc"); + minimalFixture.setTypeVersion(1); + minimalFixture.setOwner("tester"); + minimalFixture.setBlueprintTemplate("{ blueprint template goes here }"); + + UUID expectedTypeIdUUID = UUID.randomUUID(); + + DCAEServiceTypeObject fakeExistingType = new DCAEServiceTypeObject(); + fakeExistingType.setTypeId(expectedTypeIdUUID.toString()); + when(mockTypesDAO.getByRequestFromNotASDC(minimalFixture)).thenReturn(fakeExistingType); + when(mockServicesDAO.countByType(DCAEServiceObject.DCAEServiceStatus.RUNNING, fakeExistingType.getTypeId())).thenReturn(10); + + try { + Response response = api.dcaeServiceTypesTypeIdPost(minimalFixture, uriInfo, null); + assertEquals("POST - 401 test case failed", 409, response.getStatus()); + } catch (Exception e) { + throw new RuntimeException("Unexpected exception: post new 200", e); + } + } + +} diff --git a/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplH2Tests.java b/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplH2Tests.java new file mode 100644 index 0000000..0a3b777 --- /dev/null +++ b/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplH2Tests.java @@ -0,0 +1,132 @@ +/*- + * ============LICENSE_START======================================================= + * dcae-inventory + * ================================================================================ + * Copyright (C) 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========================================================= + */ + +package io.swagger.api.impl; + +import com.codahale.metrics.MetricRegistry; +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.model.DCAEServiceRequest; +import org.joda.time.DateTime; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.dcae.inventory.clients.DatabusControllerClient; +import org.onap.dcae.inventory.daos.*; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.skife.jdbi.v2.DBI; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.ws.rs.core.Response; +import javax.ws.rs.core.SecurityContext; +import javax.ws.rs.core.UriInfo; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Created by mhwang on 9/25/17. + * + * This test covers the gap in DcaeServicesApiServiceImplTests - testing the GET query interface + * The approach taken here is standing up an H2 in-memory database which is different from the + * original test which made an attempt to mock the database calls but ran into an impasse. The + * test here was not included in the original class because of conflicting setup operations. + */ +@PrepareForTest({InventoryDAOManager.class}) +@RunWith(PowerMockRunner.class) +public class DcaeServicesApiServiceImplH2Tests { + + private final static Logger LOG = LoggerFactory.getLogger(DcaeServicesApiServiceImplH2Tests.class); + + private DCAEServicesDAO mockServicesDAO = null; + private DCAEServiceComponentsDAO mockComponentsDAO = null; + private DCAEServicesComponentsMapsDAO mockComponentsMapsDAO = null; + + // https://stackoverflow.com/questions/35825383/how-to-test-jdbi-daos-with-h2-in-memory-database + // Caused by: java.lang.ClassNotFoundException: Unable to load class: org.h2.Driver from ClassLoader:sun.misc.Launcher$AppClassLoader@18b4aac2;ClassLoader:sun.misc.Launcher$AppClassLoader@18b4aac2 + protected DataSourceFactory getDataSourceFactory() + { + DataSourceFactory dataSourceFactory = new DataSourceFactory(); + dataSourceFactory.setDriverClass( "org.h2.Driver" ); + dataSourceFactory.setUrl( "jdbc:h2:mem:testDb" ); + dataSourceFactory.setUser( "sa" ); + dataSourceFactory.setPassword( "sa" ); + + return dataSourceFactory; + } + + @Before + public void setUp() { + Environment env = new Environment( "test-env", Jackson.newObjectMapper(), null, new MetricRegistry(), null ); + DBI dbi = new DBIFactory().build( env, getDataSourceFactory(), "test" ); + mockServicesDAO = dbi.onDemand(DCAEServicesDAO.class); + mockComponentsDAO = dbi.onDemand(DCAEServiceComponentsDAO.class); + mockComponentsMapsDAO = dbi.onDemand(DCAEServicesComponentsMapsDAO.class); + + // 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); + when(mockDAOManager.getHandle()).thenReturn(dbi.open()); + } + + @Test + public void testDcaeServicesGet () { + mockServicesDAO.createTable(); + mockComponentsDAO.createTable(); + mockComponentsMapsDAO.createTable(); + + DCAEServiceRequest request = new DCAEServiceRequest(); + request.setTypeId("some-type-id"); + request.setVnfId("some-vnf-id"); + request.setVnfType("some-vnf-type"); + request.setVnfLocation("some-vnf-location"); + request.setDeploymentRef("some-deployment-ref"); + DCAEServiceObject so = new DCAEServiceObject("some-service-id", request); + mockServicesDAO.insert(so); + + DatabusControllerClient dbcc = mock(DatabusControllerClient.class); + DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc); + String typeId = "some-type-id"; + String vnfId = "some-vnf-id"; + String vnfType = "some-vnf-type"; + String vnfLocation = "some-vnf-location"; + String componentType = "some-component-type"; + Boolean shareable = Boolean.TRUE; + DateTime created = null; + Integer offset = 0; + UriInfo uriInfo = new Util.FakeUriInfo(); + SecurityContext securityContext = null; + Response response = api.dcaeServicesGet(typeId, vnfId, vnfType, vnfLocation, componentType, shareable, created, + offset, uriInfo, securityContext); + assertEquals(response.getStatus(), 200); + } + +} diff --git a/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplTests.java b/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplTests.java new file mode 100644 index 0000000..64b7f88 --- /dev/null +++ b/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplTests.java @@ -0,0 +1,236 @@ +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 io.swagger.api.NotFoundException; +import io.swagger.api.Util; +import io.swagger.model.DCAEService; +import io.swagger.model.DCAEServiceRequest; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.dcae.inventory.clients.DatabusControllerClient; +import org.onap.dcae.inventory.daos.DCAEServiceComponentsDAO; +import org.onap.dcae.inventory.daos.DCAEServiceTypesDAO; +import org.onap.dcae.inventory.daos.DCAEServicesDAO; +import org.onap.dcae.inventory.daos.InventoryDAOManager; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceComponentObject; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.*; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import static org.junit.Assert.fail; + +/** + * Created by mhwang on 9/25/17. + */ +@PrepareForTest({InventoryDAOManager.class}) +@RunWith(PowerMockRunner.class) +public class DcaeServicesApiServiceImplTests { + + private final static Logger LOG = LoggerFactory.getLogger(DcaeServicesApiServiceImplTests.class); + + private DCAEServiceTypesDAO mockTypesDAO = null; + private DCAEServicesDAO mockServicesDAO = null; + private DCAEServiceComponentsDAO mockComponentsDAO = null; + + @Before + public void setupClass() { + mockTypesDAO = mock(DCAEServiceTypesDAO.class); + mockServicesDAO = mock(DCAEServicesDAO.class); + mockComponentsDAO = mock(DCAEServiceComponentsDAO.class); + + // 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); + when(mockDAOManager.getDCAEServicesDAO()).thenReturn(mockServicesDAO); + when(mockDAOManager.getDCAEServiceComponentsDAO()).thenReturn(mockComponentsDAO); + when(mockDAOManager.getDCAEServiceTypesDAO()).thenReturn(mockTypesDAO); + } + + @Test + public void testCreateDCAEService() { + Method createDCAEService = null; + + // This block is a trick to make a private method accessible for testing + try { + createDCAEService = DcaeServicesApiServiceImpl.class.getDeclaredMethod("createDCAEService", DCAEServiceObject.class, + Collection.class, UriInfo.class); + createDCAEService.setAccessible(true); + } catch(NoSuchMethodException e) { + fail("Failed to do the reflection trick to test the private method: createDCAEService"); + } + + DatabusControllerClient dbcc = mock(DatabusControllerClient.class); + DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc); + + DCAEServiceRequest serviceRequest = new DCAEServiceRequest(); + serviceRequest.setTypeId("type-id-abc"); + 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(); + + try { + DCAEService service = (DCAEService) createDCAEService.invoke(api, serviceObject, components, uriInfo); + assertEquals(service.getServiceId(), serviceObject.getServiceId()); + } catch(Exception e) { + fail("Failed to execute the hacked createDCAEService method"); + } + } + + @Test + public void testDcaeServicesServiceIdGet() { + String serviceId = "service-id-123"; + DCAEServiceRequest serviceRequest = new DCAEServiceRequest(); + serviceRequest.setTypeId("type-id-abc"); + 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()); + + DatabusControllerClient dbcc = mock(DatabusControllerClient.class); + DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc); + UriInfo uriInfo = new Util.FakeUriInfo(); + + try { + Response response = api.dcaeServicesServiceIdGet(serviceId, uriInfo, null); + assertEquals(response.getStatus(), 200); + } catch(NotFoundException e) { + fail("Service should have been found"); + } + } + + /* + 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 testDcaeServicesGet() { + Handle mockHandle = mock(Handle.class); + when(InventoryDAOManager.getInstance().getHandle()).thenReturn(mockHandle); + Query mockQueryGeneric = mock(Query.class); + Query mockQuery = mock(Query.class); + when(mockHandle.createQuery(any())).thenReturn(mockQueryGeneric); + when(mockQueryGeneric.map(any(DCAEServiceObjectMapper.class))).thenReturn(mockQuery); + // LOOK HERE! + doReturn(null).when(mockQuery.bind(anyString(), any(DateTime.class))); + when(mockQuery.bind(anyString(), anyInt())).thenReturn(null); + + DatabusControllerClient dbcc = mock(DatabusControllerClient.class); + DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc); + + String typeId = "some-type-id"; + String vnfId = "some-vnf-id"; + String vnfType = "some-vnf-type"; + String vnfLocation = "some-vnf-location"; + String componentType = "some-component-type"; + Boolean shareable = Boolean.TRUE; + DateTime created = null; + Integer offset = 0; + UriInfo uriInfo = new io.swagger.api.Util.FakeUriInfo(); + SecurityContext securityContext = null; + + when(mockQuery.list()).thenReturn(new ArrayList()); + + Response response = api.dcaeServicesGet(typeId, vnfId, vnfType, vnfLocation, componentType, shareable, created, + offset, uriInfo, securityContext); + assertEquals(response.getStatus(), 200); + } + */ + + @Test + public void testDcaeServicesServiceIdPutMissingType() { + String serviceId = "service-id-123"; + DCAEServiceRequest serviceRequest = new DCAEServiceRequest(); + serviceRequest.setTypeId("type-id-abc"); + serviceRequest.setVnfId("vnf-id-def"); + + when(mockTypesDAO.getByTypeIdActiveOnly(serviceRequest.getTypeId())).thenReturn(null); + + DatabusControllerClient dbcc = mock(DatabusControllerClient.class); + DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc); + UriInfo uriInfo = new Util.FakeUriInfo(); + + Response response = api.dcaeServicesServiceIdPut(serviceId, serviceRequest, uriInfo, null); + assertEquals(response.getStatus(), 422); + } + + @Test + public void testDcaeServicesServiceIdDelete() { + String serviceId = "service-id-123"; + DCAEServiceRequest serviceRequest = new DCAEServiceRequest(); + serviceRequest.setTypeId("type-id-abc"); + serviceRequest.setVnfId("vnf-id-def"); + DCAEServiceObject serviceObject = new DCAEServiceObject(serviceId, serviceRequest); + when(mockServicesDAO.getByServiceId(DCAEServiceObject.DCAEServiceStatus.RUNNING, serviceId)).thenReturn(serviceObject); + + 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); + } catch(NotFoundException e) { + fail("Should have NOT thrown a NotFoundException"); + } catch(Exception e) { + LOG.error("Unexpected exception", e); + fail("Unexpected exception"); + } + } + + @Test + public void testDcaeServicesServiceIdDeleteMissingService() { + String serviceId = "service-id-123"; + when(mockServicesDAO.getByServiceId(DCAEServiceObject.DCAEServiceStatus.RUNNING, serviceId)).thenReturn(null); + + DatabusControllerClient dbcc = mock(DatabusControllerClient.class); + DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc); + UriInfo uriInfo = new Util.FakeUriInfo(); + + try { + api.dcaeServicesServiceIdDelete(serviceId, null); + fail("Should have thrown a NotFoundException"); + } catch(NotFoundException e) { + LOG.info("NotFoundException successfully thrown"); + } catch(Exception e) { + LOG.error("Unexpected exception", e); + fail("Unexpected exception"); + } + } + +} diff --git a/src/test/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImplTests.java b/src/test/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImplTests.java new file mode 100644 index 0000000..0ae2c3a --- /dev/null +++ b/src/test/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImplTests.java @@ -0,0 +1,88 @@ +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 io.swagger.api.Util; +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 javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * 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