905a27f382acdd0d5f702c6d9385d59c3383eaa5
[policy/apex-pdp.git] /
1 /*-
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
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.basicmodel.handling;
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
29 import java.util.List;
30 import java.util.Set;
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;
44
45 public class SupportApexBasicModelConceptsTest {
46     TestApexModel<AxModel> testApexModel;
47
48     @Before
49     public void setup() throws Exception {
50         testApexModel = new TestApexModel<AxModel>(AxModel.class, new DummyApexBasicModelCreator());
51     }
52
53     @Test
54     public void testModelConcepts() {
55         final AxModel model = testApexModel.getModel();
56         assertNotNull(model);
57         model.clean();
58         assertNotNull(model);
59
60         AxValidationResult result = new AxValidationResult();
61         result = model.validate(result);
62         assertEquals(ValidationResult.WARNING, result.getValidationResult());
63
64         model.register();
65         assertEquals(model.getKeyInformation(), ModelService.getModel(AxKeyInformation.class));
66
67         final AxModel clonedModel = new AxModel(model);
68         assertTrue(clonedModel.toString().startsWith("AxModel:(key=AxArtifactKey:(name=BasicModel"));
69
70         assertNotEquals(0, model.hashCode());
71
72         // disabling sonar because this code tests the equals() method
73         assertEquals(model, model); // NOSONAR
74         assertEquals(model, clonedModel);
75         assertNotNull(model);
76         assertNotEquals(model, (Object) "Hello");
77         clonedModel.getKey().setVersion("0.0.2");
78         assertNotEquals(model, clonedModel);
79         clonedModel.getKey().setVersion("0.0.1");
80
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");
88
89         assertNotNull(model.getKeys());
90
91         model.getKeyInformation().generateKeyInfo(model);
92         assertNotNull(model.getKeyInformation());
93
94     }
95
96     @Test
97     public void testKeyInformation() {
98
99         final AxModel model = testApexModel.getModel();
100         final AxKeyInformation keyI = model.getKeyInformation();
101         final AxKeyInformation clonedKeyI = new AxKeyInformation(keyI);
102
103         assertNotNull(keyI);
104         assertNotEquals(keyI, (Object) new AxArtifactKey());
105         assertEquals(keyI, clonedKeyI);
106
107         clonedKeyI.setKey(new AxArtifactKey());
108         assertNotEquals(keyI, clonedKeyI);
109         clonedKeyI.setKey(keyI.getKey());
110
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()));
115
116         clonedKeyI.setKey(new AxArtifactKey());
117         assertNotEquals(0, keyI.compareTo(clonedKeyI));
118         clonedKeyI.setKey(keyI.getKey());
119         assertEquals(0, keyI.compareTo(clonedKeyI));
120
121         clonedKeyI.getKeyInfoMap().clear();
122         assertNotEquals(0, keyI.compareTo(clonedKeyI));
123
124         AxKeyInfo keyInfo = keyI.get("BasicModel");
125         assertNotNull(keyInfo);
126
127         keyInfo = keyI.get(new AxArtifactKey("BasicModel", "0.0.1"));
128         assertNotNull(keyInfo);
129
130         Set<AxKeyInfo> keyInfoSet = keyI.getAll("BasicModel");
131         assertNotNull(keyInfoSet);
132
133         keyInfoSet = keyI.getAll("BasicModel", "0..0.1");
134         assertNotNull(keyInfoSet);
135
136         List<AxKey> keys = model.getKeys();
137         assertNotEquals(0, keys.size());
138
139         keys = keyI.getKeys();
140         assertNotEquals(0, keys.size());
141
142         model.getKeyInformation().generateKeyInfo(model);
143         assertNotNull(model.getKeyInformation());
144         model.getKeyInformation().getKeyInfoMap().clear();
145         model.getKeyInformation().generateKeyInfo(model);
146         assertNotNull(model.getKeyInformation());
147     }
148
149     @Test
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();
155
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());
161
162         clonedKeyI.getKeyInfoMap().clear();
163         result = new AxValidationResult();
164         result = clonedKeyI.validate(result);
165         assertEquals(ValidationResult.INVALID, result.getValidationResult());
166         clonedKeyI.generateKeyInfo(model);
167
168         result = new AxValidationResult();
169         result = clonedKeyI.validate(result);
170         assertEquals(ValidationResult.VALID, result.getValidationResult());
171
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);
178
179         result = new AxValidationResult();
180         result = clonedKeyI.validate(result);
181         assertEquals(ValidationResult.VALID, result.getValidationResult());
182
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);
189
190         result = new AxValidationResult();
191         result = clonedKeyI.validate(result);
192         assertEquals(ValidationResult.VALID, result.getValidationResult());
193
194         final AxKeyInfo mk = clonedKeyI.get(new AxArtifactKey("BasicModel", "0.0.1"));
195         assertNotNull(mk);
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);
202
203         result = new AxValidationResult();
204         result = clonedKeyI.validate(result);
205         assertEquals(ValidationResult.VALID, result.getValidationResult());
206
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());
212
213         clonedModel.setKey(model.getKey());
214         result = new AxValidationResult();
215         result = clonedKeyI.validate(result);
216         assertEquals(ValidationResult.VALID, result.getValidationResult());
217     }
218
219     @Test
220     public void testModelConceptsWithReferences() {
221         final DummyAxModelWithReferences mwr = new DummyApexBasicModelCreator().getModelWithReferences();
222         assertNotNull(mwr);
223         mwr.getKeyInformation().getKeyInfoMap().clear();
224         mwr.getKeyInformation().generateKeyInfo(mwr);
225
226         AxValidationResult result = new AxValidationResult();
227         result = mwr.validate(result);
228         assertEquals(ValidationResult.VALID, result.getValidationResult());
229
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());
236
237         result = new AxValidationResult();
238         result = mwr.validate(result);
239         assertEquals(ValidationResult.VALID, result.getValidationResult());
240
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());
247
248         result = new AxValidationResult();
249         result = mwr.validate(result);
250         assertEquals(ValidationResult.VALID, result.getValidationResult());
251
252         // Duplicate Reference Key
253         final AxReferenceKey rKey = new AxReferenceKey(mwr.getKey(), "LocalName");
254         mwr.addKey(rKey);
255         mwr.addKey(rKey);
256         result = new AxValidationResult();
257         result = mwr.validate(result);
258         assertEquals(ValidationResult.INVALID, result.getValidationResult());
259         mwr.removeKey(rKey);
260         mwr.removeKey(rKey);
261
262         result = new AxValidationResult();
263         result = mwr.validate(result);
264         assertEquals(ValidationResult.VALID, result.getValidationResult());
265
266         // Key Use is legal
267         final AxKeyUse keyU = new AxKeyUse(mwr.getKey());
268         mwr.addKey(keyU);
269         result = new AxValidationResult();
270         result = mwr.validate(result);
271         assertEquals(ValidationResult.VALID, result.getValidationResult());
272         mwr.removeKey(keyU);
273
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);
281
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);
289
290         result = new AxValidationResult();
291         result = mwr.validate(result);
292         assertEquals(ValidationResult.VALID, result.getValidationResult());
293     }
294 }