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