2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.server.restful;
23 import static org.awaitility.Awaitility.await;
24 import static org.hamcrest.Matchers.equalTo;
25 import static org.junit.Assert.assertEquals;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
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 javax.ws.rs.core.Response.Status;
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.kie.api.builder.ReleaseId;
39 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
40 import org.onap.policy.common.utils.coder.CoderException;
41 import org.onap.policy.common.utils.logging.LoggerUtils;
42 import org.onap.policy.common.utils.network.NetworkUtil;
43 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
44 import org.onap.policy.drools.properties.DroolsPropertyConstants;
45 import org.onap.policy.drools.system.PolicyControllerConstants;
46 import org.onap.policy.drools.system.PolicyEngine;
47 import org.onap.policy.drools.system.PolicyEngineConstants;
48 import org.onap.policy.drools.util.KieUtils;
49 import org.onap.policy.simulators.Util;
52 * Test RestControlLoopManager.
54 public class RestControlLoopManagerTest {
56 private static final String KSESSION = "op";
57 private static final String KMODULE_DRL_PATH = "src/test/resources/op.drl";
58 private static final String KMODULE_POM_PATH = "src/test/resources/op.pom";
59 private static final String KMODULE_PATH = "src/test/resources/op.kmodule";
60 private static final String KJAR_DRL_PATH =
61 "src/main/resources/kbop/org/onap/policy/drools/test/op.drl";
63 private static final String CONTROLLER = KSESSION;
64 private static final String CONTROLOOP_NAME =
65 "ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e";
67 private static final String CLIENT_CONFIG = "op-http";
69 private static final String URL_CONTEXT_PATH_CONTROLLERS = "controllers/";
70 private static final String URL_CONTEXT_PATH_CONTROLLER =
71 URL_CONTEXT_PATH_CONTROLLERS + CONTROLLER;
72 private static final String URL_CONTEXT_PATH_KSESSION =
73 URL_CONTEXT_PATH_CONTROLLER + "/drools/facts/" + KSESSION;
74 private static final String URL_CONTEXT_PATH_CONTROLLOOPS =
75 URL_CONTEXT_PATH_KSESSION + "/controlloops/";
76 private static final String URL_CONTEXT_PATH_CONTROLLOOP =
77 URL_CONTEXT_PATH_CONTROLLOOPS + CONTROLOOP_NAME;
78 private static final String URL_CONTEXT_PATH_CONTROLLOOP_POLICY =
79 URL_CONTEXT_PATH_CONTROLLOOP + "/policy";
81 private static final String URL_CONTEXT_PATH_TOOLS = "tools/controlloops/";
82 private static final String URL_CONTEXT_PATH_TOOLS_AAI = URL_CONTEXT_PATH_TOOLS + "aai/";
83 private static final String URL_CONTEXT_PATH_TOOLS_AAI_CQ =
84 URL_CONTEXT_PATH_TOOLS_AAI + "customQuery/";
86 private static final String CONTROLLER_FILE = "op-controller.properties";
87 private static final String CONTROLLER_FILE_BAK = "op-controller.properties.bak";
92 * @throws Exception if failure to complete the set up.
95 public static void setUp() throws Exception {
96 System.setProperty("kie.maven.settings.custom", "src/test/resources/settings.xml");
97 LoggerUtils.setLevel(LoggerUtils.ROOT_LOGGER, "WARN");
99 SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
100 PolicyEngineConstants.getManager()
101 .configure(PolicyEngineConstants.getManager().defaultTelemetryConfig());
103 ReleaseId releaseId = KieUtils.installArtifact(Paths.get(KMODULE_PATH).toFile(),
104 Paths.get(KMODULE_POM_PATH).toFile(), KJAR_DRL_PATH,
105 Paths.get(KMODULE_DRL_PATH).toFile());
107 Properties controllerProperties = new Properties();
108 controllerProperties.put(DroolsPropertyConstants.RULES_GROUPID, releaseId.getGroupId());
109 controllerProperties.put(DroolsPropertyConstants.RULES_ARTIFACTID,
110 releaseId.getArtifactId());
111 controllerProperties.put(DroolsPropertyConstants.RULES_VERSION, releaseId.getVersion());
113 PolicyEngineConstants.getManager().createPolicyController(CONTROLLER, controllerProperties);
114 PolicyEngineConstants.getManager().start();
116 HttpClientFactoryInstance.getClientFactory()
117 .build(SystemPersistenceConstants.getManager().getProperties(CLIENT_CONFIG));
119 if (!NetworkUtil.isTcpPortOpen("localhost", 9696, 6, 10000L)) {
120 throw new IllegalStateException("cannot connect to port 9696");
123 await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive());
125 PolicyEngine mgr = PolicyEngineConstants.getManager();
126 mgr.setEnvironmentProperty("aai.url", "http://localhost:6666");
127 mgr.setEnvironmentProperty("aai.username", "AAI");
128 mgr.setEnvironmentProperty("aai.password", "AAI");
137 public static void tearDown() {
138 PolicyControllerConstants.getFactory().get(CONTROLLER).stop();
139 await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive(), equalTo(Boolean.FALSE));
141 PolicyEngineConstants.getManager().removePolicyController(CONTROLLER);
142 PolicyEngineConstants.getManager().stop();
144 final Path controllerPath =
145 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
148 Files.deleteIfExists(controllerPath);
149 } catch (Exception ignored) {
150 /* to satisfy checkstyle */
153 Path controllerBakPath =
154 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
155 CONTROLLER_FILE_BAK);
158 Files.deleteIfExists(controllerBakPath);
159 } catch (Exception ignored) {
160 /* to satisfy checkstyle */
165 * Test Operational Policies.
168 public void testOperationalPolicy() throws IOException {
169 assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
170 .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOPS).getStatus());
172 assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
173 .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOP).getStatus());
175 assertEquals(Status.NOT_FOUND.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
176 .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOP_POLICY).getStatus());
180 * Test AAI Custom Query.
183 public void testAaiCq() throws CoderException {
184 assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
185 .get(CONTROLLER).get(URL_CONTEXT_PATH_TOOLS_AAI_CQ + "dummy").getStatus());
189 * Test if the session is alive.
191 * @return if the container is alive.
193 private static Callable<Boolean> isContainerAlive() {
194 return () -> PolicyControllerConstants.getFactory().get(CONTROLLER).getDrools()
195 .getContainer().isAlive();