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.controlloop.runtime.monitoring;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.when;
30 import java.time.Instant;
31 import java.util.LinkedHashMap;
33 import java.util.UUID;
34 import org.junit.jupiter.api.AfterEach;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.Test;
37 import org.mockito.Mockito;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
41 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList;
42 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ClElementStatisticsProvider;
43 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
44 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantStatisticsProvider;
45 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
46 import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData;
47 import org.onap.policy.common.utils.coder.Coder;
48 import org.onap.policy.common.utils.coder.CoderException;
49 import org.onap.policy.common.utils.coder.StandardCoder;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
52 class TestMonitoringProvider {
54 private static final String CL_PARTICIPANT_STATISTICS_JSON =
55 "src/test/resources/rest/monitoring/TestParticipantStatistics.json";
56 private static final String INVALID_PARTICIPANT_JSON_INPUT =
57 "src/test/resources/rest/monitoring/TestParticipantStatistics_Invalid.json";
58 private static final String CL_ELEMENT_STATISTICS_JSON =
59 "src/test/resources/rest/monitoring/TestClElementStatistics.json";
60 private static final String INVALID_CL_ELEMENT_JSON_INPUT =
61 "src/test/resources/rest/monitoring/TestClElementStatistics_Invalid.json";
62 private static final Coder CODER = new StandardCoder();
64 private static final String LIST_IS_NULL = ".*StatisticsList is marked .*ull but is null";
65 private static ParticipantStatisticsList inputParticipantStatistics;
66 private static ParticipantStatisticsList invalidParticipantInput;
67 private static ClElementStatisticsList inputClElementStatistics;
68 private static ClElementStatisticsList invalidClElementInput;
70 private ParticipantStatisticsProvider participantStatisticsProvider = null;
71 private ClElementStatisticsProvider clElementStatisticsProvider = null;
72 private ControlLoopProvider clProvider = null;
75 public static void beforeSetupStatistics() throws CoderException {
76 // Reading input json for statistics data
77 inputParticipantStatistics =
78 CODER.decode(new File(CL_PARTICIPANT_STATISTICS_JSON), ParticipantStatisticsList.class);
79 invalidParticipantInput =
80 CODER.decode(new File(INVALID_PARTICIPANT_JSON_INPUT), ParticipantStatisticsList.class);
81 inputClElementStatistics = CODER.decode(new File(CL_ELEMENT_STATISTICS_JSON), ClElementStatisticsList.class);
82 invalidClElementInput = CODER.decode(new File(INVALID_CL_ELEMENT_JSON_INPUT), ClElementStatisticsList.class);
86 void close() throws Exception {
87 if (participantStatisticsProvider != null) {
88 participantStatisticsProvider.close();
90 if (clElementStatisticsProvider != null) {
91 clElementStatisticsProvider.close();
93 if (clProvider != null) {
99 void testCreateParticipantStatistics() throws Exception {
100 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("createparStat");
101 participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters());
102 clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters());
103 clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters());
104 MonitoringProvider provider =
105 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
106 // Creating statistics data in db with null input
107 assertThatThrownBy(() -> {
108 provider.createParticipantStatistics(null);
109 }).hasMessageMatching(LIST_IS_NULL);
111 assertThatThrownBy(() -> {
112 provider.createParticipantStatistics(invalidParticipantInput.getStatisticsList());
113 }).hasMessageMatching("participantStatisticsList is marked .*null but is null");
115 // Creating statistics data from input json
116 ParticipantStatisticsList createResponse =
117 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
119 assertThat(createResponse.getStatisticsList()).hasSize(3);
120 assertEquals(createResponse.getStatisticsList().toString().replaceAll("\\s+", ""),
121 inputParticipantStatistics.getStatisticsList().toString().replaceAll("\\s+", ""));
125 void testGetParticipantStatistics() throws Exception {
126 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("getparStat");
127 participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters());
128 clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters());
129 clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters());
130 MonitoringProvider provider =
131 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
133 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
135 assertThatThrownBy(() -> {
136 provider.fetchFilteredParticipantStatistics(null, null, 0, null, null);
137 }).hasMessageMatching("name is marked .*null but is null");
139 // Fetch specific statistics record with name, version and record count
140 ParticipantStatisticsList getResponse =
141 provider.fetchFilteredParticipantStatistics("name2", "1.001", 1, null, null);
142 assertThat(getResponse.getStatisticsList()).hasSize(1);
143 assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""),
144 inputParticipantStatistics.getStatisticsList().get(2).toString().replaceAll("\\s+", ""));
146 // Fetch statistics using timestamp
147 getResponse = provider.fetchFilteredParticipantStatistics("name1", "1.001", 0, null,
148 Instant.parse("2021-01-10T15:00:00.000Z"));
149 assertThat(getResponse.getStatisticsList()).hasSize(1);
151 getResponse = provider.fetchFilteredParticipantStatistics("name1", "1.001", 0,
152 Instant.parse("2021-01-11T12:00:00.000Z"), Instant.parse("2021-01-11T16:00:00.000Z"));
154 assertThat(getResponse.getStatisticsList()).isEmpty();
158 void testCreateClElementStatistics() throws Exception {
159 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("createelemstat");
160 participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters());
161 clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters());
162 clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters());
164 MonitoringProvider provider =
165 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
166 // Creating statistics data in db with null input
167 assertThatThrownBy(() -> {
168 provider.createClElementStatistics(null);
169 }).hasMessageMatching(LIST_IS_NULL);
171 assertThatThrownBy(() -> {
172 provider.createClElementStatistics(invalidClElementInput.getClElementStatistics());
173 }).hasMessageMatching("clElementStatisticsList is marked .*null but is null");
175 // Creating clElement statistics data from input json
176 ClElementStatisticsList createResponse =
177 provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
179 assertThat(createResponse.getClElementStatistics()).hasSize(4);
180 assertEquals(createResponse.getClElementStatistics().toString().replaceAll("\\s+", ""),
181 inputClElementStatistics.getClElementStatistics().toString().replaceAll("\\s+", ""));
185 void testGetClElementStatistics() throws Exception {
186 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("getelemstat");
187 participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters());
188 clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters());
189 clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters());
191 MonitoringProvider provider =
192 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider);
193 assertThatThrownBy(() -> {
194 provider.fetchFilteredClElementStatistics(null, null, null, null, null, 0);
195 }).hasMessageMatching("name is marked .*null but is null");
197 provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
199 ClElementStatisticsList getResponse =
200 provider.fetchFilteredClElementStatistics("name1", null, null, null, null, 0);
202 assertThat(getResponse.getClElementStatistics()).hasSize(2);
203 assertEquals(getResponse.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""),
204 inputClElementStatistics.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""));
206 // Fetch specific statistics record with name, id and record count
207 getResponse = provider.fetchFilteredClElementStatistics("name1", "1.001",
208 "709c62b3-8918-41b9-a747-d21eb79c6c20", null, null, 0);
209 assertThat(getResponse.getClElementStatistics()).hasSize(2);
211 // Fetch statistics using timestamp
212 getResponse = provider.fetchFilteredClElementStatistics("name1", "1.001", null,
213 Instant.parse("2021-01-10T13:45:00.000Z"), null, 0);
214 assertThat(getResponse.getClElementStatistics()).hasSize(2);
218 void testGetParticipantStatsPerCL() throws Exception {
219 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("getparStatCL");
220 participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters());
221 clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters());
222 var mockClProvider = Mockito.mock(ControlLoopProvider.class);
224 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, mockClProvider);
226 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
228 var controlLoop = new ControlLoop();
229 var element = new ControlLoopElement();
230 element.setParticipantId(new ToscaConceptIdentifier("name1", "1.001"));
231 controlLoop.setElements(Map.of(UUID.randomUUID(), element));
232 when(mockClProvider.getControlLoop(eq(new ToscaConceptIdentifier("testName", "1.001"))))
233 .thenReturn(controlLoop);
235 ParticipantStatisticsList getResponse = provider.fetchParticipantStatsPerControlLoop("testName", "1.001");
236 assertThat(getResponse.getStatisticsList()).hasSize(2);
237 assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""),
238 inputParticipantStatistics.getStatisticsList().get(0).toString().replaceAll("\\s+", ""));
239 assertThat(provider.fetchParticipantStatsPerControlLoop("invalidCLName", "1.002").getStatisticsList())
244 void testClElementStatsPerCL() throws Exception {
245 // Setup a dummy Control loop data
246 ControlLoopElement mockClElement = new ControlLoopElement();
247 mockClElement.setId(inputClElementStatistics.getClElementStatistics().get(0).getId());
248 mockClElement.setParticipantId(new ToscaConceptIdentifier(
249 inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getName(),
250 inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getVersion()));
251 ControlLoop mockCL = new ControlLoop();
252 mockCL.setElements(new LinkedHashMap<>());
253 mockCL.getElements().put(mockClElement.getId(), mockClElement);
255 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("getelemstatPerCL");
256 participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters());
257 clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters());
258 ControlLoopProvider mockClProvider = Mockito.mock(ControlLoopProvider.class);
259 var monitoringProvider =
260 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, mockClProvider);
262 // Mock controlloop data to be returned for the given CL Id
263 when(mockClProvider.getControlLoop(new ToscaConceptIdentifier("testCLName", "1.001"))).thenReturn(mockCL);
265 monitoringProvider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
267 ClElementStatisticsList getResponse =
268 monitoringProvider.fetchClElementStatsPerControlLoop("testCLName", "1.001");
270 assertThat(getResponse.getClElementStatistics()).hasSize(2);
271 assertEquals(getResponse.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""),
272 inputClElementStatistics.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""));
275 monitoringProvider.fetchClElementStatsPerControlLoop("invalidCLName", "1.002").getClElementStatistics())
278 Map<String, ToscaConceptIdentifier> clElementIds =
279 monitoringProvider.getAllClElementsIdPerControlLoop("testCLName", "1.001");
280 assertThat(clElementIds)
281 .containsKey(inputClElementStatistics.getClElementStatistics().get(0).getId().toString());