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;
26 import static org.junit.Assert.assertNotNull;
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.util.Collections;
33 import java.util.Properties;
34 import java.util.concurrent.Callable;
35 import java.util.concurrent.TimeUnit;
36 import javax.ws.rs.client.Entity;
37 import javax.ws.rs.core.Response.Status;
38 import org.junit.AfterClass;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.kie.api.builder.ReleaseId;
42 import org.onap.policy.aai.AaiCqResponse;
43 import org.onap.policy.aai.AaiNqResponseWrapper;
44 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
45 import org.onap.policy.common.utils.coder.CoderException;
46 import org.onap.policy.common.utils.coder.StandardCoder;
47 import org.onap.policy.common.utils.network.NetworkUtil;
48 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
49 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
50 import org.onap.policy.drools.properties.DroolsPropertyConstants;
51 import org.onap.policy.drools.system.PolicyControllerConstants;
52 import org.onap.policy.drools.system.PolicyEngineConstants;
53 import org.onap.policy.drools.util.KieUtils;
54 import org.onap.policy.drools.utils.logging.LoggerUtil;
55 import org.onap.policy.simulators.Util;
58 * Test RestControlLoopManager.
60 public class RestControlLoopManagerTest {
62 private static final String KSESSION = "op";
63 private static final String KMODULE_DRL_PATH = "src/test/resources/op.drl";
64 private static final String KMODULE_POM_PATH = "src/test/resources/op.pom";
65 private static final String KMODULE_PATH = "src/test/resources/op.kmodule";
66 private static final String KJAR_DRL_PATH = "src/main/resources/kbop/org/onap/policy/drools/test/op.drl";
68 private static final String CONTROLLER = KSESSION;
69 private static final String CONTROLOOP_NAME = "ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e";
71 private static final String CLIENT_CONFIG = "op-http";
73 private static final String URL_CONTEXT_PATH_CONTROLLERS = "controllers/";
74 private static final String URL_CONTEXT_PATH_CONTROLLER = URL_CONTEXT_PATH_CONTROLLERS + CONTROLLER;
75 private static final String URL_CONTEXT_PATH_KSESSION = URL_CONTEXT_PATH_CONTROLLER + "/drools/facts/" + KSESSION;
76 private static final String URL_CONTEXT_PATH_CONTROLLOOPS = URL_CONTEXT_PATH_KSESSION + "/controlloops/";
77 private static final String URL_CONTEXT_PATH_CONTROLLOOP = URL_CONTEXT_PATH_CONTROLLOOPS + CONTROLOOP_NAME;
78 private static final String URL_CONTEXT_PATH_CONTROLLOOP_POLICY = URL_CONTEXT_PATH_CONTROLLOOP + "/policy";
80 private static final String URL_CONTEXT_PATH_TOOLS = "tools/controlloops/";
81 private static final String URL_CONTEXT_PATH_TOOLS_AAI = URL_CONTEXT_PATH_TOOLS + "aai/";
82 private static final String URL_CONTEXT_PATH_TOOLS_AAI_NQ = URL_CONTEXT_PATH_TOOLS_AAI + "namedQuery/";
83 private static final String URL_CONTEXT_PATH_TOOLS_AAI_CQ = 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().configure(PolicyEngineConstants.getManager().defaultTelemetryConfig());
103 ReleaseId releaseId =
104 KieUtils.installArtifact(Paths.get(KMODULE_PATH).toFile(), Paths.get(KMODULE_POM_PATH).toFile(),
105 KJAR_DRL_PATH, 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, releaseId.getArtifactId());
110 controllerProperties.put(DroolsPropertyConstants.RULES_VERSION, releaseId.getVersion());
112 PolicyEngineConstants.getManager().createPolicyController(CONTROLLER, controllerProperties);
113 PolicyEngineConstants.getManager().start();
115 HttpClientFactoryInstance.getClientFactory()
116 .build(SystemPersistenceConstants.getManager().getProperties(CLIENT_CONFIG));
118 if (!NetworkUtil.isTcpPortOpen("localhost", 9696, 6, 10000L)) {
119 throw new IllegalStateException("cannot connect to port 9696");
122 await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive());
124 PolicyEngineConstants.getManager().setEnvironmentProperty(ControlLoopEventManager.AAI_URL, "http://localhost:6666");
125 PolicyEngineConstants.getManager().setEnvironmentProperty(ControlLoopEventManager.AAI_USERNAME_PROPERTY, "AAI");
126 PolicyEngineConstants.getManager().setEnvironmentProperty(ControlLoopEventManager.AAI_PASS_PROPERTY, "AAI");
135 public static void tearDown() {
136 PolicyControllerConstants.getFactory().get(CONTROLLER).stop();
137 await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive(), equalTo(Boolean.FALSE));
139 PolicyEngineConstants.getManager().removePolicyController(CONTROLLER);
140 PolicyEngineConstants.getManager().stop();
142 final Path controllerPath =
143 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), CONTROLLER_FILE);
145 Files.deleteIfExists(controllerPath);
146 } catch (Exception ignored) {
147 /* to satisfy checkstyle */
150 Path controllerBakPath =
151 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(), CONTROLLER_FILE_BAK);
154 Files.deleteIfExists(controllerBakPath);
155 } catch (Exception ignored) {
156 /* to satisfy checkstyle */
161 * Test Operational Policies.
164 public void testOperationalPolicy() throws IOException {
165 assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
166 .get(URL_CONTEXT_PATH_CONTROLLOOPS).getStatus());
168 assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
169 .get(URL_CONTEXT_PATH_CONTROLLOOP).getStatus());
171 assertEquals(Status.NOT_FOUND.getStatusCode(), HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
172 .get(URL_CONTEXT_PATH_CONTROLLOOP_POLICY).getStatus());
174 String policyFromFile = new String(Files.readAllBytes(Paths.get(POLICY)));
175 HttpClientFactoryInstance.getClientFactory().get(CONTROLLER).put(URL_CONTEXT_PATH_CONTROLLOOP_POLICY,
176 Entity.text(policyFromFile), Collections.emptyMap());
178 assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
179 .get(URL_CONTEXT_PATH_CONTROLLOOP_POLICY).getStatus());
181 String policyFromPdpD = HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
182 .get(URL_CONTEXT_PATH_CONTROLLOOP_POLICY)
183 .readEntity(String.class);
185 assertEquals(policyFromFile, policyFromPdpD);
187 assertEquals(Status.CONFLICT.getStatusCode(),
188 HttpClientFactoryInstance.getClientFactory().get(CONTROLLER).put(URL_CONTEXT_PATH_CONTROLLOOP_POLICY,
189 Entity.text(policyFromFile), Collections.emptyMap()).getStatus());
193 * Test AAI Named Query.
196 public void testAaiNq() throws CoderException {
197 assertEquals(Status.OK.getStatusCode(),
198 HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
199 .get(URL_CONTEXT_PATH_TOOLS_AAI_NQ + "dummy")
203 HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
204 .get(URL_CONTEXT_PATH_TOOLS_AAI_NQ + "dummy")
205 .readEntity(String.class);
207 assertNotNull(new StandardCoder().decode(nqResponse, AaiNqResponseWrapper.class));
211 * Test AAI Custom Query.
214 public void testAaiCq() throws CoderException {
215 assertEquals(Status.OK.getStatusCode(),
216 HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
217 .get(URL_CONTEXT_PATH_TOOLS_AAI_CQ + "dummy")
221 HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
222 .get(URL_CONTEXT_PATH_TOOLS_AAI_CQ + "dummy")
223 .readEntity(String.class);
225 assertNotNull(new StandardCoder().decode(cqResponse, AaiCqResponse.class));
229 * Test if the session is alive.
231 * @return if the container is alive.
233 private static Callable<Boolean> isContainerAlive() {
234 return () -> PolicyControllerConstants.getFactory().get(CONTROLLER).getDrools().getContainer().isAlive();