Removing deprecated DMAAP library
[policy/drools-pdp.git] / policy-management / src / test / java / org / onap / policy / drools / stats / PolicyStatsTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.drools.stats;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26
27 import org.junit.jupiter.api.Test;
28 import org.onap.policy.drools.metrics.Metric;
29
30 class PolicyStatsTest {
31
32     @Test
33     void testStat() {
34         Metric trans1 = createTrans();
35         trans1.setSuccess(true);
36
37         PolicyStats stats = new PolicyStats();
38         stats.stat(trans1);
39
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());
47
48         Metric trans2 = createTrans();
49         trans2.setSuccess(false);
50         trans2.setEndTime(trans2.getStartTime().plusMillis(5));
51         trans2.setElapsedTime(null);
52         stats.stat(trans2);
53
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());
61
62         Metric trans3 = createTrans();
63         trans3.setSuccess(false);
64         trans3.setEndTime(trans3.getStartTime().plusMillis(9));
65         trans3.setElapsedTime(null);
66         stats.stat(trans3);
67
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());
75     }
76
77     private Metric createTrans() {
78         Metric trans = new Metric();
79         trans.setStartTime(null);
80         trans.setEndTime(trans.getStartTime().plusMillis(1));
81
82         trans.setElapsedTime(null);
83         assertEquals(1L, trans.getElapsedTime().longValue());
84         return trans;
85     }
86
87 }