Improve test case for catalog database 75/28175/1
authorsubhash kumar singh <subhash.kumar.singh@huawei.com>
Mon, 15 Jan 2018 12:12:09 +0000 (12:12 +0000)
committersubhash kumar singh <subhash.kumar.singh@huawei.com>
Mon, 15 Jan 2018 12:12:09 +0000 (12:12 +0000)
Improve test case for catalog database.

Change-Id: I9938ca12edf2927368a79bffc21b199651f77879
Issue-ID: SO-360
Signed-off-by: subhash kumar singh <subhash.kumar.singh@huawei.com>
mso-catalog-db/src/main/java/org/openecomp/mso/db/catalog/CatalogDatabase.java
mso-catalog-db/src/test/java/org/openecomp/mso/db/catalog/test/CatalogDatabaseTest.java

index 926c483..e7491b7 100644 (file)
@@ -314,19 +314,18 @@ public class CatalogDatabase implements Closeable {
      * @return HeatEnvironment object or null if none found
      */    
     public HeatEnvironment getHeatEnvironmentByArtifactUuid(String artifactUuid) {
-        long startTime = System.currentTimeMillis ();
-        LOGGER.debug ("Catalog database - get Heat Environment with artifactUuid " + artifactUuid);
+        long startTime = System.currentTimeMillis();
+        LOGGER.debug("Catalog database - get Heat Environment with artifactUuid " + artifactUuid);
 
         String hql = "FROM HeatEnvironment WHERE artifactUuid = :artifactUuidValue";
         Query query = getSession().createQuery(hql);
-        query.setParameter ("artifactUuidValue", artifactUuid);
+        query.setParameter("artifactUuidValue", artifactUuid);
         HeatEnvironment environment = null;
         try {
-               environment = (HeatEnvironment) query.uniqueResult ();
+            environment = (HeatEnvironment) query.uniqueResult();
         } catch (org.hibernate.NonUniqueResultException nure) {
                LOGGER.debug("Non Unique Result Exception - the Catalog Database does not match a unique row for Envt - data integrity error: artifactUuid='" + artifactUuid +"'", nure);
                LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for heatEnvironment artifactUuid=" + artifactUuid, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for artifactUuid==" + artifactUuid);
-               environment = null;
         } catch (org.hibernate.HibernateException he) {
                LOGGER.debug("Hibernate Exception - while searching for envt: artifactUuid='" + artifactUuid + "' " + he.getMessage());
                LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " Hibernate exception searching envt for artifactUuid=" + artifactUuid, "", "", MsoLogger.ErrorCode.DataError, "Hibernate exception searching envt for artifactUuid=" + artifactUuid);
index 84e697b..705f64c 100644 (file)
@@ -468,9 +468,91 @@ public class CatalogDatabaseTest {
         List<HeatTemplateParam> htList = cd.getParametersForHeatTemplate("12l3");
     }
 
+    @Test
+    public void getHeatEnvironmentByArtifactUuidTest(){
+
+        MockUp<Query> mockUpQuery = new MockUp<Query>() {
+
+            @Mock
+            public Object uniqueResult() {
+                HeatEnvironment heatEnvironment = new HeatEnvironment();
+                heatEnvironment.setArtifactUuid("123-uuid");
+                return heatEnvironment;
+            }
+        };
+
+        MockUp<Session> mockedSession = new MockUp<Session>() {
+            @Mock
+            public Query createQuery(String hql) {
+                return mockUpQuery.getMockInstance();
+            }
+        };
+
+        new MockUp<CatalogDatabase>() {
+            @Mock
+            private Session getSession() {
+                return mockedSession.getMockInstance();
+            }
+        };
+
+        HeatEnvironment he = cd.getHeatEnvironmentByArtifactUuid("123");
+        assertEquals("123-uuid", he.getArtifactUuid());
+    }
+
+    @Test(expected = HibernateException.class)
+    public void getHeatEnvironmentByArtifactUuidHibernateExceptionTest(){
+
+        MockUp<Query> mockUpQuery = new MockUp<Query>() {
+
+            @Mock
+            public Object uniqueResult() {
+                throw new HibernateException("hibernate exception");
+            }
+        };
+
+        MockUp<Session> mockedSession = new MockUp<Session>() {
+            @Mock
+            public Query createQuery(String hql) {
+                return mockUpQuery.getMockInstance();
+            }
+        };
+
+        new MockUp<CatalogDatabase>() {
+            @Mock
+            private Session getSession() {
+                return mockedSession.getMockInstance();
+            }
+        };
+
+        HeatEnvironment he = cd.getHeatEnvironmentByArtifactUuid("123");
+    }
+
     @Test(expected = Exception.class)
-    public void getHeatEnvironmentByArtifactUuidTestException(){
-        HeatEnvironment ht = cd.getHeatEnvironmentByArtifactUuid("123");
+    public void getHeatEnvironmentByArtifactUuidExceptionTest(){
+
+        MockUp<Query> mockUpQuery = new MockUp<Query>() {
+
+            @Mock
+            public Object uniqueResult() throws Exception {
+                throw new Exception();
+            }
+        };
+
+        MockUp<Session> mockedSession = new MockUp<Session>() {
+            @Mock
+            public Query createQuery(String hql) {
+                return mockUpQuery.getMockInstance();
+            }
+        };
+
+        new MockUp<CatalogDatabase>() {
+            @Mock
+            private Session getSession() {
+                return mockedSession.getMockInstance();
+            }
+        };
+
+        HeatEnvironment he = cd.getHeatEnvironmentByArtifactUuid("123");
     }
 
     @Test(expected = Exception.class)