2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.lifecycle;
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
29 import java.io.IOException;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.common.utils.coder.CoderException;
33 import org.onap.policy.drools.controller.DroolsControllerConstants;
34 import org.onap.policy.drools.controller.internal.MavenDroolsController;
35 import org.onap.policy.drools.controller.internal.NullDroolsController;
36 import org.onap.policy.drools.domain.models.artifact.NativeArtifactPolicy;
37 import org.onap.policy.drools.system.PolicyControllerConstants;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
42 * Rules Controller Test.
44 public class PolicyTypeNativeArtifactControllerTest extends LifecycleStateRunningTest {
45 // Native Drools Policy
46 private static final String EXAMPLE_NATIVE_DROOLS_POLICY_NAME = "example";
47 private static final String EXAMPLE_NATIVE_DROOLS_POLICY_JSON =
48 "src/test/resources/tosca-policy-native-artifact-example.json";
50 private ToscaPolicy policy;
51 private NativeArtifactPolicy nativePolicy;
52 private PolicyTypeNativeArtifactController controller;
55 * Test Set initialization.
58 public void init() throws IOException, CoderException {
59 fsm = makeFsmWithPseudoTime();
60 policy = getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_POLICY_NAME);
61 nativePolicy = fsm.getDomainMaker().convertTo(policy, NativeArtifactPolicy.class);
63 new PolicyTypeNativeArtifactController(fsm,
64 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Artifact", "1.0.0"));
66 assertTrue(controllerSupport.getController().getDrools().isBrained());
67 assertFalse(controllerSupport.getController().isAlive());
68 assertFalse(controllerSupport.getController().getDrools().isAlive());
69 assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
71 /* start controller */
72 assertTrue(controllerSupport.getController().start());
74 assertTrue(controllerSupport.getController().isAlive());
75 assertTrue(controllerSupport.getController().getDrools().isAlive());
76 assertTrue(controllerSupport.getController().getDrools().isBrained());
77 assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
78 assertSame(controllerSupport.getController(),
79 PolicyControllerConstants.getFactory().get(
80 nativePolicy.getProperties().getRulesArtifact().getGroupId(),
81 nativePolicy.getProperties().getRulesArtifact().getArtifactId()));
82 assertEquals(controllerSupport.getController().getDrools().getGroupId(),
83 nativePolicy.getProperties().getRulesArtifact().getGroupId());
84 assertEquals(controllerSupport.getController().getDrools().getArtifactId(),
85 nativePolicy.getProperties().getRulesArtifact().getArtifactId());
86 assertEquals(controllerSupport.getController().getDrools().getVersion(),
87 nativePolicy.getProperties().getRulesArtifact().getVersion());
91 public void testUndeployDeploy() {
95 PolicyControllerConstants.getFactory().destroy("lifecycle");
96 assertThatIllegalArgumentException().isThrownBy(() -> PolicyControllerConstants.getFactory().get("lifecycle"));
99 private void undeploy() {
100 assertTrue(controller.undeploy(policy));
104 assertTrue(controller.undeploy(policy));
109 private void deploy() {
110 assertTrue(controller.deploy(policy));
114 assertTrue(controller.deploy(policy));
117 // TODO: test a point version upgrade
120 private void assertUndeployed() {
121 assertFalse(controllerSupport.getController().getDrools().isBrained());
122 assertFalse(controllerSupport.getController().getDrools().isAlive());
123 assertTrue(controllerSupport.getController().isAlive());
124 assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
125 assertThatIllegalArgumentException().isThrownBy(() -> PolicyControllerConstants.getFactory()
126 .get(nativePolicy.getProperties()
127 .getRulesArtifact().getGroupId(),
128 nativePolicy.getProperties()
131 assertTrue(controllerSupport.getController().getDrools() instanceof NullDroolsController);
132 assertEquals(DroolsControllerConstants.NO_GROUP_ID, controllerSupport.getController().getDrools().getGroupId());
133 assertEquals(DroolsControllerConstants.NO_ARTIFACT_ID,
134 controllerSupport.getController().getDrools().getArtifactId());
135 assertEquals(DroolsControllerConstants.NO_VERSION, controllerSupport.getController().getDrools().getVersion());
138 private void assertDeployed() {
139 assertTrue(controllerSupport.getController().getDrools().isBrained());
140 assertTrue(controllerSupport.getController().getDrools().isAlive());
141 assertTrue(controllerSupport.getController().isAlive());
142 assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
143 assertSame(controllerSupport.getController(),
144 PolicyControllerConstants.getFactory().get(
145 nativePolicy.getProperties().getRulesArtifact().getGroupId(),
146 nativePolicy.getProperties().getRulesArtifact().getArtifactId()));
147 assertTrue(controllerSupport.getController().getDrools() instanceof MavenDroolsController);