2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.runtime.monitoring.rest;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import java.time.Instant;
29 import javax.ws.rs.client.Invocation;
30 import javax.ws.rs.core.Response;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.onap.policy.clamp.acm.runtime.monitoring.MonitoringProvider;
36 import org.onap.policy.clamp.acm.runtime.util.rest.CommonRestController;
37 import org.onap.policy.clamp.models.acm.concepts.AcElementStatisticsList;
38 import org.onap.policy.clamp.models.acm.concepts.ParticipantStatisticsList;
39 import org.onap.policy.common.utils.coder.Coder;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
44 import org.springframework.boot.web.server.LocalServerPort;
45 import org.springframework.test.context.TestPropertySource;
46 import org.springframework.test.context.junit.jupiter.SpringExtension;
48 @ExtendWith(SpringExtension.class)
49 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
50 @TestPropertySource(locations = {"classpath:application_test.properties"})
51 class MonitoringQueryControllerTest extends CommonRestController {
53 private static final String AC_PARTICIPANT_STATISTICS_JSON =
54 "src/test/resources/rest/monitoring/TestParticipantStatistics.json";
55 private static final String AC_ELEMENT_STATISTICS_JSON =
56 "src/test/resources/rest/monitoring/TestAcElementStatistics.json";
58 private static final Coder CODER = new StandardCoder();
60 private static ParticipantStatisticsList inputParticipantStatistics;
61 private static AcElementStatisticsList inputAcElementStatistics;
63 private static ParticipantStatisticsList participantStatisticsList;
64 private static AcElementStatisticsList acElementStatisticsList;
66 private static final String AC_ELEMENT_STATS_ENDPOINT = "monitoring/acelement";
67 private static final String PARTICIPANT_STATS_ENDPOINT = "monitoring/participant";
68 private static final String PARTICIPANT_STATS_PER_AC_ENDPOINT = "monitoring/participants/automationcomposition";
69 private static final String AC_ELEMENT_STATS_PER_AC_ENDPOINT = "monitoring/acelements/automationcomposition";
72 private MonitoringProvider monitoringProvider;
75 private int randomServerPort;
80 * @throws Exception if an error occurs
83 public static void setUpBeforeAll() throws Exception {
85 inputParticipantStatistics =
86 CODER.decode(new File(AC_PARTICIPANT_STATISTICS_JSON), ParticipantStatisticsList.class);
87 inputAcElementStatistics = CODER.decode(new File(AC_ELEMENT_STATISTICS_JSON), AcElementStatisticsList.class);
91 public void setUpBeforeEach() throws Exception {
92 super.setHttpPrefix(randomServerPort);
94 // Insert Participant statistics to DB
95 participantStatisticsList =
96 monitoringProvider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
97 // Insert AC Element statistics to DB
98 acElementStatisticsList =
99 monitoringProvider.createAcElementStatistics(inputAcElementStatistics.getAcElementStatistics());
103 void testQuery_Unauthorized_for_AcElementStats() throws Exception {
104 assertUnauthorizedGet(AC_ELEMENT_STATS_ENDPOINT);
108 void testQuery_Unauthorized_for_AcParticipantStats() throws Exception {
109 assertUnauthorizedGet(PARTICIPANT_STATS_ENDPOINT);
113 void testQuery_Unauthorized_for_ParticipantStatsPerAc() throws Exception {
114 assertUnauthorizedGet(PARTICIPANT_STATS_PER_AC_ENDPOINT);
118 void testQuery_Unauthorized_for_AcElementStatsPerAc() throws Exception {
119 assertUnauthorizedGet(AC_ELEMENT_STATS_PER_AC_ENDPOINT);
123 void testSwagger_AcStats() throws Exception {
124 super.testSwagger(AC_ELEMENT_STATS_ENDPOINT);
125 super.testSwagger(PARTICIPANT_STATS_ENDPOINT);
126 super.testSwagger(AC_ELEMENT_STATS_PER_AC_ENDPOINT);
127 super.testSwagger(PARTICIPANT_STATS_PER_AC_ENDPOINT);
131 void testAcElementStatisticsEndpoint() throws Exception {
132 // Filter statistics only based on participant Id and UUID
133 Invocation.Builder invokeRequest1 = super.sendRequest(AC_ELEMENT_STATS_ENDPOINT + "?name="
134 + acElementStatisticsList.getAcElementStatistics().get(0).getParticipantId().getName() + "&version="
135 + acElementStatisticsList.getAcElementStatistics().get(0).getParticipantId().getVersion() + "&id="
136 + acElementStatisticsList.getAcElementStatistics().get(0).getId().toString());
137 Response response1 = invokeRequest1.buildGet().invoke();
138 assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
140 AcElementStatisticsList result1 = response1.readEntity(AcElementStatisticsList.class);
142 assertNotNull(result1);
143 assertThat(result1.getAcElementStatistics()).hasSize(2);
145 var acElementStat0 = acElementStatisticsList.getAcElementStatistics().get(0);
146 for (var acElement : result1.getAcElementStatistics()) {
147 assertEquals(acElement.getParticipantId().asConceptKey(), acElementStat0.getParticipantId().asConceptKey());
148 assertEquals(acElement.getId(), acElementStat0.getId());
151 // Filter statistics based on timestamp
152 Invocation.Builder invokeRequest2 = super.sendRequest(AC_ELEMENT_STATS_ENDPOINT + "?name="
153 + acElementStatisticsList.getAcElementStatistics().get(1).getParticipantId().getName() + "&version="
154 + acElementStatisticsList.getAcElementStatistics().get(1).getParticipantId().getVersion()
155 + "&startTime=" + Instant.parse("2021-01-10T13:00:00.000Z") + "&endTime="
156 + Instant.parse("2021-01-10T14:00:00.000Z"));
157 Response response2 = invokeRequest2.buildGet().invoke();
158 assertEquals(Response.Status.OK.getStatusCode(), response2.getStatus());
159 AcElementStatisticsList result2 = response2.readEntity(AcElementStatisticsList.class);
161 assertNotNull(result2);
162 assertThat(result2.getAcElementStatistics()).hasSize(1);
163 assertEquals(result2.getAcElementStatistics().get(0), acElementStat0);
167 void testAcElementStats_BadRequest() throws Exception {
168 Invocation.Builder invokeRequest1 = super.sendRequest(AC_ELEMENT_STATS_ENDPOINT + "?version=1.0.0");
169 Response response1 = invokeRequest1.buildGet().invoke();
170 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
174 void testParticipantStatisticsEndpoint() throws Exception {
176 // Filter statistics only based on participant Id
177 Invocation.Builder invokeRequest1 = super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?name="
178 + participantStatisticsList.getStatisticsList().get(0).getParticipantId().getName() + "&version="
179 + participantStatisticsList.getStatisticsList().get(0).getParticipantId().getVersion());
180 Response response1 = invokeRequest1.buildGet().invoke();
181 assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
182 ParticipantStatisticsList result1 = response1.readEntity(ParticipantStatisticsList.class);
184 assertNotNull(result1);
185 assertThat(result1.getStatisticsList()).hasSize(2);
186 assertThat(result1.getStatisticsList()).contains(participantStatisticsList.getStatisticsList().get(0));
188 // Filter statistics based on timestamp
189 Invocation.Builder invokeRequest2 = super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?name="
190 + participantStatisticsList.getStatisticsList().get(1).getParticipantId().getName() + "&version="
191 + participantStatisticsList.getStatisticsList().get(1).getParticipantId().getVersion() + "&startTime="
192 + Instant.parse("2021-01-10T13:00:00.000Z") + "&endTime=" + Instant.parse("2021-01-10T14:00:00.000Z"));
193 Response response2 = invokeRequest2.buildGet().invoke();
194 assertEquals(Response.Status.OK.getStatusCode(), response2.getStatus());
195 ParticipantStatisticsList result2 = response2.readEntity(ParticipantStatisticsList.class);
197 assertNotNull(result2);
198 assertThat(result2.getStatisticsList()).hasSize(1);
199 assertEquals(result2.getStatisticsList().get(0), participantStatisticsList.getStatisticsList().get(0));
203 void testParticipantStats_BadRequest() throws Exception {
204 Invocation.Builder invokeRequest1 = super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?version=0.0");
205 Response response1 = invokeRequest1.buildGet().invoke();
206 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
210 void testParticipantStatsPerAcEndpoint() throws Exception {
211 Invocation.Builder invokeRequest1 =
212 super.sendRequest(PARTICIPANT_STATS_PER_AC_ENDPOINT + "?name=dummyName&version=1.001");
213 Response response1 = invokeRequest1.buildGet().invoke();
214 assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
215 ParticipantStatisticsList result1 = response1.readEntity(ParticipantStatisticsList.class);
216 assertThat(result1.getStatisticsList()).isEmpty();
220 void testParticipantStatsPerAc_BadRequest() throws Exception {
221 Invocation.Builder invokeRequest1 = super.sendRequest(PARTICIPANT_STATS_PER_AC_ENDPOINT);
222 Response response1 = invokeRequest1.buildGet().invoke();
223 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
227 void testAcElementStatisticsPerAcEndpoint() throws Exception {
228 Invocation.Builder invokeRequest1 =
229 super.sendRequest(AC_ELEMENT_STATS_PER_AC_ENDPOINT + "?name=dummyName&version=1.001");
230 Response response1 = invokeRequest1.buildGet().invoke();
231 assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
232 AcElementStatisticsList result1 = response1.readEntity(AcElementStatisticsList.class);
233 assertThat(result1.getAcElementStatistics()).isEmpty();
237 void testAcElementStatsPerAc_BadRequest() throws Exception {
238 Invocation.Builder invokeRequest1 = super.sendRequest(AC_ELEMENT_STATS_PER_AC_ENDPOINT);
239 Response response1 = invokeRequest1.buildGet().invoke();
240 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());