diff options
author | adheli.tavares <adheli.tavares@est.tech> | 2024-09-20 17:33:08 +0100 |
---|---|---|
committer | adheli.tavares <adheli.tavares@est.tech> | 2024-09-23 14:51:04 +0100 |
commit | cedfc60c8266c39ea72efa17aa3a644cbc223d19 (patch) | |
tree | 2146164f826b1d36e95e9aed7491ed949db4c3d7 /feature-lifecycle/src | |
parent | c274fd4a91bdbd86d8fc719f12834133adc6584d (diff) |
Increase code coverage overall
Issue-ID: POLICY-5068
Change-Id: I1763d3c00392ac655a998cd2fb869748a7323812
Signed-off-by: adheli.tavares <adheli.tavares@est.tech>
Diffstat (limited to 'feature-lifecycle/src')
16 files changed, 482 insertions, 204 deletions
diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFeature.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFeature.java index a50deb21..1fc45747 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFeature.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleFeature.java @@ -3,7 +3,7 @@ * ONAP * ================================================================================ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2023 Nordix Foundation. + * Modifications Copyright (C) 2023-2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,7 +38,7 @@ public class LifecycleFeature * Lifecycle FSM. */ @Getter - public static LifecycleFsm fsm = new LifecycleFsm(); + static LifecycleFsm fsm = new LifecycleFsm(); @Override public int getSequenceNumber() { diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateRunning.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateRunning.java index 2d5c66b3..14796cea 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateRunning.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/lifecycle/LifecycleStateRunning.java @@ -4,7 +4,7 @@ * ================================================================================ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Bell Canada. - * Modifications Copyright (C) 2021, 2023 Nordix Foundation. + * Modifications Copyright (C) 2021, 2023-2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -203,13 +203,15 @@ public abstract class LifecycleStateRunning extends LifecycleStateDefault { List<ToscaPolicy> failedUndeployPolicies = undeployPolicies(policies); if (!failedUndeployPolicies.isEmpty()) { - logger.warn("update-policies: undeployment failures: {}", fsm.getPolicyIdsMessage(failedUndeployPolicies)); + var failures = fsm.getPolicyIdsMessage(failedUndeployPolicies); + logger.warn("update-policies: undeployment failures: {}", failures); failedUndeployPolicies.forEach(fsm::failedUndeployPolicyAction); } List<ToscaPolicy> failedDeployPolicies = deployPolicies(policies); if (!failedDeployPolicies.isEmpty()) { - logger.warn("update-policies: deployment failures: {}", fsm.getPolicyIdsMessage(failedDeployPolicies)); + var failures = fsm.getPolicyIdsMessage(failedDeployPolicies); + logger.warn("update-policies: deployment failures: {}", failures); failedDeployPolicies.forEach(fsm::failedDeployPolicyAction); } @@ -237,11 +239,15 @@ public abstract class LifecycleStateRunning extends LifecycleStateDefault { List<ToscaPolicy> preNonNativePolicies = fsm.getNonNativePolicies(preActivePoliciesMap); preNonNativePolicies.retainAll(fsm.getNonNativePolicies(activePoliciesByType)); + var nonNativePoliciesIds = fsm.getPolicyIdsMessage(preNonNativePolicies); + var activePoliciesIds = fsm.getPolicyIdsMessage(activeNativeArtifactPolicies); + logger.info("re-applying non-native policies {} because new native artifact policies have been found: {}", - fsm.getPolicyIdsMessage(preNonNativePolicies), fsm.getPolicyIdsMessage(activeNativeArtifactPolicies)); + nonNativePoliciesIds, activePoliciesIds); List<ToscaPolicy> failedPolicies = syncPolicies(preNonNativePolicies, this::deployPolicy); - logger.info("re-applying non-native policies failures: {}", fsm.getPolicyIdsMessage(failedPolicies)); + var failedPoliciesIds = fsm.getPolicyIdsMessage(failedPolicies); + logger.info("re-applying non-native policies failures: {}", failedPoliciesIds); return failedPolicies; } @@ -268,9 +274,11 @@ public abstract class LifecycleStateRunning extends LifecycleStateDefault { List<ToscaPolicy> preNativeArtifactPolicies = fsm.getNativeArtifactPolicies(preActivePoliciesMap); preNativeArtifactPolicies.retainAll(fsm.getNativeArtifactPolicies(activePoliciesByType)); + var candidateIds = fsm.getPolicyIdsMessage(preNativeArtifactPolicies); + var activeIds = fsm.getPolicyIdsMessage(activeNativeControllerPolicies); + logger.info("reapply candidate native artifact policies {} as new native controller policies {} were found", - fsm.getPolicyIdsMessage(preNativeArtifactPolicies), - fsm.getPolicyIdsMessage(activeNativeControllerPolicies)); + candidateIds, activeIds); // from the intersection, only need to reapply those for which there is a new native // controller policy @@ -295,12 +303,14 @@ public abstract class LifecycleStateRunning extends LifecycleStateDefault { } } + var nativeArtPolIds = fsm.getPolicyIdsMessage(preNativeArtifactPoliciesToApply); + var newNativeCtrlPolIds = fsm.getPolicyIdsMessage(activeNativeControllerPolicies); logger.info("reapply set of native artifact policies {} as new native controller policies {} were found", - fsm.getPolicyIdsMessage(preNativeArtifactPoliciesToApply), - fsm.getPolicyIdsMessage(activeNativeControllerPolicies)); + nativeArtPolIds, newNativeCtrlPolIds); List<ToscaPolicy> failedPolicies = syncPolicies(preNativeArtifactPoliciesToApply, this::deployPolicy); - logger.info("re-applying native artifact policies failures: {}", fsm.getPolicyIdsMessage(failedPolicies)); + var failedIds = fsm.getPolicyIdsMessage(failedPolicies); + logger.info("re-applying native artifact policies failures: {}", failedIds); // since we want non-native policies to be reapplied when a new native artifact policy has been // reapplied here, remove it from the preActivePolicies, so it is detected as new. diff --git a/feature-lifecycle/src/main/java/org/onap/policy/drools/server/restful/RestLifecycleManager.java b/feature-lifecycle/src/main/java/org/onap/policy/drools/server/restful/RestLifecycleManager.java index a4abdf37..c755c444 100644 --- a/feature-lifecycle/src/main/java/org/onap/policy/drools/server/restful/RestLifecycleManager.java +++ b/feature-lifecycle/src/main/java/org/onap/policy/drools/server/restful/RestLifecycleManager.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2021, 2023-2023 Nordix Foundation. + * Modifications Copyright (C) 2021, 2023-2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -417,7 +417,7 @@ public class RestLifecycleManager implements LifecycleApi { private PdpUpdate getUndeployPolicyUpdate(List<ToscaPolicy> policies) { PdpUpdate update = getPolicyUpdate(); - update.setPoliciesToBeUndeployed(LifecycleFeature.fsm.getPolicyIds(policies)); + update.setPoliciesToBeUndeployed(LifecycleFeature.getFsm().getPolicyIds(policies)); return update; } diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/ControllerSupport.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/ControllerSupport.java index 858e8495..eadafe7c 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/ControllerSupport.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/ControllerSupport.java @@ -35,6 +35,8 @@ import org.onap.policy.drools.system.PolicyController; import org.onap.policy.drools.system.PolicyControllerConstants; import org.onap.policy.drools.util.KieUtils; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Controller Test Support. @@ -42,6 +44,8 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; @Getter public class ControllerSupport { + private static final Logger logger = LoggerFactory.getLogger(ControllerSupport.class); + protected static final String JUNIT_KMODULE_DRL_PATH = "src/test/resources/lifecycle.drl"; protected static final String JUNIT_KMODULE_POM_PATH = "src/test/resources/lifecycle.pom"; protected static final String JUNIT_KMODULE_PATH = "src/test/resources/lifecycle.kmodule"; @@ -62,20 +66,19 @@ public class ControllerSupport { /** * Create controller. */ - public PolicyController createController() throws IOException { + public void createController() throws IOException { try { PolicyController controller = getController(); controller.getDrools().delete(ToscaPolicy.class); - return controller; - } catch (IllegalArgumentException ignored) { // NOSONAR - ; // checkstyle + } catch (IllegalArgumentException e) { + logger.debug("error when creating controller", e); } ReleaseId coordinates = installArtifact(); Properties controllerProps = getControllerProps(coordinates); - return PolicyControllerConstants.getFactory().build(name, controllerProps); + PolicyControllerConstants.getFactory().build(name, controllerProps); } private Properties getControllerProps(ReleaseId coordinates) { @@ -140,7 +143,7 @@ public class ControllerSupport { * Change final marker in static field. */ public static <T> Field unsetFinalStaticAccess(Class<T> clazz, String fieldName) - throws NoSuchFieldException { + throws NoSuchFieldException { Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFeatureTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFeatureTest.java new file mode 100644 index 00000000..bb22316c --- /dev/null +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFeatureTest.java @@ -0,0 +1,164 @@ +/* + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2024 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.lifecycle; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.onap.policy.drools.lifecycle.LifecycleFsm.CONFIGURATION_PROPERTIES_NAME; + +import java.util.Properties; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.jupiter.MockitoExtension; +import org.onap.policy.drools.persistence.FileSystemPersistence; +import org.onap.policy.drools.persistence.SystemPersistenceConstants; +import org.onap.policy.drools.system.PolicyController; +import org.onap.policy.drools.system.PolicyEngine; + +@ExtendWith(MockitoExtension.class) +class LifecycleFeatureTest { + + LifecycleFeature feature; + + @Mock + LifecycleFsm fsm; + + @Mock + PolicyController controller; + + @Mock + PolicyEngine engine; + + AutoCloseable closeable; + + @BeforeEach + void setUp() { + closeable = MockitoAnnotations.openMocks(this); + + var props = new Properties(); + var fileManager = mock(FileSystemPersistence.class); + lenient().when(fileManager.getProperties(CONFIGURATION_PROPERTIES_NAME)).thenReturn(props); + + try (MockedStatic<SystemPersistenceConstants> constants = + Mockito.mockStatic(SystemPersistenceConstants.class)) { + constants.when(SystemPersistenceConstants::getManager).thenReturn(fileManager); + feature = mock(LifecycleFeature.class); + } + } + + @AfterEach + void tearDown() throws Exception { + closeable.close(); + } + + @Test + void getSequenceNumber() { + when(feature.getSequenceNumber()).thenCallRealMethod(); + + assertEquals(1, feature.getSequenceNumber()); + } + + @Test + void afterStart() { + when(fsm.start()).thenReturn(true); + doNothing().when(fsm).start(controller); + + when(feature.afterStart(engine)).thenCallRealMethod(); + when(feature.afterStart(controller)).thenCallRealMethod(); + + try (MockedStatic<LifecycleFeature> factory = Mockito.mockStatic(LifecycleFeature.class)) { + factory.when(LifecycleFeature::getFsm).thenReturn(fsm); + assertEquals(fsm, LifecycleFeature.getFsm()); + + assertFalse(feature.afterStart(controller)); + assertFalse(feature.afterStart(engine)); + } + } + + @Test + void beforeStop() { + when(fsm.stop()).thenReturn(true); + doNothing().when(fsm).stop(controller); + + when(feature.beforeStop(engine)).thenCallRealMethod(); + when(feature.beforeStop(controller)).thenCallRealMethod(); + + try (MockedStatic<LifecycleFeature> factory = Mockito.mockStatic(LifecycleFeature.class)) { + factory.when(LifecycleFeature::getFsm).thenReturn(fsm); + assertEquals(fsm, LifecycleFeature.getFsm()); + + assertFalse(feature.beforeStop(controller)); + assertFalse(feature.beforeStop(engine)); + } + } + + @Test + void beforeShutdown() { + doNothing().when(fsm).shutdown(); + + when(feature.beforeShutdown(engine)).thenCallRealMethod(); + + try (MockedStatic<LifecycleFeature> factory = Mockito.mockStatic(LifecycleFeature.class)) { + factory.when(LifecycleFeature::getFsm).thenReturn(fsm); + assertEquals(fsm, LifecycleFeature.getFsm()); + + assertFalse(feature.beforeShutdown(engine)); + } + } + + @Test + void beforeLock() { + doNothing().when(fsm).stop(controller); + + when(feature.beforeLock(controller)).thenCallRealMethod(); + + try (MockedStatic<LifecycleFeature> factory = Mockito.mockStatic(LifecycleFeature.class)) { + factory.when(LifecycleFeature::getFsm).thenReturn(fsm); + assertEquals(fsm, LifecycleFeature.getFsm()); + + assertFalse(feature.beforeLock(controller)); + } + } + + @Test + void afterUnlock() { + doNothing().when(fsm).start(controller); + + when(feature.afterUnlock(controller)).thenCallRealMethod(); + + try (MockedStatic<LifecycleFeature> factory = Mockito.mockStatic(LifecycleFeature.class)) { + factory.when(LifecycleFeature::getFsm).thenReturn(fsm); + assertEquals(fsm, LifecycleFeature.getFsm()); + + assertFalse(feature.afterUnlock(controller)); + } + } +}
\ No newline at end of file diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFsmTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFsmTest.java index cae3bb3b..9538cc44 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFsmTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFsmTest.java @@ -228,6 +228,11 @@ public class LifecycleFsmTest { fsm.getPolicyIds(List.of(opPolicy, controllerPolicy)).toString()); } + @Test + void testGetSequenceNumber() { + assertEquals(1, new LifecycleFeature().getSequenceNumber()); + } + protected void deployAllPolicies() { fsm.deployedPolicyAction(controllerPolicy); fsm.deployedPolicyAction(controller2Policy); diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFsmUpdateTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFsmUpdateTest.java index 8f0c19db..c24c61cf 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFsmUpdateTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleFsmUpdateTest.java @@ -143,7 +143,7 @@ public class LifecycleFsmUpdateTest { noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, TestConstants.APPC_CL_TOPIC); TopicEndpointManager.getManager().addTopics(noopTopicProperties); - savedFsm = LifecycleFeature.fsm; + savedFsm = LifecycleFeature.getFsm(); } /** @@ -174,7 +174,7 @@ public class LifecycleFsmUpdateTest { public void init() throws CoderException, IOException, NoSuchFieldException, IllegalAccessException { fsm = new LifecycleFsm() { @Override - protected ScheduledExecutorService makeExecutor() { // NOSONAR + protected ScheduledExecutorService makeExecutor() { return new PseudoScheduledExecutorService(new TestTimeMulti()); } }; @@ -417,14 +417,14 @@ public class LifecycleFsmUpdateTest { protected void verifyNativeArtifactPolicies(List<ToscaPolicy> policies) throws CoderException { // check that a brained controller exists for each native artifact policy for (ToscaPolicy policy : policies) { - NativeArtifactPolicy artifactPolicy = fsm.getDomainMaker().convertTo(policy, NativeArtifactPolicy.class); - String controllerName = artifactPolicy.getProperties().getController().getName(); + NativeArtifactPolicy naPolicy = fsm.getDomainMaker().convertTo(policy, NativeArtifactPolicy.class); + String controllerName = naPolicy.getProperties().getController().getName(); assertTrue(PolicyControllerConstants.getFactory().get(controllerName).getDrools().isBrained()); - assertEquals(artifactPolicy.getProperties().getRulesArtifact().getGroupId(), + assertEquals(naPolicy.getProperties().getRulesArtifact().getGroupId(), PolicyControllerConstants.getFactory().get(controllerName).getDrools().getGroupId()); - assertEquals(artifactPolicy.getProperties().getRulesArtifact().getArtifactId(), + assertEquals(naPolicy.getProperties().getRulesArtifact().getArtifactId(), PolicyControllerConstants.getFactory().get(controllerName).getDrools().getArtifactId()); - assertEquals(artifactPolicy.getProperties().getRulesArtifact().getVersion(), + assertEquals(naPolicy.getProperties().getRulesArtifact().getVersion(), PolicyControllerConstants.getFactory().get(controllerName).getDrools().getVersion()); } } @@ -446,9 +446,9 @@ public class LifecycleFsmUpdateTest { // and there is a controller policy for each controllerName for (ToscaPolicy nativePolicy : nativeArtifactPolicies) { - NativeArtifactPolicy artifactPolicy = + NativeArtifactPolicy naPolicy = fsm.getDomainMaker().convertTo(nativePolicy, NativeArtifactPolicy.class); - String artifactControllerName = artifactPolicy.getProperties().getController().getName(); + String artifactControllerName = naPolicy.getProperties().getController().getName(); // brained controller check assertTrue(PolicyControllerConstants.getFactory().get(artifactControllerName).getDrools().isBrained(), @@ -545,13 +545,13 @@ public class LifecycleFsmUpdateTest { protected List<ControllerPolicy> getNativeControllerPolicies(List<ToscaPolicy> nativePolicies, String controllerName) { - return nativePolicies.stream().map(controllerPolicy -> { + return nativePolicies.stream().map(nativePolicy -> { try { - return fsm.getDomainMaker().convertTo(controllerPolicy, ControllerPolicy.class); + return fsm.getDomainMaker().convertTo(nativePolicy, ControllerPolicy.class); } catch (CoderException ex) { - throw new RuntimeException(controllerPolicy.getIdentifier().toString(), ex); + throw new RuntimeException(nativePolicy.getIdentifier().toString(), ex); } - }).filter(controllerPolicy -> controllerName.equals(controllerPolicy.getProperties().getControllerName())) + }).filter(nativePolicy -> controllerName.equals(nativePolicy.getProperties().getControllerName())) .collect(Collectors.toList()); } diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java index 86e6ce3c..80fe946f 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateActiveTest.java @@ -186,8 +186,6 @@ class LifecycleStateActiveTest extends LifecycleStateRunningTest { @Test void testUpdate() throws IOException, CoderException { - // TODO: extract repeated similar assertion blocks into their own helper methods - PdpUpdate update = new PdpUpdate(); update.setName(PolicyEngineConstants.getManager().getPdpName()); update.setPdpGroup("W"); @@ -311,9 +309,9 @@ class LifecycleStateActiveTest extends LifecycleStateRunningTest { factPolicies = controllerSupport.getFacts(ToscaPolicy.class); assertEquals(2, factPolicies.size()); - assertTrue(factPolicies.stream().noneMatch((ff) -> Objects.equals(toscaPolicyRestartV1, ff))); - assertTrue(factPolicies.stream().anyMatch((ff) -> Objects.equals(toscaPolicyRestartV2, ff))); - assertTrue(factPolicies.stream().anyMatch((ff) -> Objects.equals(toscaPolicyFirewall, ff))); + assertTrue(factPolicies.stream().noneMatch(ff -> Objects.equals(toscaPolicyRestartV1, ff))); + assertTrue(factPolicies.stream().anyMatch(ff -> Objects.equals(toscaPolicyRestartV2, ff))); + assertTrue(factPolicies.stream().anyMatch(ff -> Objects.equals(toscaPolicyFirewall, ff))); assertEquals(2, fsm.policiesMap.size()); long originalInterval = fsm.getStatusTimerSeconds(); diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java index 01fb90b5..67e741ac 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStatePassiveTest.java @@ -72,13 +72,13 @@ class LifecycleStatePassiveTest extends LifecycleStateRunningTest { fsm.start(controllerSupport.getController()); assertSame(controllerSupport.getController(), ((PolicyTypeDroolsController) fsm.getController( - new ToscaConceptIdentifier( - ControllerSupport.POLICY_TYPE_COMPLIANT_OP, ControllerSupport.POLICY_TYPE_VERSION))) + new ToscaConceptIdentifier( + ControllerSupport.POLICY_TYPE_COMPLIANT_OP, ControllerSupport.POLICY_TYPE_VERSION))) .controllers().get(0)); fsm.stop(controllerSupport.getController()); assertNull(fsm.getController(new ToscaConceptIdentifier(ControllerSupport.POLICY_TYPE_COMPLIANT_OP, - ControllerSupport.POLICY_TYPE_VERSION))); + ControllerSupport.POLICY_TYPE_VERSION))); fsm.shutdown(); } @@ -98,16 +98,6 @@ class LifecycleStatePassiveTest extends LifecycleStateRunningTest { assertBasicTerminated(); } - private void simpleStart() { - assertTrue(fsm.start()); - assertBasicPassive(); - } - - private void simpleStop() { - assertTrue(fsm.stop()); - assertBasicTerminated(); - } - @Test void testShutdown() throws Exception { simpleStop(); @@ -129,14 +119,6 @@ class LifecycleStatePassiveTest extends LifecycleStateRunningTest { fsm.shutdown(); } - private void status() { - waitUntil(5, TimeUnit.SECONDS, isStatus(PdpState.PASSIVE, 1)); - waitUntil(fsm.statusTimerSeconds + 2, TimeUnit.SECONDS, isStatus(PdpState.PASSIVE, 1 + 1)); - waitUntil(fsm.statusTimerSeconds + 2, TimeUnit.SECONDS, isStatus(PdpState.PASSIVE, 1 + 2)); - assertTrue(fsm.status()); - waitUntil(200, TimeUnit.MILLISECONDS, isStatus(PdpState.PASSIVE, 1 + 3)); - } - @Test void testUpdate() throws CoderException { controllerSupport.getController().getDrools().delete(ToscaPolicy.class); @@ -154,7 +136,7 @@ class LifecycleStatePassiveTest extends LifecycleStateRunningTest { int qlength = fsm.client.getSink().getRecentEvents().length; PdpStatus lastStatus = new StandardCoder().decode(fsm.client.getSink().getRecentEvents()[qlength - 1], - PdpStatus.class); + PdpStatus.class); assertEquals("foo", lastStatus.getPdpType()); assertEquals(update.getRequestId(), lastStatus.getRequestId()); assertEquals(update.getRequestId(), lastStatus.getResponse().getResponseTo()); @@ -165,21 +147,10 @@ class LifecycleStatePassiveTest extends LifecycleStateRunningTest { assertEquals("z", fsm.getSubGroup()); assertBasicPassive(); - ToscaPolicy toscaPolicy = - getExamplesPolicy("policies/vCPE.policy.operational.input.tosca.json", "operational.restart"); + var policyResourcePath = "policies/vCPE.policy.operational.input.tosca.json"; + ToscaPolicy toscaPolicy = getExamplesPolicy(policyResourcePath, "operational.restart"); toscaPolicy.getProperties().put("controllerName", "lifecycle"); - update.setPoliciesToBeDeployed(List.of(toscaPolicy)); - - assertFalse(fsm.update(update)); - - assertEquals(PdpState.PASSIVE, fsm.state()); - assertEquals(interval, fsm.getStatusTimerSeconds()); - assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup()); - assertEquals("z", fsm.getSubGroup()); - assertBasicPassive(); - - assertEquals(2, fsm.policyTypesMap.size()); - assertTrue(fsm.policiesMap.isEmpty()); + verifyVcpePolicyDeployed(update, toscaPolicy, interval); update.setPdpGroup(null); update.setPdpSubgroup(null); @@ -222,32 +193,7 @@ class LifecycleStatePassiveTest extends LifecycleStateRunningTest { assertBasicPassive(); assertEquals(0, controllerSupport.getController().getDrools().factCount("junits")); - // The "update" event will undeploy "toscaPolicy" and deploy "toscaPolicy2" - ToscaPolicy toscaPolicy2 = - getExamplesPolicy("policies/vFirewall.policy.operational.input.tosca.json", "operational.modifyconfig"); - toscaPolicy.getProperties().remove("controllerName"); - update.setPoliciesToBeUndeployed(List.of(toscaPolicy.getIdentifier())); - update.setPoliciesToBeDeployed(List.of(toscaPolicy2)); - assertTrue(fsm.update(update)); - assertEquals(3, fsm.policyTypesMap.size()); - assertEquals(1, fsm.policiesMap.size()); - assertEquals(toscaPolicy2, fsm.policiesMap.get(toscaPolicy2.getIdentifier())); - assertNull(fsm.policiesMap.get(toscaPolicy.getIdentifier())); - assertEquals(0, controllerSupport.getController().getDrools().factCount("junits")); - - update.setPdpGroup(null); - update.setPdpSubgroup(null); - update.setPoliciesToBeUndeployed(List.of(toscaPolicy2.getIdentifier())); - update.setPoliciesToBeDeployed(List.of()); - assertTrue(fsm.update(update)); - assertEquals(3, fsm.policyTypesMap.size()); - assertEquals(0, fsm.policiesMap.size()); - assertEquals(PdpState.PASSIVE, fsm.state()); - assertEquals(interval, fsm.getStatusTimerSeconds()); - assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup()); - assertNull(fsm.getSubGroup()); - assertBasicPassive(); - assertEquals(0, controllerSupport.getController().getDrools().factCount("junits")); + verifyUpdateUndeployToscaPolicyDeployToscaPolicy2(toscaPolicy, update, interval); fsm.shutdown(); } @@ -309,6 +255,16 @@ class LifecycleStatePassiveTest extends LifecycleStateRunningTest { fsm.shutdown(); } + private void simpleStart() { + assertTrue(fsm.start()); + assertBasicPassive(); + } + + private void simpleStop() { + assertTrue(fsm.stop()); + assertBasicTerminated(); + } + private void assertBasicTerminated() { assertEquals(PdpState.TERMINATED, fsm.state.state()); assertFalse(fsm.isAlive()); @@ -348,4 +304,57 @@ class LifecycleStatePassiveTest extends LifecycleStateRunningTest { assertFalse(fsm.statusTask.isCancelled()); assertFalse(fsm.statusTask.isDone()); } + + private void status() { + waitUntil(5, TimeUnit.SECONDS, isStatus(PdpState.PASSIVE, 1)); + waitUntil(fsm.statusTimerSeconds + 2, TimeUnit.SECONDS, isStatus(PdpState.PASSIVE, 1 + 1)); + waitUntil(fsm.statusTimerSeconds + 2, TimeUnit.SECONDS, isStatus(PdpState.PASSIVE, 1 + 2)); + assertTrue(fsm.status()); + waitUntil(200, TimeUnit.MILLISECONDS, isStatus(PdpState.PASSIVE, 1 + 3)); + } + + private void verifyVcpePolicyDeployed(PdpUpdate update, ToscaPolicy toscaPolicy, long interval) { + update.setPoliciesToBeDeployed(List.of(toscaPolicy)); + + assertFalse(fsm.update(update)); + + assertEquals(PdpState.PASSIVE, fsm.state()); + assertEquals(interval, fsm.getStatusTimerSeconds()); + assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup()); + assertEquals("z", fsm.getSubGroup()); + assertBasicPassive(); + + assertEquals(2, fsm.policyTypesMap.size()); + assertTrue(fsm.policiesMap.isEmpty()); + } + + private void verifyUpdateUndeployToscaPolicyDeployToscaPolicy2(ToscaPolicy toscaPolicy, PdpUpdate update, + long interval) throws CoderException { + // The "update" event will undeploy "toscaPolicy" and deploy "toscaPolicy2" + String policyResourcePath = "policies/vFirewall.policy.operational.input.tosca.json"; + ToscaPolicy toscaPolicy2 = getExamplesPolicy(policyResourcePath, "operational.modifyconfig"); + toscaPolicy.getProperties().remove("controllerName"); + update.setPoliciesToBeUndeployed(List.of(toscaPolicy.getIdentifier())); + update.setPoliciesToBeDeployed(List.of(toscaPolicy2)); + assertTrue(fsm.update(update)); + assertEquals(3, fsm.policyTypesMap.size()); + assertEquals(1, fsm.policiesMap.size()); + assertEquals(toscaPolicy2, fsm.policiesMap.get(toscaPolicy2.getIdentifier())); + assertNull(fsm.policiesMap.get(toscaPolicy.getIdentifier())); + assertEquals(0, controllerSupport.getController().getDrools().factCount("junits")); + + update.setPdpGroup(null); + update.setPdpSubgroup(null); + update.setPoliciesToBeUndeployed(List.of(toscaPolicy2.getIdentifier())); + update.setPoliciesToBeDeployed(List.of()); + assertTrue(fsm.update(update)); + assertEquals(3, fsm.policyTypesMap.size()); + assertEquals(0, fsm.policiesMap.size()); + assertEquals(PdpState.PASSIVE, fsm.state()); + assertEquals(interval, fsm.getStatusTimerSeconds()); + assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup()); + assertNull(fsm.getSubGroup()); + assertBasicPassive(); + assertEquals(0, controllerSupport.getController().getDrools().factCount("junits")); + } } diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateSafeTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateSafeTest.java index 4c292b42..3d69ea9a 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateSafeTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateSafeTest.java @@ -36,10 +36,11 @@ class LifecycleStateSafeTest extends LifecycleStateUnsupportedTest { } @Override - public LifecycleState create(LifecycleFsm fsm) { - return new LifecycleStateSafe(fsm); + public void create(LifecycleFsm fsm) { + new LifecycleStateSafe(fsm); } + @Override @Test public void constructor() { super.constructor(); diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateTestTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateTestTest.java index 89aae201..e6c7d145 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateTestTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateTestTest.java @@ -36,10 +36,11 @@ class LifecycleStateTestTest extends LifecycleStateUnsupportedTest { } @Override - public LifecycleState create(LifecycleFsm fsm) { - return new LifecycleStateTest(fsm); + public void create(LifecycleFsm fsm) { + new LifecycleStateTest(fsm); } + @Override @Test public void constructor() { super.constructor(); diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateUnsupportedTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateUnsupportedTest.java index b5ec6aca..bd412179 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateUnsupportedTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/LifecycleStateUnsupportedTest.java @@ -24,12 +24,14 @@ package org.onap.policy.drools.lifecycle; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import java.util.ArrayList; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.onap.policy.drools.persistence.SystemPersistenceConstants; import org.onap.policy.models.pdp.concepts.PdpStateChange; import org.onap.policy.models.pdp.concepts.PdpUpdate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; /** * Lifecycle State Unsupported Test. @@ -52,7 +54,7 @@ public abstract class LifecycleStateUnsupportedTest { this.state = state; } - public abstract LifecycleState create(LifecycleFsm fsm); + public abstract void create(LifecycleFsm fsm); @Test public void constructor() { @@ -109,4 +111,11 @@ public abstract class LifecycleStateUnsupportedTest { assertThatThrownBy(() -> state.transitionToState(active)) .isInstanceOf(UnsupportedOperationException.class); } + + @Test + public void updatePolicies() { + var list = new ArrayList<ToscaPolicy>(); + assertThatThrownBy(() -> state.updatePolicies(list)) + .isInstanceOf(UnsupportedOperationException.class); + } } diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeDroolsControllerTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeDroolsControllerTest.java index 999860a0..1208662a 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeDroolsControllerTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeDroolsControllerTest.java @@ -21,16 +21,24 @@ package org.onap.policy.drools.lifecycle; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; -import org.junit.jupiter.api.BeforeEach; +import java.util.ArrayList; +import java.util.List; import org.junit.jupiter.api.Test; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.drools.domain.models.operational.OperationalPolicy; import org.onap.policy.drools.system.PolicyControllerConstants; +import org.onap.policy.drools.system.internal.AggregatedPolicyController; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; /** @@ -41,7 +49,7 @@ class PolicyTypeDroolsControllerTest extends LifecycleStateRunningTest { // Operational vCPE Policies private static final String OP_POLICY_NAME_VCPE = "operational.restart"; private static final String VCPE_OPERATIONAL_DROOLS_POLICY_JSON = - "policies/vCPE.policy.operational.input.tosca.json"; + "policies/vCPE.policy.operational.input.tosca.json"; private ToscaPolicy policy; private PolicyTypeDroolsController controller; @@ -49,7 +57,6 @@ class PolicyTypeDroolsControllerTest extends LifecycleStateRunningTest { /** * Test initialization. */ - @BeforeEach public void init() throws CoderException { fsm = makeFsmWithPseudoTime(); policy = getExamplesPolicy(VCPE_OPERATIONAL_DROOLS_POLICY_JSON, OP_POLICY_NAME_VCPE); @@ -72,7 +79,8 @@ class PolicyTypeDroolsControllerTest extends LifecycleStateRunningTest { } @Test - void testDeployUndeploy() { + void testDeployUndeploy() throws CoderException { + init(); /* non-existing controller */ assertFalse(controller.undeploy(policy)); assertFalse(controller.deploy(policy)); @@ -97,6 +105,40 @@ class PolicyTypeDroolsControllerTest extends LifecycleStateRunningTest { undeploy(); // one more time } + @Test + void testNullExceptions() { + var mockController = mock(PolicyTypeDroolsController.class); + when(mockController.deploy(isNull())).thenCallRealMethod(); + doCallRealMethod().when(mockController).remove(isNull()); + when(mockController.undeploy(isNull())).thenCallRealMethod(); + doCallRealMethod().when(mockController).add(isNull()); + + assertThatThrownBy(() -> mockController.deploy(null)) + .hasMessageContaining("policy is marked non-null but is null"); + + assertThatThrownBy(() -> mockController.remove(null)) + .hasMessageContaining("controller is marked non-null but is null"); + + assertThatThrownBy(() -> mockController.undeploy(null)) + .hasMessageContaining("policy is marked non-null but is null"); + + assertThatThrownBy(() -> mockController.add(null)) + .hasMessageContaining("controller is marked non-null but is null"); + } + + @Test + void testAddController_DoesNotMatchPolicyType() { + var newController = mock(AggregatedPolicyController.class); + when(newController.getPolicyTypes()).thenReturn(new ArrayList<>(List.of(mock(ToscaConceptIdentifier.class)))); + when(newController.getName()).thenReturn("mockControllerName"); + + var mockController = mock(PolicyTypeDroolsController.class); + doCallRealMethod().when(mockController).add(newController); + + assertThatThrownBy(() -> mockController.add(newController)) + .hasMessageContaining("controller mockControllerName does not support"); + } + protected void undeploy() { assertTrue(controller.undeploy(policy)); assertFalse(controllerSupport.getController().getDrools().exists(policy)); diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeArtifactControllerTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeArtifactControllerTest.java index 26e6bbe4..ae11fd27 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeArtifactControllerTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeArtifactControllerTest.java @@ -114,8 +114,6 @@ class PolicyTypeNativeArtifactControllerTest extends LifecycleStateRunningTest { /* idempotence */ assertTrue(controller.deploy(policy)); assertDeployed(); - - // TODO: test a point version upgrade } private void assertUndeployed() { diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeDroolsControllerTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeDroolsControllerTest.java index a964d134..29272a01 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeDroolsControllerTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/lifecycle/PolicyTypeNativeDroolsControllerTest.java @@ -46,7 +46,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; class PolicyTypeNativeDroolsControllerTest extends LifecycleStateRunningTest { private static final String EXAMPLE_NATIVE_DROOLS_POLICY_NAME = "example.controller"; private static final String EXAMPLE_NATIVE_DROOLS_POLICY_JSON = - "src/test/resources/tosca-policy-native-controller-example.json"; + "src/test/resources/tosca-policy-native-controller-example.json"; /** * Test initialization. @@ -75,7 +75,7 @@ class PolicyTypeNativeDroolsControllerTest extends LifecycleStateRunningTest { ToscaPolicy policy = getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_POLICY_NAME); ControllerPolicy controllerPolicy = fsm.getDomainMaker().convertTo(policy, ControllerPolicy.class); PolicyTypeNativeDroolsController controller = - new PolicyTypeNativeDroolsController(policy.getTypeIdentifier(), fsm); + new PolicyTypeNativeDroolsController(policy.getTypeIdentifier(), fsm); assertTrue(controller.undeploy(policy)); assertThatIllegalArgumentException().isThrownBy( () -> PolicyControllerConstants.getFactory().get(controllerPolicy.getName())); @@ -103,9 +103,9 @@ class PolicyTypeNativeDroolsControllerTest extends LifecycleStateRunningTest { TopicEndpointManager.getManager().addTopics(noopTopicProperties); ToscaPolicy nativeControllerPolicy = - getExamplesPolicy("policies/usecases.native.controller.policy.input.tosca.json", "usecases"); + getExamplesPolicy("policies/usecases.native.controller.policy.input.tosca.json", "usecases"); PolicyTypeNativeDroolsController controller = - new PolicyTypeNativeDroolsController(nativeControllerPolicy.getTypeIdentifier(), fsm); + new PolicyTypeNativeDroolsController(nativeControllerPolicy.getTypeIdentifier(), fsm); assertTrue(controller.deploy(nativeControllerPolicy)); Properties properties = PolicyControllerConstants.getFactory().get("usecases").getProperties(); @@ -115,70 +115,90 @@ class PolicyTypeNativeDroolsControllerTest extends LifecycleStateRunningTest { assertNull(properties.getProperty("rules.artifactId")); assertNull(properties.getProperty("rules.version")); + assertSourceTopics(properties); + + assertSinkTopics(properties); + + assertEquals("test", properties.getProperty("notes")); + assertEquals("auto", properties.getProperty("persistence.type")); + + assertTrue(controller.undeploy(nativeControllerPolicy)); + } + + private static void assertSourceTopics(Properties properties) { assertEquals("dcae_topic,appc-cl,appc-lcm-write,sdnr-cl-rsp", - properties.getProperty("noop.source.topics")); - assertEquals("appc-cl,appc-lcm-read,policy-cl-mgt,dcae_cl_rsp", - properties.getProperty("noop.sink.topics")); + properties.getProperty("noop.source.topics")); assertEquals("org.onap.policy.controlloop.CanonicalOnset,org.onap.policy.controlloop.CanonicalAbated", - properties.getProperty("noop.source.topics.dcae_topic.events")); + properties.getProperty("noop.source.topics.dcae_topic.events")); + assertEquals("[?($.closedLoopEventStatus == 'ONSET')]", - properties - .getProperty("noop.source.topics.dcae_topic.events.org.onap.policy.controlloop.CanonicalOnset.filter")); + properties.getProperty("noop.source.topics.dcae_topic.events." + + "org.onap.policy.controlloop.CanonicalOnset.filter")); + assertEquals("[?($.closedLoopEventStatus == 'ABATED')]", - properties - .getProperty("noop.source.topics.dcae_topic.events." - + "org.onap.policy.controlloop.CanonicalAbated.filter")); + properties.getProperty("noop.source.topics.dcae_topic.events." + + "org.onap.policy.controlloop.CanonicalAbated.filter")); + assertEquals("org.onap.policy.controlloop.util.Serialization,gson", - properties.getProperty("noop.source.topics.dcae_topic.events.custom.gson")); + properties.getProperty("noop.source.topics.dcae_topic.events.custom.gson")); + + assertEquals("org.onap.policy.appc.Response", + properties.getProperty("noop.source.topics.appc-cl.events")); - assertEquals("org.onap.policy.appc.Response", properties.getProperty("noop.source.topics.appc-cl.events")); assertEquals("[?($.CommonHeader && $.Status)]", - properties - .getProperty("noop.source.topics.appc-cl.events.org.onap.policy.appc.Response.filter")); + properties.getProperty("noop.source.topics.appc-cl.events.org.onap.policy.appc.Response.filter")); + assertEquals("org.onap.policy.appc.util.Serialization,gsonPretty", - properties.getProperty("noop.source.topics.appc-cl.events.custom.gson")); + properties.getProperty("noop.source.topics.appc-cl.events.custom.gson")); assertEquals("org.onap.policy.appclcm.AppcLcmMessageWrapper", - properties.getProperty("noop.source.topics.appc-lcm-write.events")); + properties.getProperty("noop.source.topics.appc-lcm-write.events")); + assertEquals("[?($.type == 'response')]", - properties - .getProperty("noop.source.topics.appc-lcm-write.events." - + "org.onap.policy.appclcm.AppcLcmMessageWrapper.filter")); + properties.getProperty("noop.source.topics.appc-lcm-write.events." + + "org.onap.policy.appclcm.AppcLcmMessageWrapper.filter")); + assertEquals("org.onap.policy.appclcm.util.Serialization,gson", - properties.getProperty("noop.source.topics.appc-lcm-write.events.custom.gson")); + properties.getProperty("noop.source.topics.appc-lcm-write.events.custom.gson")); assertEquals("org.onap.policy.sdnr.PciResponseWrapper", - properties.getProperty("noop.source.topics.sdnr-cl-rsp.events")); + properties.getProperty("noop.source.topics.sdnr-cl-rsp.events")); + assertEquals("[?($.type == 'response')]", - properties - .getProperty("noop.source.topics.sdnr-cl-rsp.events." - + "org.onap.policy.sdnr.PciResponseWrapper.filter")); + properties.getProperty("noop.source.topics.sdnr-cl-rsp.events." + + "org.onap.policy.sdnr.PciResponseWrapper.filter")); + assertEquals("org.onap.policy.sdnr.util.Serialization,gson", - properties.getProperty("noop.source.topics.sdnr-cl-rsp.events.custom.gson")); + properties.getProperty("noop.source.topics.sdnr-cl-rsp.events.custom.gson")); + } + + private static void assertSinkTopics(Properties properties) { + assertEquals("appc-cl,appc-lcm-read,policy-cl-mgt,dcae_cl_rsp", + properties.getProperty("noop.sink.topics")); + + assertEquals("org.onap.policy.appc.Request", + properties.getProperty("noop.sink.topics.appc-cl.events")); - assertEquals("org.onap.policy.appc.Request", properties.getProperty("noop.sink.topics.appc-cl.events")); assertEquals("org.onap.policy.appc.util.Serialization,gsonPretty", - properties.getProperty("noop.sink.topics.appc-cl.events.custom.gson")); + properties.getProperty("noop.sink.topics.appc-cl.events.custom.gson")); assertEquals("org.onap.policy.appclcm.AppcLcmMessageWrapper", - properties.getProperty("noop.sink.topics.appc-lcm-read.events")); + properties.getProperty("noop.sink.topics.appc-lcm-read.events")); + assertEquals("org.onap.policy.appclcm.util.Serialization,gson", - properties.getProperty("noop.sink.topics.appc-lcm-read.events.custom.gson")); + properties.getProperty("noop.sink.topics.appc-lcm-read.events.custom.gson")); assertEquals("org.onap.policy.controlloop.VirtualControlLoopNotification", - properties.getProperty("noop.sink.topics.policy-cl-mgt.events")); - assertEquals("org.onap.policy.controlloop.util.Serialization,gsonPretty", - properties.getProperty("noop.sink.topics.policy-cl-mgt.events.custom.gson")); + properties.getProperty("noop.sink.topics.policy-cl-mgt.events")); - assertEquals("org.onap.policy.controlloop.ControlLoopResponse", - properties.getProperty("noop.sink.topics.dcae_cl_rsp.events")); assertEquals("org.onap.policy.controlloop.util.Serialization,gsonPretty", - properties.getProperty("noop.sink.topics.dcae_cl_rsp.events.custom.gson")); + properties.getProperty("noop.sink.topics.policy-cl-mgt.events.custom.gson")); - assertEquals("test", properties.getProperty("notes")); - assertEquals("auto", properties.getProperty("persistence.type")); + assertEquals("org.onap.policy.controlloop.ControlLoopResponse", + properties.getProperty("noop.sink.topics.dcae_cl_rsp.events")); - assertTrue(controller.undeploy(nativeControllerPolicy)); + assertEquals("org.onap.policy.controlloop.util.Serialization,gsonPretty", + properties.getProperty("noop.sink.topics.dcae_cl_rsp.events.custom.gson")); } } diff --git a/feature-lifecycle/src/test/java/org/onap/policy/drools/server/restful/RestLifecycleManagerTest.java b/feature-lifecycle/src/test/java/org/onap/policy/drools/server/restful/RestLifecycleManagerTest.java index fcf946e4..8aa3f43b 100644 --- a/feature-lifecycle/src/test/java/org/onap/policy/drools/server/restful/RestLifecycleManagerTest.java +++ b/feature-lifecycle/src/test/java/org/onap/policy/drools/server/restful/RestLifecycleManagerTest.java @@ -187,14 +187,7 @@ public class RestLifecycleManagerTest { /* start up configuration */ - resourceLists("policyTypes", 2); - get("policyTypes/onap.policies.native.drools.Artifact/1.0.0", Status.OK.getStatusCode()); - get("policyTypes/onap.policies.native.drools.Controller/1.0.0", Status.OK.getStatusCode()); - get("policyTypes/onap.policies.controlloop.operational.common.Drools/1.0.0", Status.NOT_FOUND.getStatusCode()); - - resourceLists("policies", 0); - get("policies/example.controller/1.0.0", Status.NOT_FOUND.getStatusCode()); - get("policies/example.artifact/1.0.0", Status.NOT_FOUND.getStatusCode()); + assertStartup(); /* start lifecycle */ @@ -208,35 +201,17 @@ public class RestLifecycleManagerTest { ToscaPolicy nativeControllerPolicy = getPolicyFromFile(EXAMPLE_NATIVE_CONTROLLER_POLICY_JSON, EXAMPLE_NATIVE_CONTROLLER_POLICY_NAME); - booleanPost("policies", toString(nativeControllerPolicy), Status.OK.getStatusCode(), Boolean.TRUE); - - assertTrue(PolicyControllerConstants.getFactory().get("lifecycle").isAlive()); - assertFalse(PolicyControllerConstants.getFactory().get("lifecycle").getDrools().isBrained()); - assertFalse(PolicyControllerConstants.getFactory().get("lifecycle").getDrools().isAlive()); - - get("policyTypes/onap.policies.controlloop.operational.common.Drools/1.0.0", Status.NOT_FOUND.getStatusCode()); - - resourceLists("policies", 1); - get("policies/example.controller/1.0.0", Status.OK.getStatusCode()); + assertAddNativeControllerPolicy(nativeControllerPolicy); /* add native artifact policy */ ToscaPolicy nativeArtifactPolicy = getPolicyFromFile(EXAMPLE_NATIVE_ARTIFACT_POLICY_JSON, EXAMPLE_NATIVE_ARTIFACT_POLICY_NAME); - booleanPost("policies", toString(nativeArtifactPolicy), Status.OK.getStatusCode(), Boolean.TRUE); - - assertTrue(PolicyControllerConstants.getFactory().get("lifecycle").isAlive()); - assertTrue(PolicyControllerConstants.getFactory().get("lifecycle").getDrools().isBrained()); - assertTrue(PolicyControllerConstants.getFactory().get("lifecycle").getDrools().isAlive()); + assertAddNativeArtifactPolicy(nativeArtifactPolicy); /* verify new supported operational policy types */ - resourceLists("policyTypes", 5); - get("policyTypes/onap.policies.native.drools.Artifact/1.0.0", Status.OK.getStatusCode()); - get("policyTypes/onap.policies.native.drools.Controller/1.0.0", Status.OK.getStatusCode()); - get("policyTypes/onap.policies.controlloop.operational.common.Drools/1.0.0", Status.OK.getStatusCode()); - get("policyTypes/onap.policies.type1.type2/1.0.0", Status.OK.getStatusCode()); - get("policyTypes/onap.policies.typeA/1.0.0", Status.OK.getStatusCode()); + verifySupportedOperationalPolicyTypes(5, Status.OK); /* verify controller and artifact policies */ @@ -268,6 +243,48 @@ public class RestLifecycleManagerTest { /* individual deploy/undeploy operations */ + assertDeployUndeploy(opPolicy); + + /* delete native artifact policy */ + + verifyDeleteNativeArtifactPolicy(opPolicy); + + /* delete native controller policy */ + + verifyDeleteNativeControllerPolicy(opPolicy); + + metrics(); + } + + private void verifyDeleteNativeControllerPolicy(ToscaPolicy opPolicy) throws CoderException { + booleanDelete("policies/example.controller/1.0.0", Status.OK.getStatusCode()); + + verifySupportedOperationalPolicyTypes(2, Status.NOT_FOUND); + + resourceLists("policies", 0); + get("policies/" + opPolicy.getName() + "/" + opPolicy.getVersion(), Status.NOT_FOUND.getStatusCode()); + get("policies/example.artifact/1.0.0", Status.NOT_FOUND.getStatusCode()); + get("policies/example.controller/1.0.0", Status.NOT_FOUND.getStatusCode()); + + assertThatIllegalArgumentException().isThrownBy(() -> PolicyControllerConstants.getFactory().get("lifecycle")); + opPolicy.getMetadata().remove("policy-id"); + assertThat(listPost(toString(opPolicy), Status.NOT_ACCEPTABLE.getStatusCode())).isEmpty(); + } + + private void verifyDeleteNativeArtifactPolicy(ToscaPolicy opPolicy) { + booleanDelete("policies/example.artifact/1.0.0", Status.OK.getStatusCode()); + assertTrue(PolicyControllerConstants.getFactory().get("lifecycle").isAlive()); + assertFalse(PolicyControllerConstants.getFactory().get("lifecycle").getDrools().isBrained()); + + verifySupportedOperationalPolicyTypes(2, Status.NOT_FOUND); + + resourceLists("policies", 1); + get("policies/" + opPolicy.getName() + "/" + opPolicy.getVersion(), Status.NOT_FOUND.getStatusCode()); + get("policies/example.artifact/1.0.0", Status.NOT_FOUND.getStatusCode()); + get("policies/example.controller/1.0.0", Status.OK.getStatusCode()); + } + + private void assertDeployUndeploy(ToscaPolicy opPolicy) throws CoderException { resourceLists("policies/operations", 3); booleanPost("policies/operations/deployment", toString(opPolicy), Status.OK.getStatusCode(), Boolean.TRUE); @@ -290,46 +307,47 @@ public class RestLifecycleManagerTest { get("policies/" + opPolicy.getName() + "/" + opPolicy.getVersion(), Status.NOT_FOUND.getStatusCode()); get("policies/example.controller/1.0.0", Status.OK.getStatusCode()); get("policies/example.artifact/1.0.0", Status.OK.getStatusCode()); + } - /* delete native artifact policy */ + private void verifySupportedOperationalPolicyTypes(int size, Status status) { + resourceLists("policyTypes", size); + get("policyTypes/onap.policies.native.drools.Artifact/1.0.0", Status.OK.getStatusCode()); + get("policyTypes/onap.policies.native.drools.Controller/1.0.0", Status.OK.getStatusCode()); + get("policyTypes/onap.policies.controlloop.operational.common.Drools/1.0.0", status.getStatusCode()); + get("policyTypes/onap.policies.type1.type2/1.0.0", status.getStatusCode()); + get("policyTypes/onap.policies.typeA/1.0.0", status.getStatusCode()); + } + + private void assertAddNativeArtifactPolicy(ToscaPolicy nativeArtifactPolicy) throws CoderException { + booleanPost("policies", toString(nativeArtifactPolicy), Status.OK.getStatusCode(), Boolean.TRUE); + + assertTrue(PolicyControllerConstants.getFactory().get("lifecycle").isAlive()); + assertTrue(PolicyControllerConstants.getFactory().get("lifecycle").getDrools().isBrained()); + assertTrue(PolicyControllerConstants.getFactory().get("lifecycle").getDrools().isAlive()); + } + + private void assertAddNativeControllerPolicy(ToscaPolicy nativeControllerPolicy) throws CoderException { + booleanPost("policies", toString(nativeControllerPolicy), Status.OK.getStatusCode(), Boolean.TRUE); - booleanDelete("policies/example.artifact/1.0.0", Status.OK.getStatusCode()); assertTrue(PolicyControllerConstants.getFactory().get("lifecycle").isAlive()); assertFalse(PolicyControllerConstants.getFactory().get("lifecycle").getDrools().isBrained()); + assertFalse(PolicyControllerConstants.getFactory().get("lifecycle").getDrools().isAlive()); - resourceLists("policyTypes", 2); - get("policyTypes/onap.policies.native.drools.Artifact/1.0.0", Status.OK.getStatusCode()); - get("policyTypes/onap.policies.native.drools.Controller/1.0.0", Status.OK.getStatusCode()); get("policyTypes/onap.policies.controlloop.operational.common.Drools/1.0.0", Status.NOT_FOUND.getStatusCode()); - get("policyTypes/onap.policies.type1.type2/1.0.0", Status.NOT_FOUND.getStatusCode()); - get("policyTypes/onap.policies.typeA/1.0.0", Status.NOT_FOUND.getStatusCode()); resourceLists("policies", 1); - get("policies/" + opPolicy.getName() + "/" + opPolicy.getVersion(), Status.NOT_FOUND.getStatusCode()); - get("policies/example.artifact/1.0.0", Status.NOT_FOUND.getStatusCode()); get("policies/example.controller/1.0.0", Status.OK.getStatusCode()); + } - /* delete native controller policy */ - - booleanDelete("policies/example.controller/1.0.0", Status.OK.getStatusCode()); - + private void assertStartup() { resourceLists("policyTypes", 2); get("policyTypes/onap.policies.native.drools.Artifact/1.0.0", Status.OK.getStatusCode()); get("policyTypes/onap.policies.native.drools.Controller/1.0.0", Status.OK.getStatusCode()); get("policyTypes/onap.policies.controlloop.operational.common.Drools/1.0.0", Status.NOT_FOUND.getStatusCode()); - get("policyTypes/onap.policies.type1.type2/1.0.0", Status.NOT_FOUND.getStatusCode()); - get("policyTypes/onap.policies.typeA/1.0.0", Status.NOT_FOUND.getStatusCode()); resourceLists("policies", 0); - get("policies/" + opPolicy.getName() + "/" + opPolicy.getVersion(), Status.NOT_FOUND.getStatusCode()); - get("policies/example.artifact/1.0.0", Status.NOT_FOUND.getStatusCode()); get("policies/example.controller/1.0.0", Status.NOT_FOUND.getStatusCode()); - - assertThatIllegalArgumentException().isThrownBy(() -> PolicyControllerConstants.getFactory().get("lifecycle")); - opPolicy.getMetadata().remove("policy-id"); - assertThat(listPost(toString(opPolicy), Status.NOT_ACCEPTABLE.getStatusCode())).isEmpty(); - - metrics(); + get("policies/example.artifact/1.0.0", Status.NOT_FOUND.getStatusCode()); } private void testNotNativePolicy(ToscaPolicy toscaPolicy) throws CoderException { |