aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJorge Hernandez <jh1730@att.com>2018-09-05 20:08:42 +0000
committerGerrit Code Review <gerrit@onap.org>2018-09-05 20:08:42 +0000
commit6e3c351251ae1e578b3f28019de41fe03a69f1e0 (patch)
tree3f5e74d21a3a99b072aadd524400feb3c41b9917
parent09543cac5d1052d87adafaf55bd94475fd86ca68 (diff)
parent0185fd2a828f51ac8050aa6027c2837e0254dd80 (diff)
Merge "Added junit test for commitAbatement."
-rw-r--r--controlloop/common/eventmanager/pom.xml7
-rw-r--r--controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManagerTest.java98
-rw-r--r--controlloop/common/eventmanager/src/test/resources/META-INF/persistence.xml19
3 files changed, 110 insertions, 14 deletions
diff --git a/controlloop/common/eventmanager/pom.xml b/controlloop/common/eventmanager/pom.xml
index 518875b38..4e01c6699 100644
--- a/controlloop/common/eventmanager/pom.xml
+++ b/controlloop/common/eventmanager/pom.xml
@@ -19,7 +19,7 @@
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
@@ -181,5 +181,10 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>com.h2database</groupId>
+ <artifactId>h2</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManagerTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManagerTest.java
index 46452974e..d1763f52c 100644
--- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManagerTest.java
+++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopOperationManagerTest.java
@@ -36,6 +36,14 @@ import java.time.Instant;
import java.util.HashMap;
import java.util.UUID;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.NoResultException;
+import javax.persistence.NonUniqueResultException;
+import javax.persistence.Persistence;
+import javax.persistence.Query;
+
+
import org.apache.commons.io.IOUtils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
@@ -94,23 +102,62 @@ public class ControlLoopOperationManagerTest {
PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
}
+ private static EntityManagerFactory emf;
+ private static EntityManager em;
+
+
+ private static int getCount() {
+ // Create a query for number of items in DB
+ String sql = "select count(*) as count from operationshistory10";
+ Query nq = em.createNativeQuery(sql);
+
+ int numEvents = -1;
+ try {
+ numEvents = ((Number) nq.getSingleResult()).intValue();
+ } catch (NoResultException | NonUniqueResultException ex) {
+ logger.error("getCountFromDb threw: ", ex);
+ fail(ex.getMessage());
+ }
+ return numEvents;
+ }
+
+
/**
* Set up test class.
*/
@BeforeClass
- public static void setUpSimulator() {
+ public static void setUp() {
+
try {
org.onap.policy.simulators.Util.buildAaiSim();
} catch (Exception e) {
fail(e.getMessage());
}
+
+ // Set PU
+ System.setProperty("OperationsHistoryPU", "TestOperationsHistoryPU");
+
+ // Enter dummy props to avoid nullPointerException
+ PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.ONAP_KEY_URL, "a");
+ PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.ONAP_KEY_USER, "b");
+ PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.ONAP_KEY_PASS, "c");
+
+ // Connect to in-mem db
+ emf = Persistence.createEntityManagerFactory("TestOperationsHistoryPU");
+ em = emf.createEntityManager();
}
+
+ /**
+ * Clean up test class.
+ */
@AfterClass
- public static void tearDownSimulator() {
+ public static void tearDown() {
+ em.close();
+ emf.close();
HttpServletServer.factory.destroy();
}
-
+
@Test
public void testRetriesFail() {
//
@@ -759,5 +806,48 @@ public class ControlLoopOperationManagerTest {
System.setProperty("OperationsHistoryPU", "TestOperationsHistoryPU");
assertEquals(PolicyResult.FAILURE, clom.onResponse(soRw));
- }
+ }
+
+ @Test
+ public void testCommitAbatement() throws ControlLoopException, AaiException, IOException {
+
+ String yamlString = null;
+ try ( InputStream is = new FileInputStream(new File("src/test/resources/test.yaml")) ) {
+ yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
+ } catch (Exception e) {
+ fail(e.getMessage());
+ }
+
+ UUID requestId = UUID.randomUUID();
+ VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
+ onsetEvent.setClosedLoopControlName("TwoOnsetTest");
+ onsetEvent.setRequestId(requestId);
+ onsetEvent.setTarget("generic-vnf.vnf-id");
+ onsetEvent.setClosedLoopAlarmStart(Instant.now());
+ onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
+ onsetEvent.setAai(new HashMap<>());
+ onsetEvent.getAai().put("generic-vnf.vnf-name", "onsetOne");
+
+ ControlLoopEventManager manager =
+ new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestId());
+ VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
+ assertNotNull(notification);
+ assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
+
+ Policy policy = manager.getProcessor().getCurrentPolicy();
+ ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
+ assertNotNull(clom);
+
+ clom.startOperation(onsetEvent);
+
+ int numEventsBefore = getCount();
+ logger.info("numEventsBefore={}", numEventsBefore);
+
+ clom.commitAbatement("Test message","TEST_RESULT");
+
+ int numEventsAfter = getCount();
+ logger.info("numEventsAfter={}", numEventsAfter);
+
+ assertEquals(1, numEventsAfter - numEventsBefore);
+ }
}
diff --git a/controlloop/common/eventmanager/src/test/resources/META-INF/persistence.xml b/controlloop/common/eventmanager/src/test/resources/META-INF/persistence.xml
index 047d0ab6d..1c2eb12fd 100644
--- a/controlloop/common/eventmanager/src/test/resources/META-INF/persistence.xml
+++ b/controlloop/common/eventmanager/src/test/resources/META-INF/persistence.xml
@@ -22,16 +22,17 @@
<persistence-unit name="TestOperationsHistoryPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
- <class>org.onap.policy.whatever.WhatEver</class>
-
+ <class>org.onap.policy.controlloop.eventmanager.OperationsHistoryDbEntry</class>
+
<properties>
- <property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:apex_test" />
- <property name="javax.persistence.target-database" value="Derby" />
- <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
-
- <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
- <property name="eclipselink.ddl-generation.output-mode" value="database"/>
- <property name="eclipselink.logging.level" value="INFO" />
+ <property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
+ <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
+ <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
+ <property name="javax.persistence.jdbc.user" value="sa" />
+ <property name="javax.persistence.jdbc.password" value="" />
+ <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
+ <property name="eclipselink.ddl-generation.output-mode" value="database"/>
+ <property name="eclipselink.logging.level" value="INFO" />
</properties>
</persistence-unit>
</persistence>