summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeyton Puckett <peyton.puckett@att.com>2020-01-16 12:00:29 -0600
committerPeyton Puckett <peyton.puckett@att.com>2020-01-17 09:11:26 -0600
commitc579d2fab3240ff39b906ff4629fe071b54b8473 (patch)
treee3fdd25ecd8b872b7956eec922bead11a56233a4
parent9bb84e47187fef94e028ff9dc374355773205bd3 (diff)
Add jUnit Test Coverage M2 Guard
Issue-ID: POLICY-2290 Change-Id: I05a737333141576512841d6872ecdb0a089a0a90 Signed-off-by: Peyton Puckett <peyton.puckett@att.com>
-rw-r--r--controlloop/m2/guard/pom.xml12
-rw-r--r--controlloop/m2/guard/src/test/java/org/onap/policy/guard/GuardContextTest.java56
2 files changed, 67 insertions, 1 deletions
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 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>org.powermock</groupId>
+ <artifactId>powermock-api-mockito2</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
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", "");
+ }
}