18d00fc97deb478fe99d9a68558b30611a271a42
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * =============LICENSE_END========================================================
19  */
20
21 package org.onap.policy.drools.lifecycle;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.assertTrue;
27
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.common.utils.coder.CoderException;
31 import org.onap.policy.drools.domain.models.operational.OperationalPolicy;
32 import org.onap.policy.drools.system.PolicyControllerConstants;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
34
35 /**
36  * Drools Controller Policy Test.
37  */
38 public class PolicyTypeDroolsControllerTest extends LifecycleStateRunningTest {
39
40     // Operational vCPE Policies
41     private static final String OP_POLICY_NAME_VCPE = "operational.restart";
42     private static final String VCPE_OPERATIONAL_DROOLS_POLICY_JSON =
43             "policies/vCPE.policy.operational.input.tosca.json";
44
45     private ToscaPolicy policy;
46     private PolicyTypeDroolsController controller;
47
48     /**
49      * Test initialization.
50      */
51     @Before
52     public void init() throws CoderException {
53         fsm = makeFsmWithPseudoTime();
54         policy = getExamplesPolicy(VCPE_OPERATIONAL_DROOLS_POLICY_JSON, OP_POLICY_NAME_VCPE);
55         fsm.getDomainMaker().convertTo(policy, OperationalPolicy.class);
56         controller = new PolicyTypeDroolsController(
57             fsm, PolicyTypeDroolsController.compliantType, controllerSupport.getController());
58
59         assertTrue(controllerSupport.getController().getDrools().isBrained());
60         assertFalse(controllerSupport.getController().isAlive());
61         assertFalse(controllerSupport.getController().getDrools().isAlive());
62         assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
63
64         /* start controller */
65         assertTrue(controllerSupport.getController().start());
66
67         assertTrue(controllerSupport.getController().isAlive());
68         assertTrue(controllerSupport.getController().getDrools().isAlive());
69         assertTrue(controllerSupport.getController().getDrools().isBrained());
70         assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
71     }
72
73     @Test
74     public void testDeployUndeploy() {
75         /* non-existing controller */
76         assertFalse(controller.undeploy(policy));
77         assertFalse(controller.deploy(policy));
78         assertFalse(controllerSupport.getController().getDrools().exists(policy));
79         assertEquals(0, controllerSupport.getController().getDrools().factCount(ControllerSupport.SESSION_NAME));
80
81         policy.getProperties().remove("controllerName");
82
83         deploy();
84         deploy();  // one more time
85
86         undeploy();
87         undeploy();  // one more time
88
89         /* existing controller */
90         policy.getProperties().put("controllerName", "lifecycle");
91
92         deploy();
93         deploy();  // one more time
94
95         undeploy();
96         undeploy();  // one more time
97     }
98
99     protected void undeploy() {
100         assertTrue(controller.undeploy(policy));
101         assertFalse(controllerSupport.getController().getDrools().exists(policy));
102         assertEquals(0, controllerSupport.getController().getDrools().factCount(ControllerSupport.SESSION_NAME));
103     }
104
105     protected void deploy() {
106         assertTrue(controller.deploy(policy));
107         assertTrue(controllerSupport.getController().getDrools().exists(policy));
108         assertEquals(1, controllerSupport.getController().getDrools().factCount(ControllerSupport.SESSION_NAME));
109     }
110 }