Removing deprecated DMAAP library
[policy/drools-pdp.git] / feature-lifecycle / src / test / java / org / onap / policy / drools / lifecycle / PolicyTypeNativeArtifactControllerTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021, 2024 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.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
28 import static org.junit.jupiter.api.Assertions.assertSame;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30
31 import java.io.IOException;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.drools.controller.DroolsControllerConstants;
36 import org.onap.policy.drools.controller.internal.MavenDroolsController;
37 import org.onap.policy.drools.controller.internal.NullDroolsController;
38 import org.onap.policy.drools.domain.models.artifact.NativeArtifactPolicy;
39 import org.onap.policy.drools.system.PolicyControllerConstants;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
42
43 /**
44  * Rules Controller Test.
45  */
46 class PolicyTypeNativeArtifactControllerTest extends LifecycleStateRunningTest {
47     // Native Drools Policy
48     private static final String EXAMPLE_NATIVE_DROOLS_POLICY_NAME = "example.artifact";
49     private static final String EXAMPLE_NATIVE_DROOLS_POLICY_JSON =
50             "src/test/resources/tosca-policy-native-artifact-example.json";
51
52     private ToscaPolicy policy;
53     private NativeArtifactPolicy nativePolicy;
54     private PolicyTypeNativeArtifactController controller;
55
56     /**
57      * Test Set initialization.
58      */
59     @BeforeEach
60     public void init() throws IOException, CoderException {
61         fsm = makeFsmWithPseudoTime();
62         policy = getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_POLICY_NAME);
63         nativePolicy = fsm.getDomainMaker().convertTo(policy, NativeArtifactPolicy.class);
64         controller = new PolicyTypeNativeArtifactController(
65                         new ToscaConceptIdentifier("onap.policies.native.drools.Artifact", "1.0.0"), fsm);
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     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         assertInstanceOf(NullDroolsController.class, controllerSupport.getController().getDrools());
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         assertInstanceOf(MavenDroolsController.class, controllerSupport.getController().getDrools());
149     }
150
151 }