aboutsummaryrefslogtreecommitdiffstats
path: root/controlloop/common/eventmanager/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'controlloop/common/eventmanager/src/test/java')
-rw-r--r--controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/drl/legacy/ControlLoopParamsTest.java26
-rw-r--r--controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java120
-rw-r--r--controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/processor/ControlLoopProcessorTest.java34
-rw-r--r--controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/utils/ControlLoopUtilsTest.java20
4 files changed, 105 insertions, 95 deletions
diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/drl/legacy/ControlLoopParamsTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/drl/legacy/ControlLoopParamsTest.java
index 7bb2c5c96..f636e08f6 100644
--- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/drl/legacy/ControlLoopParamsTest.java
+++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/drl/legacy/ControlLoopParamsTest.java
@@ -22,6 +22,12 @@ package org.onap.policy.controlloop.drl.legacy;
import static org.junit.Assert.assertEquals;
+import com.openpojo.reflection.PojoClass;
+import com.openpojo.reflection.impl.PojoClassFactory;
+import com.openpojo.validation.Validator;
+import com.openpojo.validation.ValidatorBuilder;
+import com.openpojo.validation.test.impl.GetterTester;
+import com.openpojo.validation.test.impl.SetterTester;
import org.junit.Before;
import org.junit.Test;
@@ -30,7 +36,6 @@ public class ControlLoopParamsTest {
private static final String POLICY_NAME = "m";
private static final String POLICY_SCOPE = "s";
private static final String POLICY_VERSION = "v";
- private static final String CONTROL_LOOP_YAML = "y";
private ControlLoopParams clp = new ControlLoopParams();
@@ -43,17 +48,19 @@ public class ControlLoopParamsTest {
clp.setPolicyName(POLICY_NAME);
clp.setPolicyScope(POLICY_SCOPE);
clp.setPolicyVersion(POLICY_VERSION);
- clp.setControlLoopYaml(CONTROL_LOOP_YAML);
}
@Test
- public void getClosedLoopControlName() {
- assertEquals(CONTROL_LOOP_NAME, clp.getClosedLoopControlName());
+ public void testPojo() {
+ PojoClass controlLoopParams = PojoClassFactory.getPojoClass(ControlLoopParams.class);
+ Validator validator = ValidatorBuilder.create()
+ .with(new SetterTester(), new GetterTester()).build();
+ validator.validate(controlLoopParams);
}
@Test
- public void getControlLoopYaml() {
- assertEquals(CONTROL_LOOP_YAML, clp.getControlLoopYaml());
+ public void getClosedLoopControlName() {
+ assertEquals(CONTROL_LOOP_NAME, clp.getClosedLoopControlName());
}
@Test
@@ -78,12 +85,6 @@ public class ControlLoopParamsTest {
}
@Test
- public void setControlLoopYaml() {
- clp.setControlLoopYaml(CONTROL_LOOP_YAML.toUpperCase());
- assertEquals(CONTROL_LOOP_YAML.toUpperCase(), clp.getControlLoopYaml());
- }
-
- @Test
public void setPolicyName() {
clp.setPolicyName(POLICY_NAME.toUpperCase());
assertEquals(POLICY_NAME.toUpperCase(), clp.getPolicyName());
@@ -108,7 +109,6 @@ public class ControlLoopParamsTest {
other.setPolicyName(POLICY_NAME);
other.setPolicyScope(POLICY_SCOPE);
other.setPolicyVersion(POLICY_VERSION);
- other.setControlLoopYaml(CONTROL_LOOP_YAML);
assertEquals(clp, other);
assertEquals(clp.hashCode(), other.hashCode());
diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java
index 21b082c82..266ad1ac9 100644
--- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java
+++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/eventmanager/ControlLoopEventManagerTest.java
@@ -1,8 +1,8 @@
/*-
* ============LICENSE_START=======================================================
- * unit test
+ * ONAP
* ================================================================================
- * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,11 +39,14 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.io.IOUtils;
+import org.jetbrains.annotations.NotNull;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
@@ -51,6 +54,8 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.io.Serializer;
import org.onap.policy.controlloop.ControlLoopEventStatus;
import org.onap.policy.controlloop.ControlLoopException;
@@ -64,6 +69,7 @@ import org.onap.policy.drools.core.lock.Lock;
import org.onap.policy.drools.core.lock.LockCallback;
import org.onap.policy.drools.system.PolicyEngineConstants;
import org.onap.policy.drools.utils.Pair;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.powermock.reflect.Whitebox;
public class ControlLoopEventManagerTest {
@@ -160,16 +166,7 @@ public class ControlLoopEventManagerTest {
@Test
public void testAlreadyActivated() {
- UUID requestId = UUID.randomUUID();
- VirtualControlLoopEvent event = new VirtualControlLoopEvent();
- event.setClosedLoopControlName(TWO_ONSET_TEST);
- event.setRequestId(requestId);
- event.setTarget(VNF_ID);
- event.setClosedLoopAlarmStart(Instant.now());
- event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
- event.setAai(new HashMap<>());
- event.getAai().put(VNF_NAME, ONSET_ONE);
- event.setTargetType(ControlLoopTargetType.VNF);
+ VirtualControlLoopEvent event = getOnsetEvent();
ControlLoopEventManager manager = makeManager(event);
manager.setActivated(true);
@@ -178,23 +175,13 @@ public class ControlLoopEventManagerTest {
}
@Test
- public void testActivationYaml() throws IOException {
-
- UUID requestId = UUID.randomUUID();
- VirtualControlLoopEvent event = new VirtualControlLoopEvent();
- event.setClosedLoopControlName(TWO_ONSET_TEST);
- event.setRequestId(requestId);
- event.setTarget(VNF_ID);
- event.setClosedLoopAlarmStart(Instant.now());
- event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
- event.setAai(new HashMap<>());
- event.getAai().put(VNF_NAME, ONSET_ONE);
- event.setTargetType(ControlLoopTargetType.VNF);
+ public void testActivationYaml() throws IOException, CoderException {
+ VirtualControlLoopEvent event = getOnsetEvent();
ControlLoopEventManager manager = makeManager(event);
// Null YAML should fail
- VirtualControlLoopNotification notificationNull = manager.activate(null, event);
+ VirtualControlLoopNotification notificationNull = manager.activate((String) null, event);
assertNotNull(notificationNull);
assertEquals(ControlLoopNotificationType.REJECTED, notificationNull.getNotification());
@@ -226,17 +213,32 @@ public class ControlLoopEventManagerTest {
}
@Test
+ public void testActivateToscaLegacy() throws IOException, CoderException {
+ String policy =
+ new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-legacy-vcpe.json")));
+ ToscaPolicy toscaPolicy = new StandardCoder().decode(policy, ToscaPolicy.class);
+
+ VirtualControlLoopEvent event = getOnsetEvent();
+ ControlLoopEventManager manager = makeManager(event);
+
+ // trigger a reject by passing the wrong policy type
+ toscaPolicy.setType("onap.policies.controlloop.operational.common.Drools");
+ VirtualControlLoopNotification notification = manager.activate(toscaPolicy, event);
+ assertEquals(ControlLoopNotificationType.REJECTED, notification.getNotification());
+
+ // place back correct policy type
+ toscaPolicy.setType("onap.policies.controlloop.Operational");
+ notification = manager.activate(toscaPolicy, event);
+ assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
+
+ // another activate should fail
+ notification = manager.activate(toscaPolicy, event);
+ assertEquals(ControlLoopNotificationType.REJECTED, notification.getNotification());
+ }
+
+ @Test
public void testControlLoopFinal() throws Exception {
- UUID requestId = UUID.randomUUID();
- VirtualControlLoopEvent event = new VirtualControlLoopEvent();
- event.setClosedLoopControlName(TWO_ONSET_TEST);
- event.setRequestId(requestId);
- event.setTarget(VNF_ID);
- event.setClosedLoopAlarmStart(Instant.now());
- event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
- event.setAai(new HashMap<>());
- event.getAai().put(VNF_NAME, ONSET_ONE);
- event.setTargetType(ControlLoopTargetType.VNF);
+ VirtualControlLoopEvent event = getOnsetEvent();
ControlLoopEventManager manager = makeManager(event);
ControlLoopEventManager manager2 = manager;
@@ -293,6 +295,21 @@ public class ControlLoopEventManagerTest {
assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
}
+ @NotNull
+ private VirtualControlLoopEvent getOnsetEvent() {
+ UUID requestId = UUID.randomUUID();
+ VirtualControlLoopEvent event = new VirtualControlLoopEvent();
+ event.setClosedLoopControlName(TWO_ONSET_TEST);
+ event.setRequestId(requestId);
+ event.setTarget(VNF_ID);
+ event.setClosedLoopAlarmStart(Instant.now());
+ event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
+ event.setAai(new HashMap<>());
+ event.getAai().put(VNF_NAME, ONSET_ONE);
+ event.setTargetType(ControlLoopTargetType.VNF);
+ return event;
+ }
+
@Test
public void testProcessControlLoop() throws Exception {
UUID requestId = UUID.randomUUID();
@@ -633,16 +650,7 @@ public class ControlLoopEventManagerTest {
@Test
public void testControlLoopTimeout() throws IOException {
- UUID requestId = UUID.randomUUID();
- VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
- onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST);
- onsetEvent.setRequestId(requestId);
- onsetEvent.setTarget(VNF_ID);
- onsetEvent.setClosedLoopAlarmStart(Instant.now());
- onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
- onsetEvent.setAai(new HashMap<>());
- onsetEvent.getAai().put(VNF_NAME, ONSET_ONE);
- onsetEvent.setTargetType(ControlLoopTargetType.VNF);
+ VirtualControlLoopEvent onsetEvent = getOnsetEvent();
ControlLoopEventManager manager = makeManager(onsetEvent);
assertTrue(0 == manager.getControlLoopTimeout(null));
@@ -660,16 +668,7 @@ public class ControlLoopEventManagerTest {
@Test
public void testControlLoopTimeout_ZeroTimeout() throws IOException {
- UUID requestId = UUID.randomUUID();
- VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
- onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST);
- onsetEvent.setRequestId(requestId);
- onsetEvent.setTarget(VNF_ID);
- onsetEvent.setClosedLoopAlarmStart(Instant.now());
- onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
- onsetEvent.setAai(new HashMap<>());
- onsetEvent.getAai().put(VNF_NAME, ONSET_ONE);
- onsetEvent.setTargetType(ControlLoopTargetType.VNF);
+ VirtualControlLoopEvent onsetEvent = getOnsetEvent();
ControlLoopEventManager manager = makeManager(onsetEvent);
@@ -686,16 +685,7 @@ public class ControlLoopEventManagerTest {
@Test
public void testControlLoopTimeout_NullTimeout() throws IOException {
- UUID requestId = UUID.randomUUID();
- VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
- onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST);
- onsetEvent.setRequestId(requestId);
- onsetEvent.setTarget(VNF_ID);
- onsetEvent.setClosedLoopAlarmStart(Instant.now());
- onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
- onsetEvent.setAai(new HashMap<>());
- onsetEvent.getAai().put(VNF_NAME, ONSET_ONE);
- onsetEvent.setTargetType(ControlLoopTargetType.VNF);
+ VirtualControlLoopEvent onsetEvent = getOnsetEvent();
ControlLoopEventManager manager = makeManager(onsetEvent);
diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/processor/ControlLoopProcessorTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/processor/ControlLoopProcessorTest.java
index 84fe44914..f76c0060c 100644
--- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/processor/ControlLoopProcessorTest.java
+++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/processor/ControlLoopProcessorTest.java
@@ -1,8 +1,8 @@
/*-
* ============LICENSE_START=======================================================
- * unit test
+ * ONAP
* ================================================================================
- * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,12 +29,17 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.controlloop.ControlLoopException;
import org.onap.policy.controlloop.policy.FinalResult;
import org.onap.policy.controlloop.policy.Policy;
import org.onap.policy.controlloop.policy.PolicyResult;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -50,6 +55,31 @@ public class ControlLoopProcessorTest {
}
@Test
+ public void testControlLoopFromToscaLegacy() throws IOException, CoderException, ControlLoopException {
+ String policy =
+ new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-legacy-vcpe.json")));
+ assertNotNull(
+ new ControlLoopProcessor(new StandardCoder().decode(policy, ToscaPolicy.class)).getCurrentPolicy());
+ }
+
+ @Test
+ public void testControlLoopFromToscaCompliant() throws IOException, CoderException, ControlLoopException {
+ String policy =
+ new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-compliant-vcpe.json")));
+ assertNotNull(
+ new ControlLoopProcessor(new StandardCoder().decode(policy, ToscaPolicy.class)).getCurrentPolicy());
+ }
+
+ @Test
+ public void testControlLoopFromToscaCompliantBad() throws IOException, CoderException, ControlLoopException {
+ String policy =
+ new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-compliant-vcpe.json")));
+ ToscaPolicy toscaPolicy = new StandardCoder().decode(policy, ToscaPolicy.class);
+ toscaPolicy.setType("onap.policies.controlloop.Operational");
+ assertThatThrownBy(() -> new ControlLoopProcessor(toscaPolicy)).hasCauseInstanceOf(CoderException.class);
+ }
+
+ @Test
public void testControlLoopProcessorBadYaml() throws IOException {
InputStream is = new FileInputStream(new File("src/test/resources/string.yaml"));
String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
diff --git a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/utils/ControlLoopUtilsTest.java b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/utils/ControlLoopUtilsTest.java
index 2c26517cd..2e4811475 100644
--- a/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/utils/ControlLoopUtilsTest.java
+++ b/controlloop/common/eventmanager/src/test/java/org/onap/policy/controlloop/utils/ControlLoopUtilsTest.java
@@ -19,12 +19,10 @@
package org.onap.policy.controlloop.utils;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
import java.nio.file.Files;
import java.nio.file.Paths;
-import java.util.Map;
import org.junit.Test;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
@@ -35,22 +33,14 @@ public class ControlLoopUtilsTest {
@Test
public void testToControlLoopParams() throws Exception {
String policy =
- new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-operational-restart.json")));
-
+ new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-legacy-vcpe.json")));
ToscaPolicy toscaPolicy = new StandardCoder().decode(policy, ToscaPolicy.class);
+
ControlLoopParams params = ControlLoopUtils.toControlLoopParams(toscaPolicy);
- assertNotNull(params);
- assertNotNull(params.getClosedLoopControlName());
- assertEquals(toscaPolicy.getProperties().get("content"), params.getControlLoopYaml());
+ assertEquals("ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e", params.getClosedLoopControlName());
assertEquals(toscaPolicy.getName(), params.getPolicyName());
assertEquals(toscaPolicy.getVersion(), params.getPolicyVersion());
assertEquals(toscaPolicy.getType() + ":" + toscaPolicy.getVersion(), params.getPolicyScope());
-
- assertNull(ControlLoopUtils.toControlLoopParams(null));
-
- Map<String, Object> properties = toscaPolicy.getProperties();
- toscaPolicy.setProperties(null);
- assertNull(ControlLoopUtils.toControlLoopParams(toscaPolicy));
- toscaPolicy.setProperties(properties);
+ assertSame(toscaPolicy, params.getToscaPolicy());
}
} \ No newline at end of file