ac73f93dc99e6c380b20eabe7ac0af78ff164b44
[policy/drools-applications.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023-2024 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.drools.server.restful;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28
29 import jakarta.ws.rs.client.Entity;
30 import jakarta.ws.rs.core.Response;
31 import java.util.Collections;
32 import java.util.List;
33 import org.junit.jupiter.api.AfterAll;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.common.endpoints.event.comm.Topic;
37 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
38 import org.onap.policy.common.endpoints.http.client.HttpClient;
39 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
40 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
41 import org.onap.policy.common.utils.network.NetworkUtil;
42 import org.onap.policy.common.utils.resources.ResourceUtils;
43 import org.onap.policy.controlloop.VirtualControlLoopNotification;
44 import org.onap.policy.controlloop.util.Serialization;
45 import org.onap.policy.drools.apps.controlloop.feature.trans.ControlLoopMetricsFeature;
46 import org.onap.policy.drools.apps.controlloop.feature.trans.ControlLoopMetricsManager;
47 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
48 import org.onap.policy.drools.system.PolicyController;
49 import org.onap.policy.drools.system.PolicyEngineConstants;
50
51 class RestTransactionTrackerTest {
52
53     private static PolicyController testController;
54     private static HttpClient client;
55
56     @BeforeAll
57     public static void testBeforeClass() throws Exception {
58         SystemPersistenceConstants.getManager().setConfigurationDir("target/test-classes");
59
60         HttpServletServerFactoryInstance.getServerFactory().destroy();
61         HttpClientFactoryInstance.getClientFactory().destroy();
62
63         HttpClientFactoryInstance.getClientFactory().build(
64                 BusTopicParams.builder()
65                         .clientName("trans")
66                         .hostname("localhost")
67                         .port(8769)
68                         .basePath("policy/pdp/engine/controllers/transactions")
69                         .managed(true)
70                         .build());
71
72         var server =
73                 HttpServletServerFactoryInstance
74                         .getServerFactory()
75                         .build("trans", "localhost", 8769, "/", true, true);
76         server.addServletClass("/*", RestTransactionTracker.class.getName());
77         server.waitedStart(5000L);
78         assertTrue(NetworkUtil.isTcpPortOpen("localhost", 8769, 5, 10000L));
79
80         testController = PolicyEngineConstants.getManager().createPolicyController("metrics",
81                 SystemPersistenceConstants.getManager().getControllerProperties("metrics"));
82
83         client = HttpClientFactoryInstance.getClientFactory().get("trans");
84     }
85
86     @AfterAll
87     public static void testAfterClass() {
88         HttpClientFactoryInstance.getClientFactory().destroy();
89         HttpServletServerFactoryInstance.getServerFactory().destroy();
90
91         SystemPersistenceConstants.getManager().setConfigurationDir(null);
92     }
93
94     @Test
95     void testConfiguration() {
96         equals(get("cacheSize", Response.Status.OK.getStatusCode()), Integer.class, 3);
97         equals(get("timeout", Response.Status.OK.getStatusCode()), Integer.class, 2);
98
99         put("cacheSize/10", "", Response.Status.OK.getStatusCode());
100         put("timeout/20", "", Response.Status.OK.getStatusCode());
101
102         equals(get("cacheSize", Response.Status.OK.getStatusCode()), Integer.class, 10);
103         equals(get("timeout", Response.Status.OK.getStatusCode()), Integer.class, 20);
104
105         put("cacheSize/3", "", Response.Status.OK.getStatusCode());
106         put("timeout/2", "", Response.Status.OK.getStatusCode());
107
108         equals(get("cacheSize", Response.Status.OK.getStatusCode()), Integer.class, 3);
109         equals(get("timeout", Response.Status.OK.getStatusCode()), Integer.class, 2);
110     }
111
112     @Test
113     void testTransactions() {
114         equals(get("/inprogress", Response.Status.OK.getStatusCode()), List.class, Collections.emptyList());
115
116         ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
117
118         assertTrue(HttpClient.getBody(get("/inprogress", Response.Status.OK.getStatusCode()),
119                 List.class).isEmpty());
120         get("/inprogress/664be3d2-6c12-4f4b-a3e7-c349acced200", Response.Status.NOT_FOUND.getStatusCode());
121
122         var activeNotification = ResourceUtils.getResourceAsString("policy-cl-mgt-active.json");
123         var active =
124                 Serialization.gsonPretty.fromJson(activeNotification, VirtualControlLoopNotification.class);
125         feature.beforeDeliver(testController, Topic.CommInfrastructure.NOOP, "POLICY-CL-MGT", active);
126         assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size());
127
128         assertFalse(HttpClient.getBody(get("/inprogress", Response.Status.OK.getStatusCode()),
129                 List.class).isEmpty());
130         notNull(get("/inprogress/664be3d2-6c12-4f4b-a3e7-c349acced200", Response.Status.OK.getStatusCode()),
131                 String.class);
132     }
133
134     private Response get(String contextPath, int statusCode) {
135         var response = client.get(contextPath);
136         return checkResponse(statusCode, response);
137     }
138
139     private Response put(String contextPath, String body, int statusCode) {
140         var response = client.put(contextPath, Entity.json(body), Collections.emptyMap());
141         return checkResponse(statusCode, response);
142     }
143
144     private <T, Y> void equals(Response response, Class<T> clazz, Y expected) {
145         assertEquals(expected, HttpClient.getBody(response, clazz));
146     }
147
148     private <T> void notNull(Response response, Class<T> clazz) {
149         assertNotNull(HttpClient.getBody(response, clazz));
150     }
151
152     private Response checkResponse(int statusCode, Response response) {
153         assertEquals(statusCode, response.getStatus());
154         return response;
155     }
156 }