2233e862c0d88e313e4066f994ecb256cd46a576
[policy/clamp.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.controlloop.runtime.monitoring;
23
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;
32
33 import java.io.File;
34 import java.time.Instant;
35 import java.util.LinkedHashMap;
36 import java.util.List;
37 import java.util.Map;
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;
58
59 class TestMonitoringProvider {
60
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();
70
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;
87
88     private ControlLoopProvider clProvider = null;
89
90     @BeforeAll
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);
99     }
100
101     @AfterEach
102     void close() throws Exception {
103         if (clProvider != null) {
104             clProvider.close();
105         }
106     }
107
108     @Test
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);
116
117         when(participantStatisticsProvider.createParticipantStatistics(any()))
118                 .thenReturn(inputParticipantStatistics.getStatisticsList());
119
120         when(participantStatisticsProvider.createParticipantStatistics(eq(null)))
121                 .thenThrow(new PfModelRuntimeException(Response.Status.BAD_REQUEST, PARTICIPANT_STAT_LIST_IS_NULL));
122
123         // Creating statistics data in db with null input
124
125         assertThatThrownBy(() -> {
126             provider.createParticipantStatistics(null);
127         }).hasMessageMatching(STAT_LIST_IS_NULL);
128
129         assertThatThrownBy(() -> {
130             provider.createParticipantStatistics(invalidParticipantInput.getStatisticsList());
131         }).hasMessageMatching(PARTICIPANT_STAT_LIST_IS_NULL);
132
133         // Creating statistics data from input json
134         ParticipantStatisticsList createResponse =
135                 provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
136
137         assertThat(createResponse.getStatisticsList()).hasSize(3);
138         assertEquals(createResponse.getStatisticsList().toString().replaceAll("\\s+", ""),
139                 inputParticipantStatistics.getStatisticsList().toString().replaceAll("\\s+", ""));
140     }
141
142     @Test
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)));
147
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());
151
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)));
154
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());
161
162         assertThatThrownBy(() -> {
163             provider.fetchFilteredParticipantStatistics(null, null, 0, null, null);
164         }).hasMessageMatching(NAME_IS_NULL);
165
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+", ""));
172
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);
177
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"));
180
181         assertThat(getResponse.getStatisticsList()).isEmpty();
182     }
183
184     @Test
185     void testCreateClElementStatistics() throws Exception {
186         var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class);
187         when(clElementStatisticsProvider.createClElementStatistics(any()))
188                 .thenReturn(inputClElementStatistics.getClElementStatistics());
189
190         when(clElementStatisticsProvider.createClElementStatistics(eq(null)))
191                 .thenThrow(new PfModelRuntimeException(Response.Status.BAD_REQUEST, CL_LIST_IS_NULL));
192
193         ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("createelemstat");
194         clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters());
195
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);
203
204         assertThatThrownBy(() -> {
205             provider.createClElementStatistics(invalidClElementInput.getClElementStatistics());
206         }).hasMessageMatching(CL_LIST_IS_NULL);
207
208         // Creating clElement statistics data from input json
209         ClElementStatisticsList createResponse =
210                 provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
211
212         assertThat(createResponse.getClElementStatistics()).hasSize(4);
213         assertEquals(createResponse.getClElementStatistics().toString().replaceAll("\\s+", ""),
214                 inputClElementStatistics.getClElementStatistics().toString().replaceAll("\\s+", ""));
215     }
216
217     @Test
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());
223
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)));
228
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);
234
235         provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
236
237         ClElementStatisticsList getResponse =
238                 provider.fetchFilteredClElementStatistics(ID_NAME1, null, null, null, null, 0);
239
240         assertThat(getResponse.getClElementStatistics()).hasSize(2);
241         assertEquals(getResponse.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""),
242                 inputClElementStatistics.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""));
243
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);
248
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);
253     }
254
255     @Test
256     void testGetParticipantStatsPerCL() throws Exception {
257         var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class);
258         var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class);
259         var mockClProvider = Mockito.mock(ControlLoopProvider.class);
260         var provider =
261                 new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, mockClProvider);
262
263         provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
264
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);
270
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)));
275
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())
281                 .isEmpty();
282     }
283
284     @Test
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);
295
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);
301
302         // Mock controlloop data to be returned for the given CL Id
303         when(mockClProvider.getControlLoop(new ToscaConceptIdentifier(ID_NAME3, ID_VERSION1))).thenReturn(mockCL);
304
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)));
309
310         monitoringProvider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
311
312         ClElementStatisticsList getResponse =
313                 monitoringProvider.fetchClElementStatsPerControlLoop(ID_NAME3, ID_VERSION1);
314
315         assertThat(getResponse.getClElementStatistics()).hasSize(2);
316         assertEquals(getResponse.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""),
317                 inputClElementStatistics.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""));
318
319         assertThat(monitoringProvider.fetchClElementStatsPerControlLoop(ID_INVALID_NAME, ID_VERSION2)
320                 .getClElementStatistics()).isEmpty();
321
322         Map<String, ToscaConceptIdentifier> clElementIds =
323                 monitoringProvider.getAllClElementsIdPerControlLoop(ID_NAME3, ID_VERSION1);
324         assertThat(clElementIds)
325                 .containsKey(inputClElementStatistics.getClElementStatistics().get(0).getId().toString());
326     }
327 }