392bdd53b78a610c33114a68bad829294cb03b3c
[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.AxKeyInformation;
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 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
37 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
38
39 /**
40  * Test engine models.
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  */
43 public class EngineModelTest {
44
45     @Test
46     public void testEnginetModel() {
47         assertNotNull(new AxEngineModel());
48         assertNotNull(new AxEngineModel(new AxArtifactKey()));
49         assertNotNull(new AxEngineModel(new AxArtifactKey(), new AxContextSchemas(), new AxKeyInformation(),
50                 new AxContextAlbums()));
51         assertNotNull(new AxEngineModel(new AxArtifactKey(), new AxContextSchemas(), new AxKeyInformation(),
52                 new AxContextAlbums(), AxEngineState.UNDEFINED, new AxEngineStats()));
53
54         final AxArtifactKey modelKey = new AxArtifactKey("ModelName", "0.0.1");
55         final AxArtifactKey schemasKey = new AxArtifactKey("SchemasKey", "0.0.1");
56         final AxArtifactKey albumKey = new AxArtifactKey("AlbumKey", "0.0.1");
57         final AxArtifactKey keyInfoKey = new AxArtifactKey("SchemasKey", "0.0.1");
58         final AxEngineStats stats = new AxEngineStats(new AxReferenceKey(modelKey, "EngineStats"));
59         final AxEngineStats otherStats = new AxEngineStats();
60         otherStats.setAverageExecutionTime(100);
61
62         final AxEngineModel model = new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
63                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats);
64         model.register();
65
66         try {
67             model.setKey(null);
68             fail("test should throw an exception here");
69         } catch (final Exception e) {
70             assertEquals("key may not be null", e.getMessage());
71         }
72
73         model.setKey(modelKey);
74         assertEquals("ModelName:0.0.1", model.getKey().getId());
75         assertEquals("ModelName:0.0.1", model.getKeys().get(0).getId());
76
77         final long timestamp = System.currentTimeMillis();
78         model.setTimestamp(timestamp);
79         assertEquals(timestamp, model.getTimestamp());
80         model.setTimestamp(-1);
81         assertTrue(model.getTimeStampString().matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}.\\d{3}"));
82
83         try {
84             model.setState(null);
85             fail("test should throw an exception here");
86         } catch (final Exception e) {
87             assertEquals("state may not be null", e.getMessage());
88         }
89
90         for (final AxEngineState state : AxEngineState.values()) {
91             model.setState(state);
92             assertEquals(state, model.getState());
93         }
94
95         model.setState(AxEngineState.READY);
96         assertEquals(AxEngineState.READY, model.getState());
97
98         try {
99             model.setStats(null);
100             fail("test should throw an exception here");
101         } catch (final Exception e) {
102             assertEquals("stats may not be null", e.getMessage());
103         }
104
105         model.setStats(stats);
106         assertEquals(stats, model.getStats());
107
108         model.clean();
109         assertNotNull(model);
110         assertEquals("AxEngineModel:(AxEngineModel:(AxEngineModel:(key=A", model.toString().substring(0, 50));
111
112         final AxEngineModel clonedModel = new AxEngineModel(model);
113
114         assertFalse(model.hashCode() == 0);
115
116         assertTrue(model.equals(model));
117         assertTrue(model.equals(clonedModel));
118         assertFalse(model.equals("Hello"));
119         assertFalse(model.equals(new AxEngineModel(new AxArtifactKey())));
120         assertFalse(model.equals(new AxEngineModel(new AxArtifactKey(), new AxContextSchemas(schemasKey),
121                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
122         assertFalse(model.equals(new AxEngineModel(modelKey, new AxContextSchemas(), new AxKeyInformation(keyInfoKey),
123                 new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
124         assertFalse(model.equals(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey), new AxKeyInformation(),
125                 new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
126         assertFalse(model.equals(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
127                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(), AxEngineState.READY, stats)));
128         assertFalse(model.equals(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
129                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.STOPPED, stats)));
130         assertFalse(model.equals(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
131                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, otherStats)));
132         model.setTimestamp(timestamp);
133         assertFalse(model.equals(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
134                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
135         model.setTimestamp(0);
136         assertTrue(model.equals(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
137                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
138
139         model.setTimestamp(-1);
140         assertEquals(0, model.compareTo(model));
141         assertEquals(0, model.compareTo(clonedModel));
142         assertNotEquals(0, model.compareTo(new AxArtifactKey()));
143         assertFalse(model.equals(new AxEngineModel(new AxArtifactKey())));
144         assertNotEquals(0, model.compareTo(new AxEngineModel(new AxArtifactKey(), new AxContextSchemas(schemasKey),
145                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
146         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(),
147                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
148         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
149                 new AxKeyInformation(), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
150         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
151                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(), AxEngineState.READY, stats)));
152         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
153                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.STOPPED, stats)));
154         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
155                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, otherStats)));
156         model.setTimestamp(timestamp);
157         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
158                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
159         model.setTimestamp(0);
160         assertEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
161                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
162
163         model.setTimestamp(timestamp);
164         AxValidationResult result = new AxValidationResult();
165         result = model.validate(result);
166         assertEquals(ValidationResult.VALID, result.getValidationResult());
167
168         model.setTimestamp(-1);
169         result = new AxValidationResult();
170         result = model.validate(result);
171         assertEquals(ValidationResult.INVALID, result.getValidationResult());
172
173         model.setTimestamp(timestamp);
174         result = new AxValidationResult();
175         result = model.validate(result);
176         assertEquals(ValidationResult.VALID, result.getValidationResult());
177
178         model.setState(AxEngineState.UNDEFINED);
179         result = new AxValidationResult();
180         result = model.validate(result);
181         assertEquals(ValidationResult.INVALID, result.getValidationResult());
182
183         model.setState(AxEngineState.READY);
184         result = new AxValidationResult();
185         result = model.validate(result);
186         assertEquals(ValidationResult.VALID, result.getValidationResult());
187     }
188 }