From ac853c1e7186b10e34e392918af31e4ac62b45c2 Mon Sep 17 00:00:00 2001 From: Michael Hwang Date: Tue, 14 Feb 2017 15:10:16 +0000 Subject: Make first commit Transferring from original dcae project Issue-Id: DCAEGEN2-45 Change-Id: If8500f4a120ec2d27a714c8917f163beb57ee64c Signed-off-by: Michael Hwang --- .../inventory/daos/InventoryDAOManagerTests.java | 44 ++++++++ .../exception/mappers/DBIExceptionMapperTests.java | 117 +++++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 src/test/java/org/openecomp/dcae/inventory/daos/InventoryDAOManagerTests.java create mode 100644 src/test/java/org/openecomp/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java (limited to 'src/test/java/org') 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"); + } + +} -- cgit 1.2.3-korg