summaryrefslogtreecommitdiffstats
path: root/controlloop/common/eventmanager/src
diff options
context:
space:
mode:
authorJoshua Reich <jreich@research.att.com>2018-08-19 00:57:54 -0700
committerJoshua Reich <jreich@research.att.com>2018-09-04 21:38:51 -0700
commit0185fd2a828f51ac8050aa6027c2837e0254dd80 (patch)
tree12658f3904a69c3c104f951523e12d119d1b9e42 /controlloop/common/eventmanager/src
parenta35ca0b16ffa99119b5d16cd005f91ff734c8df8 (diff)
Added junit test for commitAbatement.
Change-Id: Iee71481e357861618bed068f8b8ef37ec0ea2ab8 Issue-ID: POLICY-975 Signed-off-by: Joshua Reich <jreich@research.att.com>
Diffstat (limited to 'controlloop/common/eventmanager/src')
-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
2 files changed, 104 insertions, 13 deletions
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>