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 --- .../dcae/inventory/InventoryConfigurationTest.java | 61 +++++++++ .../onap/dcae/inventory/LinkSerializerTest.java | 97 ++++++++++++++ .../clients/DatabusControllerClientTest.java | 146 +++++++++++++++++++++ .../dcae/inventory/daos/DCAEServicesDAOTest.java | 84 ++++++++++++ .../exception/mappers/DBIExceptionMapperTests.java | 117 ----------------- .../DatabusControllerClientExceptionTest.java | 34 +++++ .../mappers/DBIExceptionMapperTests.java | 117 +++++++++++++++++ 7 files changed, 539 insertions(+), 117 deletions(-) create mode 100644 src/test/java/org/onap/dcae/inventory/InventoryConfigurationTest.java create mode 100644 src/test/java/org/onap/dcae/inventory/LinkSerializerTest.java create mode 100644 src/test/java/org/onap/dcae/inventory/clients/DatabusControllerClientTest.java create mode 100644 src/test/java/org/onap/dcae/inventory/daos/DCAEServicesDAOTest.java delete mode 100644 src/test/java/org/onap/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java create mode 100644 src/test/java/org/onap/dcae/inventory/exceptions/DatabusControllerClientExceptionTest.java create mode 100644 src/test/java/org/onap/dcae/inventory/exceptions/mappers/DBIExceptionMapperTests.java (limited to 'src/test/java/org/onap') diff --git a/src/test/java/org/onap/dcae/inventory/InventoryConfigurationTest.java b/src/test/java/org/onap/dcae/inventory/InventoryConfigurationTest.java new file mode 100644 index 0000000..f9dadb8 --- /dev/null +++ b/src/test/java/org/onap/dcae/inventory/InventoryConfigurationTest.java @@ -0,0 +1,61 @@ +/*- + * ============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 org.onap.dcae.inventory; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.dropwizard.configuration.YamlConfigurationFactory; +import io.dropwizard.jackson.Jackson; +import io.dropwizard.jersey.validation.Validators; +import org.junit.Test; + +import javax.validation.Validator; +import java.io.File; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * Created by mhwang on 3/22/18. + */ +public class InventoryConfigurationTest { + + final private ObjectMapper objectMapper = Jackson.newObjectMapper(); + final private Validator validator = Validators.newValidator(); + final private YamlConfigurationFactory factory + = new YamlConfigurationFactory<>(InventoryConfiguration.class, validator, objectMapper, "dw"); + + @Test + public void testInventoryConfigurationLoad() { + try { + final File yaml = new File(Thread.currentThread().getContextClassLoader().getResource("config-inventory.yaml").getPath()); + InventoryConfiguration configuration = factory.build(yaml); + + assertEquals(configuration.getDatabusControllerConnection().getHost(), "databus-controller-hostname"); + assertEquals((long) configuration.getDatabusControllerConnection().getPort(), 8443); + assertEquals(configuration.getDatabusControllerConnection().getRequired(), true); + + assertEquals(configuration.getDataSourceFactory().getUrl(), "jdbc:postgresql://127.0.0.1:5432/dcae_inv"); + } catch(Exception e) { + fail("Failed to load config-inventory"); + } + } + +} diff --git a/src/test/java/org/onap/dcae/inventory/LinkSerializerTest.java b/src/test/java/org/onap/dcae/inventory/LinkSerializerTest.java new file mode 100644 index 0000000..81b0554 --- /dev/null +++ b/src/test/java/org/onap/dcae/inventory/LinkSerializerTest.java @@ -0,0 +1,97 @@ +/*- + * ============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 org.onap.dcae.inventory; + +import com.fasterxml.jackson.core.JsonGenerator; +import org.junit.Test; + +import javax.ws.rs.core.Link; +import javax.ws.rs.core.UriBuilder; + +import java.net.URI; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; + +/** + * Created by mhwang on 3/25/18. + */ +public class LinkSerializerTest { + + @Test + public void testSerialize() { + JsonGenerator jg = mock(JsonGenerator.class); + + try { + Link link = new Link() { + @Override + public URI getUri() { + try { + return new URI("http://localhost/some-title"); + } catch (Exception e) { + return null; + } + } + + @Override + public UriBuilder getUriBuilder() { + return null; + } + + @Override + public String getRel() { + return "self"; + } + + @Override + public List getRels() { + return null; + } + + @Override + public String getTitle() { + return "some-title"; + } + + @Override + public String getType() { + return null; + } + + @Override + public Map getParams() { + return null; + } + + @Override + public String toString() { + return null; + } + }; + (new LinkSerializer()).serialize(link, jg, null); + } catch (Exception e) { + fail("Failed to serialze link"); + } + } + +} diff --git a/src/test/java/org/onap/dcae/inventory/clients/DatabusControllerClientTest.java b/src/test/java/org/onap/dcae/inventory/clients/DatabusControllerClientTest.java new file mode 100644 index 0000000..ae2f2ff --- /dev/null +++ b/src/test/java/org/onap/dcae/inventory/clients/DatabusControllerClientTest.java @@ -0,0 +1,146 @@ +/*- + * ============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 org.onap.dcae.inventory.clients; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.dropwizard.configuration.ConfigurationException; +import io.dropwizard.configuration.YamlConfigurationFactory; +import io.dropwizard.jackson.Jackson; +import io.dropwizard.jersey.validation.Validators; +import org.junit.Before; +import org.junit.Test; +import org.onap.dcae.inventory.InventoryConfiguration; +import org.onap.dcae.inventory.exceptions.DatabusControllerClientException; + +import javax.validation.Validator; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.powermock.api.mockito.PowerMockito.when; + +/** + * Created by mhwang on 3/22/18. + */ +public class DatabusControllerClientTest { + + final private ObjectMapper objectMapper = Jackson.newObjectMapper(); + final private Validator validator = Validators.newValidator(); + final private YamlConfigurationFactory factory + = new YamlConfigurationFactory<>(InventoryConfiguration.DatabusControllerConnectionConfiguration.class, validator, objectMapper, "dw"); + + private InventoryConfiguration.DatabusControllerConnectionConfiguration configuration = null; + + @Before + public void setupClass() throws IOException, ConfigurationException { + final File yaml = new File(Thread.currentThread().getContextClassLoader().getResource("config-databus.yaml").getPath()); + this.configuration = factory.build(yaml); + } + + @Test + public void testConstructResourceURI() { + DatabusControllerClient client = new DatabusControllerClient(null, this.configuration); + URI uri = client.constructResourceURI("/some-path"); + assertEquals(uri.toString(), "https://databus-controller-hostname:8443/some-path"); + } + + private DatabusControllerClient setupForIsExists(Response mockResponse) { + Client mockClient = mock(Client.class); + WebTarget mockWebTarget = mock(WebTarget.class); + Invocation.Builder mockBuilder = mock(Invocation.Builder.class); + + try { + URI uri = new URI("https://databus-controller-hostname:8443/some-component-id"); + when(mockClient.target(uri)).thenReturn(mockWebTarget); + //when(mockClient.target(new URI(any()))).thenReturn(mockWebTarget); + } catch(URISyntaxException e) { + fail("URI syntax error"); + } + + when(mockWebTarget.request(MediaType.APPLICATION_JSON_TYPE)).thenReturn(mockBuilder); + when(mockBuilder.header(any(), any())).thenReturn(mockBuilder); + + when(mockBuilder.get()).thenReturn(mockResponse); + return new DatabusControllerClient(mockClient, this.configuration); + + } + + @Test + public void testIsExists() { + Response mockResponse = mock(Response.class); + when(mockResponse.getStatus()).thenReturn(200); + InputStream stream = new ByteArrayInputStream("{}".getBytes(StandardCharsets.UTF_8)); + when(mockResponse.getEntity()).thenReturn(stream); + + DatabusControllerClient client = setupForIsExists(mockResponse); + + try { + assertEquals(client.isExists("some-component-id"), false); + } catch(Exception e) { + fail("Unexpected exception"); + } + } + + private void testIsExistsErrors(int statusError) { + Response mockResponse = mock(Response.class); + when(mockResponse.getStatus()).thenReturn(statusError); + + DatabusControllerClient client = setupForIsExists(mockResponse); + + try { + client.isExists("some-component-id"); + fail("This was supposed to be a fail case. Exception not thrown."); + } catch(DatabusControllerClientException e) { + // Expected exception + } catch(Exception e) { + fail("Unexpected exception"); + } + } + + @Test + public void testIsExists401() { + testIsExistsErrors(401); + } + + @Test + public void testIsExists403() { + testIsExistsErrors(403); + } + + @Test + public void testIsExists500() { + testIsExistsErrors(500); + } + +} diff --git a/src/test/java/org/onap/dcae/inventory/daos/DCAEServicesDAOTest.java b/src/test/java/org/onap/dcae/inventory/daos/DCAEServicesDAOTest.java new file mode 100644 index 0000000..af05704 --- /dev/null +++ b/src/test/java/org/onap/dcae/inventory/daos/DCAEServicesDAOTest.java @@ -0,0 +1,84 @@ +/*- + * ============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 org.onap.dcae.inventory.daos; + +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.model.DCAEServiceRequest; +import org.junit.Before; +import org.junit.Test; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject; +import org.skife.jdbi.v2.DBI; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** + * Created by mhwang on 3/25/18. + */ +public class DCAEServicesDAOTest { + + private DCAEServicesDAO fooDAO; + + // Learned about the H2 approach from here: + // https://stackoverflow.com/questions/35825383/how-to-test-jdbi-daos-with-h2-in-memory-database + 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" ); + fooDAO = dbi.onDemand(DCAEServicesDAO.class); + } + + @Test + public void testSaveAndGet() { + try { + fooDAO.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); + fooDAO.insert(so); + + DCAEServiceObject target = fooDAO.getByServiceId("some-service-id"); + assertEquals(target.getServiceId(), so.getServiceId()); + } catch(Exception e) { + fail("Failure at some point in this compound test."); + } + } +} 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 deleted file mode 100644 index 7c04295..0000000 --- a/src/test/java/org/onap/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java +++ /dev/null @@ -1,117 +0,0 @@ -/*- - * ============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"); - } - -} diff --git a/src/test/java/org/onap/dcae/inventory/exceptions/DatabusControllerClientExceptionTest.java b/src/test/java/org/onap/dcae/inventory/exceptions/DatabusControllerClientExceptionTest.java new file mode 100644 index 0000000..fcde16e --- /dev/null +++ b/src/test/java/org/onap/dcae/inventory/exceptions/DatabusControllerClientExceptionTest.java @@ -0,0 +1,34 @@ +/*- + * ============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 org.onap.dcae.inventory.exceptions; + +import org.junit.Test; + +/** + * Created by mhwang on 3/26/18. + */ +public class DatabusControllerClientExceptionTest { + + @Test + public void testBasicConstructor() { + new DatabusControllerClientException(new RuntimeException("Boo")); + } +} diff --git a/src/test/java/org/onap/dcae/inventory/exceptions/mappers/DBIExceptionMapperTests.java b/src/test/java/org/onap/dcae/inventory/exceptions/mappers/DBIExceptionMapperTests.java new file mode 100644 index 0000000..b0d5ba4 --- /dev/null +++ b/src/test/java/org/onap/dcae/inventory/exceptions/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.exceptions.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