diff options
author | Benjamin, Max (mb388a) <mb388a@us.att.com> | 2018-03-28 15:49:21 -0400 |
---|---|---|
committer | Benjamin, Max (mb388a) <mb388a@us.att.com> | 2018-03-28 15:49:36 -0400 |
commit | 2153f867baa6966a04493fa1e11fcf9a0c260035 (patch) | |
tree | 944df4410ecd1a9b78de1fc4f4f57b24fe7fff33 /mso-api-handlers/mso-requests-db/src/test/java/org | |
parent | 28975e0498b6b66b71e3d5a888e78858a34de0ac (diff) |
improved request-db test coverage
Change-Id: I790517aaa524189c09fcc59d23dd301758f0145c
Issue-ID: SO-544
Signed-off-by: Benjamin, Max (mb388a) <mb388a@us.att.com>
Diffstat (limited to 'mso-api-handlers/mso-requests-db/src/test/java/org')
5 files changed, 369 insertions, 194 deletions
diff --git a/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/OperationalEnvDistributionStatusDbTest.java b/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/OperationalEnvDistributionStatusDbTest.java index 77f5a1d699..89213f9948 100644 --- a/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/OperationalEnvDistributionStatusDbTest.java +++ b/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/OperationalEnvDistributionStatusDbTest.java @@ -27,62 +27,86 @@ import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.util.Arrays; + +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mock; import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.openecomp.mso.db.AbstractSessionFactoryManager; public class OperationalEnvDistributionStatusDbTest { + @Mock + private AbstractSessionFactoryManager sessionFactoryRequest; + @Mock + private SessionFactory sessionFactory; + @Mock + private Session session; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + when(sessionFactory.openSession()).thenReturn(session); + when(sessionFactoryRequest.getSessionFactory()).thenReturn(sessionFactory); + + } - - private static final String distributionId = "ff3514e3-5a33-55df-13ab-12abad84e7ff"; - private static final String operationalEnvId = "12abad84e7ff"; - private static final String serviceModelVersionId = "ff305d54-75b4-431b-adb2-eb6b9e5ff001"; - private static final String requestId = "431b-adb2-eb6b9e5ff001"; - private static final String status = "SENT"; - private OperationalEnvDistributionStatus operEnvDistStatus; - - @Test - public void testGetOperationalEnvDistributionStatus() { - - OperationalEnvDistributionStatusDb oeds = Mockito.mock(OperationalEnvDistributionStatusDb.class); - Mockito.when(oeds.getOperationalEnvDistributionStatus("ff3514e3-5a33-55df-13ab-12abad84e7ff")).thenReturn(operEnvDistStatus); - OperationalEnvDistributionStatus actual = oeds.getOperationalEnvDistributionStatus(distributionId); - assertEquals(actual, operEnvDistStatus); - verify(oeds, times(1)).getOperationalEnvDistributionStatus(any(String.class)); + public void insertFailure() { + OperationalEnvDistributionStatusDb omsDB = new OperationalEnvDistributionStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.uniqueResult()).thenReturn(null); + when(session.isOpen()).thenReturn(true); + when(session.getTransaction()).thenThrow(Exception.class); + thrown.expect(Exception.class); + + omsDB.insertOperationalEnvDistributionStatus("myDistId", "myEnvId", "myModelVerId", "myDistIdStatus", "myReqId"); } @Test - public void testGetOperationalEnvDistributionStatusPerReqId() { - - OperationalEnvDistributionStatusDb oeds = Mockito.mock(OperationalEnvDistributionStatusDb.class); - Mockito.when(oeds.getOperationalEnvDistributionStatusPerReqId("ff3514e3-5a33-55df-13ab-12abad84e7ff", "431b-adb2-eb6b9e5ff001")).thenReturn(operEnvDistStatus); - OperationalEnvDistributionStatus actual = oeds.getOperationalEnvDistributionStatusPerReqId(distributionId, requestId); - assertEquals(actual, operEnvDistStatus); - verify(oeds, times(1)).getOperationalEnvDistributionStatusPerReqId(any(String.class), any(String.class)); + public void updateFailureRetryCount() { + OperationalEnvDistributionStatusDb omsDB = new OperationalEnvDistributionStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(session.isOpen()).thenReturn(true); + when(session.getTransaction()).thenThrow(Exception.class); + thrown.expect(Exception.class); + + omsDB.updateOperationalEnvDistributionStatus("asdcStatus", "myDistId", "myEnvId", "myVerId"); } - + @Test - public void testUpdateOperationalEnvDistributionStatus() { - - int val = 1; - OperationalEnvDistributionStatusDb oeds = Mockito.mock(OperationalEnvDistributionStatusDb.class); - Mockito.when(oeds.updateOperationalEnvDistributionStatus("OK", "ff3514e3-5a33", "ff3514e3-5a33", "ff3514e3-5a33-55df-13ab-12abad84e7ff")).thenReturn(val); - int actual = oeds.updateOperationalEnvDistributionStatus("OK", "ff3514e3-5a33", "ff3514e3-5a33", "ff3514e3-5a33-55df-13ab-12abad84e7ff"); - assertEquals(actual, val); - verify(oeds, times(1)).updateOperationalEnvDistributionStatus(any(String.class), any(String.class), any(String.class), any(String.class)); + public void getOperationalEnvIdStatusTest() { + OperationalEnvDistributionStatusDb omsDB = new OperationalEnvDistributionStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + OperationalEnvDistributionStatus status = new OperationalEnvDistributionStatus(); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.uniqueResult()).thenReturn(status); + when(session.isOpen()).thenReturn(true); + assertEquals(status, omsDB.getOperationalEnvDistributionStatusPerReqId("myDistId", "myReqId")); } @Test - public void testInsertOperationalEnvDistributionStatus() { - - OperationalEnvDistributionStatusDb oeds = mock(OperationalEnvDistributionStatusDb.class); - - oeds.insertOperationalEnvDistributionStatus(distributionId, operationalEnvId, serviceModelVersionId, status, requestId); - doNothing().when(oeds).insertOperationalEnvDistributionStatus(any(String.class), any(String.class), any(String.class), any(String.class), any(String.class)); - verify(oeds, times(1)).insertOperationalEnvDistributionStatus(any(String.class), any(String.class), any(String.class), any(String.class), any(String.class)); - + public void getOperationalEnvServiceModelStatusTest() { + OperationalEnvDistributionStatusDb omsDB = new OperationalEnvDistributionStatusDb(this.sessionFactoryRequest); + OperationalEnvDistributionStatus status = new OperationalEnvDistributionStatus(); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.uniqueResult()).thenReturn(status); + when(session.isOpen()).thenReturn(true); + assertEquals(status, omsDB.getOperationalEnvDistributionStatus("myDistId")); } - }
\ No newline at end of file diff --git a/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/OperationalEnvServiceModelStatusDbTest.java b/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/OperationalEnvServiceModelStatusDbTest.java index 77776c2fe1..fb2a5a44d2 100644 --- a/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/OperationalEnvServiceModelStatusDbTest.java +++ b/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/OperationalEnvServiceModelStatusDbTest.java @@ -22,88 +22,99 @@ package org.openecomp.mso.requestsdb; import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; -import java.util.ArrayList; -import java.util.List; +import java.util.Arrays; +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; -import org.mockito.Mockito; +import org.junit.rules.ExpectedException; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.mso.db.AbstractSessionFactoryManager; public class OperationalEnvServiceModelStatusDbTest { - - private static final String operationalEnvId = "12abad84e7ff"; - private static final String serviceModelVersionId = "ff305d54-75b4-431b-adb2-eb6b9e5ff001"; - private static final String requestId = "431b-adb2-eb6b9e5ff001"; - private static final String status = "SENT"; - private static final int retryCount = 1; - private static final String recoveryAction = "Retry"; - private static final String workloadContext = "VNF_D2D"; - - private OperationalEnvServiceModelStatus operEnvDistStatus; + @Mock + private AbstractSessionFactoryManager sessionFactoryRequest; + @Mock + private SessionFactory sessionFactory; + @Mock + private Session session; - - @Test - public void testGetOperationalEnvDistributionStatus() { - - OperationalEnvServiceModelStatusDb oesms = Mockito.mock(OperationalEnvServiceModelStatusDb.class); - Mockito.when(oesms.getOperationalEnvServiceModelStatus("12abad84e7ff", "ff305d54-75b4-431b-adb2-eb6b9e5ff001")).thenReturn(operEnvDistStatus); - OperationalEnvServiceModelStatus actual = oesms.getOperationalEnvServiceModelStatus(operationalEnvId, serviceModelVersionId); - assertEquals(actual, operEnvDistStatus); - verify(oesms, times(1)).getOperationalEnvServiceModelStatus(any(String.class), any(String.class)); - } - - @Test - public void testGetOperationalEnvIdStatus() { - - List<OperationalEnvServiceModelStatus> operEnvSvcModelStatus = new ArrayList<>(); - OperationalEnvServiceModelStatusDb oesms = Mockito.mock(OperationalEnvServiceModelStatusDb.class); - Mockito.when(oesms.getOperationalEnvIdStatus("12abad84e7ff", "ff305d54-75b4-431b-adb2-eb6b9e5ff001")).thenReturn(operEnvSvcModelStatus); - List<OperationalEnvServiceModelStatus> actual = oesms.getOperationalEnvIdStatus(operationalEnvId, requestId); - assertEquals(actual, operEnvSvcModelStatus); - verify(oesms, times(1)).getOperationalEnvIdStatus(any(String.class), any(String.class)); - - } - - @Test - public void testUpdateOperationalEnvRetryCountStatus() { - - int val = 1; - OperationalEnvServiceModelStatusDb oesms = Mockito.mock(OperationalEnvServiceModelStatusDb.class); - Mockito.when(oesms.updateOperationalEnvRetryCountStatus("12abad84e7ff", "ff305d54-75b4-431b-adb2-eb6b9e5ff001", "SENT", 1)).thenReturn(val); - int actual = oesms.updateOperationalEnvRetryCountStatus(operationalEnvId, serviceModelVersionId, status, retryCount); - assertEquals(actual, val); - verify(oesms, times(1)).updateOperationalEnvRetryCountStatus(any(String.class), any(String.class), any(String.class), any(int.class)); - } + @Rule + public ExpectedException thrown = ExpectedException.none(); - @Test - public void testUpdateOperationalEnvRetryCountStatusPerReqId() { - - int val = 1; - OperationalEnvServiceModelStatusDb oesms = Mockito.mock(OperationalEnvServiceModelStatusDb.class); - Mockito.when(oesms.updateOperationalEnvRetryCountStatusPerReqId("12abad84e7ff", "ff305d54-75b4-431b-adb2-eb6b9e5ff001", "SENT", 1, "431b-adb2-eb6b9e5ff001")).thenReturn(val); - int actual = oesms.updateOperationalEnvRetryCountStatusPerReqId(operationalEnvId, serviceModelVersionId, status, retryCount, requestId); - assertEquals(actual, val); - verify(oesms, times(1)).updateOperationalEnvRetryCountStatusPerReqId(any(String.class), any(String.class), any(String.class), - any(int.class), any(String.class)); - } - + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + when(sessionFactory.openSession()).thenReturn(session); + when(sessionFactoryRequest.getSessionFactory()).thenReturn(sessionFactory); - @Test - public void testInsertOperationalEnvServiceModelStatus() { - - OperationalEnvServiceModelStatusDb oesms = mock(OperationalEnvServiceModelStatusDb.class); - - oesms.insertOperationalEnvServiceModelStatus(requestId, operationalEnvId, serviceModelVersionId, status, recoveryAction, retryCount, workloadContext); - doNothing().when(oesms).insertOperationalEnvServiceModelStatus(any(String.class), any(String.class), any(String.class), - any(String.class), any(String.class), any(int.class), any(String.class)); - verify(oesms, times(1)).insertOperationalEnvServiceModelStatus(any(String.class), any(String.class), any(String.class), - any(String.class), any(String.class), any(int.class), any(String.class)); + } + + @Test + public void insertFailure() { + OperationalEnvServiceModelStatusDb omsDB = new OperationalEnvServiceModelStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.uniqueResult()).thenReturn(null); + when(session.isOpen()).thenReturn(true); + when(session.getTransaction()).thenThrow(Exception.class); + thrown.expect(Exception.class); + + omsDB.insertOperationalEnvServiceModelStatus("myRequestId", "myOperationalEnvId", "myServiceModelVersionId", "myVal", "myRecoveryAction", 1, "myWorkloadContext"); + } + + @Test + public void updateFailureRetryCount() { + OperationalEnvServiceModelStatusDb omsDB = new OperationalEnvServiceModelStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(session.isOpen()).thenReturn(true); + when(session.getTransaction()).thenThrow(Exception.class); + thrown.expect(Exception.class); - } + omsDB.updateOperationalEnvRetryCountStatus("myOperationalEnvId", "myServiceModelVersionId", "status", 1); + } + + @Test + public void updateFailureRetryCountPerReqId() { + OperationalEnvServiceModelStatusDb omsDB = new OperationalEnvServiceModelStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(session.isOpen()).thenReturn(true); + when(session.getTransaction()).thenThrow(Exception.class); + thrown.expect(Exception.class); + omsDB.updateOperationalEnvRetryCountStatusPerReqId("myOperationalEnvId", "myServiceModelVersionId", "status", 1, "myReqId"); + } + + + @Test + public void getOperationalEnvIdStatusTest() { + OperationalEnvServiceModelStatusDb omsDB = new OperationalEnvServiceModelStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + OperationalEnvServiceModelStatus status = new OperationalEnvServiceModelStatus(); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.list()).thenReturn(Arrays.asList(status)); + when(session.isOpen()).thenReturn(true); + assertEquals(status, omsDB.getOperationalEnvIdStatus("myEnvId", "myReqId").get(0)); + } + + @Test + public void getOperationalEnvServiceModelStatusTest() { + OperationalEnvServiceModelStatusDb omsDB = new OperationalEnvServiceModelStatusDb(this.sessionFactoryRequest); + OperationalEnvServiceModelStatus status = new OperationalEnvServiceModelStatus(); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.uniqueResult()).thenReturn(status); + when(session.isOpen()).thenReturn(true); + assertEquals(status, omsDB.getOperationalEnvServiceModelStatus("myEnvId", "myModelId")); + } } diff --git a/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/WatchdogComponentDistributionStatusDbTest.java b/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/WatchdogComponentDistributionStatusDbTest.java index cc0a5983b6..a86812e167 100644 --- a/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/WatchdogComponentDistributionStatusDbTest.java +++ b/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/WatchdogComponentDistributionStatusDbTest.java @@ -20,48 +20,108 @@ package org.openecomp.mso.requestsdb; -import org.junit.Test; -import org.mockito.Mockito; - import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; -import java.util.ArrayList; -import java.util.List; +import java.util.Arrays; + +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.mso.db.AbstractSessionFactoryManager; public class WatchdogComponentDistributionStatusDbTest { - private static final String distributionId = "ff3514e3-5a33-55df-13ab-12abad84e7ff"; - private static final String componentName = "MSO"; - private static final String componentDistributionStatus = "SENT"; - - + @Mock + private AbstractSessionFactoryManager sessionFactoryRequest; + @Mock + private SessionFactory sessionFactory; + @Mock + private Session session; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + when(sessionFactory.openSession()).thenReturn(session); + when(sessionFactoryRequest.getSessionFactory()).thenReturn(sessionFactory); + + } + @Test - public void testGetWatchdogComponentDistributionStatus() { - List<WatchdogComponentDistributionStatus> watchDogCompDistStatus = new ArrayList<>(); - WatchdogComponentDistributionStatusDb wdcds = Mockito.mock(WatchdogComponentDistributionStatusDb.class); - Mockito.when(wdcds.getWatchdogComponentDistributionStatus("ff3514e3-5a33-55df-13ab-12abad84e7ff")).thenReturn(watchDogCompDistStatus); - List<WatchdogComponentDistributionStatus> actual = wdcds.getWatchdogComponentDistributionStatus(distributionId); - - assertEquals(actual, watchDogCompDistStatus); - verify(wdcds, times(1)).getWatchdogComponentDistributionStatus(any(String.class)); + public void insertFailure() { + WatchdogComponentDistributionStatusDb wds = new WatchdogComponentDistributionStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.uniqueResult()).thenReturn(null); + when(session.isOpen()).thenReturn(true); + when(session.getTransaction()).thenThrow(Exception.class); + thrown.expect(Exception.class); + + wds.insertWatchdogComponentDistributionStatus("myId", "myComponentName", "myStatus"); } - - + + @Test - public void testInsertWatchdogComponentDistributionStatus() { - - WatchdogComponentDistributionStatusDb wdcds = mock(WatchdogComponentDistributionStatusDb.class); - - wdcds.insertWatchdogComponentDistributionStatus(distributionId, componentName, componentDistributionStatus); - doNothing().when(wdcds).insertWatchdogComponentDistributionStatus(any(String.class), any(String.class), any(String.class)); - verify(wdcds, times(1)).insertWatchdogComponentDistributionStatus(any(String.class), any(String.class), any(String.class)); - + public void getWatchdogComponentNamesTest() { + WatchdogComponentDistributionStatusDb wds = new WatchdogComponentDistributionStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.list()).thenReturn(Arrays.asList("myValue")); + when(session.isOpen()).thenReturn(true); + assertEquals("myValue", wds.getWatchdogComponentNames("myId").get(0)); + } + + @Test + public void getWatchdogComponentDistributionStatusTest() { + WatchdogComponentDistributionStatusDb wds = new WatchdogComponentDistributionStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.list()).thenReturn(Arrays.asList("myValue")); + when(session.isOpen()).thenReturn(true); + assertEquals("myValue", wds.getWatchdogComponentDistributionStatus("myStatus").get(0)); + assertEquals("myValue", wds.getWatchdogComponentDistributionStatus("myId", "myName").get(0)); } + @Test + public void testGetWatchdogDistributionIdNotFound() { + WatchdogDistributionStatusDb wds = new WatchdogDistributionStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.uniqueResult()).thenReturn(null); + when(session.isOpen()).thenReturn(true); + assertEquals(null, wds.getWatchdogDistributionId("test")); + } + + @Test + public void testGetWatchdogDistributionIdStatus() { + WatchdogDistributionStatusDb wds = new WatchdogDistributionStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.uniqueResult()).thenReturn("myValue"); + when(session.isOpen()).thenReturn(true); + assertEquals("myValue", wds.getWatchdogDistributionIdStatus("test")); + } + + @Test + public void testGetWatchdogDistributionIdStatusNotFound() { + WatchdogDistributionStatusDb wds = new WatchdogDistributionStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.uniqueResult()).thenReturn(null); + when(session.isOpen()).thenReturn(true); + assertEquals(null, wds.getWatchdogDistributionIdStatus("test")); + } + } diff --git a/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/WatchdogDistributionStatusDbTest.java b/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/WatchdogDistributionStatusDbTest.java index 3343e0aaf6..3371018605 100644 --- a/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/WatchdogDistributionStatusDbTest.java +++ b/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/WatchdogDistributionStatusDbTest.java @@ -20,48 +20,105 @@ package org.openecomp.mso.requestsdb; -import org.junit.Test; -import org.mockito.Mockito; - import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.mso.db.AbstractSessionFactoryManager; public class WatchdogDistributionStatusDbTest { - private static final String distributionId = "ff3514e3-5a33-55df-13ab-12abad84e7ff"; - + @Mock + private AbstractSessionFactoryManager sessionFactoryRequest; + @Mock + private SessionFactory sessionFactory; + @Mock + private Session session; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + when(sessionFactory.openSession()).thenReturn(session); + when(sessionFactoryRequest.getSessionFactory()).thenReturn(sessionFactory); + + } + @Test - public void testUpdateWatchdogDistributionIdStatus() { - WatchdogDistributionStatusDb wdds = Mockito.mock(WatchdogDistributionStatusDb.class); + public void insertFailure() { + WatchdogDistributionStatusDb wds = new WatchdogDistributionStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.uniqueResult()).thenReturn(null); + when(session.isOpen()).thenReturn(true); + when(session.getTransaction()).thenThrow(Exception.class); + thrown.expect(Exception.class); - doNothing().when(wdds).updateWatchdogDistributionIdStatus("ff3514e3-5a33-55df-13ab-12abad84e7ff", "SENT"); - wdds.updateWatchdogDistributionIdStatus(any(String.class), any(String.class)); - verify(wdds, times(1)).updateWatchdogDistributionIdStatus(any(String.class), any(String.class)); - } + wds.insertWatchdogDistributionId("myId"); + } @Test - public void testInsertWatchdogDistributionId() { - - WatchdogDistributionStatusDb wdds = mock(WatchdogDistributionStatusDb.class); + public void updateFailure() { + WatchdogDistributionStatusDb wds = new WatchdogDistributionStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(session.isOpen()).thenReturn(true); + when(session.getTransaction()).thenThrow(Exception.class); + thrown.expect(Exception.class); - wdds.insertWatchdogDistributionId(distributionId); - doNothing().when(wdds).insertWatchdogDistributionId(any(String.class)); - verify(wdds, times(1)).insertWatchdogDistributionId(any(String.class)); + wds.updateWatchdogDistributionIdStatus("myId", "myStatus"); + } + @Test + public void testGetWatchdogDistributionId() { + WatchdogDistributionStatusDb wds = new WatchdogDistributionStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.uniqueResult()).thenReturn("myValue"); + when(session.isOpen()).thenReturn(true); + assertEquals("myValue", wds.getWatchdogDistributionId("test")); } - + + @Test + public void testGetWatchdogDistributionIdNotFound() { + WatchdogDistributionStatusDb wds = new WatchdogDistributionStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.uniqueResult()).thenReturn(null); + when(session.isOpen()).thenReturn(true); + assertEquals(null, wds.getWatchdogDistributionId("test")); + } + @Test public void testGetWatchdogDistributionIdStatus() { - - WatchdogDistributionStatusDb wdds = Mockito.mock(WatchdogDistributionStatusDb.class); - Mockito.when(wdds.getWatchdogDistributionIdStatus("ff305d54-75b4-431b-adb2-eb6b9e5ff001")).thenReturn("ff3514e3-5a33-55df-13ab-12abad84e7ff"); - String actual = wdds.getWatchdogDistributionIdStatus("ff305d54-75b4-431b-adb2-eb6b9e5ff001"); - assertEquals(actual, distributionId); - verify(wdds, times(1)).getWatchdogDistributionIdStatus(any(String.class)); + WatchdogDistributionStatusDb wds = new WatchdogDistributionStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.uniqueResult()).thenReturn("myValue"); + when(session.isOpen()).thenReturn(true); + assertEquals("myValue", wds.getWatchdogDistributionIdStatus("test")); + } + + @Test + public void testGetWatchdogDistributionIdStatusNotFound() { + WatchdogDistributionStatusDb wds = new WatchdogDistributionStatusDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.uniqueResult()).thenReturn(null); + when(session.isOpen()).thenReturn(true); + assertEquals(null, wds.getWatchdogDistributionIdStatus("test")); } } diff --git a/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/WatchdogServiceModVerIdLookupDbTest.java b/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/WatchdogServiceModVerIdLookupDbTest.java index 9e09aeed00..42224b0238 100644 --- a/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/WatchdogServiceModVerIdLookupDbTest.java +++ b/mso-api-handlers/mso-requests-db/src/test/java/org/openecomp/mso/requestsdb/WatchdogServiceModVerIdLookupDbTest.java @@ -23,41 +23,64 @@ */ package org.openecomp.mso.requestsdb; -import org.junit.Test; -import org.mockito.Mockito; - import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.mso.db.AbstractSessionFactoryManager; public class WatchdogServiceModVerIdLookupDbTest { + @Mock + private AbstractSessionFactoryManager sessionFactoryRequest; + @Mock + private SessionFactory sessionFactory; + @Mock + private Session session; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + when(sessionFactory.openSession()).thenReturn(session); + when(sessionFactoryRequest.getSessionFactory()).thenReturn(sessionFactory); - private static final String distributionId = "ff3514e3-5a33-55df-13ab-12abad84e7ff"; - private static final String serviceModelVersionId = "SENT"; + } @Test - public void testInsertWatchdogServiceModVerIdLookup() { - - WatchdogServiceModVerIdLookupDb wdsm = mock(WatchdogServiceModVerIdLookupDb.class); - - wdsm.insertWatchdogServiceModVerIdLookup(distributionId, serviceModelVersionId); - doNothing().when(wdsm).insertWatchdogServiceModVerIdLookup(any(String.class), any(String.class)); - verify(wdsm, times(1)).insertWatchdogServiceModVerIdLookup(any(String.class), any(String.class)); - + public void getWatchdogServiceModVerIdTest() { + WatchdogServiceModVerIdLookupDb wds = new WatchdogServiceModVerIdLookupDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.uniqueResult()).thenReturn(null); + when(session.isOpen()).thenReturn(true); + assertEquals(null, wds.getWatchdogServiceModVerId("test")); } @Test - public void testGetWatchdogServiceModVerId() { - - WatchdogServiceModVerIdLookupDb wdsm = Mockito.mock(WatchdogServiceModVerIdLookupDb.class); - Mockito.when(wdsm.getWatchdogServiceModVerId("ff305d54-75b4-431b-adb2-eb6b9e5ff001")).thenReturn("ff3514e3-5a33-55df-13ab-12abad84e7ff"); - String actual = wdsm.getWatchdogServiceModVerId("ff305d54-75b4-431b-adb2-eb6b9e5ff001"); - assertEquals(actual, "ff3514e3-5a33-55df-13ab-12abad84e7ff"); - verify(wdsm, times(1)).getWatchdogServiceModVerId(any(String.class)); + public void insertWatchdogServiceModVerIdLookupTest() { + WatchdogServiceModVerIdLookupDb wds = new WatchdogServiceModVerIdLookupDb(this.sessionFactoryRequest); + Query mockQuery = mock(Query.class); + when(session.createQuery(any(String.class))).thenReturn(mockQuery); + when(mockQuery.uniqueResult()).thenReturn(null); + when(session.isOpen()).thenReturn(true); + when(session.getTransaction()).thenThrow(Exception.class); + thrown.expect(Exception.class); + + wds.insertWatchdogServiceModVerIdLookup("myId", "myModelVersion"); } + } |