2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2020 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.network.NetworkUtil;
42 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
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.PolicyEngineConstants;
47 import org.onap.policy.drools.util.KieUtils;
48 import org.onap.policy.drools.utils.logging.LoggerUtil;
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/";
85 private static final String POLICY = "src/test/resources/vCPE.yaml";
87 private static final String CONTROLLER_FILE = "op-controller.properties";
88 private static final String CONTROLLER_FILE_BAK = "op-controller.properties.bak";
93 * @throws Exception if failure to complete the set up.
96 public static void setUp() throws Exception {
97 System.setProperty("kie.maven.settings.custom", "src/test/resources/settings.xml");
98 LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "WARN");
100 SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
101 PolicyEngineConstants.getManager()
102 .configure(PolicyEngineConstants.getManager().defaultTelemetryConfig());
104 ReleaseId releaseId = KieUtils.installArtifact(Paths.get(KMODULE_PATH).toFile(),
105 Paths.get(KMODULE_POM_PATH).toFile(), KJAR_DRL_PATH,
106 Paths.get(KMODULE_DRL_PATH).toFile());
108 Properties controllerProperties = new Properties();
109 controllerProperties.put(DroolsPropertyConstants.RULES_GROUPID, releaseId.getGroupId());
110 controllerProperties.put(DroolsPropertyConstants.RULES_ARTIFACTID,
111 releaseId.getArtifactId());
112 controllerProperties.put(DroolsPropertyConstants.RULES_VERSION, releaseId.getVersion());
114 PolicyEngineConstants.getManager().createPolicyController(CONTROLLER, controllerProperties);
115 PolicyEngineConstants.getManager().start();
117 HttpClientFactoryInstance.getClientFactory()
118 .build(SystemPersistenceConstants.getManager().getProperties(CLIENT_CONFIG));
120 if (!NetworkUtil.isTcpPortOpen("localhost", 9696, 6, 10000L)) {
121 throw new IllegalStateException("cannot connect to port 9696");
124 await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive());
126 PolicyEngineConstants.getManager().setEnvironmentProperty(ControlLoopEventManager.AAI_URL,
127 "http://localhost:6666");
128 PolicyEngineConstants.getManager()
129 .setEnvironmentProperty(ControlLoopEventManager.AAI_USERNAME_PROPERTY, "AAI");
130 PolicyEngineConstants.getManager()
131 .setEnvironmentProperty(ControlLoopEventManager.AAI_PASS_PROPERTY, "AAI");
140 public static void tearDown() {
141 PolicyControllerConstants.getFactory().get(CONTROLLER).stop();
142 await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive(), equalTo(Boolean.FALSE));
144 PolicyEngineConstants.getManager().removePolicyController(CONTROLLER);
145 PolicyEngineConstants.getManager().stop();
147 final Path controllerPath =
148 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
151 Files.deleteIfExists(controllerPath);
152 } catch (Exception ignored) {
153 /* to satisfy checkstyle */
156 Path controllerBakPath =
157 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
158 CONTROLLER_FILE_BAK);
161 Files.deleteIfExists(controllerBakPath);
162 } catch (Exception ignored) {
163 /* to satisfy checkstyle */
168 * Test Operational Policies.
171 public void testOperationalPolicy() throws IOException {
172 assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
173 .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOPS).getStatus());
175 assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
176 .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOP).getStatus());
178 assertEquals(Status.NOT_FOUND.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
179 .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOP_POLICY).getStatus());
183 * Test AAI Custom Query.
186 public void testAaiCq() throws CoderException {
187 assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
188 .get(CONTROLLER).get(URL_CONTEXT_PATH_TOOLS_AAI_CQ + "dummy").getStatus());
192 * Test if the session is alive.
194 * @return if the container is alive.
196 private static Callable<Boolean> isContainerAlive() {
197 return () -> PolicyControllerConstants.getFactory().get(CONTROLLER).getDrools()
198 .getContainer().isAlive();