diff options
author | Michael Hwang <mhwang@research.att.com> | 2017-02-14 15:10:16 +0000 |
---|---|---|
committer | Michael Hwang <mhwang@research.att.com> | 2017-08-23 13:18:27 -0400 |
commit | ac853c1e7186b10e34e392918af31e4ac62b45c2 (patch) | |
tree | 5b0acebf2704317cd9fe86545313e68771bad04f /src/test | |
parent | 19488409d66aa5bbbc2856022f1187ce3f563b83 (diff) |
Make first commit
Transferring from original dcae project
Issue-Id: DCAEGEN2-45
Change-Id: If8500f4a120ec2d27a714c8917f163beb57ee64c
Signed-off-by: Michael Hwang <mhwang@research.att.com>
Diffstat (limited to 'src/test')
3 files changed, 381 insertions, 0 deletions
diff --git a/src/test/java/DcaeServiceTypesApiServiceImplTests.java b/src/test/java/DcaeServiceTypesApiServiceImplTests.java new file mode 100644 index 0000000..ce0011b --- /dev/null +++ b/src/test/java/DcaeServiceTypesApiServiceImplTests.java @@ -0,0 +1,220 @@ +/*- + * ============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========================================================= + */ + +import org.openecomp.dcae.inventory.daos.DCAEServiceTypesDAO; +import org.openecomp.dcae.inventory.daos.DCAEServicesDAO; +import org.openecomp.dcae.inventory.daos.InventoryDAOManager; +import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceObject; +import org.openecomp.dcae.inventory.dbthings.models.DCAEServiceTypeObject; +import io.swagger.api.impl.DcaeServiceTypesApiServiceImpl; +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; + + +/** + * 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 }"); + + String expectedTypeId = String.format("%s:%s", minimalFixture.getTypeName(), minimalFixture.getTypeVersion()); + + when(mockTypesDAO.getByTypeId(expectedTypeId)).thenReturn(new DCAEServiceTypeObject()); + when(mockServicesDAO.countByType(DCAEServiceObject.DCAEServiceStatus.RUNNING, expectedTypeId)).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/org/openecomp/dcae/inventory/daos/InventoryDAOManagerTests.java b/src/test/java/org/openecomp/dcae/inventory/daos/InventoryDAOManagerTests.java new file mode 100644 index 0000000..16e3688 --- /dev/null +++ b/src/test/java/org/openecomp/dcae/inventory/daos/InventoryDAOManagerTests.java @@ -0,0 +1,44 @@ +/*- + * ============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 org.openecomp.dcae.inventory.daos; + +import org.openecomp.dcae.inventory.InventoryConfiguration; +import io.dropwizard.setup.Environment; +import org.junit.Test; +import static org.mockito.Mockito.mock; + +/** + * Created by mhwang on 3/8/17. + */ +public class InventoryDAOManagerTests { + + @Test(expected=InventoryDAOManager.InventoryDAOManagerSetupException.class) + public void testInitializeStrictness() { + InventoryDAOManager daoManager = InventoryDAOManager.getInstance(); + Environment environment = mock(Environment.class); + InventoryConfiguration configuration = mock(InventoryConfiguration.class); + // This should be ok + daoManager.setup(environment, configuration); + // Cannot do another call + daoManager.setup(environment, configuration); + } + +} diff --git a/src/test/java/org/openecomp/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java b/src/test/java/org/openecomp/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java new file mode 100644 index 0000000..6e72182 --- /dev/null +++ b/src/test/java/org/openecomp/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java @@ -0,0 +1,117 @@ +/*- + * ============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 org.openecomp.dcae.inventory.exception.mappers; + +import org.openecomp.dcae.inventory.daos.InventoryDAOManager; +import org.openecomp.dcae.inventory.exceptions.mappers.DBIExceptionMapper; +import io.swagger.api.ApiResponseMessage; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.skife.jdbi.v2.exceptions.UnableToCreateStatementException; +import org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException; +import org.skife.jdbi.v2.exceptions.UnableToObtainConnectionException; + +import javax.ws.rs.core.Response; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Created by mhwang on 3/8/17. + */ +@PrepareForTest({InventoryDAOManager.class}) +@RunWith(PowerMockRunner.class) +public class DBIExceptionMapperTests { + + @Test + public void testReinitializeSuccess() { + // 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); + + RuntimeException fakeException = new RuntimeException("Spoofing database failure"); + + // Test UnableToObtainConnectionExceptionMapper + + DBIExceptionMapper.UnableToObtainConnectionExceptionMapper mapperOne = new DBIExceptionMapper.UnableToObtainConnectionExceptionMapper(); + Response responseOne = mapperOne.toResponse(new UnableToObtainConnectionException(fakeException)); + assert responseOne.getStatus() == 502; + String messageOne = ((ApiResponseMessage) responseOne.getEntity()).getMessage(); + assert messageOne.contains("successfully reset"); + + // Test UnableToCreateStatementExceptionMapper + + DBIExceptionMapper.UnableToCreateStatementExceptionMapper mapperTwo = new DBIExceptionMapper.UnableToCreateStatementExceptionMapper(); + Response responseTwo = mapperTwo.toResponse(new UnableToCreateStatementException(fakeException)); + assert responseTwo.getStatus() == 502; + String messageTwo = ((ApiResponseMessage) responseTwo.getEntity()).getMessage(); + assert messageTwo.contains("successfully reset"); + + // Test UnableToExecuteStatementExceptionMapper + + DBIExceptionMapper.UnableToExecuteStatementExceptionMapper mapperThree = new DBIExceptionMapper.UnableToExecuteStatementExceptionMapper(); + Response responseThree = mapperThree.toResponse(new UnableToExecuteStatementException(fakeException)); + assert responseThree.getStatus() == 502; + String messageThree = ((ApiResponseMessage) responseThree.getEntity()).getMessage(); + assert messageThree.contains("successfully reset"); + } + + @Test + public void testReinitializeFailed() { + // 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); + Mockito.doThrow(new RuntimeException("Spoof initialization failure")).when(mockDAOManager).initialize(); + + RuntimeException fakeException = new RuntimeException("Spoofing database failure"); + + // Test UnableToObtainConnectionExceptionMapper + + DBIExceptionMapper.UnableToObtainConnectionExceptionMapper mapperOne = new DBIExceptionMapper.UnableToObtainConnectionExceptionMapper(); + Response responseOne = mapperOne.toResponse(new UnableToObtainConnectionException(fakeException)); + assert responseOne.getStatus() == 502; + String messageOne = ((ApiResponseMessage) responseOne.getEntity()).getMessage(); + assert !messageOne.contains("successfully reset"); + + // Test UnableToCreateStatementExceptionMapper + + DBIExceptionMapper.UnableToCreateStatementExceptionMapper mapperTwo = new DBIExceptionMapper.UnableToCreateStatementExceptionMapper(); + Response responseTwo = mapperTwo.toResponse(new UnableToCreateStatementException(fakeException)); + assert responseTwo.getStatus() == 502; + String messageTwo = ((ApiResponseMessage) responseTwo.getEntity()).getMessage(); + assert !messageTwo.contains("successfully reset"); + + // Test UnableToExecuteStatementExceptionMapper + + DBIExceptionMapper.UnableToExecuteStatementExceptionMapper mapperThree = new DBIExceptionMapper.UnableToExecuteStatementExceptionMapper(); + Response responseThree = mapperThree.toResponse(new UnableToExecuteStatementException(fakeException)); + assert responseThree.getStatus() == 502; + String messageThree = ((ApiResponseMessage) responseThree.getEntity()).getMessage(); + assert !messageThree.contains("successfully reset"); + } + +} |