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
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.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 the 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(Double.valueOf(123.45), Double.valueOf(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 assertNotEquals(0, stats.hashCode());
141 assertEquals(stats, stats);
142 assertEquals(stats, clonedStats);
143 assertNotNull(stats);
145 Object helloObject = "Hello";
146 assertNotEquals(stats, helloObject);
147 assertNotEquals(stats, new AxEngineStats(new AxReferenceKey()));
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())));
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)));
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)));
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)));
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)));
184 assertNotEquals(stats, new AxEngineStats(statsKey));
185 assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
187 assertEquals(stats, new AxEngineStats(statsKey));
188 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
190 assertEquals(-1, stats.compareTo(new AxEngineStats(statsKey, 0, 0, 0, 0.0, 0, 1)));
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));
200 assertEquals(stats, new AxEngineStats(statsKey));
201 assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));