From bde8e855fa7177a8b230008b936f5457c5e5d94a Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Wed, 17 Jan 2018 12:03:49 +0000 Subject: Improve UT for catalog db Improve UT for catalog db. Change-Id: If3c86e76b36c69bd99c1f6536fb3f2d787e6ed0d Issue-ID: SO-360 Signed-off-by: subhash kumar singh --- .../openecomp/mso/db/catalog/CatalogDatabase.java | 14 +-- .../mso/db/catalog/test/CatalogDatabaseTest.java | 106 ++++++++++++++++++++- 2 files changed, 111 insertions(+), 9 deletions(-) (limited to 'mso-catalog-db') 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 079d105dd4..af4d28d3ba 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 @@ -759,22 +759,22 @@ public class CatalogDatabase implements Closeable { * Return the newest version of a specific VNF resource (queried by Name). * * @param vnfType - * @param version + * @param serviceVersion * @return VnfResource object or null if none found */ public VnfResource getVnfResource (String vnfType, String serviceVersion) { - long startTime = System.currentTimeMillis (); - LOGGER.debug ("Catalog database - get VNF resource with model_name " + vnfType + " and version=" + serviceVersion); + long startTime = System.currentTimeMillis(); + LOGGER.debug("Catalog database - get VNF resource with model_name " + vnfType + " and version=" + serviceVersion); String hql = "FROM VnfResource WHERE modelName = :vnfName and version = :serviceVersion"; - Query query = getSession ().createQuery (hql); - query.setParameter ("vnfName", vnfType); - query.setParameter ("serviceVersion", serviceVersion); + Query query = getSession().createQuery(hql); + query.setParameter("vnfName", vnfType); + query.setParameter("serviceVersion", serviceVersion); VnfResource resource = null; try { - resource = (VnfResource) query.uniqueResult (); + resource = (VnfResource) 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: vnfType='" + vnfType + "', serviceVersion='" + serviceVersion + "'"); LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for vnfType=" + vnfType + " and serviceVersion=" + serviceVersion, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for vnfType=" + vnfType); 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 cad4093936..ee12fe4eba 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 @@ -1080,9 +1080,111 @@ public class CatalogDatabaseTest { assertEquals(null, vnfResource); } + @Test + public void getVnfResourceByTypeTest() { + MockUp mockUpQuery = new MockUp() { + + @Mock + public Object uniqueResult() { + VnfResource vnfResource = new VnfResource(); + vnfResource.setModelUuid("123-uuid"); + return vnfResource; + } + }; + + MockUp mockedSession = new MockUp() { + @Mock + public Query createQuery(String hql) { + return mockUpQuery.getMockInstance(); + } + }; + + new MockUp() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + VnfResource vnfResource = cd.getVnfResource("vnf","3992"); + assertEquals("123-uuid", vnfResource.getModelUuid()); + } + + @Test(expected = NonUniqueResultException.class) + public void getVnfResourceNURExceptionTest() { + 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(); + } + }; + VnfResource vnfResource = cd.getVnfResource("vnf","3992"); + } + + @Test(expected = HibernateException.class) + public void getVnfResourceHibernateExceptionTest() { + 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(); + } + }; + VnfResource vnfResource = cd.getVnfResource("vnf","3992"); + } + @Test(expected = Exception.class) - public void getVnfResource2TestException() throws Exception{ - VnfResource ht = cd.getVnfResource("vnf","3992"); + public void getVnfResourceExceptionTest() { + 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(); + } + }; + VnfResource vnfResource = cd.getVnfResource("vnf","3992"); } @Test(expected = Exception.class) -- cgit 1.2.3-korg