a40e8cbbf622496daa975fd3f5bdc126ef22cd4f
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
5  *  Modifications Copyright (C) 2022 Bell Canada.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.model.enginemodel.concepts;
24
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
31
32 import io.prometheus.client.CollectorRegistry;
33 import org.junit.Test;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
38
39 /**
40  * Test the engine statistics.
41  *
42  * @author Liam Fallon (liam.fallon@ericsson.com)
43  */
44 public class EngineStatsTest {
45     private static final Object WAIT_LOCK = new Object();
46     private static final String ENGINE_KEY = "EngineKey";
47     private static final String ENGINE_VERSION = "0.0.1";
48
49     @Test
50     public void testEngineStats() {
51         assertNotNull(new AxEngineStats());
52         assertNotNull(new AxEngineStats(new AxReferenceKey()));
53
54         final AxReferenceKey statsKey = new AxReferenceKey(ENGINE_KEY, ENGINE_VERSION, "EngineStats");
55         final AxEngineStats stats = new AxEngineStats(statsKey);
56
57         assertThatThrownBy(() -> stats.setKey(null))
58             .hasMessage("key may not be null");
59         stats.setKey(statsKey);
60         assertEquals("EngineKey:0.0.1:NULL:EngineStats", stats.getKey().getId());
61         assertEquals("EngineKey:0.0.1:NULL:EngineStats", stats.getKeys().get(0).getId());
62
63         stats.setAverageExecutionTime(123.45);
64         assertEquals(Double.valueOf(123.45), Double.valueOf(stats.getAverageExecutionTime()));
65         checkAvgExecTimeMetric(stats);
66
67         stats.setEventCount(987);
68         assertEquals(987, stats.getEventCount());
69         checkEventsCountMetric(stats);
70
71         final long lastExecutionTime = System.currentTimeMillis();
72         stats.setLastExecutionTime(lastExecutionTime);
73         assertEquals(lastExecutionTime, stats.getLastExecutionTime());
74         checkLastExecTimeMetric(stats);
75
76         final long timestamp = System.currentTimeMillis();
77         stats.setTimeStamp(timestamp);
78         assertEquals(timestamp, stats.getTimeStamp());
79         assertNotNull(stats.getTimeStampString());
80
81         final long upTime = System.currentTimeMillis() - timestamp;
82         stats.setUpTime(upTime);
83         assertEquals(upTime, stats.getUpTime());
84         checkUpTimeMetric(stats);
85
86         stats.engineStart();
87         assertTrue(stats.getUpTime() > -1);
88         checkEngineStartTimestampMetric(stats);
89         checkLastExecTimeMetric(stats);
90         stats.engineStop();
91         assertTrue(stats.getUpTime() >= 0);
92
93         stats.engineStop();
94         checkUpTimeMetric(stats);
95         checkEngineStartTimestampMetric(stats);
96
97         stats.reset();
98
99         stats.setEventCount(-2);
100         stats.executionEnter(new AxArtifactKey());
101         assertEquals(2, stats.getEventCount());
102         checkEventsCountMetric(stats);
103
104         stats.setEventCount(10);
105         stats.executionEnter(new AxArtifactKey());
106         assertEquals(11, stats.getEventCount());
107         checkEventsCountMetric(stats);
108
109         stats.reset();
110         stats.engineStart();
111         stats.setEventCount(4);
112         checkUpTimeMetric(stats);
113         stats.executionEnter(new AxArtifactKey());
114         checkEventsCountMetric(stats);
115         checkAvgExecTimeMetric(stats);
116         checkEngineStartTimestampMetric(stats);
117
118         synchronized (WAIT_LOCK) {
119             try {
120                 WAIT_LOCK.wait(10);
121             } catch (InterruptedException e) {
122                 fail("test should not throw an exception");
123             }
124         }
125
126         stats.executionExit();
127         final double avExecutionTime = stats.getAverageExecutionTime();
128         assertTrue(avExecutionTime >= 2.0 && avExecutionTime < 10.0);
129         stats.engineStop();
130         checkUpTimeMetric(stats);
131
132         AxValidationResult result = new AxValidationResult();
133         result = stats.validate(result);
134         assertEquals(ValidationResult.VALID, result.getValidationResult());
135
136         stats.setKey(new AxReferenceKey());
137         result = new AxValidationResult();
138         result = stats.validate(result);
139         assertEquals(ValidationResult.INVALID, result.getValidationResult());
140
141         stats.setKey(statsKey);
142         result = new AxValidationResult();
143         result = stats.validate(result);
144         assertEquals(ValidationResult.VALID, result.getValidationResult());
145
146         stats.clean();
147         stats.reset();
148         checkAllPrometheusMetrics(stats);
149
150         final AxEngineStats clonedStats = new AxEngineStats(stats);
151         assertEquals("AxEngineStats:(engineKey=AxReferenceKey:(parentKey", clonedStats.toString().substring(0, 50));
152
153         assertNotNull(stats.getKeys());
154
155         assertNotEquals(0, stats.hashCode());
156
157         // disabling sonar because this code tests the equals() method
158         assertEquals(stats, stats); // NOSONAR
159         assertEquals(stats, clonedStats);
160         assertNotNull(stats);
161         checkAllPrometheusMetrics(clonedStats);
162
163         Object helloObject = "Hello";
164         assertNotEquals(stats, helloObject);
165         assertNotEquals(stats, new AxEngineStats(new AxReferenceKey()));
166
167         assertEquals(0, stats.compareTo(stats));
168         assertEquals(0, stats.compareTo(clonedStats));
169         assertNotEquals(0, stats.compareTo(new AxArtifactKey()));
170         assertNotEquals(0, stats.compareTo(null));
171         assertNotEquals(0, stats.compareTo(new AxEngineStats(new AxReferenceKey())));
172
173         stats.setTimeStamp(1);
174         assertNotEquals(stats, new AxEngineStats(statsKey));
175         assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
176         stats.setTimeStamp(0);
177         assertEquals(stats, new AxEngineStats(statsKey));
178         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
179         checkAllPrometheusMetrics(clonedStats);
180
181         stats.setEventCount(1);
182         assertNotEquals(stats, new AxEngineStats(statsKey));
183         assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
184         stats.setEventCount(0);
185         assertEquals(stats, new AxEngineStats(statsKey));
186         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
187
188         stats.setLastExecutionTime(1);
189         assertNotEquals(stats, new AxEngineStats(statsKey));
190         assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
191         stats.setLastExecutionTime(0);
192         assertEquals(stats, new AxEngineStats(statsKey));
193         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
194
195         stats.setAverageExecutionTime(1);
196         assertNotEquals(stats, new AxEngineStats(statsKey));
197         assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
198         stats.setAverageExecutionTime(0);
199         assertEquals(stats, new AxEngineStats(statsKey));
200         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
201
202         stats.setUpTime(1);
203         assertNotEquals(stats, new AxEngineStats(statsKey));
204         assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
205         stats.setUpTime(0);
206         assertEquals(stats, new AxEngineStats(statsKey));
207         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
208
209         assertEquals(-1, stats.compareTo(new AxEngineStats(statsKey, 0, 0, 0, 0.0, 0, 1)));
210
211         stats.engineStart();
212         assertNotEquals(stats, new AxEngineStats(statsKey));
213         final AxEngineStats newStats = new AxEngineStats(statsKey);
214         newStats.setTimeStamp(stats.getTimeStamp());
215         assertNotEquals(stats, newStats);
216         assertNotEquals(0, stats.compareTo(newStats));
217         stats.engineStop();
218         checkUpTimeMetric(stats);
219         checkEngineStartTimestampMetric(stats);
220         stats.reset();
221         assertEquals(stats, new AxEngineStats(statsKey));
222         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
223         checkAllPrometheusMetrics(stats);
224     }
225
226     private void checkUpTimeMetric(AxEngineStats stats) {
227         Double upTimeMetric = CollectorRegistry.defaultRegistry.getSampleValue("apex_engine_uptime",
228                 new String[]{AxEngineStats.ENGINE_INSTANCE_ID},
229                 new String[]{ENGINE_KEY + ":" + ENGINE_VERSION}) * 1000d;
230         assertEquals(upTimeMetric.longValue(), stats.getUpTime());
231     }
232
233     private void checkEventsCountMetric(AxEngineStats stats) {
234         Double eventsCountMetric = CollectorRegistry.defaultRegistry
235                 .getSampleValue("apex_engine_events_executed_count",
236                         new String[]{AxEngineStats.ENGINE_INSTANCE_ID},
237                         new String[]{ENGINE_KEY + ":" + ENGINE_VERSION});
238         assertEquals(eventsCountMetric.longValue(), stats.getEventCount());
239     }
240
241     private void checkLastExecTimeMetric(AxEngineStats stats) {
242         Double lastExecTimeMetric = CollectorRegistry.defaultRegistry
243                 .getSampleValue("apex_engine_last_execution_time_sum", new String[]{AxEngineStats.ENGINE_INSTANCE_ID},
244                         new String[]{ENGINE_KEY + ":" + ENGINE_VERSION}) * 1000d;
245         assertEquals(lastExecTimeMetric.longValue(), stats.getLastExecutionTime());
246     }
247
248     private void checkEngineStartTimestampMetric(AxEngineStats stats) {
249         Double engineStartTimestampMetric = CollectorRegistry.defaultRegistry
250                 .getSampleValue("apex_engine_last_start_timestamp_epoch",
251                         new String[]{AxEngineStats.ENGINE_INSTANCE_ID},
252                         new String[]{ENGINE_KEY + ":" + ENGINE_VERSION});
253         assertEquals(engineStartTimestampMetric.longValue(), stats.getLastStart());
254     }
255
256     private void checkAvgExecTimeMetric(AxEngineStats stats) {
257         Double avgExecTimeMetric = CollectorRegistry.defaultRegistry
258                 .getSampleValue("apex_engine_average_execution_time_seconds",
259                         new String[]{AxEngineStats.ENGINE_INSTANCE_ID},
260                         new String[]{ENGINE_KEY + ":" + ENGINE_VERSION}) * 1000d;
261         assertEquals(avgExecTimeMetric, Double.valueOf(stats.getAverageExecutionTime()));
262     }
263
264     private void checkAllPrometheusMetrics(AxEngineStats stats) {
265         checkEventsCountMetric(stats);
266         checkUpTimeMetric(stats);
267         checkAvgExecTimeMetric(stats);
268         checkEngineStartTimestampMetric(stats);
269         checkEngineStartTimestampMetric(stats);
270     }
271 }