2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.drools.stats;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import org.junit.jupiter.api.Test;
28 import org.onap.policy.drools.metrics.Metric;
30 class PolicyStatsTest {
34 Metric trans1 = createTrans();
35 trans1.setSuccess(true);
37 PolicyStats stats = new PolicyStats();
40 assertEquals(1, stats.getPolicyExecutedCount());
41 assertEquals(trans1.getStartTime().toEpochMilli(), stats.getLastStart());
42 assertEquals((double) trans1.getElapsedTime(), stats.getAverageExecutionTime(), 0.0d);
43 assertEquals(trans1.getEndTime().toEpochMilli(), stats.getLastExecutionTime());
44 assertEquals(0, stats.getPolicyExecutedFailCount());
45 assertEquals(1, stats.getPolicyExecutedSuccessCount());
46 assertThat(stats.getBirthTime()).isGreaterThanOrEqualTo(trans1.getStartTime().toEpochMilli());
48 Metric trans2 = createTrans();
49 trans2.setSuccess(false);
50 trans2.setEndTime(trans2.getStartTime().plusMillis(5));
51 trans2.setElapsedTime(null);
54 assertEquals(2, stats.getPolicyExecutedCount());
55 assertEquals(trans2.getStartTime().toEpochMilli(), stats.getLastStart());
56 assertEquals((5 + 1) / 2d, stats.getAverageExecutionTime(), 0.0d);
57 assertEquals(trans2.getEndTime().toEpochMilli(), stats.getLastExecutionTime());
58 assertEquals(1, stats.getPolicyExecutedFailCount());
59 assertEquals(1, stats.getPolicyExecutedSuccessCount());
60 assertThat(stats.getBirthTime()).isLessThanOrEqualTo(trans2.getStartTime().toEpochMilli());
62 Metric trans3 = createTrans();
63 trans3.setSuccess(false);
64 trans3.setEndTime(trans3.getStartTime().plusMillis(9));
65 trans3.setElapsedTime(null);
68 assertEquals(3, stats.getPolicyExecutedCount());
69 assertEquals(trans3.getStartTime().toEpochMilli(), stats.getLastStart());
70 assertEquals((5 + 1 + 9) / 3d, stats.getAverageExecutionTime(), 0.0d);
71 assertEquals(trans3.getEndTime().toEpochMilli(), stats.getLastExecutionTime());
72 assertEquals(2, stats.getPolicyExecutedFailCount());
73 assertEquals(1, stats.getPolicyExecutedSuccessCount());
74 assertThat(stats.getBirthTime()).isLessThanOrEqualTo(trans2.getStartTime().toEpochMilli());
77 private Metric createTrans() {
78 Metric trans = new Metric();
79 trans.setStartTime(null);
80 trans.setEndTime(trans.getStartTime().plusMillis(1));
82 trans.setElapsedTime(null);
83 assertEquals(1L, trans.getElapsedTime().longValue());