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