8ce072c684fa64fbf2d6b4e47cd3720e7ec1569c
[policy/apex-pdp.git] /
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.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 SupportApexBasicModelConceptsTester {
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         final AxKeyInformation keyI = model.getKeyInformation();
95         final AxKeyInformation clonedKeyI = new AxKeyInformation(keyI);
96
97         assertNotNull(keyI);
98         assertNotEquals(keyI, (Object) new AxArtifactKey());
99         assertEquals(keyI, clonedKeyI);
100
101         clonedKeyI.setKey(new AxArtifactKey());
102         assertNotEquals(keyI, clonedKeyI);
103         clonedKeyI.setKey(keyI.getKey());
104
105         assertEquals(0, keyI.compareTo(keyI));
106         assertEquals(0, keyI.compareTo(clonedKeyI));
107         assertNotEquals(0, keyI.compareTo(null));
108         assertNotEquals(0, keyI.compareTo(new AxArtifactKey()));
109
110         clonedKeyI.setKey(new AxArtifactKey());
111         assertNotEquals(0, keyI.compareTo(clonedKeyI));
112         clonedKeyI.setKey(keyI.getKey());
113         assertEquals(0, keyI.compareTo(clonedKeyI));
114
115         clonedKeyI.getKeyInfoMap().clear();
116         assertNotEquals(0, keyI.compareTo(clonedKeyI));
117
118         AxKeyInfo keyInfo = keyI.get("BasicModel");
119         assertNotNull(keyInfo);
120
121         keyInfo = keyI.get(new AxArtifactKey("BasicModel", "0.0.1"));
122         assertNotNull(keyInfo);
123
124         Set<AxKeyInfo> keyInfoSet = keyI.getAll("BasicModel");
125         assertNotNull(keyInfoSet);
126
127         keyInfoSet = keyI.getAll("BasicModel", "0..0.1");
128         assertNotNull(keyInfoSet);
129
130         List<AxKey> keys = model.getKeys();
131         assertNotEquals(0, keys.size());
132
133         keys = keyI.getKeys();
134         assertNotEquals(0, keys.size());
135
136         model.getKeyInformation().generateKeyInfo(model);
137         assertNotNull(model.getKeyInformation());
138         model.getKeyInformation().getKeyInfoMap().clear();
139         model.getKeyInformation().generateKeyInfo(model);
140         assertNotNull(model.getKeyInformation());
141
142         clonedKeyI.setKey(AxArtifactKey.getNullKey());
143         result = new AxValidationResult();
144         result = clonedKeyI.validate(result);
145         assertEquals(ValidationResult.INVALID, result.getValidationResult());
146         clonedKeyI.setKey(keyI.getKey());
147
148         clonedKeyI.getKeyInfoMap().clear();
149         result = new AxValidationResult();
150         result = clonedKeyI.validate(result);
151         assertEquals(ValidationResult.INVALID, result.getValidationResult());
152         clonedKeyI.generateKeyInfo(model);
153
154         result = new AxValidationResult();
155         result = clonedKeyI.validate(result);
156         assertEquals(ValidationResult.VALID, result.getValidationResult());
157
158         clonedKeyI.getKeyInfoMap().put(AxArtifactKey.getNullKey(), null);
159         result = new AxValidationResult();
160         result = clonedKeyI.validate(result);
161         assertEquals(ValidationResult.INVALID, result.getValidationResult());
162         clonedKeyI.getKeyInfoMap().clear();
163         clonedKeyI.generateKeyInfo(model);
164
165         result = new AxValidationResult();
166         result = clonedKeyI.validate(result);
167         assertEquals(ValidationResult.VALID, result.getValidationResult());
168
169         clonedKeyI.getKeyInfoMap().put(new AxArtifactKey("SomeKey", "0.0.1"), null);
170         result = new AxValidationResult();
171         result = clonedKeyI.validate(result);
172         assertEquals(ValidationResult.INVALID, result.getValidationResult());
173         clonedKeyI.getKeyInfoMap().clear();
174         clonedKeyI.generateKeyInfo(model);
175
176         result = new AxValidationResult();
177         result = clonedKeyI.validate(result);
178         assertEquals(ValidationResult.VALID, result.getValidationResult());
179
180         final AxKeyInfo mk = clonedKeyI.get(new AxArtifactKey("BasicModel", "0.0.1"));
181         assertNotNull(mk);
182         mk.setKey(AxArtifactKey.getNullKey());
183         result = new AxValidationResult();
184         result = clonedKeyI.validate(result);
185         assertEquals(ValidationResult.INVALID, result.getValidationResult());
186         clonedKeyI.getKeyInfoMap().clear();
187         clonedKeyI.generateKeyInfo(model);
188
189         result = new AxValidationResult();
190         result = clonedKeyI.validate(result);
191         assertEquals(ValidationResult.VALID, result.getValidationResult());
192
193         clonedModel.setKey(AxArtifactKey.getNullKey());
194         result = new AxValidationResult();
195         result = clonedModel.validate(result);
196         assertEquals(ValidationResult.INVALID, result.getValidationResult());
197
198         clonedModel.setKey(model.getKey());
199         result = new AxValidationResult();
200         result = clonedKeyI.validate(result);
201         assertEquals(ValidationResult.VALID, result.getValidationResult());
202     }
203
204     @Test
205     public void testModelConceptsWithReferences() {
206         final DummyAxModelWithReferences mwr = new DummyApexBasicModelCreator().getModelWithReferences();
207         assertNotNull(mwr);
208         mwr.getKeyInformation().getKeyInfoMap().clear();
209         mwr.getKeyInformation().generateKeyInfo(mwr);
210
211         AxValidationResult result = new AxValidationResult();
212         result = mwr.validate(result);
213         assertEquals(ValidationResult.VALID, result.getValidationResult());
214
215         // Duplicate key error
216         mwr.addKey(mwr.getKey());
217         result = new AxValidationResult();
218         result = mwr.validate(result);
219         assertEquals(ValidationResult.INVALID, result.getValidationResult());
220         mwr.removeKey(mwr.getKey());
221
222         result = new AxValidationResult();
223         result = mwr.validate(result);
224         assertEquals(ValidationResult.VALID, result.getValidationResult());
225
226         // Null Reference Key
227         mwr.addKey(AxReferenceKey.getNullKey());
228         result = new AxValidationResult();
229         result = mwr.validate(result);
230         assertEquals(ValidationResult.INVALID, result.getValidationResult());
231         mwr.removeKey(AxReferenceKey.getNullKey());
232
233         result = new AxValidationResult();
234         result = mwr.validate(result);
235         assertEquals(ValidationResult.VALID, result.getValidationResult());
236
237         // Duplicate Reference Key
238         final AxReferenceKey rKey = new AxReferenceKey(mwr.getKey(), "LocalName");
239         mwr.addKey(rKey);
240         mwr.addKey(rKey);
241         result = new AxValidationResult();
242         result = mwr.validate(result);
243         assertEquals(ValidationResult.INVALID, result.getValidationResult());
244         mwr.removeKey(rKey);
245         mwr.removeKey(rKey);
246
247         result = new AxValidationResult();
248         result = mwr.validate(result);
249         assertEquals(ValidationResult.VALID, result.getValidationResult());
250
251         // Key Use is legal
252         final AxKeyUse keyU = new AxKeyUse(mwr.getKey());
253         mwr.addKey(keyU);
254         result = new AxValidationResult();
255         result = mwr.validate(result);
256         assertEquals(ValidationResult.VALID, result.getValidationResult());
257         mwr.removeKey(keyU);
258
259         // Key Use on bad artifact key
260         final AxKeyUse keyBadUsage = new AxKeyUse(new AxArtifactKey("SomeKey", "0.0.1"));
261         mwr.addKey(keyBadUsage);
262         result = new AxValidationResult();
263         result = mwr.validate(result);
264         assertEquals(ValidationResult.INVALID, result.getValidationResult());
265         mwr.removeKey(keyBadUsage);
266
267         // Key Use on bad reference key
268         final AxKeyUse keyBadReferenceUsage = new AxKeyUse(new AxReferenceKey("SomeKey", "0.0.1", "Local"));
269         mwr.addKey(keyBadReferenceUsage);
270         result = new AxValidationResult();
271         result = mwr.validate(result);
272         assertEquals(ValidationResult.INVALID, result.getValidationResult());
273         mwr.removeKey(keyBadReferenceUsage);
274
275         result = new AxValidationResult();
276         result = mwr.validate(result);
277         assertEquals(ValidationResult.VALID, result.getValidationResult());
278     }
279 }