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