05ed41a4fafb9c81d7140241cf178ddf0d0a3931
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.drools.lifecycle;
23
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertSame;
28 import static org.junit.Assert.assertTrue;
29
30 import java.io.IOException;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.common.utils.coder.CoderException;
34 import org.onap.policy.drools.controller.DroolsControllerConstants;
35 import org.onap.policy.drools.controller.internal.MavenDroolsController;
36 import org.onap.policy.drools.controller.internal.NullDroolsController;
37 import org.onap.policy.drools.domain.models.artifact.NativeArtifactPolicy;
38 import org.onap.policy.drools.system.PolicyControllerConstants;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
41
42 /**
43  * Rules Controller Test.
44  */
45 public class PolicyTypeNativeArtifactControllerTest extends LifecycleStateRunningTest {
46     // Native Drools Policy
47     private static final String EXAMPLE_NATIVE_DROOLS_POLICY_NAME = "example.artifact";
48     private static final String EXAMPLE_NATIVE_DROOLS_POLICY_JSON =
49             "src/test/resources/tosca-policy-native-artifact-example.json";
50
51     private ToscaPolicy policy;
52     private NativeArtifactPolicy nativePolicy;
53     private PolicyTypeNativeArtifactController controller;
54
55     /**
56      * Test Set initialization.
57      */
58     @Before
59     public void init() throws IOException, CoderException {
60         fsm = makeFsmWithPseudoTime();
61         policy = getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_POLICY_NAME);
62         nativePolicy = fsm.getDomainMaker().convertTo(policy, NativeArtifactPolicy.class);
63         controller = new PolicyTypeNativeArtifactController(
64                         new ToscaConceptIdentifier("onap.policies.native.drools.Artifact", "1.0.0"), fsm);
65
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"));
70
71         /* start controller */
72         assertTrue(controllerSupport.getController().start());
73
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());
88     }
89
90     @Test
91     public void testUndeployDeploy() {
92         undeploy();
93         deploy();
94
95         PolicyControllerConstants.getFactory().destroy("lifecycle");
96         assertThatIllegalArgumentException().isThrownBy(() -> PolicyControllerConstants.getFactory().get("lifecycle"));
97     }
98
99     private void undeploy() {
100         assertTrue(controller.undeploy(policy));
101         assertUndeployed();
102
103         /* idempotence */
104         assertTrue(controller.undeploy(policy));
105         assertUndeployed();
106     }
107
108
109     private void deploy() {
110         assertTrue(controller.deploy(policy));
111         assertDeployed();
112
113         /* idempotence */
114         assertTrue(controller.deploy(policy));
115         assertDeployed();
116
117         // TODO: test a point version upgrade
118     }
119
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()
129                                                                                       .getRulesArtifact()
130                                                                                       .getArtifactId()));
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());
136     }
137
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);
148     }
149
150 }