580b30f9ec59cb2ee8c147ddbc14cc53f6598722
[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;
22
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;
27
28 import java.io.File;
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;
34 import java.util.Map;
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;
49
50 class TestMonitoringProvider {
51
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();
61
62     private static final String CL_PROVIDER_FIELD = "controlLoopProvider";
63
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;
69
70     @BeforeAll
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);
79     }
80
81     @Test
82     void testCreateParticipantStatistics() throws Exception {
83         ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "createparStat");
84
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);
90
91             assertThatThrownBy(() -> {
92                 provider.createParticipantStatistics(invalidParticipantInput.getStatisticsList());
93             }).hasMessageMatching("participantStatisticsList is marked .*null but is null");
94
95             // Creating statistics data from input json
96             ParticipantStatisticsList createResponse =
97                     provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
98
99             assertThat(createResponse.getStatisticsList()).hasSize(3);
100             assertEquals(createResponse.getStatisticsList().toString().replaceAll("\\s+", ""),
101                     inputParticipantStatistics.getStatisticsList().toString().replaceAll("\\s+", ""));
102         }
103     }
104
105     @Test
106     void testGetParticipantStatistics() throws Exception {
107         ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "getparStat");
108         try (MonitoringProvider provider = new MonitoringProvider(parameters)) {
109             ParticipantStatisticsList getResponse;
110
111             provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList());
112
113             assertThatThrownBy(() -> {
114                 provider.fetchFilteredParticipantStatistics(null, null, 0, null, null);
115             }).hasMessageMatching("name is marked .*null but is null");
116
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+", ""));
122
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);
127
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"));
130
131             assertThat(getResponse.getStatisticsList()).isEmpty();
132         }
133     }
134
135     @Test
136     void testCreateClElementStatistics() throws Exception {
137         ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "createelemstat");
138
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);
144
145             assertThatThrownBy(() -> {
146                 provider.createClElementStatistics(invalidClElementInput.getClElementStatistics());
147             }).hasMessageMatching("clElementStatisticsList is marked .*null but is null");
148
149             // Creating clElement statistics data from input json
150             ClElementStatisticsList createResponse =
151                     provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
152
153             assertThat(createResponse.getClElementStatistics()).hasSize(4);
154             assertEquals(createResponse.getClElementStatistics().toString().replaceAll("\\s+", ""),
155                     inputClElementStatistics.getClElementStatistics().toString().replaceAll("\\s+", ""));
156         }
157     }
158
159     @Test
160     void testGetClElementStatistics() throws Exception {
161         ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "getelemstat");
162
163         try (MonitoringProvider provider = new MonitoringProvider(parameters)) {
164             ClElementStatisticsList getResponse;
165
166             assertThatThrownBy(() -> {
167                 provider.fetchFilteredClElementStatistics(null, null, null, null, null, 0);
168             }).hasMessageMatching("name is marked .*null but is null");
169
170             provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics());
171
172             getResponse = provider.fetchFilteredClElementStatistics("name1", null, null, null, null, 0);
173
174             assertThat(getResponse.getClElementStatistics()).hasSize(2);
175             assertEquals(getResponse.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""),
176                     inputClElementStatistics.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""));
177
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);
182
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);
187         }
188     }
189
190     @Test
191     void testGetParticipantStatsPerCL() throws Exception {
192         ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "getparStatCL");
193
194         try (MonitoringProvider provider = Mockito.spy(new MonitoringProvider(parameters))) {
195
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())
207                     .isEmpty();
208         }
209
210     }
211
212     @Test
213     void testClElementStatsPerCL() throws Exception {
214         ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup(0, "getelemstatPerCL");
215
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);
225
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);
229
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);
235
236             ClElementStatisticsList getResponse;
237             getResponse = monitoringProvider.fetchClElementStatsPerControlLoop("testCLName", "1.001");
238
239             assertThat(getResponse.getClElementStatistics()).hasSize(2);
240             assertEquals(getResponse.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""),
241                     inputClElementStatistics.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""));
242
243             assertThat(monitoringProvider.fetchClElementStatsPerControlLoop("invalidCLName", "1.002")
244                     .getClElementStatistics()).isEmpty();
245
246             Map<String, ToscaConceptIdentifier> clElementIds =
247                     monitoringProvider.getAllClElementsIdPerControlLoop("testCLName", "1.001");
248             assertThat(clElementIds)
249                     .containsKey(inputClElementStatistics.getClElementStatistics().get(0).getId().toString());
250         }
251     }
252 }