5cb1bd40edea49e20ad637e5fca802abbfdb45c1
[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  * ================================================================================
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.apex.model.enginemodel.concepts;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
30
31 import org.junit.Test;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
36
37 /**
38  * Test the engine statistics.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public class EngineStatsTest {
43     private static final Object WAIT_LOCK = new Object();
44
45     @Test
46     public void testEngineStats() {
47         assertNotNull(new AxEngineStats());
48         assertNotNull(new AxEngineStats(new AxReferenceKey()));
49
50         final AxReferenceKey statsKey = new AxReferenceKey("EngineKey", "0.0.1", "EngineStats");
51         final AxEngineStats stats = new AxEngineStats(statsKey);
52
53         assertThatThrownBy(() -> stats.setKey(null))
54             .hasMessage("key may not be null");
55         stats.setKey(statsKey);
56         assertEquals("EngineKey:0.0.1:NULL:EngineStats", stats.getKey().getId());
57         assertEquals("EngineKey:0.0.1:NULL:EngineStats", stats.getKeys().get(0).getId());
58
59         stats.setAverageExecutionTime(123.45);
60         assertEquals(Double.valueOf(123.45), Double.valueOf(stats.getAverageExecutionTime()));
61
62         stats.setEventCount(987);
63         assertEquals(987, stats.getEventCount());
64
65         final long lastExecutionTime = System.currentTimeMillis();
66         stats.setLastExecutionTime(lastExecutionTime);
67         assertEquals(lastExecutionTime, stats.getLastExecutionTime());
68
69         final long timestamp = System.currentTimeMillis();
70         stats.setTimeStamp(timestamp);
71         assertEquals(timestamp, stats.getTimeStamp());
72         assertNotNull(stats.getTimeStampString());
73
74         final long upTime = System.currentTimeMillis() - timestamp;
75         stats.setUpTime(upTime);
76         assertEquals(upTime, stats.getUpTime());
77
78         stats.engineStart();
79         assertTrue(stats.getUpTime() > -1);
80         stats.engineStop();
81         assertTrue(stats.getUpTime() >= 0);
82
83         stats.engineStop();
84
85         stats.reset();
86
87         stats.setEventCount(-2);
88         stats.executionEnter(new AxArtifactKey());
89         assertEquals(2, stats.getEventCount());
90
91         stats.setEventCount(10);
92         stats.executionEnter(new AxArtifactKey());
93         assertEquals(11, stats.getEventCount());
94
95         stats.reset();
96         stats.engineStart();
97         stats.setEventCount(4);
98         stats.executionEnter(new AxArtifactKey());
99
100         synchronized (WAIT_LOCK) {
101             try {
102                 WAIT_LOCK.wait(10);
103             } catch (InterruptedException e) {
104                 fail("test should not throw an exception");
105             }
106         }
107
108         stats.executionExit();
109         final double avExecutionTime = stats.getAverageExecutionTime();
110         assertTrue(avExecutionTime >= 2.0 && avExecutionTime < 10.0);
111         stats.engineStop();
112
113         AxValidationResult result = new AxValidationResult();
114         result = stats.validate(result);
115         assertEquals(ValidationResult.VALID, result.getValidationResult());
116
117         stats.setKey(new AxReferenceKey());
118         result = new AxValidationResult();
119         result = stats.validate(result);
120         assertEquals(ValidationResult.INVALID, result.getValidationResult());
121
122         stats.setKey(statsKey);
123         result = new AxValidationResult();
124         result = stats.validate(result);
125         assertEquals(ValidationResult.VALID, result.getValidationResult());
126
127         stats.clean();
128         stats.reset();
129
130         final AxEngineStats clonedStats = new AxEngineStats(stats);
131         assertEquals("AxEngineStats:(engineKey=AxReferenceKey:(parentKey", clonedStats.toString().substring(0, 50));
132
133         assertNotNull(stats.getKeys());
134
135         assertNotEquals(0, stats.hashCode());
136
137         assertEquals(stats, stats);
138         assertEquals(stats, clonedStats);
139         assertNotNull(stats);
140
141         Object helloObject = "Hello";
142         assertNotEquals(stats, helloObject);
143         assertNotEquals(stats, new AxEngineStats(new AxReferenceKey()));
144
145         assertEquals(0, stats.compareTo(stats));
146         assertEquals(0, stats.compareTo(clonedStats));
147         assertNotEquals(0, stats.compareTo(new AxArtifactKey()));
148         assertNotEquals(0, stats.compareTo(null));
149         assertNotEquals(0, stats.compareTo(new AxEngineStats(new AxReferenceKey())));
150
151         stats.setTimeStamp(1);
152         assertNotEquals(stats, new AxEngineStats(statsKey));
153         assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
154         stats.setTimeStamp(0);
155         assertEquals(stats, new AxEngineStats(statsKey));
156         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
157
158         stats.setEventCount(1);
159         assertNotEquals(stats, new AxEngineStats(statsKey));
160         assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
161         stats.setEventCount(0);
162         assertEquals(stats, new AxEngineStats(statsKey));
163         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
164
165         stats.setLastExecutionTime(1);
166         assertNotEquals(stats, new AxEngineStats(statsKey));
167         assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
168         stats.setLastExecutionTime(0);
169         assertEquals(stats, new AxEngineStats(statsKey));
170         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
171
172         stats.setAverageExecutionTime(1);
173         assertNotEquals(stats, new AxEngineStats(statsKey));
174         assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
175         stats.setAverageExecutionTime(0);
176         assertEquals(stats, new AxEngineStats(statsKey));
177         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
178
179         stats.setUpTime(1);
180         assertNotEquals(stats, new AxEngineStats(statsKey));
181         assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
182         stats.setUpTime(0);
183         assertEquals(stats, new AxEngineStats(statsKey));
184         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
185
186         assertEquals(-1, stats.compareTo(new AxEngineStats(statsKey, 0, 0, 0, 0.0, 0, 1)));
187
188         stats.engineStart();
189         assertNotEquals(stats, new AxEngineStats(statsKey));
190         final AxEngineStats newStats = new AxEngineStats(statsKey);
191         newStats.setTimeStamp(stats.getTimeStamp());
192         assertNotEquals(stats, newStats);
193         assertNotEquals(0, stats.compareTo(newStats));
194         stats.engineStop();
195         stats.reset();
196         assertEquals(stats, new AxEngineStats(statsKey));
197         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
198     }
199
200 }