2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2021 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.basicmodel.handling;
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;
29 import java.util.List;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
42 import org.onap.policy.apex.model.basicmodel.service.ModelService;
43 import org.onap.policy.apex.model.basicmodel.test.TestApexModel;
45 public class SupportApexBasicModelConceptsTest {
46 TestApexModel<AxModel> testApexModel;
49 public void setup() throws Exception {
50 testApexModel = new TestApexModel<AxModel>(AxModel.class, new DummyApexBasicModelCreator());
54 public void testModelConcepts() {
55 final AxModel model = testApexModel.getModel();
60 AxValidationResult result = new AxValidationResult();
61 result = model.validate(result);
62 assertEquals(ValidationResult.WARNING, result.getValidationResult());
65 assertEquals(model.getKeyInformation(), ModelService.getModel(AxKeyInformation.class));
67 final AxModel clonedModel = new AxModel(model);
68 assertTrue(clonedModel.toString().startsWith("AxModel:(key=AxArtifactKey:(name=BasicModel"));
70 assertNotEquals(0, model.hashCode());
72 // disabling sonar because this code tests the equals() method
73 assertEquals(model, model); // NOSONAR
74 assertEquals(model, clonedModel);
76 assertNotEquals(model, (Object) "Hello");
77 clonedModel.getKey().setVersion("0.0.2");
78 assertNotEquals(model, clonedModel);
79 clonedModel.getKey().setVersion("0.0.1");
81 assertEquals(0, model.compareTo(model));
82 assertNotEquals(0, model.compareTo(null));
83 assertNotEquals(0, model.compareTo(new AxReferenceKey()));
84 assertEquals(0, model.compareTo(clonedModel));
85 clonedModel.getKey().setVersion("0.0.2");
86 assertNotEquals(0, model.compareTo(clonedModel));
87 clonedModel.getKey().setVersion("0.0.1");
89 assertNotNull(model.getKeys());
91 model.getKeyInformation().generateKeyInfo(model);
92 assertNotNull(model.getKeyInformation());
97 public void testKeyInformation() {
99 final AxModel model = testApexModel.getModel();
100 final AxKeyInformation keyI = model.getKeyInformation();
101 final AxKeyInformation clonedKeyI = new AxKeyInformation(keyI);
104 assertNotEquals(keyI, (Object) new AxArtifactKey());
105 assertEquals(keyI, clonedKeyI);
107 clonedKeyI.setKey(new AxArtifactKey());
108 assertNotEquals(keyI, clonedKeyI);
109 clonedKeyI.setKey(keyI.getKey());
111 assertEquals(0, keyI.compareTo(keyI));
112 assertEquals(0, keyI.compareTo(clonedKeyI));
113 assertNotEquals(0, keyI.compareTo(null));
114 assertNotEquals(0, keyI.compareTo(new AxArtifactKey()));
116 clonedKeyI.setKey(new AxArtifactKey());
117 assertNotEquals(0, keyI.compareTo(clonedKeyI));
118 clonedKeyI.setKey(keyI.getKey());
119 assertEquals(0, keyI.compareTo(clonedKeyI));
121 clonedKeyI.getKeyInfoMap().clear();
122 assertNotEquals(0, keyI.compareTo(clonedKeyI));
124 AxKeyInfo keyInfo = keyI.get("BasicModel");
125 assertNotNull(keyInfo);
127 keyInfo = keyI.get(new AxArtifactKey("BasicModel", "0.0.1"));
128 assertNotNull(keyInfo);
130 Set<AxKeyInfo> keyInfoSet = keyI.getAll("BasicModel");
131 assertNotNull(keyInfoSet);
133 keyInfoSet = keyI.getAll("BasicModel", "0..0.1");
134 assertNotNull(keyInfoSet);
136 List<AxKey> keys = model.getKeys();
137 assertNotEquals(0, keys.size());
139 keys = keyI.getKeys();
140 assertNotEquals(0, keys.size());
142 model.getKeyInformation().generateKeyInfo(model);
143 assertNotNull(model.getKeyInformation());
144 model.getKeyInformation().getKeyInfoMap().clear();
145 model.getKeyInformation().generateKeyInfo(model);
146 assertNotNull(model.getKeyInformation());
150 public void testClonedKey() {
151 final AxModel model = testApexModel.getModel();
152 final AxKeyInformation keyI = model.getKeyInformation();
153 final AxKeyInformation clonedKeyI = new AxKeyInformation(keyI);
154 AxValidationResult result = new AxValidationResult();
156 clonedKeyI.setKey(AxArtifactKey.getNullKey());
157 result = new AxValidationResult();
158 result = clonedKeyI.validate(result);
159 assertEquals(ValidationResult.INVALID, result.getValidationResult());
160 clonedKeyI.setKey(keyI.getKey());
162 clonedKeyI.getKeyInfoMap().clear();
163 result = new AxValidationResult();
164 result = clonedKeyI.validate(result);
165 assertEquals(ValidationResult.INVALID, result.getValidationResult());
166 clonedKeyI.generateKeyInfo(model);
168 result = new AxValidationResult();
169 result = clonedKeyI.validate(result);
170 assertEquals(ValidationResult.VALID, result.getValidationResult());
172 clonedKeyI.getKeyInfoMap().put(AxArtifactKey.getNullKey(), null);
173 result = new AxValidationResult();
174 result = clonedKeyI.validate(result);
175 assertEquals(ValidationResult.INVALID, result.getValidationResult());
176 clonedKeyI.getKeyInfoMap().clear();
177 clonedKeyI.generateKeyInfo(model);
179 result = new AxValidationResult();
180 result = clonedKeyI.validate(result);
181 assertEquals(ValidationResult.VALID, result.getValidationResult());
183 clonedKeyI.getKeyInfoMap().put(new AxArtifactKey("SomeKey", "0.0.1"), null);
184 result = new AxValidationResult();
185 result = clonedKeyI.validate(result);
186 assertEquals(ValidationResult.INVALID, result.getValidationResult());
187 clonedKeyI.getKeyInfoMap().clear();
188 clonedKeyI.generateKeyInfo(model);
190 result = new AxValidationResult();
191 result = clonedKeyI.validate(result);
192 assertEquals(ValidationResult.VALID, result.getValidationResult());
194 final AxKeyInfo mk = clonedKeyI.get(new AxArtifactKey("BasicModel", "0.0.1"));
196 mk.setKey(AxArtifactKey.getNullKey());
197 result = new AxValidationResult();
198 result = clonedKeyI.validate(result);
199 assertEquals(ValidationResult.INVALID, result.getValidationResult());
200 clonedKeyI.getKeyInfoMap().clear();
201 clonedKeyI.generateKeyInfo(model);
203 result = new AxValidationResult();
204 result = clonedKeyI.validate(result);
205 assertEquals(ValidationResult.VALID, result.getValidationResult());
207 final AxModel clonedModel = new AxModel(model);
208 clonedModel.setKey(AxArtifactKey.getNullKey());
209 result = new AxValidationResult();
210 result = clonedModel.validate(result);
211 assertEquals(ValidationResult.INVALID, result.getValidationResult());
213 clonedModel.setKey(model.getKey());
214 result = new AxValidationResult();
215 result = clonedKeyI.validate(result);
216 assertEquals(ValidationResult.VALID, result.getValidationResult());
220 public void testModelConceptsWithReferences() {
221 final DummyAxModelWithReferences mwr = new DummyApexBasicModelCreator().getModelWithReferences();
223 mwr.getKeyInformation().getKeyInfoMap().clear();
224 mwr.getKeyInformation().generateKeyInfo(mwr);
226 AxValidationResult result = new AxValidationResult();
227 result = mwr.validate(result);
228 assertEquals(ValidationResult.VALID, result.getValidationResult());
230 // Duplicate key error
231 mwr.addKey(mwr.getKey());
232 result = new AxValidationResult();
233 result = mwr.validate(result);
234 assertEquals(ValidationResult.INVALID, result.getValidationResult());
235 mwr.removeKey(mwr.getKey());
237 result = new AxValidationResult();
238 result = mwr.validate(result);
239 assertEquals(ValidationResult.VALID, result.getValidationResult());
241 // Null Reference Key
242 mwr.addKey(AxReferenceKey.getNullKey());
243 result = new AxValidationResult();
244 result = mwr.validate(result);
245 assertEquals(ValidationResult.INVALID, result.getValidationResult());
246 mwr.removeKey(AxReferenceKey.getNullKey());
248 result = new AxValidationResult();
249 result = mwr.validate(result);
250 assertEquals(ValidationResult.VALID, result.getValidationResult());
252 // Duplicate Reference Key
253 final AxReferenceKey rKey = new AxReferenceKey(mwr.getKey(), "LocalName");
256 result = new AxValidationResult();
257 result = mwr.validate(result);
258 assertEquals(ValidationResult.INVALID, result.getValidationResult());
262 result = new AxValidationResult();
263 result = mwr.validate(result);
264 assertEquals(ValidationResult.VALID, result.getValidationResult());
267 final AxKeyUse keyU = new AxKeyUse(mwr.getKey());
269 result = new AxValidationResult();
270 result = mwr.validate(result);
271 assertEquals(ValidationResult.VALID, result.getValidationResult());
274 // Key Use on bad artifact key
275 final AxKeyUse keyBadUsage = new AxKeyUse(new AxArtifactKey("SomeKey", "0.0.1"));
276 mwr.addKey(keyBadUsage);
277 result = new AxValidationResult();
278 result = mwr.validate(result);
279 assertEquals(ValidationResult.INVALID, result.getValidationResult());
280 mwr.removeKey(keyBadUsage);
282 // Key Use on bad reference key
283 final AxKeyUse keyBadReferenceUsage = new AxKeyUse(new AxReferenceKey("SomeKey", "0.0.1", "Local"));
284 mwr.addKey(keyBadReferenceUsage);
285 result = new AxValidationResult();
286 result = mwr.validate(result);
287 assertEquals(ValidationResult.INVALID, result.getValidationResult());
288 mwr.removeKey(keyBadReferenceUsage);
290 result = new AxValidationResult();
291 result = mwr.validate(result);
292 assertEquals(ValidationResult.VALID, result.getValidationResult());