2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.enginemodel.concepts;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
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;
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;
37 * Test engine statistics.
38 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public class EngineStatsTest {
41 private static final Object WAIT_LOCK = new Object();
44 public void testEngineStats() {
45 assertNotNull(new AxEngineStats());
46 assertNotNull(new AxEngineStats(new AxReferenceKey()));
48 final AxReferenceKey statsKey = new AxReferenceKey("EngineKey", "0.0.1", "EngineStats");
49 final AxEngineStats stats = new AxEngineStats(statsKey);
53 fail("test should throw an exception here");
54 } catch (final Exception e) {
55 assertEquals("key may not be null", e.getMessage());
58 stats.setKey(statsKey);
59 assertEquals("EngineKey:0.0.1:NULL:EngineStats", stats.getKey().getId());
60 assertEquals("EngineKey:0.0.1:NULL:EngineStats", stats.getKeys().get(0).getId());
62 stats.setAverageExecutionTime(123.45);
63 assertEquals(new Double(123.45), new Double(stats.getAverageExecutionTime()));
65 stats.setEventCount(987);
66 assertEquals(987, stats.getEventCount());
68 final long lastExecutionTime = System.currentTimeMillis();
69 stats.setLastExecutionTime(lastExecutionTime);
70 assertEquals(lastExecutionTime, stats.getLastExecutionTime());
72 final long timestamp = System.currentTimeMillis();
73 stats.setTimeStamp(timestamp);
74 assertEquals(timestamp, stats.getTimeStamp());
75 assertNotNull(stats.getTimeStampString());
77 final long upTime = System.currentTimeMillis() - timestamp;
78 stats.setUpTime(upTime);
79 assertEquals(upTime, stats.getUpTime());
82 assertTrue(stats.getUpTime() > -1);
84 assertTrue(stats.getUpTime() >= 0);
90 stats.setEventCount(-2);
91 stats.executionEnter(new AxArtifactKey());
92 assertEquals(2, stats.getEventCount());
94 stats.setEventCount(10);
95 stats.executionEnter(new AxArtifactKey());
96 assertEquals(11, stats.getEventCount());
100 stats.setEventCount(4);
101 stats.executionEnter(new AxArtifactKey());
103 synchronized (WAIT_LOCK) {
106 } catch (InterruptedException e) {
107 fail("test should not throw an exception");
111 stats.executionExit();
112 final double avExecutionTime = stats.getAverageExecutionTime();
113 assertTrue(avExecutionTime >= 2.0 && avExecutionTime < 10.0);
116 AxValidationResult result = new AxValidationResult();
117 result = stats.validate(result);
118 assertEquals(ValidationResult.VALID, result.getValidationResult());
120 stats.setKey(new AxReferenceKey());
121 result = new AxValidationResult();
122 result = stats.validate(result);
123 assertEquals(ValidationResult.INVALID, result.getValidationResult());
125 stats.setKey(statsKey);
126 result = new AxValidationResult();
127 result = stats.validate(result);
128 assertEquals(ValidationResult.VALID, result.getValidationResult());
133 final AxEngineStats clonedStats = new AxEngineStats(stats);
134 assertEquals("AxEngineStats:(engineKey=AxReferenceKey:(parentKey", clonedStats.toString().substring(0, 50));
136 assertNotNull(stats.getKeys());
138 assertFalse(stats.hashCode() == 0);
140 assertTrue(stats.equals(stats));
141 assertTrue(stats.equals(clonedStats));
142 assertFalse(stats.equals(null));
143 assertFalse(stats.equals("Hello"));
144 assertFalse(stats.equals(new AxEngineStats(new AxReferenceKey())));
146 assertEquals(0, stats.compareTo(stats));
147 assertEquals(0, stats.compareTo(clonedStats));
148 assertNotEquals(0, stats.compareTo(new AxArtifactKey()));
149 assertNotEquals(0, stats.compareTo(null));
150 assertNotEquals(0, stats.compareTo(new AxEngineStats(new AxReferenceKey())));
152 stats.setTimeStamp(1);
153 assertFalse(stats.equals(new AxEngineStats(statsKey)));
154 assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
155 stats.setTimeStamp(0);
156 assertTrue(stats.equals(new AxEngineStats(statsKey)));
157 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
159 stats.setEventCount(1);
160 assertFalse(stats.equals(new AxEngineStats(statsKey)));
161 assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
162 stats.setEventCount(0);
163 assertTrue(stats.equals(new AxEngineStats(statsKey)));
164 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
166 stats.setLastExecutionTime(1);
167 assertFalse(stats.equals(new AxEngineStats(statsKey)));
168 assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
169 stats.setLastExecutionTime(0);
170 assertTrue(stats.equals(new AxEngineStats(statsKey)));
171 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
173 stats.setAverageExecutionTime(1);
174 assertFalse(stats.equals(new AxEngineStats(statsKey)));
175 assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
176 stats.setAverageExecutionTime(0);
177 assertTrue(stats.equals(new AxEngineStats(statsKey)));
178 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
181 assertFalse(stats.equals(new AxEngineStats(statsKey)));
182 assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
184 assertTrue(stats.equals(new AxEngineStats(statsKey)));
185 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
187 assertEquals(-1, stats.compareTo(new AxEngineStats(statsKey, 0, 0, 0, 0.0, 0, 1)));
190 assertFalse(stats.equals(new AxEngineStats(statsKey)));
191 final AxEngineStats newStats = new AxEngineStats(statsKey);
192 newStats.setTimeStamp(stats.getTimeStamp());
193 assertFalse(stats.equals(newStats));
194 assertNotEquals(0, stats.compareTo(newStats));
197 assertTrue(stats.equals(new AxEngineStats(statsKey)));
198 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));