diff options
-rw-r--r-- | mso-catalog-db/src/main/java/org/openecomp/mso/db/catalog/CatalogDatabase.java | 14 | ||||
-rw-r--r-- | mso-catalog-db/src/test/java/org/openecomp/mso/db/catalog/test/CatalogDatabaseTest.java | 177 |
2 files changed, 176 insertions, 15 deletions
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 7ac188b027..d27de9828f 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 @@ -378,16 +378,16 @@ public class CatalogDatabase implements Closeable { */ public Service getService (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); Service service = null; try { - service = (Service) query.uniqueResult (); + service = (Service) 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: modelName='" + modelName + "'"); LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " non unique result for modelName=" + modelName, "", "", MsoLogger.ErrorCode.DataError, "Non unique result for modelName=" + modelName); @@ -412,8 +412,8 @@ public class CatalogDatabase implements Closeable { public Service getServiceByModelUUID (String modelUUID) { - long startTime = System.currentTimeMillis (); - LOGGER.debug ("Catalog database - get service with Model UUID " + modelUUID); + long startTime = System.currentTimeMillis(); + LOGGER.debug("Catalog database - get service with Model UUID " + modelUUID); String hql = "FROM Service WHERE modelUUID = :MODEL_UUID"; HashMap<String, String> parameters = new HashMap<>(); 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 7d39dd6409..dadedfb389 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 @@ -615,21 +615,182 @@ public class CatalogDatabaseTest { assertEquals(null, service); } - @Test(expected = Exception.class) - public void getServiceTestException(){ - Service ht = cd.getService("123"); + @Test + public void getServiceTest(){ + + MockUp<Query> mockUpQuery = new MockUp<Query>() { + + @Mock + public Object uniqueResult() throws Exception { + Service service = new Service(); + service.setModelUUID("123-uuid"); + return service; + } + }; + + 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(); + } + }; + + Service service = cd.getService("123"); + assertEquals("123-uuid", service.getModelUUID()); } - @Test(expected = Exception.class) - public void getServiceByModelUUIDTestException(){ - Service ht = cd.getServiceByModelUUID("123"); + @Test(expected = NonUniqueResultException.class) + public void getServiceNoUniqueResultTest(){ + + MockUp<Query> mockUpQuery = new MockUp<Query>() { + + @Mock + public Object uniqueResult() throws Exception { + throw new NonUniqueResultException(-1); + } + }; + + 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(); + } + }; + + Service service = cd.getService("123"); + } + + @Test(expected = HibernateException.class) + public void getServiceHibernateExceptionTest(){ + + MockUp<Query> mockUpQuery = new MockUp<Query>() { + + @Mock + public Object uniqueResult() throws Exception { + 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(); + } + }; + + Service service = cd.getService("123"); } @Test(expected = Exception.class) - public void getService2TestException(){ + public void getServiceExceptionTest(){ + + MockUp<Query> mockUpQuery = new MockUp<Query>() { + + @Mock + public Object uniqueResult() throws Exception { + throw new Exception("generic 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(); + } + }; + + Service service = cd.getService("123"); + } + + @Test + public void getServiceByModelUUIDTest(){ + + MockUp<Query> mockUpQuery = new MockUp<Query>() { + + @Mock + public Object uniqueResult() throws Exception { + Service service = new Service(); + service.setModelUUID("123-uuid"); + return service; + } + }; + + 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(); + } + }; + Service service = cd.getServiceByModelUUID("123"); + assertEquals("123-uuid", service.getModelUUID()); + } + + @Test + public void getService2Test(){ + MockUp<Query> mockUpQuery = new MockUp<Query>() { + + @Mock + public Object uniqueResult() throws Exception { + Service service = new Service(); + service.setModelUUID("123-uuid"); + return service; + } + }; + + 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(); + } + }; + HashMap<String, String> map = new HashMap<>(); map.put("serviceNameVersionId", "v2"); - Service ht = cd.getService(map, "123"); + Service service = cd.getService(map, "123"); + + assertEquals("123-uuid", service.getModelUUID()); } @Test(expected = Exception.class) |