aboutsummaryrefslogtreecommitdiffstats
path: root/controlloop/common/feature-controlloop-management/src/test/java/org/onap/policy/drools/server/restful/RestControlLoopManagerTest.java
blob: 1b37c3eee4ff7fdab0716e0178d1de7821cf9544 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.drools.server.restful;

import static org.awaitility.Awaitility.await;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;

import jakarta.ws.rs.core.Response.Status;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.logging.LoggerUtils;
import org.onap.policy.common.utils.network.NetworkUtil;
import org.onap.policy.drools.persistence.SystemPersistenceConstants;
import org.onap.policy.drools.properties.DroolsPropertyConstants;
import org.onap.policy.drools.system.PolicyControllerConstants;
import org.onap.policy.drools.system.PolicyEngine;
import org.onap.policy.drools.system.PolicyEngineConstants;
import org.onap.policy.drools.util.KieUtils;
import org.onap.policy.simulators.Util;

/**
 * Test RestControlLoopManager.
 */
class RestControlLoopManagerTest {

    private static final String KSESSION = "op";
    private static final String KMODULE_DRL_PATH = "src/test/resources/op.drl";
    private static final String KMODULE_POM_PATH = "src/test/resources/op.pom";
    private static final String KMODULE_PATH = "src/test/resources/op.kmodule";
    private static final String KJAR_DRL_PATH =
        "src/main/resources/kbop/org/onap/policy/drools/test/op.drl";

    private static final String CONTROLLER = KSESSION;
    private static final String CONTROLOOP_NAME =
        "ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e";

    private static final String CLIENT_CONFIG = "op-http";

    private static final String URL_CONTEXT_PATH_CONTROLLERS = "controllers/";
    private static final String URL_CONTEXT_PATH_CONTROLLER =
        URL_CONTEXT_PATH_CONTROLLERS + CONTROLLER;
    private static final String URL_CONTEXT_PATH_KSESSION =
        URL_CONTEXT_PATH_CONTROLLER + "/drools/facts/" + KSESSION;
    private static final String URL_CONTEXT_PATH_CONTROLLOOPS =
        URL_CONTEXT_PATH_KSESSION + "/controlloops/";
    private static final String URL_CONTEXT_PATH_CONTROLLOOP =
        URL_CONTEXT_PATH_CONTROLLOOPS + CONTROLOOP_NAME;
    private static final String URL_CONTEXT_PATH_CONTROLLOOP_POLICY =
        URL_CONTEXT_PATH_CONTROLLOOP + "/policy";

    private static final String URL_CONTEXT_PATH_TOOLS = "tools/controlloops/";
    private static final String URL_CONTEXT_PATH_TOOLS_AAI = URL_CONTEXT_PATH_TOOLS + "aai/";
    private static final String URL_CONTEXT_PATH_TOOLS_AAI_CQ =
        URL_CONTEXT_PATH_TOOLS_AAI + "customQuery/";

    private static final String CONTROLLER_FILE = "op-controller.properties";
    private static final String CONTROLLER_FILE_BAK = "op-controller.properties.bak";

    /**
     * test set up.
     *
     * @throws Exception if failure to complete the set up.
     */
    @BeforeAll
    public static void setUp() throws Exception {
        System.setProperty("kie.maven.settings.custom", "src/test/resources/settings.xml");
        LoggerUtils.setLevel(LoggerUtils.ROOT_LOGGER, "WARN");

        SystemPersistenceConstants.getManager().setConfigurationDir("src/test/resources");
        PolicyEngineConstants.getManager()
            .configure(PolicyEngineConstants.getManager().defaultTelemetryConfig());

        var releaseId = KieUtils.installArtifact(Paths.get(KMODULE_PATH).toFile(),
            Paths.get(KMODULE_POM_PATH).toFile(), KJAR_DRL_PATH,
            Paths.get(KMODULE_DRL_PATH).toFile());

        var controllerProperties = new Properties();
        controllerProperties.put(DroolsPropertyConstants.RULES_GROUPID, releaseId.getGroupId());
        controllerProperties.put(DroolsPropertyConstants.RULES_ARTIFACTID,
            releaseId.getArtifactId());
        controllerProperties.put(DroolsPropertyConstants.RULES_VERSION, releaseId.getVersion());

        PolicyEngineConstants.getManager().createPolicyController(CONTROLLER, controllerProperties);
        PolicyEngineConstants.getManager().start();

        HttpClientFactoryInstance.getClientFactory()
            .build(SystemPersistenceConstants.getManager().getProperties(CLIENT_CONFIG));

        if (!NetworkUtil.isTcpPortOpen("localhost", 9696, 6, 10000L)) {
            throw new IllegalStateException("cannot connect to port 9696");
        }

        await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive());

        PolicyEngine mgr = PolicyEngineConstants.getManager();
        mgr.setEnvironmentProperty("aai.url", "http://localhost:6666");
        mgr.setEnvironmentProperty("aai.username", "AAI");
        mgr.setEnvironmentProperty("aai.password", "AAI");

        Util.buildAaiSim();
    }

    /**
     * test tear down.
     */
    @AfterAll
    public static void tearDown() {
        PolicyControllerConstants.getFactory().get(CONTROLLER).stop();
        await().atMost(1, TimeUnit.MINUTES).until(isContainerAlive(), equalTo(Boolean.FALSE));

        PolicyEngineConstants.getManager().removePolicyController(CONTROLLER);
        PolicyEngineConstants.getManager().stop();

        final var controllerPath =
            Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
                CONTROLLER_FILE);
        try {
            Files.deleteIfExists(controllerPath);
        } catch (Exception ignored) {
            /* to satisfy checkstyle */
        }

        var controllerBakPath =
            Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
                CONTROLLER_FILE_BAK);

        try {
            Files.deleteIfExists(controllerBakPath);
        } catch (Exception ignored) {
            /* to satisfy checkstyle */
        }
    }

    /**
     * Test Operational Policies.
     */
    @Test
    void testOperationalPolicy() throws IOException {
        assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
            .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOPS).getStatus());

        assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
            .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOP).getStatus());

        assertEquals(Status.NOT_FOUND.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
            .get(CONTROLLER).get(URL_CONTEXT_PATH_CONTROLLOOP_POLICY).getStatus());
    }

    /**
     * Test AAI Custom Query.
     */
    @Test
    void testAaiCq() throws CoderException {
        assertEquals(Status.OK.getStatusCode(), HttpClientFactoryInstance.getClientFactory()
            .get(CONTROLLER).get(URL_CONTEXT_PATH_TOOLS_AAI_CQ + "dummy").getStatus());
    }

    /**
     * Test if the session is alive.
     *
     * @return if the container is alive.
     */
    private static Callable<Boolean> isContainerAlive() {
        return () -> PolicyControllerConstants.getFactory().get(CONTROLLER).getDrools()
            .getContainer().isAlive();
    }
}