bb4b5638b7de724cfd67e1a80bb2f8ad7e9c0ce7
[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.common.utils.coder.StandardCoder;
31 import org.onap.policy.common.utils.resources.ResourceUtils;
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 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
36
37 /**
38  * Drools Controller Policy Test.
39  */
40 public class PolicyTypeDroolsControllerTest extends LifecycleStateRunningTest {
41
42     // Operational vCPE Policies
43     private static final String OP_POLICY_NAME_VCPE = "operational.restart";
44     private static final String VCPE_OPERATIONAL_DROOLS_POLICY_JSON =
45             "policies/vCPE.policy.operational.input.tosca.json";
46
47     private ToscaPolicy policy;
48     private PolicyTypeDroolsController controller;
49
50     /**
51      * Test initialization.
52      */
53     @Before
54     public void init() throws CoderException {
55         fsm = makeFsmWithPseudoTime();
56         policy = getExamplesPolicy(VCPE_OPERATIONAL_DROOLS_POLICY_JSON, OP_POLICY_NAME_VCPE);
57         fsm.getDomainMaker().convertTo(policy, OperationalPolicy.class);
58         controller = new PolicyTypeDroolsController(
59             fsm, PolicyTypeDroolsController.compliantType, controllerSupport.getController());
60
61         assertTrue(controllerSupport.getController().getDrools().isBrained());
62         assertFalse(controllerSupport.getController().isAlive());
63         assertFalse(controllerSupport.getController().getDrools().isAlive());
64         assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
65
66         /* start controller */
67         assertTrue(controllerSupport.getController().start());
68
69         assertTrue(controllerSupport.getController().isAlive());
70         assertTrue(controllerSupport.getController().getDrools().isAlive());
71         assertTrue(controllerSupport.getController().getDrools().isBrained());
72         assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
73     }
74
75     @Test
76     public void testDeployUndeploy() {
77         /* non-existing controller */
78         assertFalse(controller.undeploy(policy));
79         assertFalse(controller.deploy(policy));
80
81         policy.getProperties().remove("controllerName");
82         assertTrue(controller.deploy(policy));
83         assertTrue(controller.undeploy(policy));
84         assertFalse(controller.undeploy(policy));
85
86         /* existing controller */
87         policy.getProperties().put("controllerName", "lifecycle");
88         assertTrue(controller.deploy(policy));
89         assertTrue(controller.undeploy(policy));
90         assertFalse(controller.undeploy(policy));
91     }
92
93     private ToscaPolicy getExamplesPolicy(String resourcePath, String policyName) throws CoderException {
94         String policyJson = ResourceUtils.getResourceAsString(resourcePath);
95         ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(policyJson, ToscaServiceTemplate.class);
96         return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName);
97     }
98
99 }