Fix op policies distribution to controllers
[policy/drools-pdp.git] / feature-lifecycle / src / test / java / org / onap / policy / drools / lifecycle / PolicyTypeNativeDroolsControllerTest.java
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.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.IOException;
29 import java.util.Properties;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
33 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.drools.domain.models.controller.ControllerPolicy;
36 import org.onap.policy.drools.system.PolicyControllerConstants;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
38
39 /**
40  * Native Controller Policy Test.
41  */
42 public class PolicyTypeNativeDroolsControllerTest extends LifecycleStateRunningTest {
43     // Native Drools Policy
44     private static final String EXAMPLE_NATIVE_DROOLS_POLICY_NAME = "example.controller";
45     private static final String EXAMPLE_NATIVE_DROOLS_POLICY_JSON =
46             "src/test/resources/tosca-policy-native-controller-example.json";
47
48     private ToscaPolicy policy;
49     private ControllerPolicy controllerPolicy;
50     private PolicyTypeNativeDroolsController controller;
51
52     /**
53      * Test initialization.
54      */
55     @Before
56     public void init() throws IOException, CoderException {
57         fsm = makeFsmWithPseudoTime();
58         policy = getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_POLICY_NAME);
59         controllerPolicy = fsm.getDomainMaker().convertTo(policy, ControllerPolicy.class);
60         controller = new PolicyTypeNativeDroolsController(fsm, policy.getTypeIdentifier());
61
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"));
66
67         /* start controller */
68         assertTrue(controllerSupport.getController().start());
69
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"));
74     }
75
76     @Test
77     public void testUndeployDeploy() {
78         assertTrue(controller.undeploy(policy));
79         assertThatIllegalArgumentException().isThrownBy(
80             () -> PolicyControllerConstants.getFactory().get(controllerPolicy.getName()));
81
82         assertFalse(controller.deploy(policy));
83
84         Properties noopTopicProperties = new Properties();
85         noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS, "DCAE_TOPIC");
86         noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, "APPC-CL");
87         TopicEndpointManager.getManager().addTopics(noopTopicProperties);
88
89         assertTrue(controller.deploy(policy));
90     }
91 }