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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * =============LICENSE_END========================================================
22 package org.onap.policy.drools.lifecycle;
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;
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;
37 * Drools Controller Policy Test.
39 class PolicyTypeDroolsControllerTest extends LifecycleStateRunningTest {
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";
46 private ToscaPolicy policy;
47 private PolicyTypeDroolsController controller;
50 * Test initialization.
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());
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"));
65 /* start controller */
66 assertTrue(controllerSupport.getController().start());
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"));
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));
82 policy.getProperties().remove("controllerName");
85 deploy(); // one more time
88 undeploy(); // one more time
90 /* existing controller */
91 policy.getProperties().put("controllerName", "lifecycle");
94 deploy(); // one more time
97 undeploy(); // one more time
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));
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));