b9caef9da95cbe2bb58d1710dcd967a6ccd50c4d
[policy/drools-applications.git] / controlloop / common / feature-controlloop-management / src / test / java / org / onap / policy / drools / server / restful / RestControlLoopManagerTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.drools.server.restful;
22
23 import static org.awaitility.Awaitility.await;
24 import static org.hamcrest.Matchers.equalTo;
25 import static org.junit.Assert.assertEquals;
26
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;
50
51 /**
52  * Test RestControlLoopManager.
53  */
54 public class RestControlLoopManagerTest {
55
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";
62
63     private static final String CONTROLLER = KSESSION;
64     private static final String CONTROLOOP_NAME =
65         "ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e";
66
67     private static final String CLIENT_CONFIG = "op-http";
68
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";
80
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
86     private static final String CONTROLLER_FILE = "op-controller.properties";
87     private static final String CONTROLLER_FILE_BAK = "op-controller.properties.bak";
88
89     /**
90      * test set up.
91      *
92      * @throws Exception if failure to complete the set up.
93      */
94     @BeforeClass
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");
98
99         SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
100         PolicyEngineConstants.getManager()
101             .configure(PolicyEngineConstants.getManager().defaultTelemetryConfig());
102
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());
106
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());
112
113         PolicyEngineConstants.getManager().createPolicyController(CONTROLLER, controllerProperties);
114         PolicyEngineConstants.getManager().start();
115
116         HttpClientFactoryInstance.getClientFactory()
117             .build(SystemPersistenceConstants.getManager().getProperties(CLIENT_CONFIG));
118
119         if (!NetworkUtil.isTcpPortOpen("localhost", 9696, 6, 10000L)) {
120             throw new IllegalStateException("cannot connect to port 9696");
121         }
122
123         await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive());
124
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");
129
130         Util.buildAaiSim();
131     }
132
133     /**
134      * test tear down.
135      */
136     @AfterClass
137     public static void tearDown() {
138         PolicyControllerConstants.getFactory().get(CONTROLLER).stop();
139         await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive(), equalTo(Boolean.FALSE));
140
141         PolicyEngineConstants.getManager().removePolicyController(CONTROLLER);
142         PolicyEngineConstants.getManager().stop();
143
144         final Path controllerPath =
145             Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
146                 CONTROLLER_FILE);
147         try {
148             Files.deleteIfExists(controllerPath);
149         } catch (Exception ignored) {
150             /* to satisfy checkstyle */
151         }
152
153         Path controllerBakPath =
154             Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
155                 CONTROLLER_FILE_BAK);
156
157         try {
158             Files.deleteIfExists(controllerBakPath);
159         } catch (Exception ignored) {
160             /* to satisfy checkstyle */
161         }
162     }
163
164     /**
165      * Test Operational Policies.
166      */
167     @Test
168     public void testOperationalPolicy() throws IOException {
169         assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
170             .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOPS).getStatus());
171
172         assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
173             .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOP).getStatus());
174
175         assertEquals(Status.NOT_FOUND.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
176             .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOP_POLICY).getStatus());
177     }
178
179     /**
180      * Test AAI Custom Query.
181      */
182     @Test
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());
186     }
187
188     /**
189      * Test if the session is alive.
190      *
191      * @return if the container is alive.
192      */
193     private static Callable<Boolean> isContainerAlive() {
194         return () -> PolicyControllerConstants.getFactory().get(CONTROLLER).getDrools()
195             .getContainer().isAlive();
196     }
197 }