ae14a64b780da5f00a2dc708101bab55e65d6329
[policy/drools-applications.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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 import static org.junit.Assert.assertNotNull;
27
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.SystemPersistence;
50 import org.onap.policy.drools.properties.DroolsProperties;
51 import org.onap.policy.drools.system.PolicyController;
52 import org.onap.policy.drools.system.PolicyEngine;
53 import org.onap.policy.drools.util.KieUtils;
54 import org.onap.policy.drools.utils.logging.LoggerUtil;
55 import org.onap.policy.simulators.Util;
56
57 /**
58  * Test RestControlLoopManager.
59  */
60 public class RestControlLoopManagerTest {
61
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";
67
68     private static final String CONTROLLER = KSESSION;
69     private static final String CONTROLOOP_NAME = "ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e";
70
71     private static final String CLIENT_CONFIG = "op-http";
72
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";
79
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/";
84
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         SystemPersistence.manager.setConfigurationDir("src/test/resources");
101         PolicyEngine.manager.configure(PolicyEngine.manager.defaultTelemetryConfig());
102
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());
106
107         Properties controllerProperties = new Properties();
108         controllerProperties.put(DroolsProperties.RULES_GROUPID, releaseId.getGroupId());
109         controllerProperties.put(DroolsProperties.RULES_ARTIFACTID, releaseId.getArtifactId());
110         controllerProperties.put(DroolsProperties.RULES_VERSION, releaseId.getVersion());
111
112         PolicyEngine.manager.createPolicyController(CONTROLLER, controllerProperties);
113         PolicyEngine.manager.start();
114
115         HttpClientFactoryInstance.getClientFactory().build(SystemPersistence.manager.getProperties(CLIENT_CONFIG));
116
117         if (!NetworkUtil.isTcpPortOpen("localhost", 9696, 6, 10000L)) {
118             throw new IllegalStateException("cannot connect to port 9696");
119         }
120
121         await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive());
122
123         PolicyEngine.manager.setEnvironmentProperty(ControlLoopEventManager.AAI_URL, "http://localhost:6666");
124         PolicyEngine.manager.setEnvironmentProperty(ControlLoopEventManager.AAI_USERNAME_PROPERTY, "AAI");
125         PolicyEngine.manager.setEnvironmentProperty(ControlLoopEventManager.AAI_PASS_PROPERTY, "AAI");
126
127         Util.buildAaiSim();
128     }
129
130     /**
131      * test tear down.
132      */
133     @AfterClass
134     public static void tearDown() {
135         PolicyController.factory.get(CONTROLLER).stop();
136         await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive(), equalTo(Boolean.FALSE));
137
138         PolicyEngine.manager.removePolicyController(CONTROLLER);
139         PolicyEngine.manager.stop();
140
141         final Path controllerPath =
142             Paths.get(SystemPersistence.manager.getConfigurationPath().toString(), CONTROLLER_FILE);
143         try {
144             Files.deleteIfExists(controllerPath);
145         } catch (Exception ignored) {
146             /* to satisfy checkstyle */
147         }
148
149         Path controllerBakPath =
150             Paths.get(SystemPersistence.manager.getConfigurationPath().toString(), CONTROLLER_FILE_BAK);
151
152         try {
153             Files.deleteIfExists(controllerBakPath);
154         } catch (Exception ignored) {
155             /* to satisfy checkstyle */
156         }
157     }
158
159     /**
160      * Test Operational Policies.
161      */
162     @Test
163     public void testOperationalPolicy() throws IOException {
164         assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
165                         .get(URL_CONTEXT_PATH_CONTROLLOOPS).getStatus());
166
167         assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
168                         .get(URL_CONTEXT_PATH_CONTROLLOOP).getStatus());
169
170         assertEquals(Status.NOT_FOUND.getStatusCode(), HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
171                         .get(URL_CONTEXT_PATH_CONTROLLOOP_POLICY).getStatus());
172
173         String policyFromFile = new String(Files.readAllBytes(Paths.get(POLICY)));
174         HttpClientFactoryInstance.getClientFactory().get(CONTROLLER).put(URL_CONTEXT_PATH_CONTROLLOOP_POLICY,
175                         Entity.text(policyFromFile), Collections.emptyMap());
176
177         assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
178                         .get(URL_CONTEXT_PATH_CONTROLLOOP_POLICY).getStatus());
179
180         String policyFromPdpD = HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
181             .get(URL_CONTEXT_PATH_CONTROLLOOP_POLICY)
182             .readEntity(String.class);
183
184         assertEquals(policyFromFile, policyFromPdpD);
185
186         assertEquals(Status.CONFLICT.getStatusCode(),
187             HttpClientFactoryInstance.getClientFactory().get(CONTROLLER).put(URL_CONTEXT_PATH_CONTROLLOOP_POLICY,
188                 Entity.text(policyFromFile), Collections.emptyMap()).getStatus());
189     }
190
191     /**
192      * Test AAI Named Query.
193      */
194     @Test
195     public void testAaiNq() throws CoderException {
196         assertEquals(Status.OK.getStatusCode(),
197             HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
198                 .get(URL_CONTEXT_PATH_TOOLS_AAI_NQ + "dummy")
199                 .getStatus());
200
201         String nqResponse =
202             HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
203                 .get(URL_CONTEXT_PATH_TOOLS_AAI_NQ + "dummy")
204                 .readEntity(String.class);
205
206         assertNotNull(new StandardCoder().decode(nqResponse, AaiNqResponseWrapper.class));
207     }
208
209     /**
210      * Test AAI Custom Query.
211      */
212     @Test
213     public void testAaiCq() throws CoderException {
214         assertEquals(Status.OK.getStatusCode(),
215             HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
216                 .get(URL_CONTEXT_PATH_TOOLS_AAI_CQ + "dummy")
217                 .getStatus());
218
219         String cqResponse =
220             HttpClientFactoryInstance.getClientFactory().get(CONTROLLER)
221                 .get(URL_CONTEXT_PATH_TOOLS_AAI_CQ + "dummy")
222                 .readEntity(String.class);
223
224         assertNotNull(new StandardCoder().decode(cqResponse, AaiCqResponse.class));
225     }
226
227     /**
228      * Test if the session is alive.
229      *
230      * @return if the container is alive.
231      */
232     private static Callable<Boolean> isContainerAlive() {
233         return () -> PolicyController.factory.get(CONTROLLER).getDrools().getContainer().isAlive();
234     }
235 }