From c42e71947301a615bbd4492aa70303bf3ac7414c Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Fri, 19 Jan 2018 06:48:20 +0000 Subject: Improve UT for catalog db Improve UT for catalog db. Change-Id: I80eb6d4f58ea9de2d73a92a917dad8f9c98dc88a Issue-ID: SO-360 Signed-off-by: subhash kumar singh --- .../openecomp/mso/db/catalog/CatalogDatabase.java | 24 +++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'mso-catalog-db/src/main/java/org') 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 0219e304cb..0efcb484cb 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 @@ -958,28 +958,28 @@ public class CatalogDatabase implements Closeable { * Return the newest version of a vfModule - 1607 * */ - public VfModule getVfModuleModelName (String modelName) { + public VfModule getVfModuleModelName(String modelName) { - long startTime = System.currentTimeMillis (); - LOGGER.debug ("Catalog database - get vfModuleModelName with name " + modelName); + long startTime = System.currentTimeMillis(); + LOGGER.debug("Catalog database - get vfModuleModelName with name " + modelName); String hql = "FROM VfModule WHERE modelName = :model_name"; - Query query = getSession ().createQuery (hql); - query.setParameter ("model_name", modelName); + Query query = getSession().createQuery(hql); + query.setParameter("model_name", modelName); @SuppressWarnings("unchecked") - List resultList = query.list (); + List 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. VF not found", "CatalogDB", "getVfModuleModelName", null); + if (resultList.isEmpty()) { + LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VF not found", "CatalogDB", "getVfModuleModelName", null); return null; } - 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", "getVfModuleModelName", null); - return resultList.get (0); + LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleModelName", null); + return resultList.get(0); } public VfModule getVfModuleModelName (String modelName, String model_version) { -- cgit 1.2.3-korg 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(-) (limited to 'mso-catalog-db/src/main/java/org') 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 From c3894b6acd5feb6bc0d6a99a48700e98b76bed3a Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Fri, 19 Jan 2018 09:10:52 +0000 Subject: Improve UT for catalog db Improve UT for catalog db. Change-Id: I4beee70d0e78fca0d792480477d1010d5faa086d Issue-ID: SO-360 Signed-off-by: subhash kumar singh --- .../openecomp/mso/db/catalog/CatalogDatabase.java | 14 +++--- .../mso/db/catalog/test/CatalogDatabaseTest.java | 56 ++++++++++++++++++++-- 2 files changed, 60 insertions(+), 10 deletions(-) (limited to 'mso-catalog-db/src/main/java/org') 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 0048a34d6c..0a5bdc51f3 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 @@ -1026,21 +1026,21 @@ public class CatalogDatabase implements Closeable { *@author cb645j * */ - public VfModuleCustomization getVfModuleCustomizationByModelName (String modelName) { + public VfModuleCustomization getVfModuleCustomizationByModelName(String modelName) { - long startTime = System.currentTimeMillis (); - LOGGER.debug ("Catalog database - get VfModuleCustomization By VfModule's ModelName: " + modelName); + long startTime = System.currentTimeMillis(); + LOGGER.debug("Catalog database - get VfModuleCustomization By VfModule's ModelName: " + modelName); String hql = "SELECT VfModuleCustomization FROM VfModuleCustomization as vfmc LEFT OUTER JOIN VfModule as vfm on vfm.modelUUID = vfmc.vfModuleModelUuid where vfm.modelName = :model_name"; - Query query = getSession ().createQuery (hql); - query.setParameter ("model_name", modelName); + Query query = getSession().createQuery(hql); + query.setParameter("model_name", modelName); @SuppressWarnings("unchecked") List resultList = query.list(); // See if something came back. Name is unique, so - if (resultList.isEmpty ()) { - LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful query but Vf module NOT found", "CatalogDB", "getVfModuleCustomizationByModelName", null); + if (resultList.isEmpty()) { + LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successful query but Vf module NOT found", "CatalogDB", "getVfModuleCustomizationByModelName", null); return null; } 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 9ea1ef8bd4..b32e705252 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 @@ -1607,9 +1607,59 @@ public class CatalogDatabaseTest { VfModule vfModule = cd.getVfModuleModelName("tetes","4kidsl"); } - @Test(expected = Exception.class) - public void ggetVfModuleCustomizationByModelNameTestException(){ - VfModuleCustomization vnf = cd.getVfModuleCustomizationByModelName("tetes"); + + @Test + public void ggetVfModuleCustomizationByModelNameTest(){ + MockUp mockUpQuery = new MockUp() { + @Mock + public List list() throws Exception { + VfModuleCustomization vfModuleCustomization = new VfModuleCustomization(); + vfModuleCustomization.setVfModuleModelUuid("123-uuid"); + return Arrays.asList(vfModuleCustomization); + } + }; + + MockUp mockedSession = new MockUp() { + @Mock + public Query createQuery(String hql) { + return mockUpQuery.getMockInstance(); + } + }; + + new MockUp() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + VfModuleCustomization vfModuleCustomization = cd.getVfModuleCustomizationByModelName("tetes"); + assertEquals("123-uuid", vfModuleCustomization.getVfModuleModelUuid()); + } + + @Test + public void ggetVfModuleCustomizationByModelNameEmptyTest(){ + MockUp mockUpQuery = new MockUp() { + @Mock + public List list() throws Exception { + return Arrays.asList(); + } + }; + + MockUp mockedSession = new MockUp() { + @Mock + public Query createQuery(String hql) { + return mockUpQuery.getMockInstance(); + } + }; + + new MockUp() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + VfModuleCustomization vfModuleCustomization = cd.getVfModuleCustomizationByModelName("tetes"); + assertEquals(null, vfModuleCustomization); } @Test(expected = Exception.class) -- cgit 1.2.3-korg From 9a739d06b6a1821230090812b20d559f082051cc Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Fri, 19 Jan 2018 09:16:22 +0000 Subject: Improve UT for catalog db Improve UT for catalog db. Change-Id: I1fbe5516b29763fdaf2a2734f1aa7432a1261eae Issue-ID: SO-360 Signed-off-by: subhash kumar singh --- .../openecomp/mso/db/catalog/CatalogDatabase.java | 24 +++++----- .../mso/db/catalog/test/CatalogDatabaseTest.java | 55 ++++++++++++++++++++-- 2 files changed, 64 insertions(+), 15 deletions(-) (limited to 'mso-catalog-db/src/main/java/org') 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 0a5bdc51f3..774a77a2bb 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 @@ -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 resultList = query.list (); + List 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); } /** 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 b32e705252..c08cb3d222 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 @@ -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 mockUpQuery = new MockUp() { + @Mock + public List list() throws Exception { + NetworkResource networkResource = new NetworkResource(); + networkResource.setModelUUID("123-uuid"); + return Arrays.asList(networkResource); + } + }; + + MockUp mockedSession = new MockUp() { + @Mock + public Query createQuery(String hql) { + return mockUpQuery.getMockInstance(); + } + }; + + new MockUp() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + NetworkResource networkResource = cd.getNetworkResource("tetes"); + assertEquals("123-uuid", networkResource.getModelUUID()); + } + + @Test + public void getNetworkResourceTestEmptyException(){ + MockUp mockUpQuery = new MockUp() { + @Mock + public List list() throws Exception { + return Arrays.asList(); + } + }; + + MockUp mockedSession = new MockUp() { + @Mock + public Query createQuery(String hql) { + return mockUpQuery.getMockInstance(); + } + }; + + new MockUp() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + NetworkResource networkResource = cd.getNetworkResource("tetes"); + assertEquals(null, networkResource); } @Test(expected = Exception.class) -- cgit 1.2.3-korg From cf7df14e048465bd4dbb917851d0065691b416c2 Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Sun, 21 Jan 2018 22:42:04 +0530 Subject: Improve UT for catalog db Improve UT for catalog db. Change-Id: Ie0f9c31f9ec39e4cc693b8b1076702fb1fe11a25 Issue-ID: SO-360 Signed-off-by: subhash kumar singh --- .../openecomp/mso/db/catalog/CatalogDatabase.java | 104 ++--- .../mso/db/catalog/test/CatalogDatabaseTest.java | 458 ++++++++++++++++++++- 2 files changed, 488 insertions(+), 74 deletions(-) (limited to 'mso-catalog-db/src/main/java/org') 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 774a77a2bb..aefe0d5a8b 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 @@ -1087,46 +1087,46 @@ public class CatalogDatabase implements Closeable { * @param serviceType The service Name, if null or empty is provided, it won't be taken into account * @return VnfRecipe object or null if none found */ - public VnfRecipe getVnfRecipe (String vnfType, String action, String serviceType) { + public VnfRecipe getVnfRecipe(String vnfType, String action, String serviceType) { boolean withServiceType = false; - StringBuilder hql = new StringBuilder ("FROM VnfRecipe WHERE vnfType = :vnfType AND action = :action "); + StringBuilder hql = new StringBuilder("FROM VnfRecipe WHERE vnfType = :vnfType AND action = :action "); // If query c - if (serviceType == null || serviceType.isEmpty ()) { - hql.append ("AND serviceType is NULL "); + if (serviceType == null || serviceType.isEmpty()) { + hql.append("AND serviceType is NULL "); } else { - hql.append ("AND serviceType = :serviceType "); + hql.append("AND serviceType = :serviceType "); withServiceType = true; } - long startTime = System.currentTimeMillis (); - LOGGER.debug ("Catalog database - get VNF recipe with name " + vnfType + long startTime = System.currentTimeMillis(); + LOGGER.debug("Catalog database - get VNF recipe with name " + vnfType + " and action " + action + " and service type " + serviceType); - Query query = getSession ().createQuery (hql.toString ()); - query.setParameter (VNF_TYPE, vnfType); - query.setParameter (ACTION, action); + Query query = getSession().createQuery(hql.toString()); + query.setParameter(VNF_TYPE, vnfType); + query.setParameter(ACTION, action); if (withServiceType) { - query.setParameter (SERVICE_TYPE, serviceType); + query.setParameter(SERVICE_TYPE, serviceType); } @SuppressWarnings("unchecked") - List resultList = query.list (); + List resultList = query.list(); - if (resultList.isEmpty ()) { - LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe not found", "CatalogDB", "getVnfRecipe", null); + if (resultList.isEmpty()) { + LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe not found", "CatalogDB", "getVnfRecipe", null); return null; } - 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", "getVnfRecipe", null); - return resultList.get (0); + LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfRecipe", null); + return resultList.get(0); } /** @@ -1136,31 +1136,31 @@ public class CatalogDatabase implements Closeable { * @param action * @return VnfRecipe object or null if none found */ - public VnfRecipe getVnfRecipe (String vnfType, String action) { - StringBuilder hql = new StringBuilder ("FROM VnfRecipe WHERE vnfType = :vnfType AND action = :action "); + public VnfRecipe getVnfRecipe(String vnfType, String action) { + StringBuilder hql = new StringBuilder("FROM VnfRecipe WHERE vnfType = :vnfType AND action = :action "); - long startTime = System.currentTimeMillis (); - LOGGER.debug ("Catalog database - get VNF recipe with name " + vnfType + long startTime = System.currentTimeMillis(); + LOGGER.debug("Catalog database - get VNF recipe with name " + vnfType + " and action " + action); - Query query = getSession ().createQuery (hql.toString ()); - query.setParameter (VNF_TYPE, vnfType); - query.setParameter (ACTION, action); + Query query = getSession().createQuery(hql.toString()); + query.setParameter(VNF_TYPE, vnfType); + query.setParameter(ACTION, action); @SuppressWarnings("unchecked") - List resultList = query.list (); + List resultList = query.list(); - if (resultList.isEmpty ()) { - LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe not found", "CatalogDB", "getVnfRecipe", null); + if (resultList.isEmpty()) { + LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe not found", "CatalogDB", "getVnfRecipe", null); return null; } - 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", "getVnfRecipe", null); - return resultList.get (0); + LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVnfRecipe", null); + return resultList.get(0); } /** @@ -1170,30 +1170,30 @@ public class CatalogDatabase implements Closeable { * @param action * @return VnfRecipe object or null if none found */ - public VnfRecipe getVnfRecipeByVfModuleId (String vnfType, String vfModuleId, String action) { + public VnfRecipe getVnfRecipeByVfModuleId(String vnfType, String vfModuleId, String action) { - StringBuilder hql = new StringBuilder ("FROM VnfRecipe WHERE vfModuleId = :vfModuleId and action = :action "); + StringBuilder hql = new StringBuilder("FROM VnfRecipe WHERE vfModuleId = :vfModuleId and action = :action "); - long startTime = System.currentTimeMillis (); - LOGGER.debug ("Catalog database - get VNF Recipe with vfModuleId " + vfModuleId); + long startTime = System.currentTimeMillis(); + LOGGER.debug("Catalog database - get VNF Recipe with vfModuleId " + vfModuleId); - Query query = getSession ().createQuery (hql.toString ()); - query.setParameter (VF_MODULE_MODEL_UUID, vfModuleId); - query.setParameter (ACTION, action); + Query query = getSession().createQuery(hql.toString ()); + query.setParameter(VF_MODULE_MODEL_UUID, vfModuleId); + query.setParameter(ACTION, action); @SuppressWarnings("unchecked") - List resultList = query.list (); + List resultList = query.list(); - if (resultList.isEmpty ()) { - LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe Entry not found", "CatalogDB", "getVnfRecipeByVfModuleId", null); + if (resultList.isEmpty()) { + LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF recipe Entry not found", "CatalogDB", "getVnfRecipeByVfModuleId", null); return null; } - 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. VNF Recipe Entry found", "CatalogDB", "getVnfRecipeByVfModuleId", null); - return resultList.get (0); + LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. VNF Recipe Entry found", "CatalogDB", "getVnfRecipeByVfModuleId", null); + return resultList.get(0); } public VfModule getVfModuleTypeByUuid(String modelCustomizationUuid) { @@ -1299,7 +1299,7 @@ public class CatalogDatabase implements Closeable { query.setParameter ("serviceModelInvariantUuid", serviceModelInvariantUuid); VnfResource vnfResource = null; try { - vnfResource = (VnfResource) query.uniqueResult (); + vnfResource = (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: serviceModelInvariantUuid='" + serviceModelInvariantUuid); LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for serviceModelInvariantUuid=" + serviceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for serviceModelInvariantUuid==" + serviceModelInvariantUuid); @@ -1317,23 +1317,23 @@ public class CatalogDatabase implements Closeable { throw e; } if (vnfResource == null) { - LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleType", null); + LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "NotFound", "CatalogDB", "getVfModuleType", null); } else { - LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleType", null); + LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getVfModuleType", null); } return vnfResource; } public VnfResource getVnfResourceByVnfUuid(String vnfResourceModelInvariantUuid) { long startTime = System.currentTimeMillis(); - LOGGER.debug ("Catalog database - get vfModuleType with vnfResourceModelInvariantUuid " + vnfResourceModelInvariantUuid); + LOGGER.debug("Catalog database - get vfModuleType with vnfResourceModelInvariantUuid " + vnfResourceModelInvariantUuid); String hql = "FROM VnfResource WHERE vnfResourceModelInvariantUuid = :vnfResourceModelInvariantUuid"; Query query = getSession().createQuery(hql); - query.setParameter ("vnfResourceModelInvariantUuid", vnfResourceModelInvariantUuid); + query.setParameter("vnfResourceModelInvariantUuid", vnfResourceModelInvariantUuid); VnfResource vnfResource = null; try { - vnfResource = (VnfResource) query.uniqueResult (); + vnfResource = (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: vnfResourceModelInvariantUuid='" + vnfResourceModelInvariantUuid); LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for vnfResourceModelInvariantUuid=" + vnfResourceModelInvariantUuid, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for vnfResourceModelInvariantUuid==" + vnfResourceModelInvariantUuid); 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 c08cb3d222..e9f080b1dd 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 @@ -53,12 +53,7 @@ import org.openecomp.mso.db.catalog.beans.VnfResourceCustomization; import org.openecomp.mso.db.catalog.utils.RecordNotFoundException; import java.io.Serializable; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -1716,19 +1711,172 @@ public class CatalogDatabaseTest { assertEquals(null, networkResource); } - @Test(expected = Exception.class) - public void getVnfRecipeTestException(){ - VnfRecipe vnf = cd.getVnfRecipe("tetes","ergfedrf","4993493"); + @Test + public void getVnfRecipeTest(){ + + MockUp mockUpQuery = new MockUp() { + @Mock + public List list() throws Exception { + VnfRecipe vnfRecipe = new VnfRecipe(); + vnfRecipe.setVfModuleId("123-id"); + return Arrays.asList(vnfRecipe); + } + }; + + MockUp mockedSession = new MockUp() { + @Mock + public Query createQuery(String hql) { + return mockUpQuery.getMockInstance(); + } + }; + + new MockUp() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + + VnfRecipe vnfRecipe = cd.getVnfRecipe("tetes","ergfedrf","4993493"); + assertEquals("123-id", vnfRecipe.getVfModuleId()); } - @Test(expected = Exception.class) - public void getVnfRecipe2TestException(){ - VnfRecipe vnf = cd.getVnfRecipe("tetes","4993493"); + @Test + public void getVnfRecipeEmptyTest(){ + + MockUp mockUpQuery = new MockUp() { + @Mock + public List list() throws Exception { + return Collections.emptyList(); + } + }; + + MockUp mockedSession = new MockUp() { + @Mock + public Query createQuery(String hql) { + return mockUpQuery.getMockInstance(); + } + }; + + new MockUp() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + + VnfRecipe vnfRecipe = cd.getVnfRecipe("tetes","ergfedrf","4993493"); + assertEquals(null, vnfRecipe); } - @Test(expected = Exception.class) - public void getVnfRecipeByVfModuleIdTestException(){ - VnfRecipe vnf = cd.getVnfRecipeByVfModuleId("tetes","4993493","vnf"); + @Test + public void getVnfRecipe2Test(){ + MockUp mockUpQuery = new MockUp() { + @Mock + public List list() throws Exception { + VnfRecipe vnfRecipe = new VnfRecipe(); + vnfRecipe.setId(1); + return Arrays.asList(vnfRecipe); + } + }; + + MockUp mockedSession = new MockUp() { + @Mock + public Query createQuery(String hql) { + return mockUpQuery.getMockInstance(); + } + }; + + new MockUp() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + VnfRecipe vnfRecipe = cd.getVnfRecipe("tetes","4993493"); + assertEquals(1, vnfRecipe.getId()); + } + + @Test + public void getVnfRecipe2EmptyTest(){ + MockUp mockUpQuery = new MockUp() { + @Mock + public List list() throws Exception { + return Collections.emptyList(); + } + }; + + MockUp mockedSession = new MockUp() { + @Mock + public Query createQuery(String hql) { + return mockUpQuery.getMockInstance(); + } + }; + + new MockUp() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + VnfRecipe vnfRecipe = cd.getVnfRecipe("tetes","4993493"); + assertEquals(null, vnfRecipe); + } + + @Test + public void getVnfRecipeByVfModuleIdTest(){ + MockUp mockUpQuery = new MockUp() { + @Mock + public List list() throws Exception { + VnfRecipe vnfRecipe = new VnfRecipe(); + vnfRecipe.setId(1); + return Arrays.asList(vnfRecipe); + } + }; + + MockUp mockedSession = new MockUp() { + @Mock + public Query createQuery(String hql) { + return mockUpQuery.getMockInstance(); + } + }; + + new MockUp() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + + VnfRecipe vnfRecipe = cd.getVnfRecipeByVfModuleId("tetes","4993493","vnf"); + assertEquals(1, vnfRecipe.getId()); + } + + @Test + public void getVnfRecipeByVfModuleIdEmptyTest(){ + MockUp mockUpQuery = new MockUp() { + @Mock + public List list() throws Exception { + return Collections.emptyList(); + } + }; + + MockUp mockedSession = new MockUp() { + @Mock + public Query createQuery(String hql) { + return mockUpQuery.getMockInstance(); + } + }; + + new MockUp() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + + VnfRecipe vnfRecipe = cd.getVnfRecipeByVfModuleId("tetes","4993493","vnf"); + assertEquals(null, vnfRecipe); } @Test(expected = Exception.class) @@ -1740,18 +1888,284 @@ public class CatalogDatabaseTest { public void getVfModuleType2TestException(){ VfModule vnf = cd.getVfModuleType("4993493","vnf"); } - @Test(expected = Exception.class) - public void getVnfResourceByServiceUuidTestException(){ - VnfResource vnf = cd.getVnfResourceByServiceUuid("4993493"); + @Test + public void getVnfResourceByServiceUuidTest(){ + 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.getVnfResourceByServiceUuid("4993493"); + assertEquals("123-uuid", vnfResource.getModelUuid()); + } + + @Test(expected = NonUniqueResultException.class) + public void getVnfResourceByServiceUuidNURExceptionTest(){ + 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.getVnfResourceByServiceUuid("4993493"); + } + + @Test(expected = HibernateException.class) + public void getVnfResourceByServiceUuidHibernateExceptionTest(){ + 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.getVnfResourceByServiceUuid("4993493"); } + @Test(expected = Exception.class) - public void getVnfResourceByVnfUuidTestException(){ - VnfResource vnf = cd.getVnfResourceByVnfUuid("4993493"); + public void getVnfResourceByServiceUuidExceptionTest(){ + 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.getVnfResourceByServiceUuid("4993493"); + } + + @Test + public void getVnfResourceByVnfUuidTest(){ + 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.getVnfResourceByVnfUuid("4993493"); + assertEquals("123-uuid", vnfResource.getModelUuid()); + } + + @Test(expected = NonUniqueResultException.class) + public void getVnfResourceByVnfUuidNURExceptionTest(){ + 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.getVnfResourceByVnfUuid("4993493"); } + + @Test(expected = HibernateException.class) + public void getVnfResourceByVnfUuidHibernateExceptionTest(){ + 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.getVnfResourceByVnfUuid("4993493"); + } + @Test(expected = Exception.class) - public void getVfModuleByModelInvariantUuidTestException(){ - VfModule vnf = cd.getVfModuleByModelInvariantUuid("4993493"); + public void getVnfResourceByVnfUuidExceptionTest(){ + 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.getVnfResourceByVnfUuid("4993493"); } + + @Test + public void getVfModuleByModelInvariantUuidTest(){ + + MockUp mockUpQuery = new MockUp() { + + @Mock + public List list() throws Exception { + VfModule vfModule = new VfModule(); + vfModule.setModelUUID("123-uuid"); + return Arrays.asList(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.getVfModuleByModelInvariantUuid("4993493"); + assertEquals("123-uuid", vfModule.getModelUUID()); + } + + @Test + public void getVfModuleByModelInvariantUuidEmptyTest(){ + + MockUp mockUpQuery = new MockUp() { + + @Mock + public List list() throws Exception { + return Collections.emptyList(); + } + }; + + 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.getVfModuleByModelInvariantUuid("4993493"); + assertEquals(null, vfModule); + } + @Test(expected = Exception.class) public void getVfModuleByModelCustomizationUuidTestException(){ VfModuleCustomization vnf = cd.getVfModuleByModelCustomizationUuid("4993493"); -- cgit 1.2.3-korg