fb99e265be0d1f0d77247eb48de2a1733173f044
[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 TestEngineStats {
41
42     @Test
43     public void testEngineStats() {
44         assertNotNull(new AxEngineStats());
45         assertNotNull(new AxEngineStats(new AxReferenceKey()));
46
47         final AxReferenceKey statsKey = new AxReferenceKey("EngineKey", "0.0.1", "EngineStats");
48         final AxEngineStats stats = new AxEngineStats(statsKey);
49
50         try {
51             stats.setKey(null);
52             fail("test should throw an exception here");
53         } catch (final Exception e) {
54             assertEquals("key may not be null", e.getMessage());
55         }
56
57         stats.setKey(statsKey);
58         assertEquals("EngineKey:0.0.1:NULL:EngineStats", stats.getKey().getId());
59         assertEquals("EngineKey:0.0.1:NULL:EngineStats", stats.getKeys().get(0).getId());
60
61         stats.setAverageExecutionTime(123.45);
62         assertEquals(new Double(123.45), new Double(stats.getAverageExecutionTime()));
63
64         stats.setEventCount(987);
65         assertEquals(987, stats.getEventCount());
66
67         final long lastExecutionTime = System.currentTimeMillis();
68         stats.setLastExecutionTime(lastExecutionTime);
69         assertEquals(lastExecutionTime, stats.getLastExecutionTime());
70
71         final long timestamp = System.currentTimeMillis();
72         stats.setTimeStamp(timestamp);
73         assertEquals(timestamp, stats.getTimeStamp());
74         assertNotNull(stats.getTimeStampString());
75
76         final long upTime = System.currentTimeMillis() - timestamp;
77         stats.setUpTime(upTime);
78         assertEquals(upTime, stats.getUpTime());
79
80         stats.engineStart();
81         assertTrue(stats.getUpTime() > -1);
82         stats.engineStop();
83         assertTrue(stats.getUpTime() >= 0);
84
85         stats.engineStop();
86
87         stats.reset();
88
89         stats.setEventCount(-2);
90         stats.executionEnter(new AxArtifactKey());
91         assertEquals(2, stats.getEventCount());
92
93         stats.setEventCount(10);
94         stats.executionEnter(new AxArtifactKey());
95         assertEquals(11, stats.getEventCount());
96
97         stats.reset();
98         stats.engineStart();
99         stats.setEventCount(4);
100         stats.executionEnter(new AxArtifactKey());
101         try {
102             Thread.sleep(10);
103         } catch (final Exception e) {
104             fail("test should not throw an exeption");
105         }
106         stats.executionExit();
107         final double avExecutionTime = stats.getAverageExecutionTime();
108         assertTrue(avExecutionTime >= 2.0 && avExecutionTime < 10.0);
109         stats.engineStop();
110
111         AxValidationResult result = new AxValidationResult();
112         result = stats.validate(result);
113         assertEquals(ValidationResult.VALID, result.getValidationResult());
114
115         stats.setKey(new AxReferenceKey());
116         result = new AxValidationResult();
117         result = stats.validate(result);
118         assertEquals(ValidationResult.INVALID, result.getValidationResult());
119
120         stats.setKey(statsKey);
121         result = new AxValidationResult();
122         result = stats.validate(result);
123         assertEquals(ValidationResult.VALID, result.getValidationResult());
124
125         stats.clean();
126         stats.reset();
127
128         final AxEngineStats clonedStats = new AxEngineStats(stats);
129         assertEquals("AxEngineStats:(engineKey=AxReferenceKey:(parentKey", clonedStats.toString().substring(0, 50));
130
131         assertNotNull(stats.getKeys());
132
133         assertFalse(stats.hashCode() == 0);
134
135         assertTrue(stats.equals(stats));
136         assertTrue(stats.equals(clonedStats));
137         assertFalse(stats.equals(null));
138         assertFalse(stats.equals("Hello"));
139         assertFalse(stats.equals(new AxEngineStats(new AxReferenceKey())));
140
141         assertEquals(0, stats.compareTo(stats));
142         assertEquals(0, stats.compareTo(clonedStats));
143         assertNotEquals(0, stats.compareTo(new AxArtifactKey()));
144         assertNotEquals(0, stats.compareTo(null));
145         assertNotEquals(0, stats.compareTo(new AxEngineStats(new AxReferenceKey())));
146
147         stats.setTimeStamp(1);
148         assertFalse(stats.equals(new AxEngineStats(statsKey)));
149         assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
150         stats.setTimeStamp(0);
151         assertTrue(stats.equals(new AxEngineStats(statsKey)));
152         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
153
154         stats.setEventCount(1);
155         assertFalse(stats.equals(new AxEngineStats(statsKey)));
156         assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
157         stats.setEventCount(0);
158         assertTrue(stats.equals(new AxEngineStats(statsKey)));
159         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
160
161         stats.setLastExecutionTime(1);
162         assertFalse(stats.equals(new AxEngineStats(statsKey)));
163         assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
164         stats.setLastExecutionTime(0);
165         assertTrue(stats.equals(new AxEngineStats(statsKey)));
166         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
167
168         stats.setAverageExecutionTime(1);
169         assertFalse(stats.equals(new AxEngineStats(statsKey)));
170         assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
171         stats.setAverageExecutionTime(0);
172         assertTrue(stats.equals(new AxEngineStats(statsKey)));
173         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
174
175         stats.setUpTime(1);
176         assertFalse(stats.equals(new AxEngineStats(statsKey)));
177         assertNotEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
178         stats.setUpTime(0);
179         assertTrue(stats.equals(new AxEngineStats(statsKey)));
180         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
181
182         assertEquals(-1, stats.compareTo(new AxEngineStats(statsKey, 0, 0, 0, 0.0, 0, 1)));
183
184         stats.engineStart();
185         assertFalse(stats.equals(new AxEngineStats(statsKey)));
186         final AxEngineStats newStats = new AxEngineStats(statsKey);
187         newStats.setTimeStamp(stats.getTimeStamp());
188         assertFalse(stats.equals(newStats));
189         assertNotEquals(0, stats.compareTo(newStats));
190         stats.engineStop();
191         stats.reset();
192         assertTrue(stats.equals(new AxEngineStats(statsKey)));
193         assertEquals(0, stats.compareTo(new AxEngineStats(statsKey)));
194     }
195
196 }