From 3412ea42a4528abeb47db49d573e6ea1a9754ade Mon Sep 17 00:00:00 2001 From: Michael Hwang Date: Mon, 2 Oct 2017 19:40:06 -0400 Subject: Add more unit tests Change-Id: Ib8e586a5aa7d31756751776015bbb76f68a0cafc Issue-Id: DCAEGEN2-60 Signed-off-by: Michael Hwang --- .../inventory/daos/DCAEServiceTransactionDAO.java | 6 +- .../daos/DCAEServiceTransactionDAOTests.java | 85 ++++++++++++++++ .../DCAEServiceComponentObjectMapperTests.java | 48 +++++++++ .../mappers/DCAEServiceObjectMapperTests.java | 50 ++++++++++ .../mappers/DCAEServiceTypeObjectMapperTests.java | 109 +++++++++++++++++++++ .../providers/NotFoundExceptionMapperTests.java | 42 ++++++++ 6 files changed, 337 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/openecomp/dcae/inventory/dbthings/daos/DCAEServiceTransactionDAOTests.java create mode 100644 src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapperTests.java create mode 100644 src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapperTests.java create mode 100644 src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapperTests.java create mode 100644 src/test/java/org/openecomp/dcae/inventory/providers/NotFoundExceptionMapperTests.java diff --git a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTransactionDAO.java b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTransactionDAO.java index f0fca86..c9d6dc8 100644 --- a/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTransactionDAO.java +++ b/src/main/java/org/openecomp/dcae/inventory/daos/DCAEServiceTransactionDAO.java @@ -120,13 +120,13 @@ public abstract class DCAEServiceTransactionDAO { } @CreateSqlObject - abstract DCAEServicesDAO getServicesDAO(); + abstract public DCAEServicesDAO getServicesDAO(); @CreateSqlObject - abstract DCAEServicesComponentsMapsDAO getServicesComponentsMappingDAO(); + abstract public DCAEServicesComponentsMapsDAO getServicesComponentsMappingDAO(); @CreateSqlObject - abstract DCAEServiceComponentsDAO getComponentsDAO(); + abstract public DCAEServiceComponentsDAO getComponentsDAO(); @Transaction public void insert(DCAEServiceTransactionContext context) { diff --git a/src/test/java/org/openecomp/dcae/inventory/dbthings/daos/DCAEServiceTransactionDAOTests.java b/src/test/java/org/openecomp/dcae/inventory/dbthings/daos/DCAEServiceTransactionDAOTests.java new file mode 100644 index 0000000..2a909f4 --- /dev/null +++ b/src/test/java/org/openecomp/dcae/inventory/dbthings/daos/DCAEServiceTransactionDAOTests.java @@ -0,0 +1,85 @@ +/*- + * ============LICENSE_START======================================================= + * dcae-inventory + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.dcae.inventory.dbthings.daos; + +import org.joda.time.DateTime; +import org.junit.Test; +import org.openecomp.dcae.inventory.daos.DCAEServiceComponentsDAO; +import org.openecomp.dcae.inventory.daos.DCAEServiceTransactionDAO; +import org.openecomp.dcae.inventory.daos.DCAEServicesComponentsMapsDAO; +import org.openecomp.dcae.inventory.daos.DCAEServicesDAO; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; + +/** + * Created by mhwang on 10/2/17. + */ +public class DCAEServiceTransactionDAOTests { + + @Test + public void testCreateDCAEServiceTransactionContext() { + String serviceId = "service-foo"; + DateTime modifiedTime = new DateTime(); + DCAEServiceTransactionDAO.DCAEServiceTransactionContext context + = new DCAEServiceTransactionDAO.DCAEServiceTransactionContext(serviceId, modifiedTime); + + assertEquals(context.getServiceId(), serviceId); + assertEquals(context.getModified(), modifiedTime); + } + + @Test + public void testInsert() { + String serviceId = "service-foo"; + DateTime modifiedTime = new DateTime(); + DCAEServiceTransactionDAO.DCAEServiceTransactionContext context + = new DCAEServiceTransactionDAO.DCAEServiceTransactionContext(serviceId, modifiedTime); + + DCAEServicesDAO servicesDAO = mock(DCAEServicesDAO.class); + DCAEServicesComponentsMapsDAO componentsMapsDAO = mock(DCAEServicesComponentsMapsDAO.class); + DCAEServiceComponentsDAO componentsDAO = mock(DCAEServiceComponentsDAO.class); + + DCAEServiceTransactionDAO transactionDAO = new DCAEServiceTransactionDAO() { + + public DCAEServicesDAO getServicesDAO() { + return servicesDAO; + } + + public DCAEServicesComponentsMapsDAO getServicesComponentsMappingDAO() { + return componentsMapsDAO; + } + + public DCAEServiceComponentsDAO getComponentsDAO() { + return componentsDAO; + } + }; + + try { + transactionDAO.insert(context); + assertTrue(true); + } catch(Exception e) { + fail("Unexpected error"); + } + + } +} diff --git a/src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapperTests.java b/src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapperTests.java new file mode 100644 index 0000000..cb3c7f1 --- /dev/null +++ b/src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceComponentObjectMapperTests.java @@ -0,0 +1,48 @@ +/*- + * ============LICENSE_START======================================================= + * dcae-inventory + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.dcae.inventory.dbthings.mappers; + +import org.junit.Test; + +import java.sql.ResultSet; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; + +/** + * Created by mhwang on 10/2/17. + */ +public class DCAEServiceComponentObjectMapperTests { + + @Test + public void testMap() { + ResultSet resultSet = mock(ResultSet.class); + DCAEServiceComponentObjectMapper mapper = new DCAEServiceComponentObjectMapper(); + + try { + assertNotNull(mapper.map(0, resultSet, null)); + } catch(Exception e) { + fail("Unexpected exception"); + } + } + +} diff --git a/src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapperTests.java b/src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapperTests.java new file mode 100644 index 0000000..1946343 --- /dev/null +++ b/src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceObjectMapperTests.java @@ -0,0 +1,50 @@ +/*- + * ============LICENSE_START======================================================= + * dcae-inventory + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.dcae.inventory.dbthings.mappers; + +import org.junit.Test; + +import java.sql.ResultSet; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Created by mhwang on 10/2/17. + */ +public class DCAEServiceObjectMapperTests { + + @Test + public void testMap() { + ResultSet resultSet = mock(ResultSet.class); + DCAEServiceObjectMapper mapper = new DCAEServiceObjectMapper(); + + try { + when(resultSet.getString("status")).thenReturn("RUNNING"); + assertNotNull(mapper.map(0, resultSet, null)); + } catch(Exception e) { + fail("Unexpected exception"); + } + } + +} diff --git a/src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapperTests.java b/src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapperTests.java new file mode 100644 index 0000000..f8c2428 --- /dev/null +++ b/src/test/java/org/openecomp/dcae/inventory/dbthings/mappers/DCAEServiceTypeObjectMapperTests.java @@ -0,0 +1,109 @@ +/*- + * ============LICENSE_START======================================================= + * dcae-inventory + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.dcae.inventory.dbthings.mappers; + +import org.junit.Test; + +import java.sql.Array; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Map; + +import static junit.framework.TestCase.assertNotNull; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.powermock.api.mockito.PowerMockito.when; + +/** + * Created by mhwang on 10/2/17. + */ +public class DCAEServiceTypeObjectMapperTests { + + @Test + public void testMap() { + ResultSet resultSet = mock(ResultSet.class); + DCAEServiceTypeObjectMapper mapper = new DCAEServiceTypeObjectMapper(); + + try { + when(resultSet.getArray(anyString())).thenReturn(new Array() { + @Override + public String getBaseTypeName() throws SQLException { + return null; + } + + @Override + public int getBaseType() throws SQLException { + return 0; + } + + @Override + public Object getArray() throws SQLException { + return new String[10]; + } + + @Override + public Object getArray(Map> map) throws SQLException { + return null; + } + + @Override + public Object getArray(long index, int count) throws SQLException { + return null; + } + + @Override + public Object getArray(long index, int count, Map> map) throws SQLException { + return null; + } + + @Override + public ResultSet getResultSet() throws SQLException { + return null; + } + + @Override + public ResultSet getResultSet(Map> map) throws SQLException { + return null; + } + + @Override + public ResultSet getResultSet(long index, int count) throws SQLException { + return null; + } + + @Override + public ResultSet getResultSet(long index, int count, Map> map) throws SQLException { + return null; + } + + @Override + public void free() throws SQLException { + + } + }); + assertNotNull(mapper.map(0, resultSet, null)); + } catch(Exception e) { + fail("Unexpected exception"); + } + } + +} diff --git a/src/test/java/org/openecomp/dcae/inventory/providers/NotFoundExceptionMapperTests.java b/src/test/java/org/openecomp/dcae/inventory/providers/NotFoundExceptionMapperTests.java new file mode 100644 index 0000000..083b237 --- /dev/null +++ b/src/test/java/org/openecomp/dcae/inventory/providers/NotFoundExceptionMapperTests.java @@ -0,0 +1,42 @@ +/*- + * ============LICENSE_START======================================================= + * dcae-inventory + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.dcae.inventory.providers; + +import io.swagger.api.NotFoundException; +import org.junit.Test; + +import javax.ws.rs.core.Response; + +import static org.junit.Assert.assertEquals; + +/** + * Created by mhwang on 10/2/17. + */ +public class NotFoundExceptionMapperTests { + + @Test + public void testNotFoundExceptionMapper() { + NotFoundExceptionMapper mapper = new NotFoundExceptionMapper(); + Response response = mapper.toResponse(new NotFoundException(100, "Some error message")); + assertEquals(response.getStatus(), 404); + } + +} -- cgit 1.2.3-korg