From f758bb0ae798a53eee005b3e6e4a115f84b4ae19 Mon Sep 17 00:00:00 2001 From: Michael Hwang Date: Fri, 19 Jan 2018 10:43:43 -0500 Subject: Change package from openecomp to onap Change-Id: I2a79eb123e5fbb6a6c2f0e72030d6781fbbd7513 Issue-ID: DCAEGEN2-225 Signed-off-by: Michael Hwang --- .../exception/mappers/DBIExceptionMapperTests.java | 117 +++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 src/test/java/org/onap/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java (limited to 'src/test/java/org/onap/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java') diff --git a/src/test/java/org/onap/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java b/src/test/java/org/onap/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java new file mode 100644 index 0000000..7c04295 --- /dev/null +++ b/src/test/java/org/onap/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.onap.dcae.inventory.exception.mappers; + +import org.onap.dcae.inventory.daos.InventoryDAOManager; +import org.onap.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