From 89f89afa5bf730f359e18c3672682a8fb0970e85 Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Wed, 17 Jan 2018 05:54:54 +0000 Subject: Improve unit test for catalog Improve unit test for catalog.java. Change-Id: I622fc6ae73de365b0942d3fec5867de7688057b6 Issue-ID: SO-360 Signed-off-by: subhash kumar singh --- .../mso/db/catalog/test/CatalogDatabaseTest.java | 177 ++++++++++++++++++++- 1 file changed, 169 insertions(+), 8 deletions(-) (limited to 'mso-catalog-db/src/test/java/org') 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 mockUpQuery = new MockUp() { + + @Mock + public Object uniqueResult() throws Exception { + Service service = new Service(); + service.setModelUUID("123-uuid"); + return 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.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 mockUpQuery = new MockUp() { + + @Mock + public Object uniqueResult() throws Exception { + 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(); + } + }; + + Service service = cd.getService("123"); + } + + @Test(expected = HibernateException.class) + public void getServiceHibernateExceptionTest(){ + + MockUp mockUpQuery = new MockUp() { + + @Mock + public Object uniqueResult() throws Exception { + 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(); + } + }; + + Service service = cd.getService("123"); } @Test(expected = Exception.class) - public void getService2TestException(){ + public void getServiceExceptionTest(){ + + MockUp mockUpQuery = new MockUp() { + + @Mock + public Object uniqueResult() throws Exception { + throw new Exception("generic exception"); + } + }; + + 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.getService("123"); + } + + @Test + public void getServiceByModelUUIDTest(){ + + MockUp mockUpQuery = new MockUp() { + + @Mock + public Object uniqueResult() throws Exception { + Service service = new Service(); + service.setModelUUID("123-uuid"); + return 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.getServiceByModelUUID("123"); + assertEquals("123-uuid", service.getModelUUID()); + } + + @Test + public void getService2Test(){ + MockUp mockUpQuery = new MockUp() { + + @Mock + public Object uniqueResult() throws Exception { + Service service = new Service(); + service.setModelUUID("123-uuid"); + return service; + } + }; + + MockUp mockedSession = new MockUp() { + @Mock + public Query createQuery(String hql) { + return mockUpQuery.getMockInstance(); + } + }; + + new MockUp() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + HashMap 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) -- cgit 1.2.3-korg