6e4fbbf0529ef68e9835d045f6a359e8f6ad2b77
[policy/apex-pdp.git] / model / engine-model / src / test / java / org / onap / policy / apex / model / enginemodel / concepts / EngineModelTest.java
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.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         assertNotEquals(0, model.hashCode());
115
116         assertEquals(model, model);
117         assertEquals(model, clonedModel);
118         assertNotEquals(model, (Object) "Hello");
119         assertNotEquals(model, new AxEngineModel(new AxArtifactKey()));
120         assertNotEquals(model, new AxEngineModel(new AxArtifactKey(), new AxContextSchemas(schemasKey),
121                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats));
122         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(), new AxKeyInformation(keyInfoKey),
123                 new AxContextAlbums(albumKey), AxEngineState.READY, stats));
124         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey), new AxKeyInformation(),
125                 new AxContextAlbums(albumKey), AxEngineState.READY, stats));
126         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
127                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(), AxEngineState.READY, stats));
128         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
129                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.STOPPED, stats));
130         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
131                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, otherStats));
132         model.setTimestamp(timestamp);
133         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
134                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats));
135         model.setTimestamp(0);
136         assertEquals(model, 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         assertNotEquals(model, 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 }