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.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 SupportApexBasicModelConceptsTester {
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 assertEquals(model, model);
73 assertEquals(model, clonedModel);
75 assertNotEquals(model, (Object) "Hello");
76 clonedModel.getKey().setVersion("0.0.2");
77 assertNotEquals(model, clonedModel);
78 clonedModel.getKey().setVersion("0.0.1");
80 assertEquals(0, model.compareTo(model));
81 assertNotEquals(0, model.compareTo(null));
82 assertNotEquals(0, model.compareTo(new AxReferenceKey()));
83 assertEquals(0, model.compareTo(clonedModel));
84 clonedModel.getKey().setVersion("0.0.2");
85 assertNotEquals(0, model.compareTo(clonedModel));
86 clonedModel.getKey().setVersion("0.0.1");
88 assertNotNull(model.getKeys());
90 model.getKeyInformation().generateKeyInfo(model);
91 assertNotNull(model.getKeyInformation());
93 final AxKeyInformation keyI = model.getKeyInformation();
94 final AxKeyInformation clonedKeyI = new AxKeyInformation(keyI);
97 assertNotEquals(keyI, (Object) new AxArtifactKey());
98 assertEquals(keyI, clonedKeyI);
100 clonedKeyI.setKey(new AxArtifactKey());
101 assertNotEquals(keyI, clonedKeyI);
102 clonedKeyI.setKey(keyI.getKey());
104 assertEquals(0, keyI.compareTo(keyI));
105 assertEquals(0, keyI.compareTo(clonedKeyI));
106 assertNotEquals(0, keyI.compareTo(null));
107 assertNotEquals(0, keyI.compareTo(new AxArtifactKey()));
109 clonedKeyI.setKey(new AxArtifactKey());
110 assertNotEquals(0, keyI.compareTo(clonedKeyI));
111 clonedKeyI.setKey(keyI.getKey());
112 assertEquals(0, keyI.compareTo(clonedKeyI));
114 clonedKeyI.getKeyInfoMap().clear();
115 assertNotEquals(0, keyI.compareTo(clonedKeyI));
117 AxKeyInfo keyInfo = keyI.get("BasicModel");
118 assertNotNull(keyInfo);
120 keyInfo = keyI.get(new AxArtifactKey("BasicModel", "0.0.1"));
121 assertNotNull(keyInfo);
123 Set<AxKeyInfo> keyInfoSet = keyI.getAll("BasicModel");
124 assertNotNull(keyInfoSet);
126 keyInfoSet = keyI.getAll("BasicModel", "0..0.1");
127 assertNotNull(keyInfoSet);
129 List<AxKey> keys = model.getKeys();
130 assertNotEquals(0, keys.size());
132 keys = keyI.getKeys();
133 assertNotEquals(0, keys.size());
135 model.getKeyInformation().generateKeyInfo(model);
136 assertNotNull(model.getKeyInformation());
137 model.getKeyInformation().getKeyInfoMap().clear();
138 model.getKeyInformation().generateKeyInfo(model);
139 assertNotNull(model.getKeyInformation());
141 clonedKeyI.setKey(AxArtifactKey.getNullKey());
142 result = new AxValidationResult();
143 result = clonedKeyI.validate(result);
144 assertEquals(ValidationResult.INVALID, result.getValidationResult());
145 clonedKeyI.setKey(keyI.getKey());
147 clonedKeyI.getKeyInfoMap().clear();
148 result = new AxValidationResult();
149 result = clonedKeyI.validate(result);
150 assertEquals(ValidationResult.INVALID, result.getValidationResult());
151 clonedKeyI.generateKeyInfo(model);
153 result = new AxValidationResult();
154 result = clonedKeyI.validate(result);
155 assertEquals(ValidationResult.VALID, result.getValidationResult());
157 clonedKeyI.getKeyInfoMap().put(AxArtifactKey.getNullKey(), null);
158 result = new AxValidationResult();
159 result = clonedKeyI.validate(result);
160 assertEquals(ValidationResult.INVALID, result.getValidationResult());
161 clonedKeyI.getKeyInfoMap().clear();
162 clonedKeyI.generateKeyInfo(model);
164 result = new AxValidationResult();
165 result = clonedKeyI.validate(result);
166 assertEquals(ValidationResult.VALID, result.getValidationResult());
168 clonedKeyI.getKeyInfoMap().put(new AxArtifactKey("SomeKey", "0.0.1"), null);
169 result = new AxValidationResult();
170 result = clonedKeyI.validate(result);
171 assertEquals(ValidationResult.INVALID, result.getValidationResult());
172 clonedKeyI.getKeyInfoMap().clear();
173 clonedKeyI.generateKeyInfo(model);
175 result = new AxValidationResult();
176 result = clonedKeyI.validate(result);
177 assertEquals(ValidationResult.VALID, result.getValidationResult());
179 final AxKeyInfo mk = clonedKeyI.get(new AxArtifactKey("BasicModel", "0.0.1"));
181 mk.setKey(AxArtifactKey.getNullKey());
182 result = new AxValidationResult();
183 result = clonedKeyI.validate(result);
184 assertEquals(ValidationResult.INVALID, result.getValidationResult());
185 clonedKeyI.getKeyInfoMap().clear();
186 clonedKeyI.generateKeyInfo(model);
188 result = new AxValidationResult();
189 result = clonedKeyI.validate(result);
190 assertEquals(ValidationResult.VALID, result.getValidationResult());
192 clonedModel.setKey(AxArtifactKey.getNullKey());
193 result = new AxValidationResult();
194 result = clonedModel.validate(result);
195 assertEquals(ValidationResult.INVALID, result.getValidationResult());
197 clonedModel.setKey(model.getKey());
198 result = new AxValidationResult();
199 result = clonedKeyI.validate(result);
200 assertEquals(ValidationResult.VALID, result.getValidationResult());
204 public void testModelConceptsWithReferences() {
205 final DummyAxModelWithReferences mwr = new DummyApexBasicModelCreator().getModelWithReferences();
207 mwr.getKeyInformation().getKeyInfoMap().clear();
208 mwr.getKeyInformation().generateKeyInfo(mwr);
210 AxValidationResult result = new AxValidationResult();
211 result = mwr.validate(result);
212 assertEquals(ValidationResult.VALID, result.getValidationResult());
214 // Duplicate key error
215 mwr.addKey(mwr.getKey());
216 result = new AxValidationResult();
217 result = mwr.validate(result);
218 assertEquals(ValidationResult.INVALID, result.getValidationResult());
219 mwr.removeKey(mwr.getKey());
221 result = new AxValidationResult();
222 result = mwr.validate(result);
223 assertEquals(ValidationResult.VALID, result.getValidationResult());
225 // Null Reference Key
226 mwr.addKey(AxReferenceKey.getNullKey());
227 result = new AxValidationResult();
228 result = mwr.validate(result);
229 assertEquals(ValidationResult.INVALID, result.getValidationResult());
230 mwr.removeKey(AxReferenceKey.getNullKey());
232 result = new AxValidationResult();
233 result = mwr.validate(result);
234 assertEquals(ValidationResult.VALID, result.getValidationResult());
236 // Duplicate Reference Key
237 final AxReferenceKey rKey = new AxReferenceKey(mwr.getKey(), "LocalName");
240 result = new AxValidationResult();
241 result = mwr.validate(result);
242 assertEquals(ValidationResult.INVALID, result.getValidationResult());
246 result = new AxValidationResult();
247 result = mwr.validate(result);
248 assertEquals(ValidationResult.VALID, result.getValidationResult());
251 final AxKeyUse keyU = new AxKeyUse(mwr.getKey());
253 result = new AxValidationResult();
254 result = mwr.validate(result);
255 assertEquals(ValidationResult.VALID, result.getValidationResult());
258 // Key Use on bad artifact key
259 final AxKeyUse keyBadUsage = new AxKeyUse(new AxArtifactKey("SomeKey", "0.0.1"));
260 mwr.addKey(keyBadUsage);
261 result = new AxValidationResult();
262 result = mwr.validate(result);
263 assertEquals(ValidationResult.INVALID, result.getValidationResult());
264 mwr.removeKey(keyBadUsage);
266 // Key Use on bad reference key
267 final AxKeyUse keyBadReferenceUsage = new AxKeyUse(new AxReferenceKey("SomeKey", "0.0.1", "Local"));
268 mwr.addKey(keyBadReferenceUsage);
269 result = new AxValidationResult();
270 result = mwr.validate(result);
271 assertEquals(ValidationResult.INVALID, result.getValidationResult());
272 mwr.removeKey(keyBadReferenceUsage);
274 result = new AxValidationResult();
275 result = mwr.validate(result);
276 assertEquals(ValidationResult.VALID, result.getValidationResult());