8676362dde40b8e10b38b73b4804acf6b3c6dcfb
[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         assertEquals(model, model);
73         assertEquals(model, clonedModel);
74         assertNotNull(model);
75         assertNotEquals(model, (Object) "Hello");
76         clonedModel.getKey().setVersion("0.0.2");
77         assertNotEquals(model, clonedModel);
78         clonedModel.getKey().setVersion("0.0.1");
79
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");
87
88         assertNotNull(model.getKeys());
89
90         model.getKeyInformation().generateKeyInfo(model);
91         assertNotNull(model.getKeyInformation());
92
93         final AxKeyInformation keyI = model.getKeyInformation();
94         final AxKeyInformation clonedKeyI = new AxKeyInformation(keyI);
95
96         assertNotNull(keyI);
97         assertNotEquals(keyI, (Object) new AxArtifactKey());
98         assertEquals(keyI, clonedKeyI);
99
100         clonedKeyI.setKey(new AxArtifactKey());
101         assertNotEquals(keyI, clonedKeyI);
102         clonedKeyI.setKey(keyI.getKey());
103
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()));
108
109         clonedKeyI.setKey(new AxArtifactKey());
110         assertNotEquals(0, keyI.compareTo(clonedKeyI));
111         clonedKeyI.setKey(keyI.getKey());
112         assertEquals(0, keyI.compareTo(clonedKeyI));
113
114         clonedKeyI.getKeyInfoMap().clear();
115         assertNotEquals(0, keyI.compareTo(clonedKeyI));
116
117         AxKeyInfo keyInfo = keyI.get("BasicModel");
118         assertNotNull(keyInfo);
119
120         keyInfo = keyI.get(new AxArtifactKey("BasicModel", "0.0.1"));
121         assertNotNull(keyInfo);
122
123         Set<AxKeyInfo> keyInfoSet = keyI.getAll("BasicModel");
124         assertNotNull(keyInfoSet);
125
126         keyInfoSet = keyI.getAll("BasicModel", "0..0.1");
127         assertNotNull(keyInfoSet);
128
129         List<AxKey> keys = model.getKeys();
130         assertNotEquals(0, keys.size());
131
132         keys = keyI.getKeys();
133         assertNotEquals(0, keys.size());
134
135         model.getKeyInformation().generateKeyInfo(model);
136         assertNotNull(model.getKeyInformation());
137         model.getKeyInformation().getKeyInfoMap().clear();
138         model.getKeyInformation().generateKeyInfo(model);
139         assertNotNull(model.getKeyInformation());
140
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());
146
147         clonedKeyI.getKeyInfoMap().clear();
148         result = new AxValidationResult();
149         result = clonedKeyI.validate(result);
150         assertEquals(ValidationResult.INVALID, result.getValidationResult());
151         clonedKeyI.generateKeyInfo(model);
152
153         result = new AxValidationResult();
154         result = clonedKeyI.validate(result);
155         assertEquals(ValidationResult.VALID, result.getValidationResult());
156
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);
163
164         result = new AxValidationResult();
165         result = clonedKeyI.validate(result);
166         assertEquals(ValidationResult.VALID, result.getValidationResult());
167
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);
174
175         result = new AxValidationResult();
176         result = clonedKeyI.validate(result);
177         assertEquals(ValidationResult.VALID, result.getValidationResult());
178
179         final AxKeyInfo mk = clonedKeyI.get(new AxArtifactKey("BasicModel", "0.0.1"));
180         assertNotNull(mk);
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);
187
188         result = new AxValidationResult();
189         result = clonedKeyI.validate(result);
190         assertEquals(ValidationResult.VALID, result.getValidationResult());
191
192         clonedModel.setKey(AxArtifactKey.getNullKey());
193         result = new AxValidationResult();
194         result = clonedModel.validate(result);
195         assertEquals(ValidationResult.INVALID, result.getValidationResult());
196
197         clonedModel.setKey(model.getKey());
198         result = new AxValidationResult();
199         result = clonedKeyI.validate(result);
200         assertEquals(ValidationResult.VALID, result.getValidationResult());
201     }
202
203     @Test
204     public void testModelConceptsWithReferences() {
205         final DummyAxModelWithReferences mwr = new DummyApexBasicModelCreator().getModelWithReferences();
206         assertNotNull(mwr);
207         mwr.getKeyInformation().getKeyInfoMap().clear();
208         mwr.getKeyInformation().generateKeyInfo(mwr);
209
210         AxValidationResult result = new AxValidationResult();
211         result = mwr.validate(result);
212         assertEquals(ValidationResult.VALID, result.getValidationResult());
213
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());
220
221         result = new AxValidationResult();
222         result = mwr.validate(result);
223         assertEquals(ValidationResult.VALID, result.getValidationResult());
224
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());
231
232         result = new AxValidationResult();
233         result = mwr.validate(result);
234         assertEquals(ValidationResult.VALID, result.getValidationResult());
235
236         // Duplicate Reference Key
237         final AxReferenceKey rKey = new AxReferenceKey(mwr.getKey(), "LocalName");
238         mwr.addKey(rKey);
239         mwr.addKey(rKey);
240         result = new AxValidationResult();
241         result = mwr.validate(result);
242         assertEquals(ValidationResult.INVALID, result.getValidationResult());
243         mwr.removeKey(rKey);
244         mwr.removeKey(rKey);
245
246         result = new AxValidationResult();
247         result = mwr.validate(result);
248         assertEquals(ValidationResult.VALID, result.getValidationResult());
249
250         // Key Use is legal
251         final AxKeyUse keyU = new AxKeyUse(mwr.getKey());
252         mwr.addKey(keyU);
253         result = new AxValidationResult();
254         result = mwr.validate(result);
255         assertEquals(ValidationResult.VALID, result.getValidationResult());
256         mwr.removeKey(keyU);
257
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);
265
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);
273
274         result = new AxValidationResult();
275         result = mwr.validate(result);
276         assertEquals(ValidationResult.VALID, result.getValidationResult());
277     }
278 }