diff options
Diffstat (limited to 'src/test/java')
15 files changed, 727 insertions, 27 deletions
diff --git a/src/test/java/io/swagger/api/BootstrapTest.java b/src/test/java/io/swagger/api/BootstrapTest.java new file mode 100644 index 0000000..7c86d8c --- /dev/null +++ b/src/test/java/io/swagger/api/BootstrapTest.java @@ -0,0 +1,43 @@ +/*- + * ============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 io.swagger.api; + +import org.junit.Test; + +import javax.servlet.ServletException; + +import static org.junit.Assert.fail; + +/** + * Created by mhwang on 3/22/18. + */ +public class BootstrapTest { + + @Test + public void testNoServletConfig() { + try { + (new Bootstrap()).init(null); + } catch(ServletException e) { + fail("This might be a valid failure. Should investigate."); + } + } + +} diff --git a/src/test/java/Util.java b/src/test/java/io/swagger/api/Util.java index 9607148..8f9e09f 100644 --- a/src/test/java/Util.java +++ b/src/test/java/io/swagger/api/Util.java @@ -1,8 +1,8 @@ -/*- +package io.swagger.api;/*- * ============LICENSE_START======================================================= * dcae-inventory * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-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. diff --git a/src/test/java/io/swagger/api/factories/DcaeServiceTypesApiServiceFactoryTest.java b/src/test/java/io/swagger/api/factories/DcaeServiceTypesApiServiceFactoryTest.java new file mode 100644 index 0000000..6b385df --- /dev/null +++ b/src/test/java/io/swagger/api/factories/DcaeServiceTypesApiServiceFactoryTest.java @@ -0,0 +1,37 @@ +/*- + * ============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 io.swagger.api.factories; + +import org.junit.Test; + +import static junit.framework.TestCase.assertNotNull; + +/** + * Created by mhwang on 3/22/18. + */ +public class DcaeServiceTypesApiServiceFactoryTest { + + @Test + public void testGetDcaeServiceTypesApi() { + assertNotNull(DcaeServiceTypesApiServiceFactory.getDcaeServiceTypesApi()); + } + +} diff --git a/src/test/java/io/swagger/api/factories/DcaeServicesApiServiceFactoryTest.java b/src/test/java/io/swagger/api/factories/DcaeServicesApiServiceFactoryTest.java new file mode 100644 index 0000000..d9c6808 --- /dev/null +++ b/src/test/java/io/swagger/api/factories/DcaeServicesApiServiceFactoryTest.java @@ -0,0 +1,37 @@ +/*- + * ============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 io.swagger.api.factories; + +import org.junit.Test; + +import static junit.framework.TestCase.assertNotNull; + +/** + * Created by mhwang on 3/22/18. + */ +public class DcaeServicesApiServiceFactoryTest { + + @Test + public void testGetDcaeServicesApi() { + assertNotNull(DcaeServicesApiServiceFactory.getDcaeServicesApi()); + } + +} diff --git a/src/test/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactoryTest.java b/src/test/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactoryTest.java new file mode 100644 index 0000000..033c8a6 --- /dev/null +++ b/src/test/java/io/swagger/api/factories/DcaeServicesGroupbyApiServiceFactoryTest.java @@ -0,0 +1,37 @@ +/*- + * ============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 io.swagger.api.factories; + +import org.junit.Test; + +import static junit.framework.TestCase.assertNotNull; + +/** + * Created by mhwang on 3/22/18. + */ +public class DcaeServicesGroupbyApiServiceFactoryTest { + + @Test + public void testGetDcaeServicesGroupbyApi() { + assertNotNull(DcaeServicesGroupbyApiServiceFactory.getDcaeServicesGroupbyApi()); + } + +} diff --git a/src/test/java/DcaeServiceTypesApiServiceImplTests.java b/src/test/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImplTests.java index 3882c53..4a47787 100644 --- a/src/test/java/DcaeServiceTypesApiServiceImplTests.java +++ b/src/test/java/io/swagger/api/impl/DcaeServiceTypesApiServiceImplTests.java @@ -1,8 +1,8 @@ -/*- +package io.swagger.api.impl;/*- * ============LICENSE_START======================================================= * dcae-inventory * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-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. @@ -23,7 +23,6 @@ import org.onap.dcae.inventory.daos.DCAEServicesDAO; import org.onap.dcae.inventory.daos.InventoryDAOManager; import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject; import org.onap.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; diff --git a/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplH2Tests.java b/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplH2Tests.java new file mode 100644 index 0000000..0a3b777 --- /dev/null +++ b/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplH2Tests.java @@ -0,0 +1,132 @@ +/*- + * ============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 io.swagger.api.impl; + +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.api.Util; +import io.swagger.model.DCAEServiceRequest; +import org.joda.time.DateTime; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.dcae.inventory.clients.DatabusControllerClient; +import org.onap.dcae.inventory.daos.*; +import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.skife.jdbi.v2.DBI; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.ws.rs.core.Response; +import javax.ws.rs.core.SecurityContext; +import javax.ws.rs.core.UriInfo; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Created by mhwang on 9/25/17. + * + * This test covers the gap in DcaeServicesApiServiceImplTests - testing the GET query interface + * The approach taken here is standing up an H2 in-memory database which is different from the + * original test which made an attempt to mock the database calls but ran into an impasse. The + * test here was not included in the original class because of conflicting setup operations. + */ +@PrepareForTest({InventoryDAOManager.class}) +@RunWith(PowerMockRunner.class) +public class DcaeServicesApiServiceImplH2Tests { + + private final static Logger LOG = LoggerFactory.getLogger(DcaeServicesApiServiceImplH2Tests.class); + + private DCAEServicesDAO mockServicesDAO = null; + private DCAEServiceComponentsDAO mockComponentsDAO = null; + private DCAEServicesComponentsMapsDAO mockComponentsMapsDAO = null; + + // https://stackoverflow.com/questions/35825383/how-to-test-jdbi-daos-with-h2-in-memory-database + // Caused by: java.lang.ClassNotFoundException: Unable to load class: org.h2.Driver from ClassLoader:sun.misc.Launcher$AppClassLoader@18b4aac2;ClassLoader:sun.misc.Launcher$AppClassLoader@18b4aac2 + 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" ); + mockServicesDAO = dbi.onDemand(DCAEServicesDAO.class); + mockComponentsDAO = dbi.onDemand(DCAEServiceComponentsDAO.class); + mockComponentsMapsDAO = dbi.onDemand(DCAEServicesComponentsMapsDAO.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.getHandle()).thenReturn(dbi.open()); + } + + @Test + public void testDcaeServicesGet () { + mockServicesDAO.createTable(); + mockComponentsDAO.createTable(); + mockComponentsMapsDAO.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); + mockServicesDAO.insert(so); + + DatabusControllerClient dbcc = mock(DatabusControllerClient.class); + DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc); + String typeId = "some-type-id"; + String vnfId = "some-vnf-id"; + String vnfType = "some-vnf-type"; + String vnfLocation = "some-vnf-location"; + String componentType = "some-component-type"; + Boolean shareable = Boolean.TRUE; + DateTime created = null; + Integer offset = 0; + UriInfo uriInfo = new Util.FakeUriInfo(); + SecurityContext securityContext = null; + Response response = api.dcaeServicesGet(typeId, vnfId, vnfType, vnfLocation, componentType, shareable, created, + offset, uriInfo, securityContext); + assertEquals(response.getStatus(), 200); + } + +} diff --git a/src/test/java/DcaeServicesApiServiceImplTests.java b/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplTests.java index 75e3e00..64b7f88 100644 --- a/src/test/java/DcaeServicesApiServiceImplTests.java +++ b/src/test/java/io/swagger/api/impl/DcaeServicesApiServiceImplTests.java @@ -1,8 +1,8 @@ -/*- +package io.swagger.api.impl;/*- * ============LICENSE_START======================================================= * dcae-inventory * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-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. @@ -19,13 +19,12 @@ */ import io.swagger.api.NotFoundException; -import io.swagger.api.impl.DcaeServicesApiServiceImpl; +import io.swagger.api.Util; import io.swagger.model.DCAEService; import io.swagger.model.DCAEServiceRequest; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.onap.dcae.inventory.clients.DCAEControllerClient; import org.onap.dcae.inventory.clients.DatabusControllerClient; import org.onap.dcae.inventory.daos.DCAEServiceComponentsDAO; import org.onap.dcae.inventory.daos.DCAEServiceTypesDAO; @@ -94,9 +93,8 @@ public class DcaeServicesApiServiceImplTests { fail("Failed to do the reflection trick to test the private method: createDCAEService"); } - DCAEControllerClient dcc = mock(DCAEControllerClient.class); DatabusControllerClient dbcc = mock(DatabusControllerClient.class); - DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dcc, dbcc); + DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc); DCAEServiceRequest serviceRequest = new DCAEServiceRequest(); serviceRequest.setTypeId("type-id-abc"); @@ -124,9 +122,8 @@ public class DcaeServicesApiServiceImplTests { when(mockServicesDAO.getByServiceId(DCAEServiceObject.DCAEServiceStatus.RUNNING, serviceId)).thenReturn(serviceObject); when(mockComponentsDAO.getByServiceId(serviceId)).thenReturn(new ArrayList<DCAEServiceComponentObject>()); - DCAEControllerClient dcc = mock(DCAEControllerClient.class); DatabusControllerClient dbcc = mock(DatabusControllerClient.class); - DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dcc, dbcc); + DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc); UriInfo uriInfo = new Util.FakeUriInfo(); try { @@ -153,9 +150,8 @@ public class DcaeServicesApiServiceImplTests { doReturn(null).when(mockQuery.bind(anyString(), any(DateTime.class))); when(mockQuery.bind(anyString(), anyInt())).thenReturn(null); - DCAEControllerClient dcc = mock(DCAEControllerClient.class); DatabusControllerClient dbcc = mock(DatabusControllerClient.class); - DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dcc, dbcc); + DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc); String typeId = "some-type-id"; String vnfId = "some-vnf-id"; @@ -165,7 +161,7 @@ public class DcaeServicesApiServiceImplTests { Boolean shareable = Boolean.TRUE; DateTime created = null; Integer offset = 0; - UriInfo uriInfo = new Util.FakeUriInfo(); + UriInfo uriInfo = new io.swagger.api.Util.FakeUriInfo(); SecurityContext securityContext = null; when(mockQuery.list()).thenReturn(new ArrayList<DCAEServiceObject>()); @@ -185,9 +181,8 @@ public class DcaeServicesApiServiceImplTests { when(mockTypesDAO.getByTypeIdActiveOnly(serviceRequest.getTypeId())).thenReturn(null); - DCAEControllerClient dcc = mock(DCAEControllerClient.class); DatabusControllerClient dbcc = mock(DatabusControllerClient.class); - DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dcc, dbcc); + DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc); UriInfo uriInfo = new Util.FakeUriInfo(); Response response = api.dcaeServicesServiceIdPut(serviceId, serviceRequest, uriInfo, null); @@ -203,9 +198,8 @@ public class DcaeServicesApiServiceImplTests { DCAEServiceObject serviceObject = new DCAEServiceObject(serviceId, serviceRequest); when(mockServicesDAO.getByServiceId(DCAEServiceObject.DCAEServiceStatus.RUNNING, serviceId)).thenReturn(serviceObject); - DCAEControllerClient dcc = mock(DCAEControllerClient.class); DatabusControllerClient dbcc = mock(DatabusControllerClient.class); - DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dcc, dbcc); + DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc); UriInfo uriInfo = new Util.FakeUriInfo(); try { @@ -224,9 +218,8 @@ public class DcaeServicesApiServiceImplTests { String serviceId = "service-id-123"; when(mockServicesDAO.getByServiceId(DCAEServiceObject.DCAEServiceStatus.RUNNING, serviceId)).thenReturn(null); - DCAEControllerClient dcc = mock(DCAEControllerClient.class); DatabusControllerClient dbcc = mock(DatabusControllerClient.class); - DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dcc, dbcc); + DcaeServicesApiServiceImpl api = new DcaeServicesApiServiceImpl(dbcc); UriInfo uriInfo = new Util.FakeUriInfo(); try { diff --git a/src/test/java/DcaeServicesGroupbyApiServiceImplTests.java b/src/test/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImplTests.java index 7ca8200..0ae2c3a 100644 --- a/src/test/java/DcaeServicesGroupbyApiServiceImplTests.java +++ b/src/test/java/io/swagger/api/impl/DcaeServicesGroupbyApiServiceImplTests.java @@ -1,8 +1,8 @@ -/*- +package io.swagger.api.impl;/*- * ============LICENSE_START======================================================= * dcae-inventory * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-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. @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -import io.swagger.api.impl.DcaeServicesGroupbyApiServiceImpl; +import io.swagger.api.Util; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -70,7 +70,7 @@ public class DcaeServicesGroupbyApiServiceImplTests { DcaeServicesGroupbyApiServiceImpl api = new DcaeServicesGroupbyApiServiceImpl(); String propertyName = "type"; - UriInfo uriInfo = new Util.FakeUriInfo(); + UriInfo uriInfo = new io.swagger.api.Util.FakeUriInfo(); Handle mockHandle = mock(Handle.class); when(InventoryDAOManager.getInstance().getHandle()).thenReturn(mockHandle); 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; |