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 --- controlloop/m2/guard/pom.xml | 12 +++++ .../org/onap/policy/guard/GuardContextTest.java | 56 +++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) (limited to 'controlloop') diff --git a/controlloop/m2/guard/pom.xml b/controlloop/m2/guard/pom.xml index 6362ad43b..895cb6cdd 100644 --- a/controlloop/m2/guard/pom.xml +++ b/controlloop/m2/guard/pom.xml @@ -79,5 +79,17 @@ com.h2database h2 + + + org.powermock + powermock-api-mockito2 + test + + + + org.assertj + assertj-core + test + 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