4f4b124ed155862bc6d166623cd19e64e10b0e73
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020 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.assertFalse;
24 import static org.junit.Assert.assertSame;
25 import static org.junit.Assert.assertTrue;
26
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.onap.policy.common.utils.coder.CoderException;
30 import org.onap.policy.drools.domain.models.operational.OperationalPolicy;
31 import org.onap.policy.drools.system.PolicyControllerConstants;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
33
34 /**
35  * Drools Controller Policy Test.
36  */
37 public class PolicyTypeDroolsControllerTest extends LifecycleStateRunningTest {
38
39     // Operational vCPE Policies
40     private static final String OP_POLICY_NAME_VCPE = "operational.restart";
41     private static final String VCPE_OPERATIONAL_DROOLS_POLICY_JSON =
42             "policies/vCPE.policy.operational.input.tosca.json";
43
44     private ToscaPolicy policy;
45     private PolicyTypeDroolsController controller;
46
47     /**
48      * Test initialization.
49      */
50     @Before
51     public void init() throws CoderException {
52         fsm = makeFsmWithPseudoTime();
53         policy = getExamplesPolicy(VCPE_OPERATIONAL_DROOLS_POLICY_JSON, OP_POLICY_NAME_VCPE);
54         fsm.getDomainMaker().convertTo(policy, OperationalPolicy.class);
55         controller = new PolicyTypeDroolsController(
56             fsm, PolicyTypeDroolsController.compliantType, controllerSupport.getController());
57
58         assertTrue(controllerSupport.getController().getDrools().isBrained());
59         assertFalse(controllerSupport.getController().isAlive());
60         assertFalse(controllerSupport.getController().getDrools().isAlive());
61         assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
62
63         /* start controller */
64         assertTrue(controllerSupport.getController().start());
65
66         assertTrue(controllerSupport.getController().isAlive());
67         assertTrue(controllerSupport.getController().getDrools().isAlive());
68         assertTrue(controllerSupport.getController().getDrools().isBrained());
69         assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
70     }
71
72     @Test
73     public void testDeployUndeploy() {
74         /* non-existing controller */
75         assertFalse(controller.undeploy(policy));
76         assertFalse(controller.deploy(policy));
77
78         policy.getProperties().remove("controllerName");
79         assertTrue(controller.deploy(policy));
80         assertTrue(controller.undeploy(policy));
81         assertFalse(controller.undeploy(policy));
82
83         /* existing controller */
84         policy.getProperties().put("controllerName", "lifecycle");
85         assertTrue(controller.deploy(policy));
86         assertTrue(controller.undeploy(policy));
87         assertFalse(controller.undeploy(policy));
88     }
89
90 }