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