af0ef5f3c1c7345c365fb5e850df357a18c461f8
[policy/apex-pdp.git] / model / basic-model / src / test / java / org / onap / policy / apex / model / basicmodel / handling / SupportApexBasicModelConceptsTester.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 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.assertFalse;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.util.List;
31 import java.util.Set;
32
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
44 import org.onap.policy.apex.model.basicmodel.service.ModelService;
45 import org.onap.policy.apex.model.basicmodel.test.TestApexModel;
46
47 public class SupportApexBasicModelConceptsTester {
48     TestApexModel<AxModel> testApexModel;
49
50     @Before
51     public void setup() throws Exception {
52         testApexModel = new TestApexModel<AxModel>(AxModel.class, new DummyApexBasicModelCreator());
53     }
54
55     @Test
56     public void testModelConcepts() {
57         final AxModel model = testApexModel.getModel();
58         assertNotNull(model);
59         model.clean();
60         assertNotNull(model);
61
62         AxValidationResult result = new AxValidationResult();
63         result = model.validate(result);
64         assertEquals(ValidationResult.WARNING, result.getValidationResult());
65
66         model.register();
67         assertEquals(model.getKeyInformation(), ModelService.getModel(AxKeyInformation.class));
68
69         final AxModel clonedModel = new AxModel(model);
70         assertTrue(clonedModel.toString().startsWith("AxModel:(key=AxArtifactKey:(name=BasicModel"));
71
72         assertFalse(model.hashCode() == 0);
73
74         assertTrue(model.equals(model));
75         assertTrue(model.equals(clonedModel));
76         assertFalse(model.equals(null));
77         assertFalse(model.equals((Object)"Hello"));
78         clonedModel.getKey().setVersion("0.0.2");
79         assertFalse(model.equals(clonedModel));
80         clonedModel.getKey().setVersion("0.0.1");
81
82         assertEquals(0, model.compareTo(model));
83         assertNotEquals(0, model.compareTo(null));
84         assertNotEquals(0, model.compareTo(new AxReferenceKey()));
85         assertEquals(0, model.compareTo(clonedModel));
86         clonedModel.getKey().setVersion("0.0.2");
87         assertNotEquals(0, model.compareTo(clonedModel));
88         clonedModel.getKey().setVersion("0.0.1");
89
90         assertNotNull(model.getKeys());
91
92         model.getKeyInformation().generateKeyInfo(model);
93         assertNotNull(model.getKeyInformation());
94
95         final AxKeyInformation keyI = model.getKeyInformation();
96         final AxKeyInformation clonedKeyI = new AxKeyInformation(keyI);
97
98         assertFalse(keyI.equals(null));
99         assertFalse(keyI.equals((Object)new AxArtifactKey()));
100         assertTrue(keyI.equals(clonedKeyI));
101
102         clonedKeyI.setKey(new AxArtifactKey());
103         assertFalse(keyI.equals(clonedKeyI));
104         clonedKeyI.setKey(keyI.getKey());
105
106         assertEquals(0, keyI.compareTo(keyI));
107         assertEquals(0, keyI.compareTo(clonedKeyI));
108         assertNotEquals(0, keyI.compareTo(null));
109         assertNotEquals(0, keyI.compareTo(new AxArtifactKey()));
110
111         clonedKeyI.setKey(new AxArtifactKey());
112         assertNotEquals(0, keyI.compareTo(clonedKeyI));
113         clonedKeyI.setKey(keyI.getKey());
114         assertEquals(0, keyI.compareTo(clonedKeyI));
115
116         clonedKeyI.getKeyInfoMap().clear();
117         assertNotEquals(0, keyI.compareTo(clonedKeyI));
118
119         AxKeyInfo keyInfo = keyI.get("BasicModel");
120         assertNotNull(keyInfo);
121
122         keyInfo = keyI.get(new AxArtifactKey("BasicModel", "0.0.1"));
123         assertNotNull(keyInfo);
124
125         Set<AxKeyInfo> keyInfoSet = keyI.getAll("BasicModel");
126         assertNotNull(keyInfoSet);
127
128         keyInfoSet = keyI.getAll("BasicModel", "0..0.1");
129         assertNotNull(keyInfoSet);
130
131         List<AxKey> keys = model.getKeys();
132         assertNotEquals(0, keys.size());
133
134         keys = keyI.getKeys();
135         assertNotEquals(0, keys.size());
136
137         model.getKeyInformation().generateKeyInfo(model);
138         assertNotNull(model.getKeyInformation());
139         model.getKeyInformation().getKeyInfoMap().clear();
140         model.getKeyInformation().generateKeyInfo(model);
141         assertNotNull(model.getKeyInformation());
142
143         clonedKeyI.setKey(AxArtifactKey.getNullKey());
144         result = new AxValidationResult();
145         result = clonedKeyI.validate(result);
146         assertEquals(ValidationResult.INVALID, result.getValidationResult());
147         clonedKeyI.setKey(keyI.getKey());
148
149         clonedKeyI.getKeyInfoMap().clear();
150         result = new AxValidationResult();
151         result = clonedKeyI.validate(result);
152         assertEquals(ValidationResult.INVALID, result.getValidationResult());
153         clonedKeyI.generateKeyInfo(model);
154
155         result = new AxValidationResult();
156         result = clonedKeyI.validate(result);
157         assertEquals(ValidationResult.VALID, result.getValidationResult());
158
159         clonedKeyI.getKeyInfoMap().put(AxArtifactKey.getNullKey(), null);
160         result = new AxValidationResult();
161         result = clonedKeyI.validate(result);
162         assertEquals(ValidationResult.INVALID, result.getValidationResult());
163         clonedKeyI.getKeyInfoMap().clear();
164         clonedKeyI.generateKeyInfo(model);
165
166         result = new AxValidationResult();
167         result = clonedKeyI.validate(result);
168         assertEquals(ValidationResult.VALID, result.getValidationResult());
169
170         clonedKeyI.getKeyInfoMap().put(new AxArtifactKey("SomeKey", "0.0.1"), null);
171         result = new AxValidationResult();
172         result = clonedKeyI.validate(result);
173         assertEquals(ValidationResult.INVALID, result.getValidationResult());
174         clonedKeyI.getKeyInfoMap().clear();
175         clonedKeyI.generateKeyInfo(model);
176
177         result = new AxValidationResult();
178         result = clonedKeyI.validate(result);
179         assertEquals(ValidationResult.VALID, result.getValidationResult());
180
181         final AxKeyInfo mk = clonedKeyI.get(new AxArtifactKey("BasicModel", "0.0.1"));
182         assertNotNull(mk);
183         mk.setKey(AxArtifactKey.getNullKey());
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         clonedModel.setKey(AxArtifactKey.getNullKey());
195         result = new AxValidationResult();
196         result = clonedModel.validate(result);
197         assertEquals(ValidationResult.INVALID, result.getValidationResult());
198
199         clonedModel.setKey(model.getKey());
200         result = new AxValidationResult();
201         result = clonedKeyI.validate(result);
202         assertEquals(ValidationResult.VALID, result.getValidationResult());
203     }
204
205     @Test
206     public void testModelConceptsWithReferences() {
207         final DummyAxModelWithReferences mwr = new DummyApexBasicModelCreator().getModelWithReferences();
208         assertNotNull(mwr);
209         mwr.getKeyInformation().getKeyInfoMap().clear();
210         mwr.getKeyInformation().generateKeyInfo(mwr);
211
212         AxValidationResult result = new AxValidationResult();
213         result = mwr.validate(result);
214         assertEquals(ValidationResult.VALID, result.getValidationResult());
215
216         // Duplicate key error
217         mwr.addKey(mwr.getKey());
218         result = new AxValidationResult();
219         result = mwr.validate(result);
220         assertEquals(ValidationResult.INVALID, result.getValidationResult());
221         mwr.removeKey(mwr.getKey());
222
223         result = new AxValidationResult();
224         result = mwr.validate(result);
225         assertEquals(ValidationResult.VALID, result.getValidationResult());
226
227         // Null Reference Key
228         mwr.addKey(AxReferenceKey.getNullKey());
229         result = new AxValidationResult();
230         result = mwr.validate(result);
231         assertEquals(ValidationResult.INVALID, result.getValidationResult());
232         mwr.removeKey(AxReferenceKey.getNullKey());
233
234         result = new AxValidationResult();
235         result = mwr.validate(result);
236         assertEquals(ValidationResult.VALID, result.getValidationResult());
237
238         // Duplicate Reference Key
239         final AxReferenceKey rKey = new AxReferenceKey(mwr.getKey(), "LocalName");
240         mwr.addKey(rKey);
241         mwr.addKey(rKey);
242         result = new AxValidationResult();
243         result = mwr.validate(result);
244         assertEquals(ValidationResult.INVALID, result.getValidationResult());
245         mwr.removeKey(rKey);
246         mwr.removeKey(rKey);
247
248         result = new AxValidationResult();
249         result = mwr.validate(result);
250         assertEquals(ValidationResult.VALID, result.getValidationResult());
251
252         // Key Use is legal
253         final AxKeyUse keyU = new AxKeyUse(mwr.getKey());
254         mwr.addKey(keyU);
255         result = new AxValidationResult();
256         result = mwr.validate(result);
257         assertEquals(ValidationResult.VALID, result.getValidationResult());
258         mwr.removeKey(keyU);
259
260         // Key Use on bad artifact key
261         final AxKeyUse keyBadUsage = new AxKeyUse(new AxArtifactKey("SomeKey", "0.0.1"));
262         mwr.addKey(keyBadUsage);
263         result = new AxValidationResult();
264         result = mwr.validate(result);
265         assertEquals(ValidationResult.INVALID, result.getValidationResult());
266         mwr.removeKey(keyBadUsage);
267
268         // Key Use on bad reference key
269         final AxKeyUse keyBadReferenceUsage = new AxKeyUse(new AxReferenceKey("SomeKey", "0.0.1", "Local"));
270         mwr.addKey(keyBadReferenceUsage);
271         result = new AxValidationResult();
272         result = mwr.validate(result);
273         assertEquals(ValidationResult.INVALID, result.getValidationResult());
274         mwr.removeKey(keyBadReferenceUsage);
275
276         result = new AxValidationResult();
277         result = mwr.validate(result);
278         assertEquals(ValidationResult.VALID, result.getValidationResult());
279     }
280 }