86a8c476395a57dd41c9e6c9dcdeacdc53e9683f
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.enginemodel.concepts;
22
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;
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 engine statistics.
38  * @author Liam Fallon (liam.fallon@ericsson.com)
39  */
40 public class EngineStatsTest {
41     private static final Object WAIT_LOCK = new Object();
42
43     @Test
44     public void testEngineStats() {
45         assertNotNull(new AxEngineStats());
46         assertNotNull(new AxEngineStats(new AxReferenceKey()));
47
48         final AxReferenceKey statsKey = new AxReferenceKey("EngineKey", "0.0.1", "EngineStats");
49         final AxEngineStats stats = new AxEngineStats(statsKey);
50
51         try {
52             stats.setKey(null);
53             fail("test should throw an exception here");
54         } catch (final Exception e) {
55             assertEquals("key may not be null", e.getMessage());
56         }
57
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());
61
62         stats.setAverageExecutionTime(123.45);
63         assertEquals(new Double(123.45), new Double(stats.getAverageExecutionTime()));
64
65         stats.setEventCount(987);
66         assertEquals(987, stats.getEventCount());
67
68         final long lastExecutionTime = System.currentTimeMillis();
69         stats.setLastExecutionTime(lastExecutionTime);
70         assertEquals(lastExecutionTime, stats.getLastExecutionTime());
71
72         final long timestamp = System.currentTimeMillis();
73         stats.setTimeStamp(timestamp);
74         assertEquals(timestamp, stats.getTimeStamp());
75         assertNotNull(stats.getTimeStampString());
76
77         final long upTime = System.currentTimeMillis() - timestamp;
78         stats.setUpTime(upTime);
79         assertEquals(upTime, stats.getUpTime());
80
81         stats.engineStart();
82         assertTrue(stats.getUpTime() > -1);
83         stats.engineStop();
84         assertTrue(stats.getUpTime() >= 0);
85
86         stats.engineStop();
87
88         stats.reset();
89
90         stats.setEventCount(-2);
91         stats.executionEnter(new AxArtifactKey());
92         assertEquals(2, stats.getEventCount());
93
94         stats.setEventCount(10);
95         stats.executionEnter(new AxArtifactKey());
96         assertEquals(11, stats.getEventCount());
97
98         stats.reset();
99         stats.engineStart();
100         stats.setEventCount(4);
101         stats.executionEnter(new AxArtifactKey());
102         
103         synchronized (WAIT_LOCK) {
104             try {
105                 WAIT_LOCK.wait(10);
106             } catch (InterruptedException e) {
107                 fail("test should not throw an exception");
108             }
109         }
110
111         stats.executionExit();
112         final double avExecutionTime = stats.getAverageExecutionTime();
113         assertTrue(avExecutionTime >= 2.0 && avExecutionTime < 10.0);
114         stats.engineStop();
115
116         AxValidationResult result = new AxValidationResult();
117         result = stats.validate(result);
118         assertEquals(ValidationResult.VALID, result.getValidationResult());
119
120         stats.setKey(new AxReferenceKey());
121         result = new AxValidationResult();
122         result = stats.validate(result);
123         assertEquals(ValidationResult.INVALID, result.getValidationResult());
124
125         stats.setKey(statsKey);
126         result = new AxValidationResult();
127         result = stats.validate(result);
128         assertEquals(ValidationResult.VALID, result.getValidationResult());
129
130         stats.clean();
131         stats.reset();
132
133         final AxEngineStats clonedStats = new AxEngineStats(stats);
134         assertEquals("AxEngineStats:(engineKey=AxReferenceKey:(parentKey", clonedStats.toString().substring(0, 50));
135
136         assertNotNull(stats.getKeys());
137
138         assertFalse(stats.hashCode() == 0);
139
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())));
145
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())));
151
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)));
158
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)));
165
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)));
172
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)));
179
180         stats.setUpTime(1);
181         assertFalse(stats.equals(new AxEngineStats(statsKey)));
182         assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
183         stats.setUpTime(0);
184         assertTrue(stats.equals(new AxEngineStats(statsKey)));
185         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
186
187         assertEquals(-1, stats.compareTo(new AxEngineStats(statsKey, 0, 0, 0, 0.0, 0, 1)));
188
189         stats.engineStart();
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));
195         stats.engineStop();
196         stats.reset();
197         assertTrue(stats.equals(new AxEngineStats(statsKey)));
198         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
199     }
200
201 }