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