3c8be31c6c9028ab286c294f1e45743707dc54a3
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 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 =
64                 new PolicyTypeNativeArtifactController(fsm,
65                         new ToscaConceptIdentifier("onap.policies.native.drools.Artifact", "1.0.0"));
66
67         assertTrue(controllerSupport.getController().getDrools().isBrained());
68         assertFalse(controllerSupport.getController().isAlive());
69         assertFalse(controllerSupport.getController().getDrools().isAlive());
70         assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
71
72         /* start controller */
73         assertTrue(controllerSupport.getController().start());
74
75         assertTrue(controllerSupport.getController().isAlive());
76         assertTrue(controllerSupport.getController().getDrools().isAlive());
77         assertTrue(controllerSupport.getController().getDrools().isBrained());
78         assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
79         assertSame(controllerSupport.getController(),
80                 PolicyControllerConstants.getFactory().get(
81                         nativePolicy.getProperties().getRulesArtifact().getGroupId(),
82                         nativePolicy.getProperties().getRulesArtifact().getArtifactId()));
83         assertEquals(controllerSupport.getController().getDrools().getGroupId(),
84                 nativePolicy.getProperties().getRulesArtifact().getGroupId());
85         assertEquals(controllerSupport.getController().getDrools().getArtifactId(),
86                 nativePolicy.getProperties().getRulesArtifact().getArtifactId());
87         assertEquals(controllerSupport.getController().getDrools().getVersion(),
88                 nativePolicy.getProperties().getRulesArtifact().getVersion());
89     }
90
91     @Test
92     public void testUndeployDeploy() {
93         undeploy();
94         deploy();
95
96         PolicyControllerConstants.getFactory().destroy("lifecycle");
97         assertThatIllegalArgumentException().isThrownBy(() -> PolicyControllerConstants.getFactory().get("lifecycle"));
98     }
99
100     private void undeploy() {
101         assertTrue(controller.undeploy(policy));
102         assertUndeployed();
103
104         /* idempotence */
105         assertTrue(controller.undeploy(policy));
106         assertUndeployed();
107     }
108
109
110     private void deploy() {
111         assertTrue(controller.deploy(policy));
112         assertDeployed();
113
114         /* idempotence */
115         assertTrue(controller.deploy(policy));
116         assertDeployed();
117
118         // TODO: test a point version upgrade
119     }
120
121     private void assertUndeployed() {
122         assertFalse(controllerSupport.getController().getDrools().isBrained());
123         assertFalse(controllerSupport.getController().getDrools().isAlive());
124         assertTrue(controllerSupport.getController().isAlive());
125         assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
126         assertThatIllegalArgumentException().isThrownBy(() -> PolicyControllerConstants.getFactory()
127                                                                       .get(nativePolicy.getProperties()
128                                                                                    .getRulesArtifact().getGroupId(),
129                                                                               nativePolicy.getProperties()
130                                                                                       .getRulesArtifact()
131                                                                                       .getArtifactId()));
132         assertTrue(controllerSupport.getController().getDrools() instanceof NullDroolsController);
133         assertEquals(DroolsControllerConstants.NO_GROUP_ID, controllerSupport.getController().getDrools().getGroupId());
134         assertEquals(DroolsControllerConstants.NO_ARTIFACT_ID,
135                 controllerSupport.getController().getDrools().getArtifactId());
136         assertEquals(DroolsControllerConstants.NO_VERSION, controllerSupport.getController().getDrools().getVersion());
137     }
138
139     private void assertDeployed() {
140         assertTrue(controllerSupport.getController().getDrools().isBrained());
141         assertTrue(controllerSupport.getController().getDrools().isAlive());
142         assertTrue(controllerSupport.getController().isAlive());
143         assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
144         assertSame(controllerSupport.getController(),
145                 PolicyControllerConstants.getFactory().get(
146                         nativePolicy.getProperties().getRulesArtifact().getGroupId(),
147                         nativePolicy.getProperties().getRulesArtifact().getArtifactId()));
148         assertTrue(controllerSupport.getController().getDrools() instanceof MavenDroolsController);
149     }
150
151 }