Remove deprecated DMAAP dependency
[policy/drools-applications.git] / controlloop / common / feature-controlloop-trans / src / test / java / org / onap / policy / drools / server / restful / RestTransactionTrackerTest.java
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.jetbrains.annotations.NotNull;
34 import org.junit.jupiter.api.AfterAll;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.Test;
37 import org.onap.policy.common.endpoints.event.comm.Topic;
38 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
39 import org.onap.policy.common.endpoints.http.client.HttpClient;
40 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
41 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
42 import org.onap.policy.common.utils.network.NetworkUtil;
43 import org.onap.policy.common.utils.resources.ResourceUtils;
44 import org.onap.policy.controlloop.VirtualControlLoopNotification;
45 import org.onap.policy.controlloop.util.Serialization;
46 import org.onap.policy.drools.apps.controlloop.feature.trans.ControlLoopMetricsFeature;
47 import org.onap.policy.drools.apps.controlloop.feature.trans.ControlLoopMetricsManager;
48 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
49 import org.onap.policy.drools.system.PolicyController;
50 import org.onap.policy.drools.system.PolicyEngineConstants;
51
52 class RestTransactionTrackerTest {
53
54     private static PolicyController testController;
55     private static HttpClient client;
56
57     @BeforeAll
58     public static void testBeforeClass() throws Exception {
59         SystemPersistenceConstants.getManager().setConfigurationDir("target/test-classes");
60
61         HttpServletServerFactoryInstance.getServerFactory().destroy();
62         HttpClientFactoryInstance.getClientFactory().destroy();
63
64         HttpClientFactoryInstance.getClientFactory().build(
65                 BusTopicParams.builder()
66                         .clientName("trans")
67                         .hostname("localhost")
68                         .port(8769)
69                         .basePath("policy/pdp/engine/controllers/transactions")
70                         .managed(true)
71                         .build());
72
73         var server =
74                 HttpServletServerFactoryInstance
75                         .getServerFactory()
76                         .build("trans", "localhost", 8769, "/", true, true);
77         server.addServletClass("/*", RestTransactionTracker.class.getName());
78         server.waitedStart(5000L);
79         assertTrue(NetworkUtil.isTcpPortOpen("localhost", 8769, 5, 10000L));
80
81         testController = PolicyEngineConstants.getManager().createPolicyController("metrics",
82                 SystemPersistenceConstants.getManager().getControllerProperties("metrics"));
83
84         client = HttpClientFactoryInstance.getClientFactory().get("trans");
85     }
86
87     @AfterAll
88     public static void testAfterClass() {
89         HttpClientFactoryInstance.getClientFactory().destroy();
90         HttpServletServerFactoryInstance.getServerFactory().destroy();
91
92         SystemPersistenceConstants.getManager().setConfigurationDir(null);
93     }
94
95     @Test
96     void testConfiguration() {
97         equals(get("cacheSize", Response.Status.OK.getStatusCode()), Integer.class, 3);
98         equals(get("timeout", Response.Status.OK.getStatusCode()), Integer.class, 2);
99
100         put("cacheSize/10", "", Response.Status.OK.getStatusCode());
101         put("timeout/20", "", Response.Status.OK.getStatusCode());
102
103         equals(get("cacheSize", Response.Status.OK.getStatusCode()), Integer.class, 10);
104         equals(get("timeout", Response.Status.OK.getStatusCode()), Integer.class, 20);
105
106         put("cacheSize/3", "", Response.Status.OK.getStatusCode());
107         put("timeout/2", "", Response.Status.OK.getStatusCode());
108
109         equals(get("cacheSize", Response.Status.OK.getStatusCode()), Integer.class, 3);
110         equals(get("timeout", Response.Status.OK.getStatusCode()), Integer.class, 2);
111     }
112
113     @Test
114     void testTransactions() {
115         equals(get("/inprogress", Response.Status.OK.getStatusCode()), List.class, Collections.emptyList());
116
117         ControlLoopMetricsFeature feature = new ControlLoopMetricsFeature();
118
119         assertTrue(HttpClient.getBody(get("/inprogress", Response.Status.OK.getStatusCode()),
120                 List.class).isEmpty());
121         get("/inprogress/664be3d2-6c12-4f4b-a3e7-c349acced200", Response.Status.NOT_FOUND.getStatusCode());
122
123         var activeNotification = ResourceUtils.getResourceAsString("policy-cl-mgt-active.json");
124         var active =
125                 Serialization.gsonPretty.fromJson(activeNotification, VirtualControlLoopNotification.class);
126         feature.beforeDeliver(testController, Topic.CommInfrastructure.NOOP, "POLICY-CL-MGT", active);
127         assertEquals(1, ControlLoopMetricsManager.getManager().getTransactionIds().size());
128
129         assertFalse(HttpClient.getBody(get("/inprogress", Response.Status.OK.getStatusCode()),
130                 List.class).isEmpty());
131         notNull(get("/inprogress/664be3d2-6c12-4f4b-a3e7-c349acced200", Response.Status.OK.getStatusCode()),
132                 String.class);
133     }
134
135     private Response get(String contextPath, int statusCode) {
136         var response = client.get(contextPath);
137         return checkResponse(statusCode, response);
138     }
139
140     private Response put(String contextPath, String body, int statusCode) {
141         var response = client.put(contextPath, Entity.json(body), Collections.emptyMap());
142         return checkResponse(statusCode, response);
143     }
144
145     private <T, Y> void equals(Response response, Class<T> clazz, Y expected) {
146         assertEquals(expected, HttpClient.getBody(response, clazz));
147     }
148
149     private <T> void notNull(Response response, Class<T> clazz) {
150         assertNotNull(HttpClient.getBody(response, clazz));
151     }
152
153     @NotNull
154     private Response checkResponse(int statusCode, Response response) {
155         assertEquals(statusCode, response.getStatus());
156         return response;
157     }
158 }