2fc248ca134cc7eafc1bc1d8fbfec14e0d46da2e
[policy/drools-applications.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 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.Assert.assertEquals;
27
28 import jakarta.ws.rs.core.Response.Status;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.util.Properties;
34 import java.util.concurrent.Callable;
35 import java.util.concurrent.TimeUnit;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.kie.api.builder.ReleaseId;
40 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
41 import org.onap.policy.common.utils.coder.CoderException;
42 import org.onap.policy.common.utils.logging.LoggerUtils;
43 import org.onap.policy.common.utils.network.NetworkUtil;
44 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
45 import org.onap.policy.drools.properties.DroolsPropertyConstants;
46 import org.onap.policy.drools.system.PolicyControllerConstants;
47 import org.onap.policy.drools.system.PolicyEngine;
48 import org.onap.policy.drools.system.PolicyEngineConstants;
49 import org.onap.policy.drools.util.KieUtils;
50 import org.onap.policy.simulators.Util;
51
52 /**
53  * Test RestControlLoopManager.
54  */
55 public class RestControlLoopManagerTest {
56
57     private static final String KSESSION = "op";
58     private static final String KMODULE_DRL_PATH = "src/test/resources/op.drl";
59     private static final String KMODULE_POM_PATH = "src/test/resources/op.pom";
60     private static final String KMODULE_PATH = "src/test/resources/op.kmodule";
61     private static final String KJAR_DRL_PATH =
62         "src/main/resources/kbop/org/onap/policy/drools/test/op.drl";
63
64     private static final String CONTROLLER = KSESSION;
65     private static final String CONTROLOOP_NAME =
66         "ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e";
67
68     private static final String CLIENT_CONFIG = "op-http";
69
70     private static final String URL_CONTEXT_PATH_CONTROLLERS = "controllers/";
71     private static final String URL_CONTEXT_PATH_CONTROLLER =
72         URL_CONTEXT_PATH_CONTROLLERS + CONTROLLER;
73     private static final String URL_CONTEXT_PATH_KSESSION =
74         URL_CONTEXT_PATH_CONTROLLER + "/drools/facts/" + KSESSION;
75     private static final String URL_CONTEXT_PATH_CONTROLLOOPS =
76         URL_CONTEXT_PATH_KSESSION + "/controlloops/";
77     private static final String URL_CONTEXT_PATH_CONTROLLOOP =
78         URL_CONTEXT_PATH_CONTROLLOOPS + CONTROLOOP_NAME;
79     private static final String URL_CONTEXT_PATH_CONTROLLOOP_POLICY =
80         URL_CONTEXT_PATH_CONTROLLOOP + "/policy";
81
82     private static final String URL_CONTEXT_PATH_TOOLS = "tools/controlloops/";
83     private static final String URL_CONTEXT_PATH_TOOLS_AAI = URL_CONTEXT_PATH_TOOLS + "aai/";
84     private static final String URL_CONTEXT_PATH_TOOLS_AAI_CQ =
85         URL_CONTEXT_PATH_TOOLS_AAI + "customQuery/";
86
87     private static final String CONTROLLER_FILE = "op-controller.properties";
88     private static final String CONTROLLER_FILE_BAK = "op-controller.properties.bak";
89
90     /**
91      * test set up.
92      *
93      * @throws Exception if failure to complete the set up.
94      */
95     @BeforeClass
96     public static void setUp() throws Exception {
97         System.setProperty("kie.maven.settings.custom", "src/test/resources/settings.xml");
98         LoggerUtils.setLevel(LoggerUtils.ROOT_LOGGER, "WARN");
99
100         SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
101         PolicyEngineConstants.getManager()
102             .configure(PolicyEngineConstants.getManager().defaultTelemetryConfig());
103
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());
107
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());
113
114         PolicyEngineConstants.getManager().createPolicyController(CONTROLLER, controllerProperties);
115         PolicyEngineConstants.getManager().start();
116
117         HttpClientFactoryInstance.getClientFactory()
118             .build(SystemPersistenceConstants.getManager().getProperties(CLIENT_CONFIG));
119
120         if (!NetworkUtil.isTcpPortOpen("localhost", 9696, 6, 10000L)) {
121             throw new IllegalStateException("cannot connect to port 9696");
122         }
123
124         await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive());
125
126         PolicyEngine mgr = PolicyEngineConstants.getManager();
127         mgr.setEnvironmentProperty("aai.url", "http://localhost:6666");
128         mgr.setEnvironmentProperty("aai.username", "AAI");
129         mgr.setEnvironmentProperty("aai.password", "AAI");
130
131         Util.buildAaiSim();
132     }
133
134     /**
135      * test tear down.
136      */
137     @AfterClass
138     public static void tearDown() {
139         PolicyControllerConstants.getFactory().get(CONTROLLER).stop();
140         await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive(), equalTo(Boolean.FALSE));
141
142         PolicyEngineConstants.getManager().removePolicyController(CONTROLLER);
143         PolicyEngineConstants.getManager().stop();
144
145         final Path controllerPath =
146             Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
147                 CONTROLLER_FILE);
148         try {
149             Files.deleteIfExists(controllerPath);
150         } catch (Exception ignored) {
151             /* to satisfy checkstyle */
152         }
153
154         Path controllerBakPath =
155             Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
156                 CONTROLLER_FILE_BAK);
157
158         try {
159             Files.deleteIfExists(controllerBakPath);
160         } catch (Exception ignored) {
161             /* to satisfy checkstyle */
162         }
163     }
164
165     /**
166      * Test Operational Policies.
167      */
168     @Test
169     public void testOperationalPolicy() throws IOException {
170         assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
171             .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOPS).getStatus());
172
173         assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
174             .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOP).getStatus());
175
176         assertEquals(Status.NOT_FOUND.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
177             .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOP_POLICY).getStatus());
178     }
179
180     /**
181      * Test AAI Custom Query.
182      */
183     @Test
184     public void testAaiCq() throws CoderException {
185         assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
186             .get(CONTROLLER).get(URL_CONTEXT_PATH_TOOLS_AAI_CQ + "dummy").getStatus());
187     }
188
189     /**
190      * Test if the session is alive.
191      *
192      * @return if the container is alive.
193      */
194     private static Callable<Boolean> isContainerAlive() {
195         return () -> PolicyControllerConstants.getFactory().get(CONTROLLER).getDrools()
196             .getContainer().isAlive();
197     }
198 }