From 0bff9b79e7481f039db4b75734f9b6a363c4a612 Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Fri, 19 Jan 2018 09:05:02 +0000 Subject: Improve UT for catalog db Improve UT for catalog db Change-Id: Ib9c5dad03c0107778c554c54d1586e0ddd79c297 Issue-ID: SO-360 Signed-off-by: subhash kumar singh --- .../openecomp/mso/db/catalog/CatalogDatabase.java | 18 ++-- .../mso/db/catalog/test/CatalogDatabaseTest.java | 111 ++++++++++++++++++++- 2 files changed, 117 insertions(+), 12 deletions(-) diff --git a/mso-catalog-db/src/main/java/org/openecomp/mso/db/catalog/CatalogDatabase.java b/mso-catalog-db/src/main/java/org/openecomp/mso/db/catalog/CatalogDatabase.java index 0efcb484cb..0048a34d6c 100644 --- a/mso-catalog-db/src/main/java/org/openecomp/mso/db/catalog/CatalogDatabase.java +++ b/mso-catalog-db/src/main/java/org/openecomp/mso/db/catalog/CatalogDatabase.java @@ -982,19 +982,19 @@ public class CatalogDatabase implements Closeable { return resultList.get(0); } - public VfModule getVfModuleModelName (String modelName, String model_version) { + public VfModule getVfModuleModelName(String modelName, String model_version) { - long startTime = System.currentTimeMillis (); - LOGGER.debug ("Catalog database - get vfModuleModelName with type='" + modelName + "' and asdc_service_model_version='" + model_version + "'"); + long startTime = System.currentTimeMillis(); + LOGGER.debug("Catalog database - get vfModuleModelName with type='" + modelName + "' and asdc_service_model_version='" + model_version + "'"); String hql = "FROM VfModule WHERE Name = :model_name and version = :model_version"; - Query query = getSession ().createQuery (hql); - query.setParameter ("modelName", modelName); - query.setParameter ("model_version", model_version); + Query query = getSession().createQuery(hql); + query.setParameter("modelName", modelName); + query.setParameter("model_version", model_version); VfModule module = null; try { - module = (VfModule) query.uniqueResult (); + module = (VfModule) query.uniqueResult(); } catch (org.hibernate.NonUniqueResultException nure) { LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row - data integrity error: type='" + modelName + "', asdc_service_model_version='" + model_version + "'"); LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for type=" + modelName + " and version=" + model_version, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for type=" + modelName); @@ -1012,9 +1012,9 @@ public class CatalogDatabase implements Closeable { throw e; } if (module == null) { - LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleModelName", null); + LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleModelName", null); } else { - LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleModelName", null); + LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleModelName", null); } return module; } diff --git a/mso-catalog-db/src/test/java/org/openecomp/mso/db/catalog/test/CatalogDatabaseTest.java b/mso-catalog-db/src/test/java/org/openecomp/mso/db/catalog/test/CatalogDatabaseTest.java index 2ec75d7582..9ea1ef8bd4 100644 --- a/mso-catalog-db/src/test/java/org/openecomp/mso/db/catalog/test/CatalogDatabaseTest.java +++ b/mso-catalog-db/src/test/java/org/openecomp/mso/db/catalog/test/CatalogDatabaseTest.java @@ -1497,11 +1497,116 @@ public class CatalogDatabaseTest { assertEquals(null, vfModule); } - @Test(expected = Exception.class) - public void getVfModuleModelName2TestException(){ - VfModule vnf = cd.getVfModuleModelName("tetes","4kidsl"); + @Test + public void getVfModuleModelNameTest() { + MockUp mockUpQuery = new MockUp() { + + @Mock + public Object uniqueResult() { + VfModule vfModule = new VfModule(); + vfModule.setModelUUID("123-uuid"); + return vfModule; + } + }; + + MockUp mockedSession = new MockUp() { + @Mock + public Query createQuery(String hql) { + return mockUpQuery.getMockInstance(); + } + }; + + new MockUp() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + + VfModule vfModule = cd.getVfModuleModelName("tetes","4kidsl"); + assertEquals("123-uuid", vfModule.getModelUUID()); + } + + @Test(expected = NonUniqueResultException.class) + public void getVfModuleModelNameNURExceptionTest() { + MockUp mockUpQuery = new MockUp() { + + @Mock + public Object uniqueResult() { + throw new NonUniqueResultException(-1); + } + }; + + MockUp mockedSession = new MockUp() { + @Mock + public Query createQuery(String hql) { + return mockUpQuery.getMockInstance(); + } + }; + + new MockUp() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + + VfModule vfModule = cd.getVfModuleModelName("tetes","4kidsl"); + } + + @Test(expected = HibernateException.class) + public void getVfModuleModelNameHibernateExceptionTest() { + MockUp mockUpQuery = new MockUp() { + + @Mock + public Object uniqueResult() { + throw new HibernateException("hibernate exception"); + } + }; + + MockUp mockedSession = new MockUp() { + @Mock + public Query createQuery(String hql) { + return mockUpQuery.getMockInstance(); + } + }; + + new MockUp() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + + VfModule vfModule = cd.getVfModuleModelName("tetes","4kidsl"); } + @Test(expected = Exception.class) + public void getVfModuleModelNameGenericExceptionTest() { + MockUp mockUpQuery = new MockUp() { + + @Mock + public Object uniqueResult() throws Exception { + throw new Exception(); + } + }; + + MockUp mockedSession = new MockUp() { + @Mock + public Query createQuery(String hql) { + return mockUpQuery.getMockInstance(); + } + }; + + new MockUp() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + + VfModule vfModule = cd.getVfModuleModelName("tetes","4kidsl"); + } @Test(expected = Exception.class) public void ggetVfModuleCustomizationByModelNameTestException(){ VfModuleCustomization vnf = cd.getVfModuleCustomizationByModelName("tetes"); -- cgit 1.2.3-korg