eaf8246238911ad36fed7de70989db3e5261e048
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.runtime.monitoring.rest;
22
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;
26
27 import java.io.File;
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.controlloop.models.controlloop.concepts.ClElementStatisticsList;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList;
37 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider;
38 import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController;
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;
47
48 @ExtendWith(SpringExtension.class)
49 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
50 @TestPropertySource(locations = {"classpath:application_test.properties"})
51 class MonitoringQueryControllerTest extends CommonRestController {
52
53     private static final String CL_PARTICIPANT_STATISTICS_JSON =
54             "src/test/resources/rest/monitoring/TestParticipantStatistics.json";
55     private static final String CL_ELEMENT_STATISTICS_JSON =
56             "src/test/resources/rest/monitoring/TestClElementStatistics.json";
57
58     private static final Coder CODER = new StandardCoder();
59
60     private static ParticipantStatisticsList inputParticipantStatistics;
61     private static ClElementStatisticsList inputClElementStatistics;
62
63     private static ParticipantStatisticsList participantStatisticsList;
64     private static ClElementStatisticsList clElementStatisticsList;
65
66     private static final String CLELEMENT_STATS_ENDPOINT = "monitoring/clelement";
67     private static final String PARTICIPANT_STATS_ENDPOINT = "monitoring/participant";
68     private static final String PARTICIPANT_STATS_PER_CL_ENDPOINT = "monitoring/participants/controlloop";
69     private static final String CLELEMENT_STATS_PER_CL_ENDPOINT = "monitoring/clelements/controlloop";
70
71     @Autowired
72     private MonitoringProvider monitoringProvider;
73
74     @LocalServerPort
75     private int randomServerPort;
76
77     /**
78      * starts Main.
79      *
80      * @throws Exception if an error occurs
81      */
82     @BeforeAll
83     public static void setUpBeforeAll() throws Exception {
84
85         inputParticipantStatistics =
86                 CODER.decode(new File(CL_PARTICIPANT_STATISTICS_JSON), ParticipantStatisticsList.class);
87         inputClElementStatistics = CODER.decode(new File(CL_ELEMENT_STATISTICS_JSON), ClElementStatisticsList.class);
88     }
89
90     @BeforeEach
91     public void setUpBeforeEach() throws Exception {
92         super.setHttpPrefix(randomServerPort);
93
94         // Insert Participant statistics to DB
95         participantStatisticsList =
96                 monitoringProvider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
97         // Insert CL Element statistics to DB
98         clElementStatisticsList =
99                 monitoringProvider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
100     }
101
102     @Test
103     void testQuery_Unauthorized_for_ClElementStats() throws Exception {
104         assertUnauthorizedGet(CLELEMENT_STATS_ENDPOINT);
105     }
106
107     @Test
108     void testQuery_Unauthorized_for_ClParticipantStats() throws Exception {
109         assertUnauthorizedGet(PARTICIPANT_STATS_ENDPOINT);
110     }
111
112     @Test
113     void testQuery_Unauthorized_for_ParticipantStatsPerCl() throws Exception {
114         assertUnauthorizedGet(PARTICIPANT_STATS_PER_CL_ENDPOINT);
115     }
116
117     @Test
118     void testQuery_Unauthorized_for_ClElementStatsPerCl() throws Exception {
119         assertUnauthorizedGet(CLELEMENT_STATS_PER_CL_ENDPOINT);
120     }
121
122     @Test
123     void testSwagger_ClStats() throws Exception {
124         super.testSwagger(CLELEMENT_STATS_ENDPOINT);
125         super.testSwagger(PARTICIPANT_STATS_ENDPOINT);
126         super.testSwagger(CLELEMENT_STATS_PER_CL_ENDPOINT);
127         super.testSwagger(PARTICIPANT_STATS_PER_CL_ENDPOINT);
128     }
129
130     @Test
131     void testClElementStatisticsEndpoint() throws Exception {
132         // Filter statistics only based on participant Id and UUID
133         Invocation.Builder invokeRequest1 = super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?name="
134                 + clElementStatisticsList.getClElementStatistics().get(0).getParticipantId().getName() + "&version="
135                 + clElementStatisticsList.getClElementStatistics().get(0).getParticipantId().getVersion() + "&id="
136                 + clElementStatisticsList.getClElementStatistics().get(0).getId().toString());
137         Response response1 = invokeRequest1.buildGet().invoke();
138         assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
139
140         ClElementStatisticsList result1 = response1.readEntity(ClElementStatisticsList.class);
141
142         assertNotNull(result1);
143         assertThat(result1.getClElementStatistics()).hasSize(2);
144
145         var clElementStat0 = clElementStatisticsList.getClElementStatistics().get(0);
146         for (var clElement : result1.getClElementStatistics()) {
147             assertEquals(clElement.getParticipantId().asConceptKey(), clElementStat0.getParticipantId().asConceptKey());
148             assertEquals(clElement.getId(), clElementStat0.getId());
149         }
150
151         // Filter statistics based on timestamp
152         Invocation.Builder invokeRequest2 = super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?name="
153                 + clElementStatisticsList.getClElementStatistics().get(1).getParticipantId().getName() + "&version="
154                 + clElementStatisticsList.getClElementStatistics().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         ClElementStatisticsList result2 = response2.readEntity(ClElementStatisticsList.class);
160
161         assertNotNull(result2);
162         assertThat(result2.getClElementStatistics()).hasSize(1);
163         assertEquals(result2.getClElementStatistics().get(0), clElementStat0);
164     }
165
166     @Test
167     void testClElementStats_BadRequest() throws Exception {
168         Invocation.Builder invokeRequest1 = super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?version=1.0.0");
169         Response response1 = invokeRequest1.buildGet().invoke();
170         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
171     }
172
173     @Test
174     void testParticipantStatisticsEndpoint() throws Exception {
175
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);
183
184         assertNotNull(result1);
185         assertThat(result1.getStatisticsList()).hasSize(2);
186         assertThat(result1.getStatisticsList()).contains(participantStatisticsList.getStatisticsList().get(0));
187
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);
196
197         assertNotNull(result2);
198         assertThat(result2.getStatisticsList()).hasSize(1);
199         assertEquals(result2.getStatisticsList().get(0), participantStatisticsList.getStatisticsList().get(0));
200     }
201
202     @Test
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());
207     }
208
209     @Test
210     void testParticipantStatsPerClEndpoint() throws Exception {
211         Invocation.Builder invokeRequest1 =
212                 super.sendRequest(PARTICIPANT_STATS_PER_CL_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();
217     }
218
219     @Test
220     void testParticipantStatsPerCl_BadRequest() throws Exception {
221         Invocation.Builder invokeRequest1 = super.sendRequest(PARTICIPANT_STATS_PER_CL_ENDPOINT);
222         Response response1 = invokeRequest1.buildGet().invoke();
223         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
224     }
225
226     @Test
227     void testClElementStatisticsPerClEndpoint() throws Exception {
228         Invocation.Builder invokeRequest1 =
229                 super.sendRequest(CLELEMENT_STATS_PER_CL_ENDPOINT + "?name=dummyName&version=1.001");
230         Response response1 = invokeRequest1.buildGet().invoke();
231         assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
232         ClElementStatisticsList result1 = response1.readEntity(ClElementStatisticsList.class);
233         assertThat(result1.getClElementStatistics()).isEmpty();
234     }
235
236     @Test
237     void testClElementStatsPerCl_BadRequest() throws Exception {
238         Invocation.Builder invokeRequest1 = super.sendRequest(CLELEMENT_STATS_PER_CL_ENDPOINT);
239         Response response1 = invokeRequest1.buildGet().invoke();
240         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
241     }
242 }