From ec3394aa110893362424cc3b4518da0b5f6415c9 Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Wed, 17 Jan 2018 10:29:30 +0000 Subject: Unit test improvement for catalog db unit test improvement for catalog db. Change-Id: Iff84fb58e973120d9dcaddc4ab3dba6e6ce34975 Issue-ID: SO-360 Signed-off-by: subhash kumar singh --- .../openecomp/mso/db/catalog/CatalogDatabase.java | 42 +++++++-------- .../mso/db/catalog/test/CatalogDatabaseTest.java | 59 ++++++++++++++++++++-- 2 files changed, 77 insertions(+), 24 deletions(-) (limited to 'mso-catalog-db/src') 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 d27de9828f..7d59d2e0ae 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 @@ -462,25 +462,25 @@ public class CatalogDatabase implements Closeable { LOGGER.debug ("Catalog database - get service modelUUID with id " + serviceNameVersionId); String hql = "FROM Service WHERE MODEL_UUID = :MODEL_UUID and http_method = :http_method"; - query = getSession ().createQuery (hql); - query.setParameter ("MODEL_UUID", serviceNameVersionId); + query = getSession().createQuery(hql); + query.setParameter("MODEL_UUID", serviceNameVersionId); } else { serviceId = map.get("serviceId"); serviceVersion = map.get("serviceVersion"); - LOGGER.debug ("Catalog database - get serviceId with id " + serviceId + " and serviceVersion with " + serviceVersion); + LOGGER.debug("Catalog database - get serviceId with id " + serviceId + " and serviceVersion with " + serviceVersion); String hql = "FROM Service WHERE service_id = :service_id and service_version = :service_version and http_method = :http_method"; - query = getSession ().createQuery (hql); - query.setParameter ("service_id", serviceId); - query.setParameter ("service_version", serviceVersion); + query = getSession().createQuery(hql); + query.setParameter("service_id", serviceId); + query.setParameter("service_version", serviceVersion); } - query.setParameter ("http_method", httpMethod); + query.setParameter("http_method", httpMethod); - long startTime = System.currentTimeMillis (); + long startTime = System.currentTimeMillis(); Service service = null; try { - service = (Service) query.uniqueResult (); + service = (Service) query.uniqueResult(); } catch (org.hibernate.NonUniqueResultException nure) { LOGGER.debug("Non Unique Result Exception - data integrity error: service_id='" + serviceId + "', serviceVersion='" + serviceVersion + "'"); LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for service_id=" + serviceId + " and serviceVersion=" + serviceVersion, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for service_id=" + serviceId); @@ -511,28 +511,28 @@ public class CatalogDatabase implements Closeable { * @param modelName * @return Service object or null if none found */ - public Service getServiceByModelName (String modelName){ + public Service getServiceByModelName(String modelName){ - long startTime = System.currentTimeMillis (); - LOGGER.debug ("Catalog database - get service with name " + modelName); + long startTime = System.currentTimeMillis(); + LOGGER.debug("Catalog database - get service with name " + modelName); String hql = "FROM Service 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. - if (resultList.isEmpty ()) { - LOGGER.recordMetricEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Service not found", "CatalogDB", "getServiceByModelName", null); + if (resultList.isEmpty()) { + LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully. Service not found", "CatalogDB", "getServiceByModelName", 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", "getServiceByModelName", null); - return resultList.get (0); + LOGGER.recordMetricEvent(startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully", "CatalogDB", "getServiceByModelName", null); + return resultList.get(0); } public Service getServiceByVersionAndInvariantId(String modelInvariantId, String modelVersion) throws Exception { 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 dadedfb389..46c175c9fb 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 @@ -793,9 +793,62 @@ public class CatalogDatabaseTest { assertEquals("123-uuid", service.getModelUUID()); } - @Test(expected = Exception.class) - public void getServiceByModelNameTestException(){ - Service ht = cd.getServiceByModelName("123"); + @Test + public void getServiceByModelNameTest(){ + + MockUp mockUpQuery = new MockUp() { + @Mock + public List list() throws Exception { + Service service = new Service(); + service.setModelUUID("123-uuid"); + return Arrays.asList(service); + } + }; + + MockUp mockedSession = new MockUp() { + @Mock + public Query createQuery(String hql) { + return mockUpQuery.getMockInstance(); + } + }; + + new MockUp() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + + Service service = cd.getServiceByModelName("123"); + assertEquals("123-uuid", service.getModelUUID()); + } + + @Test + public void getServiceByModelNameEmptyTest(){ + + 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(); + } + }; + + Service service = cd.getServiceByModelName("123"); + assertEquals(null, service); } @Test(expected = Exception.class) -- cgit 1.2.3-korg