1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2016-2018 Ericsson. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.policy.apex.model.enginemodel.concepts;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
/**
* @author Liam Fallon (liam.fallon@ericsson.com)
*/
public class TestEngineStats {
@Test
public void testEngineStats() {
assertNotNull(new AxEngineStats());
assertNotNull(new AxEngineStats(new AxReferenceKey()));
final AxReferenceKey statsKey = new AxReferenceKey("EngineKey", "0.0.1", "EngineStats");
final AxEngineStats stats = new AxEngineStats(statsKey);
try {
stats.setKey(null);
fail("test should throw an exception here");
} catch (final Exception e) {
assertEquals("key may not be null", e.getMessage());
}
stats.setKey(statsKey);
assertEquals("EngineKey:0.0.1:NULL:EngineStats", stats.getKey().getID());
assertEquals("EngineKey:0.0.1:NULL:EngineStats", stats.getKeys().get(0).getID());
stats.setAverageExecutionTime(123.45);
assertEquals(new Double(123.45), new Double(stats.getAverageExecutionTime()));
stats.setEventCount(987);
assertEquals(987, stats.getEventCount());
final long lastExecutionTime = System.currentTimeMillis();
stats.setLastExecutionTime(lastExecutionTime);
assertEquals(lastExecutionTime, stats.getLastExecutionTime());
final long timestamp = System.currentTimeMillis();
stats.setTimeStamp(timestamp);
assertEquals(timestamp, stats.getTimeStamp());
assertNotNull(stats.getTimeStampString());
final long upTime = System.currentTimeMillis() - timestamp;
stats.setUpTime(upTime);
assertEquals(upTime, stats.getUpTime());
stats.engineStart();
assertTrue(stats.getUpTime() > -1);
stats.engineStop();
assertTrue(stats.getUpTime() >= 0);
stats.engineStop();
stats.reset();
stats.setEventCount(-2);
stats.executionEnter(new AxArtifactKey());
assertEquals(2, stats.getEventCount());
stats.setEventCount(10);
stats.executionEnter(new AxArtifactKey());
assertEquals(11, stats.getEventCount());
stats.reset();
stats.engineStart();
stats.setEventCount(4);
stats.executionEnter(new AxArtifactKey());
try {
Thread.sleep(10);
} catch (final Exception e) {
fail("test should not throw an exeption");
}
stats.executionExit();
final double avExecutionTime = stats.getAverageExecutionTime();
assertTrue(avExecutionTime >= 2.0 && avExecutionTime < 3.0);
stats.engineStop();
AxValidationResult result = new AxValidationResult();
result = stats.validate(result);
assertEquals(ValidationResult.VALID, result.getValidationResult());
stats.setKey(new AxReferenceKey());
result = new AxValidationResult();
result = stats.validate(result);
assertEquals(ValidationResult.INVALID, result.getValidationResult());
stats.setKey(statsKey);
result = new AxValidationResult();
result = stats.validate(result);
assertEquals(ValidationResult.VALID, result.getValidationResult());
stats.clean();
stats.reset();
final AxEngineStats clonedStats = new AxEngineStats(stats);
assertEquals("AxEngineStats:(engineKey=AxReferenceKey:(parentKey", clonedStats.toString().substring(0, 50));
assertNotNull(stats.getKeys());
assertFalse(stats.hashCode() == 0);
assertTrue(stats.equals(stats));
assertTrue(stats.equals(clonedStats));
assertFalse(stats.equals(null));
assertFalse(stats.equals("Hello"));
assertFalse(stats.equals(new AxEngineStats(new AxReferenceKey())));
assertEquals(0, stats.compareTo(stats));
assertEquals(0, stats.compareTo(clonedStats));
assertNotEquals(0, stats.compareTo(new AxArtifactKey()));
assertNotEquals(0, stats.compareTo(null));
assertNotEquals(0, stats.compareTo(new AxEngineStats(new AxReferenceKey())));
stats.setTimeStamp(1);
assertFalse(stats.equals(new AxEngineStats(statsKey)));
assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
stats.setTimeStamp(0);
assertTrue(stats.equals(new AxEngineStats(statsKey)));
assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
stats.setEventCount(1);
assertFalse(stats.equals(new AxEngineStats(statsKey)));
assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
stats.setEventCount(0);
assertTrue(stats.equals(new AxEngineStats(statsKey)));
assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
stats.setLastExecutionTime(1);
assertFalse(stats.equals(new AxEngineStats(statsKey)));
assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
stats.setLastExecutionTime(0);
assertTrue(stats.equals(new AxEngineStats(statsKey)));
assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
stats.setAverageExecutionTime(1);
assertFalse(stats.equals(new AxEngineStats(statsKey)));
assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
stats.setAverageExecutionTime(0);
assertTrue(stats.equals(new AxEngineStats(statsKey)));
assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
stats.setUpTime(1);
assertFalse(stats.equals(new AxEngineStats(statsKey)));
assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
stats.setUpTime(0);
assertTrue(stats.equals(new AxEngineStats(statsKey)));
assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
assertEquals(-1, stats.compareTo(new AxEngineStats(statsKey, 0, 0, 0, 0.0, 0, 1)));
stats.engineStart();
assertFalse(stats.equals(new AxEngineStats(statsKey)));
final AxEngineStats newStats = new AxEngineStats(statsKey);
newStats.setTimeStamp(stats.getTimeStamp());
assertFalse(stats.equals(newStats));
assertNotEquals(0, stats.compareTo(newStats));
stats.engineStop();
stats.reset();
assertTrue(stats.equals(new AxEngineStats(statsKey)));
assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
}
}
|