2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.controlloop.runtime.monitoring;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.anyMap;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
34 import java.time.Instant;
35 import java.util.LinkedHashMap;
36 import java.util.List;
38 import java.util.UUID;
39 import javax.ws.rs.core.Response;
40 import org.junit.jupiter.api.AfterEach;
41 import org.junit.jupiter.api.BeforeAll;
42 import org.junit.jupiter.api.Test;
43 import org.mockito.Mockito;
44 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
45 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
46 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
47 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList;
48 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ClElementStatisticsProvider;
49 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
50 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantStatisticsProvider;
51 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
52 import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData;
53 import org.onap.policy.common.utils.coder.Coder;
54 import org.onap.policy.common.utils.coder.CoderException;
55 import org.onap.policy.common.utils.coder.StandardCoder;
56 import org.onap.policy.models.base.PfModelRuntimeException;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
59 class TestMonitoringProvider {
61 private static final String CL_PARTICIPANT_STATISTICS_JSON =
62 "src/test/resources/rest/monitoring/TestParticipantStatistics.json";
63 private static final String INVALID_PARTICIPANT_JSON_INPUT =
64 "src/test/resources/rest/monitoring/TestParticipantStatistics_Invalid.json";
65 private static final String CL_ELEMENT_STATISTICS_JSON =
66 "src/test/resources/rest/monitoring/TestClElementStatistics.json";
67 private static final String INVALID_CL_ELEMENT_JSON_INPUT =
68 "src/test/resources/rest/monitoring/TestClElementStatistics_Invalid.json";
69 private static final Coder CODER = new StandardCoder();
71 private static final String STAT_LIST_IS_NULL = ".*StatisticsList is marked .*ull but is null";
72 private static final String PARTICIPANT_STAT_LIST_IS_NULL =
73 "participantStatisticsList is marked .*null but is null";
74 private static final String NAME_IS_NULL = "name is marked .*null but is null";
75 private static final String CL_LIST_IS_NULL = "clElementStatisticsList is marked .*null but is null";
76 private static final String ID_VERSION1 = "1.001";
77 private static final String ID_VERSION2 = "1.002";
78 private static final String ID_NAME1 = "name1";
79 private static final String ID_NAME2 = "name2";
80 private static final String SORT_DESC = "DESC";
81 private static final String ID_NAME3 = "testCLName";
82 private static final String ID_INVALID_NAME = "invalidCLName";
83 private static ParticipantStatisticsList inputParticipantStatistics;
84 private static ParticipantStatisticsList invalidParticipantInput;
85 private static ClElementStatisticsList inputClElementStatistics;
86 private static ClElementStatisticsList invalidClElementInput;
88 private ControlLoopProvider clProvider = null;
91 public static void beforeSetupStatistics() throws CoderException {
92 // Reading input json for statistics data
93 inputParticipantStatistics =
94 CODER.decode(new File(CL_PARTICIPANT_STATISTICS_JSON), ParticipantStatisticsList.class);
95 invalidParticipantInput =
96 CODER.decode(new File(INVALID_PARTICIPANT_JSON_INPUT), ParticipantStatisticsList.class);
97 inputClElementStatistics = CODER.decode(new File(CL_ELEMENT_STATISTICS_JSON), ClElementStatisticsList.class);
98 invalidClElementInput = CODER.decode(new File(INVALID_CL_ELEMENT_JSON_INPUT), ClElementStatisticsList.class);
102 void close() throws Exception {
103 if (clProvider != null) {
109 void testCreateParticipantStatistics() throws Exception {
110 var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class);
111 var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class);
112 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("createparStat");
113 clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters());
114 MonitoringProvider provider =
115 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
117 when(participantStatisticsProvider.createParticipantStatistics(any()))
118 .thenReturn(inputParticipantStatistics.getStatisticsList());
120 when(participantStatisticsProvider.createParticipantStatistics(eq(null)))
121 .thenThrow(new PfModelRuntimeException(Response.Status.BAD_REQUEST, PARTICIPANT_STAT_LIST_IS_NULL));
123 // Creating statistics data in db with null input
125 assertThatThrownBy(() -> {
126 provider.createParticipantStatistics(null);
127 }).hasMessageMatching(STAT_LIST_IS_NULL);
129 assertThatThrownBy(() -> {
130 provider.createParticipantStatistics(invalidParticipantInput.getStatisticsList());
131 }).hasMessageMatching(PARTICIPANT_STAT_LIST_IS_NULL);
133 // Creating statistics data from input json
134 ParticipantStatisticsList createResponse =
135 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
137 assertThat(createResponse.getStatisticsList()).hasSize(3);
138 assertEquals(createResponse.getStatisticsList().toString().replaceAll("\\s+", ""),
139 inputParticipantStatistics.getStatisticsList().toString().replaceAll("\\s+", ""));
143 void testGetParticipantStatistics() throws Exception {
144 var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class);
145 when(participantStatisticsProvider.getFilteredParticipantStatistics(eq(ID_NAME1), any(), any(), any(), eq(null),
146 eq(SORT_DESC), eq(0))).thenReturn(List.of(inputParticipantStatistics.getStatisticsList().get(0)));
148 when(participantStatisticsProvider.getFilteredParticipantStatistics(eq(ID_NAME1), any(),
149 eq(Instant.parse("2021-01-11T12:00:00.000Z")), eq(Instant.parse("2021-01-11T16:00:00.000Z")), eq(null),
150 eq(SORT_DESC), eq(0))).thenReturn(List.of());
152 when(participantStatisticsProvider.getFilteredParticipantStatistics(eq(ID_NAME2), any(), any(), any(), eq(null),
153 eq(SORT_DESC), eq(1))).thenReturn(List.of(inputParticipantStatistics.getStatisticsList().get(2)));
155 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("getparStat");
156 clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters());
157 var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class);
158 MonitoringProvider provider =
159 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
160 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
162 assertThatThrownBy(() -> {
163 provider.fetchFilteredParticipantStatistics(null, null, 0, null, null);
164 }).hasMessageMatching(NAME_IS_NULL);
166 // Fetch specific statistics record with name, version and record count
167 ParticipantStatisticsList getResponse =
168 provider.fetchFilteredParticipantStatistics(ID_NAME2, ID_VERSION1, 1, null, null);
169 assertThat(getResponse.getStatisticsList()).hasSize(1);
170 assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""),
171 inputParticipantStatistics.getStatisticsList().get(2).toString().replaceAll("\\s+", ""));
173 // Fetch statistics using timestamp
174 getResponse = provider.fetchFilteredParticipantStatistics(ID_NAME1, ID_VERSION1, 0, null,
175 Instant.parse("2021-01-10T15:00:00.000Z"));
176 assertThat(getResponse.getStatisticsList()).hasSize(1);
178 getResponse = provider.fetchFilteredParticipantStatistics(ID_NAME1, ID_VERSION1, 0,
179 Instant.parse("2021-01-11T12:00:00.000Z"), Instant.parse("2021-01-11T16:00:00.000Z"));
181 assertThat(getResponse.getStatisticsList()).isEmpty();
185 void testCreateClElementStatistics() throws Exception {
186 var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class);
187 when(clElementStatisticsProvider.createClElementStatistics(any()))
188 .thenReturn(inputClElementStatistics.getClElementStatistics());
190 when(clElementStatisticsProvider.createClElementStatistics(eq(null)))
191 .thenThrow(new PfModelRuntimeException(Response.Status.BAD_REQUEST, CL_LIST_IS_NULL));
193 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("createelemstat");
194 clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters());
196 var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class);
197 MonitoringProvider provider =
198 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
199 // Creating statistics data in db with null input
200 assertThatThrownBy(() -> {
201 provider.createClElementStatistics(null);
202 }).hasMessageMatching(STAT_LIST_IS_NULL);
204 assertThatThrownBy(() -> {
205 provider.createClElementStatistics(invalidClElementInput.getClElementStatistics());
206 }).hasMessageMatching(CL_LIST_IS_NULL);
208 // Creating clElement statistics data from input json
209 ClElementStatisticsList createResponse =
210 provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
212 assertThat(createResponse.getClElementStatistics()).hasSize(4);
213 assertEquals(createResponse.getClElementStatistics().toString().replaceAll("\\s+", ""),
214 inputClElementStatistics.getClElementStatistics().toString().replaceAll("\\s+", ""));
218 void testGetClElementStatistics() throws Exception {
219 var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class);
220 var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class);
221 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("getelemstat");
222 clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters());
224 when(clElementStatisticsProvider.getFilteredClElementStatistics(eq(ID_NAME1), any(), any(), any(), anyMap(),
225 eq(SORT_DESC), eq(0)))
226 .thenReturn(List.of(inputClElementStatistics.getClElementStatistics().get(0),
227 inputClElementStatistics.getClElementStatistics().get(1)));
229 MonitoringProvider provider =
230 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
231 assertThatThrownBy(() -> {
232 provider.fetchFilteredClElementStatistics(null, null, null, null, null, 0);
233 }).hasMessageMatching(NAME_IS_NULL);
235 provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
237 ClElementStatisticsList getResponse =
238 provider.fetchFilteredClElementStatistics(ID_NAME1, null, null, null, null, 0);
240 assertThat(getResponse.getClElementStatistics()).hasSize(2);
241 assertEquals(getResponse.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""),
242 inputClElementStatistics.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""));
244 // Fetch specific statistics record with name, id and record count
245 getResponse = provider.fetchFilteredClElementStatistics(ID_NAME1, ID_VERSION1,
246 "709c62b3-8918-41b9-a747-d21eb79c6c20", null, null, 0);
247 assertThat(getResponse.getClElementStatistics()).hasSize(2);
249 // Fetch statistics using timestamp
250 getResponse = provider.fetchFilteredClElementStatistics(ID_NAME1, ID_VERSION1, null,
251 Instant.parse("2021-01-10T13:45:00.000Z"), null, 0);
252 assertThat(getResponse.getClElementStatistics()).hasSize(2);
256 void testGetParticipantStatsPerCL() throws Exception {
257 var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class);
258 var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class);
259 var mockClProvider = Mockito.mock(ControlLoopProvider.class);
261 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, mockClProvider);
263 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
265 var controlLoop = new ControlLoop();
266 var element = new ControlLoopElement();
267 element.setParticipantId(new ToscaConceptIdentifier(ID_NAME1, ID_VERSION1));
268 controlLoop.setElements(Map.of(UUID.randomUUID(), element));
269 when(mockClProvider.getControlLoop(new ToscaConceptIdentifier(ID_NAME2, ID_VERSION1))).thenReturn(controlLoop);
271 when(participantStatisticsProvider.getFilteredParticipantStatistics(eq(ID_NAME1), eq(ID_VERSION1), any(), any(),
272 eq(null), eq(SORT_DESC), eq(0)))
273 .thenReturn(List.of(inputParticipantStatistics.getStatisticsList().get(0),
274 inputParticipantStatistics.getStatisticsList().get(1)));
276 ParticipantStatisticsList getResponse = provider.fetchParticipantStatsPerControlLoop(ID_NAME2, ID_VERSION1);
277 assertThat(getResponse.getStatisticsList()).hasSize(2);
278 assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""),
279 inputParticipantStatistics.getStatisticsList().get(0).toString().replaceAll("\\s+", ""));
280 assertThat(provider.fetchParticipantStatsPerControlLoop(ID_INVALID_NAME, ID_VERSION2).getStatisticsList())
285 void testClElementStatsPerCL() throws Exception {
286 // Setup a dummy Control loop data
287 var mockClElement = new ControlLoopElement();
288 mockClElement.setId(inputClElementStatistics.getClElementStatistics().get(0).getId());
289 mockClElement.setParticipantId(new ToscaConceptIdentifier(
290 inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getName(),
291 inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getVersion()));
292 var mockCL = new ControlLoop();
293 mockCL.setElements(new LinkedHashMap<>());
294 mockCL.getElements().put(mockClElement.getId(), mockClElement);
296 var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class);
297 var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class);
298 var mockClProvider = Mockito.mock(ControlLoopProvider.class);
299 var monitoringProvider =
300 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, mockClProvider);
302 // Mock controlloop data to be returned for the given CL Id
303 when(mockClProvider.getControlLoop(new ToscaConceptIdentifier(ID_NAME3, ID_VERSION1))).thenReturn(mockCL);
305 when(clElementStatisticsProvider.getFilteredClElementStatistics(eq(ID_NAME1), eq(ID_VERSION1), any(), any(),
306 anyMap(), eq(SORT_DESC), eq(0)))
307 .thenReturn(List.of(inputClElementStatistics.getClElementStatistics().get(0),
308 inputClElementStatistics.getClElementStatistics().get(1)));
310 monitoringProvider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
312 ClElementStatisticsList getResponse =
313 monitoringProvider.fetchClElementStatsPerControlLoop(ID_NAME3, ID_VERSION1);
315 assertThat(getResponse.getClElementStatistics()).hasSize(2);
316 assertEquals(getResponse.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""),
317 inputClElementStatistics.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""));
319 assertThat(monitoringProvider.fetchClElementStatsPerControlLoop(ID_INVALID_NAME, ID_VERSION2)
320 .getClElementStatistics()).isEmpty();
322 Map<String, ToscaConceptIdentifier> clElementIds =
323 monitoringProvider.getAllClElementsIdPerControlLoop(ID_NAME3, ID_VERSION1);
324 assertThat(clElementIds)
325 .containsKey(inputClElementStatistics.getClElementStatistics().get(0).getId().toString());