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.Mockito.when;
29 import java.lang.reflect.Field;
30 import java.time.Instant;
31 import java.util.ArrayList;
32 import java.util.LinkedHashMap;
33 import java.util.List;
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.ControlLoopProvider;
43 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
44 import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData;
45 import org.onap.policy.common.utils.coder.Coder;
46 import org.onap.policy.common.utils.coder.CoderException;
47 import org.onap.policy.common.utils.coder.StandardCoder;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
50 class TestMonitoringProvider {
52 private static final String CL_PARTICIPANT_STATISTICS_JSON =
53 "src/test/resources/rest/monitoring/TestParticipantStatistics.json";
54 private static final String INVALID_PARTICIPANT_JSON_INPUT =
55 "src/test/resources/rest/monitoring/TestParticipantStatistics_Invalid.json";
56 private static final String CL_ELEMENT_STATISTICS_JSON =
57 "src/test/resources/rest/monitoring/TestClElementStatistics.json";
58 private static final String INVALID_CL_ELEMENT_JSON_INPUT =
59 "src/test/resources/rest/monitoring/TestClElementStatistics_Invalid.json";
60 private static final Coder CODER = new StandardCoder();
62 private static final String CL_PROVIDER_FIELD = "controlLoopProvider";
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;
71 public static void beforeSetupStatistics() throws CoderException {
72 // Reading input json for statistics data
73 inputParticipantStatistics =
74 CODER.decode(new File(CL_PARTICIPANT_STATISTICS_JSON), ParticipantStatisticsList.class);
75 invalidParticipantInput =
76 CODER.decode(new File(INVALID_PARTICIPANT_JSON_INPUT), ParticipantStatisticsList.class);
77 inputClElementStatistics = CODER.decode(new File(CL_ELEMENT_STATISTICS_JSON), ClElementStatisticsList.class);
78 invalidClElementInput = CODER.decode(new File(INVALID_CL_ELEMENT_JSON_INPUT), ClElementStatisticsList.class);
82 void testCreateParticipantStatistics() throws Exception {
83 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "createparStat");
85 try (MonitoringProvider provider = new MonitoringProvider(parameters)) {
86 // Creating statistics data in db with null input
87 assertThatThrownBy(() -> {
88 provider.createParticipantStatistics(null);
89 }).hasMessageMatching(LIST_IS_NULL);
91 assertThatThrownBy(() -> {
92 provider.createParticipantStatistics(invalidParticipantInput.getStatisticsList());
93 }).hasMessageMatching("participantStatisticsList is marked .*null but is null");
95 // Creating statistics data from input json
96 ParticipantStatisticsList createResponse =
97 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
99 assertThat(createResponse.getStatisticsList()).hasSize(3);
100 assertEquals(createResponse.getStatisticsList().toString().replaceAll("\\s+", ""),
101 inputParticipantStatistics.getStatisticsList().toString().replaceAll("\\s+", ""));
106 void testGetParticipantStatistics() throws Exception {
107 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "getparStat");
108 try (MonitoringProvider provider = new MonitoringProvider(parameters)) {
109 ParticipantStatisticsList getResponse;
111 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
113 assertThatThrownBy(() -> {
114 provider.fetchFilteredParticipantStatistics(null, null, 0, null, null);
115 }).hasMessageMatching("name is marked .*null but is null");
117 // Fetch specific statistics record with name, version and record count
118 getResponse = provider.fetchFilteredParticipantStatistics("name2", "1.001", 1, null, null);
119 assertThat(getResponse.getStatisticsList()).hasSize(1);
120 assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""),
121 inputParticipantStatistics.getStatisticsList().get(2).toString().replaceAll("\\s+", ""));
123 // Fetch statistics using timestamp
124 getResponse = provider.fetchFilteredParticipantStatistics("name1", "1.001", 0, null,
125 Instant.parse("2021-01-10T15:00:00.000Z"));
126 assertThat(getResponse.getStatisticsList()).hasSize(1);
128 getResponse = provider.fetchFilteredParticipantStatistics("name1", "1.001", 0,
129 Instant.parse("2021-01-11T12:00:00.000Z"), Instant.parse("2021-01-11T16:00:00.000Z"));
131 assertThat(getResponse.getStatisticsList()).isEmpty();
136 void testCreateClElementStatistics() throws Exception {
137 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "createelemstat");
139 try (MonitoringProvider provider = new MonitoringProvider(parameters)) {
140 // Creating statistics data in db with null input
141 assertThatThrownBy(() -> {
142 provider.createClElementStatistics(null);
143 }).hasMessageMatching(LIST_IS_NULL);
145 assertThatThrownBy(() -> {
146 provider.createClElementStatistics(invalidClElementInput.getClElementStatistics());
147 }).hasMessageMatching("clElementStatisticsList is marked .*null but is null");
149 // Creating clElement statistics data from input json
150 ClElementStatisticsList createResponse =
151 provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
153 assertThat(createResponse.getClElementStatistics()).hasSize(4);
154 assertEquals(createResponse.getClElementStatistics().toString().replaceAll("\\s+", ""),
155 inputClElementStatistics.getClElementStatistics().toString().replaceAll("\\s+", ""));
160 void testGetClElementStatistics() throws Exception {
161 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "getelemstat");
163 try (MonitoringProvider provider = new MonitoringProvider(parameters)) {
164 ClElementStatisticsList getResponse;
166 assertThatThrownBy(() -> {
167 provider.fetchFilteredClElementStatistics(null, null, null, null, null, 0);
168 }).hasMessageMatching("name is marked .*null but is null");
170 provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
172 getResponse = provider.fetchFilteredClElementStatistics("name1", null, null, null, null, 0);
174 assertThat(getResponse.getClElementStatistics()).hasSize(2);
175 assertEquals(getResponse.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""),
176 inputClElementStatistics.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""));
178 // Fetch specific statistics record with name, id and record count
179 getResponse = provider.fetchFilteredClElementStatistics("name1", "1.001",
180 "709c62b3-8918-41b9-a747-d21eb79c6c20", null, null, 0);
181 assertThat(getResponse.getClElementStatistics()).hasSize(2);
183 // Fetch statistics using timestamp
184 getResponse = provider.fetchFilteredClElementStatistics("name1", "1.001", null,
185 Instant.parse("2021-01-10T13:45:00.000Z"), null, 0);
186 assertThat(getResponse.getClElementStatistics()).hasSize(2);
191 void testGetParticipantStatsPerCL() throws Exception {
192 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "getparStatCL");
194 try (MonitoringProvider provider = Mockito.spy(new MonitoringProvider(parameters))) {
196 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
197 // Mock the response for fetching participant conceptIdentifiers per control loop
198 List<ToscaConceptIdentifier> conceptIdentifiers = new ArrayList<>();
199 conceptIdentifiers.add(new ToscaConceptIdentifier("name1", "1.001"));
200 when(provider.getAllParticipantIdsPerControlLoop("testName", "1.001")).thenReturn(conceptIdentifiers);
201 ParticipantStatisticsList getResponse;
202 getResponse = provider.fetchParticipantStatsPerControlLoop("testName", "1.001");
203 assertThat(getResponse.getStatisticsList()).hasSize(2);
204 assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""),
205 inputParticipantStatistics.getStatisticsList().get(0).toString().replaceAll("\\s+", ""));
206 assertThat(provider.fetchParticipantStatsPerControlLoop("invalidCLName", "1.002").getStatisticsList())
213 void testClElementStatsPerCL() throws Exception {
214 ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "getelemstatPerCL");
216 // Setup a dummy Control loop data
217 ControlLoopElement mockClElement = new ControlLoopElement();
218 mockClElement.setId(inputClElementStatistics.getClElementStatistics().get(0).getId());
219 mockClElement.setParticipantId(new ToscaConceptIdentifier(
220 inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getName(),
221 inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getVersion()));
222 ControlLoop mockCL = new ControlLoop();
223 mockCL.setElements(new LinkedHashMap<>());
224 mockCL.getElements().put(mockClElement.getId(), mockClElement);
226 // Mock controlloop data to be returned for the given CL Id
227 ControlLoopProvider mockClProvider = Mockito.mock(ControlLoopProvider.class);
228 when(mockClProvider.getControlLoop(new ToscaConceptIdentifier("testCLName", "1.001"))).thenReturn(mockCL);
230 try (MonitoringProvider monitoringProvider = new MonitoringProvider(parameters)) {
231 monitoringProvider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
232 Field controlLoopProviderField = monitoringProvider.getClass().getDeclaredField(CL_PROVIDER_FIELD);
233 controlLoopProviderField.setAccessible(true);
234 controlLoopProviderField.set(monitoringProvider, mockClProvider);
236 ClElementStatisticsList getResponse;
237 getResponse = monitoringProvider.fetchClElementStatsPerControlLoop("testCLName", "1.001");
239 assertThat(getResponse.getClElementStatistics()).hasSize(2);
240 assertEquals(getResponse.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""),
241 inputClElementStatistics.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""));
243 assertThat(monitoringProvider.fetchClElementStatsPerControlLoop("invalidCLName", "1.002")
244 .getClElementStatistics()).isEmpty();
246 Map<String, ToscaConceptIdentifier> clElementIds =
247 monitoringProvider.getAllClElementsIdPerControlLoop("testCLName", "1.001");
248 assertThat(clElementIds)
249 .containsKey(inputClElementStatistics.getClElementStatistics().get(0).getId().toString());