cb55e6373ee441457914c04bcf55b78bbd6c5a6a
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.drools.lifecycle;
22
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;
28
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;
40
41 /**
42  * Rules Controller Test.
43  */
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";
49
50     private ToscaPolicy policy;
51     private NativeArtifactPolicy nativePolicy;
52     private PolicyTypeNativeArtifactController controller;
53
54     /**
55      * Test Set initialization.
56      */
57     @Before
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);
62         controller =
63                 new PolicyTypeNativeArtifactController(fsm,
64                         new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Artifact", "1.0.0"));
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 }