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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * =============LICENSE_END========================================================
21 package org.onap.policy.drools.lifecycle;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertSame;
25 import static org.junit.Assert.assertTrue;
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;
38 * Drools Controller Policy Test.
40 public class PolicyTypeDroolsControllerTest extends LifecycleStateRunningTest {
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";
47 private ToscaPolicy policy;
48 private OperationalPolicy operationalPolicy;
49 private PolicyTypeDroolsController controller;
52 * Test initialization.
55 public void init() throws CoderException {
56 fsm = makeFsmWithPseudoTime();
57 policy = getExamplesPolicy(VCPE_OPERATIONAL_DROOLS_POLICY_JSON, OP_POLICY_NAME_VCPE);
58 operationalPolicy = fsm.getDomainMaker().convertTo(policy, OperationalPolicy.class);
59 controller = new PolicyTypeDroolsController(
60 fsm, PolicyTypeDroolsController.compliantType, controllerSupport.getController());
62 assertTrue(controllerSupport.getController().getDrools().isBrained());
63 assertFalse(controllerSupport.getController().isAlive());
64 assertFalse(controllerSupport.getController().getDrools().isAlive());
65 assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
67 /* start controller */
68 assertTrue(controllerSupport.getController().start());
70 assertTrue(controllerSupport.getController().isAlive());
71 assertTrue(controllerSupport.getController().getDrools().isAlive());
72 assertTrue(controllerSupport.getController().getDrools().isBrained());
73 assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
77 public void testDeployUndeploy() {
78 /* non-existing controller */
79 assertFalse(controller.undeploy(policy));
80 assertFalse(controller.deploy(policy));
82 policy.getProperties().remove("controllerName");
83 assertTrue(controller.deploy(policy));
84 assertTrue(controller.undeploy(policy));
85 assertFalse(controller.undeploy(policy));
87 /* existing controller */
88 policy.getProperties().put("controllerName", "lifecycle");
89 assertTrue(controller.deploy(policy));
90 assertTrue(controller.undeploy(policy));
91 assertFalse(controller.undeploy(policy));
94 private ToscaPolicy getExamplesPolicy(String resourcePath, String policyName) throws CoderException {
95 String policyJson = ResourceUtils.getResourceAsString(resourcePath);
96 ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(policyJson, ToscaServiceTemplate.class);
97 return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName);