2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.enginemodel.concepts;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
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;
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;
38 * Test engine statistics.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class EngineStatsTest {
42 private static final Object WAIT_LOCK = new Object();
45 public void testEngineStats() {
46 assertNotNull(new AxEngineStats());
47 assertNotNull(new AxEngineStats(new AxReferenceKey()));
49 final AxReferenceKey statsKey = new AxReferenceKey("EngineKey", "0.0.1", "EngineStats");
50 final AxEngineStats stats = new AxEngineStats(statsKey);
54 fail("test should throw an exception here");
55 } catch (final Exception e) {
56 assertEquals("key may not be null", e.getMessage());
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());
63 stats.setAverageExecutionTime(123.45);
64 assertEquals(new Double(123.45), new Double(stats.getAverageExecutionTime()));
66 stats.setEventCount(987);
67 assertEquals(987, stats.getEventCount());
69 final long lastExecutionTime = System.currentTimeMillis();
70 stats.setLastExecutionTime(lastExecutionTime);
71 assertEquals(lastExecutionTime, stats.getLastExecutionTime());
73 final long timestamp = System.currentTimeMillis();
74 stats.setTimeStamp(timestamp);
75 assertEquals(timestamp, stats.getTimeStamp());
76 assertNotNull(stats.getTimeStampString());
78 final long upTime = System.currentTimeMillis() - timestamp;
79 stats.setUpTime(upTime);
80 assertEquals(upTime, stats.getUpTime());
83 assertTrue(stats.getUpTime() > -1);
85 assertTrue(stats.getUpTime() >= 0);
91 stats.setEventCount(-2);
92 stats.executionEnter(new AxArtifactKey());
93 assertEquals(2, stats.getEventCount());
95 stats.setEventCount(10);
96 stats.executionEnter(new AxArtifactKey());
97 assertEquals(11, stats.getEventCount());
101 stats.setEventCount(4);
102 stats.executionEnter(new AxArtifactKey());
104 synchronized (WAIT_LOCK) {
107 } catch (InterruptedException e) {
108 fail("test should not throw an exception");
112 stats.executionExit();
113 final double avExecutionTime = stats.getAverageExecutionTime();
114 assertTrue(avExecutionTime >= 2.0 && avExecutionTime < 10.0);
117 AxValidationResult result = new AxValidationResult();
118 result = stats.validate(result);
119 assertEquals(ValidationResult.VALID, result.getValidationResult());
121 stats.setKey(new AxReferenceKey());
122 result = new AxValidationResult();
123 result = stats.validate(result);
124 assertEquals(ValidationResult.INVALID, result.getValidationResult());
126 stats.setKey(statsKey);
127 result = new AxValidationResult();
128 result = stats.validate(result);
129 assertEquals(ValidationResult.VALID, result.getValidationResult());
134 final AxEngineStats clonedStats = new AxEngineStats(stats);
135 assertEquals("AxEngineStats:(engineKey=AxReferenceKey:(parentKey", clonedStats.toString().substring(0, 50));
137 assertNotNull(stats.getKeys());
139 assertFalse(stats.hashCode() == 0);
141 assertTrue(stats.equals(stats));
142 assertTrue(stats.equals(clonedStats));
143 assertFalse(stats.equals(null));
144 assertFalse(stats.equals((Object)"Hello"));
145 assertFalse(stats.equals(new AxEngineStats(new AxReferenceKey())));
147 assertEquals(0, stats.compareTo(stats));
148 assertEquals(0, stats.compareTo(clonedStats));
149 assertNotEquals(0, stats.compareTo(new AxArtifactKey()));
150 assertNotEquals(0, stats.compareTo(null));
151 assertNotEquals(0, stats.compareTo(new AxEngineStats(new AxReferenceKey())));
153 stats.setTimeStamp(1);
154 assertFalse(stats.equals(new AxEngineStats(statsKey)));
155 assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
156 stats.setTimeStamp(0);
157 assertTrue(stats.equals(new AxEngineStats(statsKey)));
158 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
160 stats.setEventCount(1);
161 assertFalse(stats.equals(new AxEngineStats(statsKey)));
162 assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
163 stats.setEventCount(0);
164 assertTrue(stats.equals(new AxEngineStats(statsKey)));
165 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
167 stats.setLastExecutionTime(1);
168 assertFalse(stats.equals(new AxEngineStats(statsKey)));
169 assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
170 stats.setLastExecutionTime(0);
171 assertTrue(stats.equals(new AxEngineStats(statsKey)));
172 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
174 stats.setAverageExecutionTime(1);
175 assertFalse(stats.equals(new AxEngineStats(statsKey)));
176 assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
177 stats.setAverageExecutionTime(0);
178 assertTrue(stats.equals(new AxEngineStats(statsKey)));
179 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
182 assertFalse(stats.equals(new AxEngineStats(statsKey)));
183 assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
185 assertTrue(stats.equals(new AxEngineStats(statsKey)));
186 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
188 assertEquals(-1, stats.compareTo(new AxEngineStats(statsKey, 0, 0, 0, 0.0, 0, 1)));
191 assertFalse(stats.equals(new AxEngineStats(statsKey)));
192 final AxEngineStats newStats = new AxEngineStats(statsKey);
193 newStats.setTimeStamp(stats.getTimeStamp());
194 assertFalse(stats.equals(newStats));
195 assertNotEquals(0, stats.compareTo(newStats));
198 assertTrue(stats.equals(new AxEngineStats(statsKey)));
199 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));