2a5bffc7ff11202a584f6a6b5da73db3fbee6096
[policy/drools-applications.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.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;
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         LoggerUtil.setLevel(LoggerUtil.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         PolicyEngineConstants.getManager().setEnvironmentProperty(ControlLoopEventManager.AAI_URL,
126             "http://localhost:6666");
127         PolicyEngineConstants.getManager()
128             .setEnvironmentProperty(ControlLoopEventManager.AAI_USERNAME_PROPERTY, "AAI");
129         PolicyEngineConstants.getManager()
130             .setEnvironmentProperty(ControlLoopEventManager.AAI_PASS_PROPERTY, "AAI");
131
132         Util.buildAaiSim();
133     }
134
135     /**
136      * test tear down.
137      */
138     @AfterClass
139     public static void tearDown() {
140         PolicyControllerConstants.getFactory().get(CONTROLLER).stop();
141         await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive(), equalTo(Boolean.FALSE));
142
143         PolicyEngineConstants.getManager().removePolicyController(CONTROLLER);
144         PolicyEngineConstants.getManager().stop();
145
146         final Path controllerPath =
147             Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
148                 CONTROLLER_FILE);
149         try {
150             Files.deleteIfExists(controllerPath);
151         } catch (Exception ignored) {
152             /* to satisfy checkstyle */
153         }
154
155         Path controllerBakPath =
156             Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
157                 CONTROLLER_FILE_BAK);
158
159         try {
160             Files.deleteIfExists(controllerBakPath);
161         } catch (Exception ignored) {
162             /* to satisfy checkstyle */
163         }
164     }
165
166     /**
167      * Test Operational Policies.
168      */
169     @Test
170     public void testOperationalPolicy() throws IOException {
171         assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
172             .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOPS).getStatus());
173
174         assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
175             .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOP).getStatus());
176
177         assertEquals(Status.NOT_FOUND.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
178             .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOP_POLICY).getStatus());
179     }
180
181     /**
182      * Test AAI Custom Query.
183      */
184     @Test
185     public void testAaiCq() throws CoderException {
186         assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
187             .get(CONTROLLER).get(URL_CONTEXT_PATH_TOOLS_AAI_CQ + "dummy").getStatus());
188     }
189
190     /**
191      * Test if the session is alive.
192      *
193      * @return if the container is alive.
194      */
195     private static Callable<Boolean> isContainerAlive() {
196         return () -> PolicyControllerConstants.getFactory().get(CONTROLLER).getDrools()
197             .getContainer().isAlive();
198     }
199 }