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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.enginemodel.concepts;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
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;
41 * @author Liam Fallon (liam.fallon@ericsson.com)
43 public class EngineModelTest {
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()));
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);
62 final AxEngineModel model = new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
63 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats);
66 assertThatThrownBy(() -> model.setKey(null))
67 .hasMessage("key may not be null");
68 model.setKey(modelKey);
69 assertEquals("ModelName:0.0.1", model.getKey().getId());
70 assertEquals("ModelName:0.0.1", model.getKeys().get(0).getId());
72 final long timestamp = System.currentTimeMillis();
73 model.setTimestamp(timestamp);
74 assertEquals(timestamp, model.getTimestamp());
75 model.setTimestamp(-1);
76 assertTrue(model.getTimeStampString().matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}.\\d{3}"));
78 assertThatThrownBy(() -> model.setState(null))
79 .hasMessage("state may not be null");
80 for (final AxEngineState state : AxEngineState.values()) {
81 model.setState(state);
82 assertEquals(state, model.getState());
85 model.setState(AxEngineState.READY);
86 assertEquals(AxEngineState.READY, model.getState());
88 assertThatThrownBy(() -> model.setStats(null))
89 .hasMessage("stats may not be null");
90 model.setStats(stats);
91 assertEquals(stats, model.getStats());
95 assertEquals("AxEngineModel:(AxEngineModel:(AxEngineModel:(key=A", model.toString().substring(0, 50));
97 final AxEngineModel clonedModel = new AxEngineModel(model);
99 assertNotEquals(0, model.hashCode());
101 // disabling sonar because this code tests the equals() method
102 assertEquals(model, model); // NOSONAR
103 assertEquals(model, clonedModel);
104 assertNotEquals(model, "Hello");
105 assertNotEquals(model, new AxEngineModel(new AxArtifactKey()));
106 assertNotEquals(model, new AxEngineModel(new AxArtifactKey(), new AxContextSchemas(schemasKey),
107 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats));
108 assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(), new AxKeyInformation(keyInfoKey),
109 new AxContextAlbums(albumKey), AxEngineState.READY, stats));
110 assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey), new AxKeyInformation(),
111 new AxContextAlbums(albumKey), AxEngineState.READY, stats));
112 assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
113 new AxKeyInformation(keyInfoKey), new AxContextAlbums(), AxEngineState.READY, stats));
114 assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
115 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.STOPPED, stats));
116 assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
117 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, otherStats));
118 model.setTimestamp(timestamp);
119 assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
120 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats));
121 model.setTimestamp(0);
122 assertEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
123 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats));
125 model.setTimestamp(-1);
126 assertEquals(0, model.compareTo(model));
127 assertEquals(0, model.compareTo(clonedModel));
128 assertNotEquals(0, model.compareTo(new AxArtifactKey()));
129 assertNotEquals(model, new AxEngineModel(new AxArtifactKey()));
130 assertNotEquals(0, model.compareTo(new AxEngineModel(new AxArtifactKey(), new AxContextSchemas(schemasKey),
131 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
132 assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(),
133 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
134 assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
135 new AxKeyInformation(), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
136 assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
137 new AxKeyInformation(keyInfoKey), new AxContextAlbums(), AxEngineState.READY, stats)));
138 assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
139 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.STOPPED, stats)));
140 assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
141 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, otherStats)));
142 model.setTimestamp(timestamp);
143 assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
144 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
145 model.setTimestamp(0);
146 assertEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
147 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
149 model.setTimestamp(timestamp);
150 AxValidationResult result = new AxValidationResult();
151 result = model.validate(result);
152 assertEquals(ValidationResult.VALID, result.getValidationResult());
154 model.setTimestamp(-1);
155 result = new AxValidationResult();
156 result = model.validate(result);
157 assertEquals(ValidationResult.INVALID, result.getValidationResult());
159 model.setTimestamp(timestamp);
160 result = new AxValidationResult();
161 result = model.validate(result);
162 assertEquals(ValidationResult.VALID, result.getValidationResult());
164 model.setState(AxEngineState.UNDEFINED);
165 result = new AxValidationResult();
166 result = model.validate(result);
167 assertEquals(ValidationResult.INVALID, result.getValidationResult());
169 model.setState(AxEngineState.READY);
170 result = new AxValidationResult();
171 result = model.validate(result);
172 assertEquals(ValidationResult.VALID, result.getValidationResult());