2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2019 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.Collections;
32 import java.util.Properties;
33 import java.util.concurrent.Callable;
34 import java.util.concurrent.TimeUnit;
35 import javax.ws.rs.client.Entity;
36 import javax.ws.rs.core.Response.Status;
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.kie.api.builder.ReleaseId;
41 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
42 import org.onap.policy.common.utils.coder.CoderException;
43 import org.onap.policy.common.utils.network.NetworkUtil;
44 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
45 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
46 import org.onap.policy.drools.properties.DroolsPropertyConstants;
47 import org.onap.policy.drools.system.PolicyControllerConstants;
48 import org.onap.policy.drools.system.PolicyEngineConstants;
49 import org.onap.policy.drools.util.KieUtils;
50 import org.onap.policy.drools.utils.logging.LoggerUtil;
51 import org.onap.policy.simulators.Util;
54 * Test RestControlLoopManager.
56 public class RestControlLoopManagerTest {
58 private static final String KSESSION = "op";
59 private static final String KMODULE_DRL_PATH = "src/test/resources/op.drl";
60 private static final String KMODULE_POM_PATH = "src/test/resources/op.pom";
61 private static final String KMODULE_PATH = "src/test/resources/op.kmodule";
62 private static final String KJAR_DRL_PATH =
63 "src/main/resources/kbop/org/onap/policy/drools/test/op.drl";
65 private static final String CONTROLLER = KSESSION;
66 private static final String CONTROLOOP_NAME =
67 "ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e";
69 private static final String CLIENT_CONFIG = "op-http";
71 private static final String URL_CONTEXT_PATH_CONTROLLERS = "controllers/";
72 private static final String URL_CONTEXT_PATH_CONTROLLER =
73 URL_CONTEXT_PATH_CONTROLLERS + CONTROLLER;
74 private static final String URL_CONTEXT_PATH_KSESSION =
75 URL_CONTEXT_PATH_CONTROLLER + "/drools/facts/" + KSESSION;
76 private static final String URL_CONTEXT_PATH_CONTROLLOOPS =
77 URL_CONTEXT_PATH_KSESSION + "/controlloops/";
78 private static final String URL_CONTEXT_PATH_CONTROLLOOP =
79 URL_CONTEXT_PATH_CONTROLLOOPS + CONTROLOOP_NAME;
80 private static final String URL_CONTEXT_PATH_CONTROLLOOP_POLICY =
81 URL_CONTEXT_PATH_CONTROLLOOP + "/policy";
83 private static final String URL_CONTEXT_PATH_TOOLS = "tools/controlloops/";
84 private static final String URL_CONTEXT_PATH_TOOLS_AAI = URL_CONTEXT_PATH_TOOLS + "aai/";
85 private static final String URL_CONTEXT_PATH_TOOLS_AAI_CQ =
86 URL_CONTEXT_PATH_TOOLS_AAI + "customQuery/";
87 private static final String POLICY = "src/test/resources/vCPE.yaml";
89 private static final String CONTROLLER_FILE = "op-controller.properties";
90 private static final String CONTROLLER_FILE_BAK = "op-controller.properties.bak";
95 * @throws Exception if failure to complete the set up.
98 public static void setUp() throws Exception {
99 System.setProperty("kie.maven.settings.custom", "src/test/resources/settings.xml");
100 LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "WARN");
102 SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
103 PolicyEngineConstants.getManager()
104 .configure(PolicyEngineConstants.getManager().defaultTelemetryConfig());
106 ReleaseId releaseId = KieUtils.installArtifact(Paths.get(KMODULE_PATH).toFile(),
107 Paths.get(KMODULE_POM_PATH).toFile(), KJAR_DRL_PATH,
108 Paths.get(KMODULE_DRL_PATH).toFile());
110 Properties controllerProperties = new Properties();
111 controllerProperties.put(DroolsPropertyConstants.RULES_GROUPID, releaseId.getGroupId());
112 controllerProperties.put(DroolsPropertyConstants.RULES_ARTIFACTID,
113 releaseId.getArtifactId());
114 controllerProperties.put(DroolsPropertyConstants.RULES_VERSION, releaseId.getVersion());
116 PolicyEngineConstants.getManager().createPolicyController(CONTROLLER, controllerProperties);
117 PolicyEngineConstants.getManager().start();
119 HttpClientFactoryInstance.getClientFactory()
120 .build(SystemPersistenceConstants.getManager().getProperties(CLIENT_CONFIG));
122 if (!NetworkUtil.isTcpPortOpen("localhost", 9696, 6, 10000L)) {
123 throw new IllegalStateException("cannot connect to port 9696");
126 await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive());
128 PolicyEngineConstants.getManager().setEnvironmentProperty(ControlLoopEventManager.AAI_URL,
129 "http://localhost:6666");
130 PolicyEngineConstants.getManager()
131 .setEnvironmentProperty(ControlLoopEventManager.AAI_USERNAME_PROPERTY, "AAI");
132 PolicyEngineConstants.getManager()
133 .setEnvironmentProperty(ControlLoopEventManager.AAI_PASS_PROPERTY, "AAI");
142 public static void tearDown() {
143 PolicyControllerConstants.getFactory().get(CONTROLLER).stop();
144 await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive(), equalTo(Boolean.FALSE));
146 PolicyEngineConstants.getManager().removePolicyController(CONTROLLER);
147 PolicyEngineConstants.getManager().stop();
149 final Path controllerPath =
150 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
153 Files.deleteIfExists(controllerPath);
154 } catch (Exception ignored) {
155 /* to satisfy checkstyle */
158 Path controllerBakPath =
159 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
160 CONTROLLER_FILE_BAK);
163 Files.deleteIfExists(controllerBakPath);
164 } catch (Exception ignored) {
165 /* to satisfy checkstyle */
170 * Test Operational Policies.
173 public void testOperationalPolicy() throws IOException {
174 assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
175 .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOPS).getStatus());
177 assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
178 .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOP).getStatus());
180 assertEquals(Status.NOT_FOUND.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
181 .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOP_POLICY).getStatus());
183 String policyFromFile = new String(Files.readAllBytes(Paths.get(POLICY)));
184 HttpClientFactoryInstance.getClientFactory().get(CONTROLLER).put(
185 URL_CONTEXT_PATH_CONTROLLOOP_POLICY, Entity.text(policyFromFile),
186 Collections.emptyMap());
188 assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
189 .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOP_POLICY).getStatus());
191 String policyFromPdpD = HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
192 .get(URL_CONTEXT_PATH_CONTROLLOOP_POLICY).readEntity(String.class);
194 assertEquals(policyFromFile, policyFromPdpD);
196 assertEquals(Status.CONFLICT.getStatusCode(),
197 HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
198 .put(URL_CONTEXT_PATH_CONTROLLOOP_POLICY, Entity.text(policyFromFile),
199 Collections.emptyMap())
204 * Test AAI Custom Query.
207 public void testAaiCq() throws CoderException {
208 assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
209 .get(CONTROLLER).get(URL_CONTEXT_PATH_TOOLS_AAI_CQ + "dummy").getStatus());
213 * Test if the session is alive.
215 * @return if the container is alive.
217 private static Callable<Boolean> isContainerAlive() {
218 return () -> PolicyControllerConstants.getFactory().get(CONTROLLER).getDrools()
219 .getContainer().isAlive();