diff options
author | 2018-03-22 11:02:57 -0400 | |
---|---|---|
committer | 2018-03-26 10:56:24 -0400 | |
commit | e65fc2fc02594a7c8640a6330b2373db1efb923e (patch) | |
tree | 4433e083f96dcb1ad26c6f73d437195d6d8f7a75 /src/test/java/org/onap/dcae/inventory | |
parent | 7f61b70d33879712c5dad64c04a5d54b2b065138 (diff) |
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 <mhwang@research.att.com>
Issue-ID: DCAEGEN2-257
Diffstat (limited to 'src/test/java/org/onap/dcae/inventory')
-rw-r--r-- | src/test/java/org/onap/dcae/inventory/InventoryConfigurationTest.java | 61 | ||||
-rw-r--r-- | src/test/java/org/onap/dcae/inventory/LinkSerializerTest.java | 97 | ||||
-rw-r--r-- | src/test/java/org/onap/dcae/inventory/clients/DatabusControllerClientTest.java | 146 | ||||
-rw-r--r-- | src/test/java/org/onap/dcae/inventory/daos/DCAEServicesDAOTest.java | 84 | ||||
-rw-r--r-- | src/test/java/org/onap/dcae/inventory/exceptions/DatabusControllerClientExceptionTest.java | 34 | ||||
-rw-r--r-- | src/test/java/org/onap/dcae/inventory/exceptions/mappers/DBIExceptionMapperTests.java (renamed from src/test/java/org/onap/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java) | 2 |
6 files changed, 423 insertions, 1 deletions
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<InventoryConfiguration> 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<String> getRels() { + return null; + } + + @Override + public String getTitle() { + return "some-title"; + } + + @Override + public String getType() { + return null; + } + + @Override + public Map<String, String> 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<InventoryConfiguration.DatabusControllerConnectionConfiguration> 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/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/exception/mappers/DBIExceptionMapperTests.java b/src/test/java/org/onap/dcae/inventory/exceptions/mappers/DBIExceptionMapperTests.java index 7c04295..b0d5ba4 100644 --- a/src/test/java/org/onap/dcae/inventory/exception/mappers/DBIExceptionMapperTests.java +++ b/src/test/java/org/onap/dcae/inventory/exceptions/mappers/DBIExceptionMapperTests.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.dcae.inventory.exception.mappers; +package org.onap.dcae.inventory.exceptions.mappers; import org.onap.dcae.inventory.daos.InventoryDAOManager; import org.onap.dcae.inventory.exceptions.mappers.DBIExceptionMapper; |