118199a2d2c002eac972941f6a78179c8da9ddf3
[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.Assert.assertEquals;
25 import static org.junit.Assert.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.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Ignore;
34 import org.junit.Test;
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
42 public class MonitoringQueryControllerTest extends CommonRestController {
43
44     private static final String CL_PARTICIPANT_STATISTICS_JSON =
45         "src/test/resources/rest/monitoring/TestParticipantStatistics.json";
46     private static final String CL_ELEMENT_STATISTICS_JSON =
47         "src/test/resources/rest/monitoring/TestClElementStatistics.json";
48
49     private static final Coder CODER = new StandardCoder();
50
51     private static ParticipantStatisticsList inputParticipantStatistics;
52     private static ClElementStatisticsList inputClElementStatistics;
53
54     private static ParticipantStatisticsList participantStatisticsList;
55     private static  ClElementStatisticsList clElementStatisticsList;
56
57     private static final String CLELEMENT_STATS_ENDPOINT = "monitoring/clelement";
58     private static final String PARTICIPANT_STATS_ENDPOINT = "monitoring/participant";
59     private static final String PARTICIPANT_STATS_PER_CL_ENDPOINT = "monitoring/participants/controlloop";
60     private static final String CLELEMENT_STATS_PER_CL_ENDPOINT = "monitoring/clelements/controlloop";
61
62
63     /**
64      * starts Main.
65      *
66      * @throws Exception if an error occurs
67      */
68     @BeforeClass
69     public static void setUpBeforeClass() throws Exception {
70         CommonRestController.setUpBeforeClass("testStatisticsQuery");
71         inputParticipantStatistics = CODER.decode(new File(CL_PARTICIPANT_STATISTICS_JSON),
72             ParticipantStatisticsList.class);
73         inputClElementStatistics = CODER.decode(new File(CL_ELEMENT_STATISTICS_JSON),
74             ClElementStatisticsList.class);
75
76         try (MonitoringProvider monitoringProvider = new MonitoringProvider(getParameters())) {
77             // Insert Participant statistics to DB
78             participantStatisticsList = monitoringProvider.createParticipantStatistics(inputParticipantStatistics
79                 .getStatisticsList());
80             // Insert CL Element statistics to DB
81             clElementStatisticsList = monitoringProvider.createClElementStatistics(inputClElementStatistics
82                 .getClElementStatistics());
83         }
84     }
85
86     @AfterClass
87     public static void teardownAfterClass() {
88         CommonRestController.teardownAfterClass();
89     }
90
91     @Test
92     public void testQuery_Unauthorized_for_ClElementStats() throws Exception {
93         assertUnauthorizedGet(CLELEMENT_STATS_ENDPOINT);
94     }
95
96     @Test
97     public void testQuery_Unauthorized_for_ClParticipantStats() throws Exception {
98         assertUnauthorizedGet(PARTICIPANT_STATS_ENDPOINT);
99     }
100
101     @Test
102     public void testQuery_Unauthorized_for_ParticipantStatsPerCl() throws Exception {
103         assertUnauthorizedGet(PARTICIPANT_STATS_PER_CL_ENDPOINT);
104     }
105
106     @Test
107     public void testQuery_Unauthorized_for_ClElementStatsPerCl() throws Exception {
108         assertUnauthorizedGet(CLELEMENT_STATS_PER_CL_ENDPOINT);
109     }
110
111     @Test
112     public void testSwagger_ClStats() throws Exception {
113         super.testSwagger(CLELEMENT_STATS_ENDPOINT);
114         super.testSwagger(PARTICIPANT_STATS_ENDPOINT);
115         super.testSwagger(CLELEMENT_STATS_PER_CL_ENDPOINT);
116         super.testSwagger(PARTICIPANT_STATS_PER_CL_ENDPOINT);
117     }
118
119     @Test
120     public void testClElementStatisticsEndpoint() throws Exception {
121         // Filter statistics only based on participant Id and UUID
122         Invocation.Builder invokeRequest1 =
123             super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?name=" + clElementStatisticsList
124                 .getClElementStatistics().get(0).getParticipantId().getName() + "&version=" + clElementStatisticsList
125                 .getClElementStatistics().get(0).getParticipantId().getVersion() + "&id=" + clElementStatisticsList
126                 .getClElementStatistics().get(0).getId().toString());
127         Response response1 = invokeRequest1.buildGet().invoke();
128         assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
129
130         ClElementStatisticsList result1 = response1.readEntity(ClElementStatisticsList.class);
131
132         assertNotNull(result1);
133         assertThat(result1.getClElementStatistics()).hasSize(2);
134         assertEquals(result1.getClElementStatistics().get(0), clElementStatisticsList
135             .getClElementStatistics().get(0));
136
137         // Filter statistics based on timestamp
138         Invocation.Builder invokeRequest2 =
139             super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?name=" + clElementStatisticsList
140                 .getClElementStatistics().get(1).getParticipantId().getName() + "&version=" + clElementStatisticsList
141                 .getClElementStatistics().get(1).getParticipantId().getVersion() + "&startTime="
142                 + Instant.parse("2021-01-10T13:00:00.000Z") + "&endTime=" + Instant.parse("2021-01-10T14:00:00.000Z"));
143         Response response2 = invokeRequest2.buildGet().invoke();
144         assertEquals(Response.Status.OK.getStatusCode(), response2.getStatus());
145         ClElementStatisticsList result2 = response2.readEntity(ClElementStatisticsList.class);
146
147         assertNotNull(result2);
148         assertThat(result2.getClElementStatistics()).hasSize(1);
149         assertEquals(result1.getClElementStatistics().get(0), clElementStatisticsList
150             .getClElementStatistics().get(0));
151     }
152
153     @Test
154     public void testClElementStats_BadRequest() throws Exception {
155         Invocation.Builder invokeRequest1 =
156             super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?version=1.0.0");
157         Response response1 = invokeRequest1.buildGet().invoke();
158         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
159     }
160
161     @Test
162     public void testParticipantStatisticsEndpoint() throws Exception {
163
164         // Filter statistics only based on participant Id
165         Invocation.Builder invokeRequest1 =
166             super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?name=" + participantStatisticsList
167                 .getStatisticsList().get(0).getParticipantId().getName() + "&version=" + participantStatisticsList
168                 .getStatisticsList().get(0).getParticipantId().getVersion());
169         Response response1 = invokeRequest1.buildGet().invoke();
170         assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
171         ParticipantStatisticsList result1 = response1.readEntity(ParticipantStatisticsList.class);
172
173         assertNotNull(result1);
174         assertThat(result1.getStatisticsList()).hasSize(2);
175         assertEquals(result1.getStatisticsList().get(0), participantStatisticsList
176             .getStatisticsList().get(0));
177
178         // Filter statistics based on timestamp
179         Invocation.Builder invokeRequest2 =
180             super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?name=" + participantStatisticsList
181                 .getStatisticsList().get(1).getParticipantId().getName() + "&version=" + participantStatisticsList
182                 .getStatisticsList().get(1).getParticipantId().getVersion() + "&startTime="
183                 + Instant.parse("2021-01-10T13:00:00.000Z") + "&endTime=" + Instant.parse("2021-01-10T14:00:00.000Z"));
184         Response response2 = invokeRequest2.buildGet().invoke();
185         assertEquals(Response.Status.OK.getStatusCode(), response2.getStatus());
186         ParticipantStatisticsList result2 = response2.readEntity(ParticipantStatisticsList.class);
187
188         assertNotNull(result2);
189         assertThat(result2.getStatisticsList()).hasSize(1);
190         assertEquals(result1.getStatisticsList().get(0), participantStatisticsList
191             .getStatisticsList().get(0));
192     }
193
194     @Test
195     public void testParticipantStats_BadRequest() throws Exception {
196         Invocation.Builder invokeRequest1 =
197             super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?version=0.0");
198         Response response1 = invokeRequest1.buildGet().invoke();
199         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
200     }
201
202     @Test
203     public void testParticipantStatsPerClEndpoint() throws Exception {
204         Invocation.Builder invokeRequest1 =
205             super.sendRequest(PARTICIPANT_STATS_PER_CL_ENDPOINT + "?name=dummyName&version=1.001");
206         Response response1 = invokeRequest1.buildGet().invoke();
207         assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
208         ParticipantStatisticsList result1 = response1.readEntity(ParticipantStatisticsList.class);
209         assertThat(result1.getStatisticsList()).isEmpty();
210     }
211
212     @Test
213     public void testParticipantStatsPerCl_BadRequest() throws Exception {
214         Invocation.Builder invokeRequest1 =
215             super.sendRequest(PARTICIPANT_STATS_PER_CL_ENDPOINT);
216         Response response1 = invokeRequest1.buildGet().invoke();
217         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
218     }
219
220     @Test
221     public void testClElementStatisticsPerClEndpoint() throws Exception {
222         Invocation.Builder invokeRequest1 =
223             super.sendRequest(CLELEMENT_STATS_PER_CL_ENDPOINT + "?name=dummyName&version=1.001");
224         Response response1 = invokeRequest1.buildGet().invoke();
225         assertEquals(Response.Status.OK.getStatusCode(), response1.getStatus());
226         ClElementStatisticsList result1 = response1.readEntity(ClElementStatisticsList.class);
227         assertThat(result1.getClElementStatistics()).isEmpty();
228     }
229
230     @Test
231     public void testClElementStatsPerCl_BadRequest() throws Exception {
232         Invocation.Builder invokeRequest1 =
233             super.sendRequest(CLELEMENT_STATS_PER_CL_ENDPOINT);
234         Response response1 = invokeRequest1.buildGet().invoke();
235         assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response1.getStatus());
236     }
237 }