Add jUnit Test Coverage M2 Guard 87/100387/7
authorPeyton Puckett <peyton.puckett@att.com>
Thu, 16 Jan 2020 18:00:29 +0000 (12:00 -0600)
committerPeyton Puckett <peyton.puckett@att.com>
Fri, 17 Jan 2020 15:11:26 +0000 (09:11 -0600)
Issue-ID: POLICY-2290
Change-Id: I05a737333141576512841d6872ecdb0a089a0a90
Signed-off-by: Peyton Puckett <peyton.puckett@att.com>
controlloop/m2/guard/pom.xml
controlloop/m2/guard/src/test/java/org/onap/policy/guard/GuardContextTest.java

index 6362ad4..895cb6c 100644 (file)
       <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>
index 777fc78..7242b9f 100644 (file)
 
 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", "");
+    }
 }