From c579d2fab3240ff39b906ff4629fe071b54b8473 Mon Sep 17 00:00:00 2001 From: Peyton Puckett Date: Thu, 16 Jan 2020 12:00:29 -0600 Subject: Add jUnit Test Coverage M2 Guard Issue-ID: POLICY-2290 Change-Id: I05a737333141576512841d6872ecdb0a089a0a90 Signed-off-by: Peyton Puckett --- .../org/onap/policy/guard/GuardContextTest.java | 56 +++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) (limited to 'controlloop/m2/guard/src/test/java/org') diff --git a/controlloop/m2/guard/src/test/java/org/onap/policy/guard/GuardContextTest.java b/controlloop/m2/guard/src/test/java/org/onap/policy/guard/GuardContextTest.java index 777fc78f0..7242b9f12 100644 --- a/controlloop/m2/guard/src/test/java/org/onap/policy/guard/GuardContextTest.java +++ b/controlloop/m2/guard/src/test/java/org/onap/policy/guard/GuardContextTest.java @@ -20,10 +20,15 @@ package org.onap.policy.guard; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.isNotNull; +import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.io.IOException; @@ -37,7 +42,9 @@ import org.drools.core.WorkingMemory; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; -import org.mockito.ArgumentMatcher; +import org.mockito.Mockito; +import org.onap.policy.drools.core.PolicyContainer; +import org.onap.policy.drools.core.PolicySession; import org.onap.policy.drools.system.PolicyEngineConstants; public class GuardContextTest { @@ -136,4 +143,51 @@ public class GuardContextTest { assertEquals("PolicyGuardResponse [requestId=", response.toString().substring(0, 31)); } + + @Test + public void testConstructors() { + PolicySession mockPolicySession = Mockito.mock(PolicySession.class); + PolicyContainer mockPolicyContainer = Mockito.mock(PolicyContainer.class); + + when(mockPolicySession.getPolicyContainer()).thenReturn(mockPolicyContainer); + when(mockPolicyContainer.getArtifactId()).thenReturn("testArtifactId"); + when(mockPolicyContainer.getGroupId()).thenReturn("testGroupId"); + + assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> { + guardContext = new GuardContext(mockPolicySession); + }); + + assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> { + guardContext = new GuardContext(mockPolicySession, "testSerializableName"); + }); + + verify(mockPolicySession, atLeast(1)).getPolicyContainer(); + verify(mockPolicyContainer, atLeast(1)).getArtifactId(); + verify(mockPolicyContainer, atLeast(1)).getGroupId(); + } + + @Test + public void testCreateDbEntry() { + Properties mockProperties = Mockito.mock(Properties.class); + Instant startTime = Instant.now(); + Instant endTime = Instant.now(); + + guardContext = new GuardContext(mockProperties); + assertFalse(guardContext.createDbEntry(startTime, endTime, "testClosedLoopControlName", "testActor", + "testRecipe", "testTarget", "testRequestId", "testSubRequestId", "testMessage", "testOutcome")); + + PolicyEngineConstants.getManager().setEnvironmentProperty("guard.disabled", "true"); + assertFalse(guardContext.createDbEntry(startTime, endTime, "testClosedLoopControlName", "testActor", + "testRecipe", "testTarget", "testRequestId", "testSubRequestId", "testMessage", "testOutcome")); + + PolicyEngineConstants.getManager().setEnvironmentProperty("guard.disabled", ""); + PolicyEngineConstants.getManager().setEnvironmentProperty("guard.jdbc.url", "jdbc:h2:file:./H2DB"); + PolicyEngineConstants.getManager().setEnvironmentProperty("sql.db.username", "user"); + PolicyEngineConstants.getManager().setEnvironmentProperty("sql.db.password", "secret"); + guardContext = new GuardContext(mockProperties); + assertTrue(guardContext.createDbEntry(startTime, endTime, "testClosedLoopControlName", "testActor", + "testRecipe", "testTarget", "testRequestId", "testSubRequestId", "testMessage", "testOutcome")); + + PolicyEngineConstants.getManager().setEnvironmentProperty("guard.disabled", ""); + } } -- cgit 1.2.3-korg