Improve UT for catalog db 37/28637/1
authorsubhash kumar singh <subhash.kumar.singh@huawei.com>
Fri, 19 Jan 2018 09:16:22 +0000 (09:16 +0000)
committersubhash kumar singh <subhash.kumar.singh@huawei.com>
Fri, 19 Jan 2018 09:16:22 +0000 (09:16 +0000)
Improve UT for catalog db.

Change-Id: I1fbe5516b29763fdaf2a2734f1aa7432a1261eae
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 0a5bdc5..774a77a 100644 (file)
@@ -1055,28 +1055,28 @@ public class CatalogDatabase implements Closeable {
      * @param networkType
      * @return NetworkResource object or null if none found
      */
-    public NetworkResource getNetworkResource (String networkType) {
+    public NetworkResource getNetworkResource(String networkType) {
 
-        long startTime = System.currentTimeMillis ();
-        LOGGER.debug ("Catalog database - get network resource with type " + networkType);
+        long startTime = System.currentTimeMillis();
+        LOGGER.debug("Catalog database - get network resource with type " + networkType);
 
         String hql = "FROM NetworkResource WHERE model_name = :network_type";
-        Query query = getSession ().createQuery (hql);
-        query.setParameter ("network_type", networkType);
+        Query query = getSession().createQuery(hql);
+        query.setParameter("network_type", networkType);
 
         @SuppressWarnings("unchecked")
-        List <NetworkResource> resultList = query.list ();
+        List <NetworkResource> resultList = query.list();
 
         // See if something came back. Name is unique, so
-        if (resultList.isEmpty ()) {
-            LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Network Resource not found", "CatalogDB", "getNetworkResource", null);
+        if (resultList.isEmpty()) {
+            LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Network Resource not found", "CatalogDB", "getNetworkResource", null);
             return null;
         }
 
-        Collections.sort (resultList, new MavenLikeVersioningComparator ());
-        Collections.reverse (resultList);
-        LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNetworkResource", null);
-        return resultList.get (0);
+        Collections.sort(resultList, new MavenLikeVersioningComparator());
+        Collections.reverse(resultList);
+        LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getNetworkResource", null);
+        return resultList.get(0);
     }
 
     /**
index b32e705..c08cb3d 100644 (file)
@@ -1662,9 +1662,58 @@ public class CatalogDatabaseTest {
         assertEquals(null, vfModuleCustomization);
     }
 
-    @Test(expected = Exception.class)
-    public void getNetworkResourceTestException(){
-        NetworkResource vnf = cd.getNetworkResource("tetes");
+    @Test
+    public void getNetworkResourceTest(){
+        MockUp<Query> mockUpQuery = new MockUp<Query>() {
+            @Mock
+            public List<NetworkResource> list() throws Exception {
+                NetworkResource networkResource = new NetworkResource();
+                networkResource.setModelUUID("123-uuid");
+                return Arrays.asList(networkResource);
+            }
+        };
+
+        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();
+            }
+        };
+        NetworkResource networkResource = cd.getNetworkResource("tetes");
+        assertEquals("123-uuid", networkResource.getModelUUID());
+    }
+
+    @Test
+    public void getNetworkResourceTestEmptyException(){
+        MockUp<Query> mockUpQuery = new MockUp<Query>() {
+            @Mock
+            public List<NetworkResource> list() throws Exception {
+                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();
+            }
+        };
+        NetworkResource networkResource = cd.getNetworkResource("tetes");
+        assertEquals(null, networkResource);
     }
 
     @Test(expected = Exception.class)