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