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