Fix topic configurations.
[policy/drools-applications.git] / controlloop / common / feature-controlloop-management / src / test / java / org / onap / policy / drools / server / restful / RestControlLoopManagerTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023-2024 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.drools.server.restful;
23
24 import static org.awaitility.Awaitility.await;
25 import static org.hamcrest.Matchers.equalTo;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27
28 import jakarta.ws.rs.core.Response.Status;
29 import java.nio.file.Files;
30 import java.nio.file.Paths;
31 import java.util.Properties;
32 import java.util.concurrent.Callable;
33 import java.util.concurrent.TimeUnit;
34 import org.junit.jupiter.api.AfterAll;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.Test;
37 import org.kie.api.builder.ReleaseId;
38 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
39 import org.onap.policy.common.utils.logging.LoggerUtils;
40 import org.onap.policy.common.utils.network.NetworkUtil;
41 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
42 import org.onap.policy.drools.properties.DroolsPropertyConstants;
43 import org.onap.policy.drools.system.PolicyControllerConstants;
44 import org.onap.policy.drools.system.PolicyEngine;
45 import org.onap.policy.drools.system.PolicyEngineConstants;
46 import org.onap.policy.drools.util.KieUtils;
47 import org.onap.policy.simulators.Util;
48
49 /**
50  * Test RestControlLoopManager.
51  */
52 class RestControlLoopManagerTest {
53
54     private static final String KSESSION = "op";
55     private static final String KMODULE_DRL_PATH = "src/test/resources/op.drl";
56     private static final String KMODULE_POM_PATH = "src/test/resources/op.pom";
57     private static final String KMODULE_PATH = "src/test/resources/op.kmodule";
58     private static final String KJAR_DRL_PATH =
59         "src/main/resources/kbop/org/onap/policy/drools/test/op.drl";
60
61     private static final String CONTROLLER = KSESSION;
62     private static final String CONTROLOOP_NAME =
63         "ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e";
64
65     private static final String CLIENT_CONFIG = "op-http";
66
67     private static final String URL_CONTEXT_PATH_CONTROLLERS = "controllers/";
68     private static final String URL_CONTEXT_PATH_CONTROLLER =
69         URL_CONTEXT_PATH_CONTROLLERS + CONTROLLER;
70     private static final String URL_CONTEXT_PATH_KSESSION =
71         URL_CONTEXT_PATH_CONTROLLER + "/drools/facts/" + KSESSION;
72     private static final String URL_CONTEXT_PATH_CONTROLLOOPS =
73         URL_CONTEXT_PATH_KSESSION + "/controlloops/";
74     private static final String URL_CONTEXT_PATH_CONTROLLOOP =
75         URL_CONTEXT_PATH_CONTROLLOOPS + CONTROLOOP_NAME;
76     private static final String URL_CONTEXT_PATH_CONTROLLOOP_POLICY =
77         URL_CONTEXT_PATH_CONTROLLOOP + "/policy";
78
79     private static final String URL_CONTEXT_PATH_TOOLS = "tools/controlloops/";
80     private static final String URL_CONTEXT_PATH_TOOLS_AAI = URL_CONTEXT_PATH_TOOLS + "aai/";
81     private static final String URL_CONTEXT_PATH_TOOLS_AAI_CQ =
82         URL_CONTEXT_PATH_TOOLS_AAI + "customQuery/";
83
84     private static final String CONTROLLER_FILE = "op-controller.properties";
85     private static final String CONTROLLER_FILE_BAK = "op-controller.properties.bak";
86
87     /**
88      * test set up.
89      *
90      * @throws Exception if failure to complete the setup.
91      */
92     @BeforeAll
93     public static void setUp() throws Exception {
94         System.setProperty("kie.maven.settings.custom", "src/test/resources/settings.xml");
95         LoggerUtils.setLevel(LoggerUtils.ROOT_LOGGER, "WARN");
96
97         SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
98         PolicyEngineConstants.getManager()
99             .configure(PolicyEngineConstants.getManager().defaultTelemetryConfig());
100
101         ReleaseId releaseId = KieUtils.installArtifact(Paths.get(KMODULE_PATH).toFile(),
102             Paths.get(KMODULE_POM_PATH).toFile(), KJAR_DRL_PATH,
103             Paths.get(KMODULE_DRL_PATH).toFile());
104
105         Properties controllerProperties = new Properties();
106         controllerProperties.put(DroolsPropertyConstants.RULES_GROUPID, releaseId.getGroupId());
107         controllerProperties.put(DroolsPropertyConstants.RULES_ARTIFACTID,
108             releaseId.getArtifactId());
109         controllerProperties.put(DroolsPropertyConstants.RULES_VERSION, releaseId.getVersion());
110
111         PolicyEngineConstants.getManager().createPolicyController(CONTROLLER, controllerProperties);
112         PolicyEngineConstants.getManager().start();
113
114         HttpClientFactoryInstance.getClientFactory()
115             .build(SystemPersistenceConstants.getManager().getProperties(CLIENT_CONFIG));
116
117         if (!NetworkUtil.isTcpPortOpen("localhost", 9696, 6, 10000L)) {
118             throw new IllegalStateException("cannot connect to port 9696");
119         }
120
121         await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive());
122
123         PolicyEngine mgr = PolicyEngineConstants.getManager();
124         mgr.setEnvironmentProperty("aai.url", "http://localhost:6666");
125         mgr.setEnvironmentProperty("aai.username", "AAI");
126         mgr.setEnvironmentProperty("aai.password", "AAI");
127
128         Util.buildAaiSim();
129     }
130
131     /**
132      * test tear down.
133      */
134     @AfterAll
135     public static void tearDown() {
136         PolicyControllerConstants.getFactory().get(CONTROLLER).stop();
137         await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive(), equalTo(Boolean.FALSE));
138
139         PolicyEngineConstants.getManager().removePolicyController(CONTROLLER);
140         PolicyEngineConstants.getManager().stop();
141
142         final var controllerPath =
143             Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
144                 CONTROLLER_FILE);
145         try {
146             Files.deleteIfExists(controllerPath);
147         } catch (Exception ignored) {
148             /* to satisfy checkstyle */
149         }
150
151         var controllerBakPath =
152             Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
153                 CONTROLLER_FILE_BAK);
154
155         try {
156             Files.deleteIfExists(controllerBakPath);
157         } catch (Exception ignored) {
158             /* to satisfy checkstyle */
159         }
160     }
161
162     /**
163      * Test Operational Policies.
164      */
165     @Test
166     void testOperationalPolicy() {
167         assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
168             .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOPS).getStatus());
169
170         assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
171             .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOP).getStatus());
172
173         assertEquals(Status.NOT_FOUND.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
174             .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOP_POLICY).getStatus());
175     }
176
177     /**
178      * Test AAI Custom Query.
179      */
180     @Test
181     void testAaiCq() {
182         assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
183             .get(CONTROLLER).get(URL_CONTEXT_PATH_TOOLS_AAI_CQ + "dummy").getStatus());
184     }
185
186     /**
187      * Test if the session is alive.
188      *
189      * @return if the container is alive.
190      */
191     private static Callable<Boolean> isContainerAlive() {
192         return () -> PolicyControllerConstants.getFactory().get(CONTROLLER).getDrools()
193             .getContainer().isAlive();
194     }
195 }