Improve UT for catalog db 91/28391/1
authorsubhash kumar singh <subhash.kumar.singh@huawei.com>
Wed, 17 Jan 2018 11:43:47 +0000 (11:43 +0000)
committersubhash kumar singh <subhash.kumar.singh@huawei.com>
Wed, 17 Jan 2018 11:43:47 +0000 (11:43 +0000)
Impove UT for catalog db.

Change-Id: Iacbcb9de5ec92f929bfce8cfaac298e4a9ccf8b9
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 ed30e61..d1e91f3 100644 (file)
@@ -651,30 +651,30 @@ public class CatalogDatabase implements Closeable {
         return resultList.get(0);
     }
 
-    public List<ServiceRecipe> getServiceRecipes (String serviceModelUuid) {
+    public List<ServiceRecipe> getServiceRecipes(String serviceModelUuid) {
 
         StringBuilder hql;
 
-               hql = new StringBuilder ("FROM ServiceRecipe WHERE serviceModelUUID = :serviceModelUUID");
+        hql = new StringBuilder("FROM ServiceRecipe WHERE serviceModelUUID = :serviceModelUUID");
 
-        long startTime = System.currentTimeMillis ();
-        LOGGER.debug ("Catalog database - get Service recipe with serviceModelUUID " + serviceModelUuid);
+        long startTime = System.currentTimeMillis();
+        LOGGER.debug("Catalog database - get Service recipe with serviceModelUUID " + serviceModelUuid);
 
-        Query query = getSession ().createQuery (hql.toString ());
-        query.setParameter ("serviceModelUUID", serviceModelUuid);
+        Query query = getSession().createQuery(hql.toString());
+        query.setParameter("serviceModelUUID", serviceModelUuid);
 
         @SuppressWarnings("unchecked")
-        List <ServiceRecipe> resultList = query.list ();
+        List <ServiceRecipe> resultList = query.list();
 
-        if (resultList.isEmpty ()) {
-            LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Service recipe not found", "CatalogDB", "getServiceRecipes", null);
+        if (resultList.isEmpty()) {
+            LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Service recipe not found", "CatalogDB", "getServiceRecipes", null);
             return Collections.EMPTY_LIST;
         }
 
-        Collections.sort (resultList, new MavenLikeVersioningComparator ());
-        Collections.reverse (resultList);
+        Collections.sort(resultList, new MavenLikeVersioningComparator());
+        Collections.reverse(resultList);
 
-        LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getServiceRecipes", null);
+        LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getServiceRecipes", null);
         return resultList;
     }
 
index 2266392..445ca12 100644 (file)
@@ -967,9 +967,58 @@ public class CatalogDatabaseTest {
         assertEquals(null, serviceRecipe);
     }
 
-    @Test(expected = Exception.class)
+    @Test
     public void getServiceRecipesTestException() throws Exception{
-        List<ServiceRecipe> ht = cd.getServiceRecipes("123");
+        MockUp<Query> mockUpQuery = new MockUp<Query>() {
+            @Mock
+            public List<ServiceRecipe> list() {
+                ServiceRecipe serviceRecipe = new ServiceRecipe();
+                serviceRecipe.setId(1);
+                return Arrays.asList(serviceRecipe);
+            }
+        };
+
+        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();
+            }
+        };
+        List<ServiceRecipe> serviceRecipes = cd.getServiceRecipes("123");
+        assertEquals(1, serviceRecipes.size());
+    }
+
+    @Test
+    public void getServiceRecipesEmptyTest() throws Exception{
+        MockUp<Query> mockUpQuery = new MockUp<Query>() {
+            @Mock
+            public List<ServiceRecipe> list() {
+                return Arrays.asList();
+            }
+        };
+
+        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();
+            }
+        };
+        List<ServiceRecipe> serviceRecipes = cd.getServiceRecipes("123");
+        assertEquals(0, serviceRecipes.size());
     }
 
     @Test(expected = Exception.class)