Removing deprecated DMAAP library
[policy/drools-pdp.git] / feature-lifecycle / src / test / java / org / onap / policy / drools / lifecycle / LifecycleStateRunningTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019-2022 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * =============LICENSE_END========================================================
20  */
21
22 package org.onap.policy.drools.lifecycle;
23
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Paths;
27 import java.util.concurrent.Callable;
28 import java.util.concurrent.ScheduledExecutorService;
29 import java.util.concurrent.TimeUnit;
30 import org.junit.jupiter.api.AfterAll;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicFactories;
33 import org.onap.policy.common.utils.coder.CoderException;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.onap.policy.common.utils.logging.LoggerUtils;
36 import org.onap.policy.common.utils.resources.ResourceUtils;
37 import org.onap.policy.common.utils.time.PseudoScheduledExecutorService;
38 import org.onap.policy.common.utils.time.TestTimeMulti;
39 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
40 import org.onap.policy.drools.system.PolicyControllerConstants;
41 import org.onap.policy.models.pdp.concepts.PdpStatus;
42 import org.onap.policy.models.pdp.enums.PdpMessageType;
43 import org.onap.policy.models.pdp.enums.PdpState;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
46
47 public abstract class LifecycleStateRunningTest {
48     private static final StandardCoder coder = new StandardCoder();
49
50     private static final String CONTROLLER_NAME = "lifecycle";
51     protected static ControllerSupport controllerSupport = new ControllerSupport(CONTROLLER_NAME);
52     protected TestTimeMulti time;
53     protected LifecycleFsm fsm;
54
55     /**
56      * Set up.
57      */
58     @BeforeAll
59     public static void setUp() throws IOException {
60         LoggerUtils.setLevel(LoggerUtils.ROOT_LOGGER, "INFO");
61         LoggerUtils.setLevel("org.onap.policy.common.endpoints", "WARN");
62         LoggerUtils.setLevel("org.onap.policy.drools", "INFO");
63         SystemPersistenceConstants.getManager().setConfigurationDir("target/test-classes");
64         controllerSupport.createController();
65     }
66
67     /**
68      * Tear Down.
69      */
70     @AfterAll
71     public static void tearDown() {
72         controllerSupport.destroyController();
73         NoopTopicFactories.getSourceFactory().destroy();
74         NoopTopicFactories.getSinkFactory().destroy();
75         PolicyControllerConstants.getFactory().destroy();
76         try {
77             Files.deleteIfExists(Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
78                                      CONTROLLER_NAME + "-controller.properties.bak"));
79         } catch (IOException ignored) {
80             // ignored
81         }
82         SystemPersistenceConstants.getManager().setConfigurationDir(null);
83     }
84
85     /**
86      * Creates an FSM that uses pseudo time.
87      * @return a new FSM
88      */
89     public LifecycleFsm makeFsmWithPseudoTime() {
90         time = new TestTimeMulti();
91
92         return new LifecycleFsm() {
93             @Override
94             protected ScheduledExecutorService makeExecutor() {
95                 return new PseudoScheduledExecutorService(time);
96             }
97         };
98     }
99
100     public void waitUntil(long twait, TimeUnit units, Callable<Boolean> condition) {
101         time.waitUntil(twait, units, condition);
102     }
103
104     protected Callable<Boolean> isStatus(PdpState state, int count) {
105         return () -> {
106             if (fsm.client.getSink().getRecentEvents().length != count) {
107                 return false;
108             }
109
110             String[] events = fsm.client.getSink().getRecentEvents();
111             PdpStatus status = new StandardCoder().decode(events[events.length - 1], PdpStatus.class);
112
113             return status.getMessageName() == PdpMessageType.PDP_STATUS && state == status.getState();
114         };
115     }
116
117     protected Callable<Boolean> isStatus(PdpState state) {
118         return isStatus(state, fsm.client.getSink().getRecentEvents().length);
119     }
120
121     protected ToscaPolicy getPolicyFromFile(String filePath, String policyName) throws CoderException, IOException {
122         String policyJson = Files.readString(Paths.get(filePath));
123         ToscaServiceTemplate serviceTemplate = coder.decode(policyJson, ToscaServiceTemplate.class);
124         return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName);
125     }
126
127     protected ToscaPolicy getExamplesPolicy(String resourcePath, String policyName) throws CoderException {
128         String policyJson = ResourceUtils.getResourceAsString(resourcePath);
129         ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(policyJson, ToscaServiceTemplate.class);
130         return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName);
131     }
132 }