Changes for checkstyle 8.32
[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 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyUse;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxModel;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
43 import org.onap.policy.apex.model.basicmodel.service.ModelService;
44 import org.onap.policy.apex.model.basicmodel.test.TestApexModel;
45
46 public class SupportApexBasicModelConceptsTester {
47     TestApexModel<AxModel> testApexModel;
48
49     @Before
50     public void setup() throws Exception {
51         testApexModel = new TestApexModel<AxModel>(AxModel.class, new DummyApexBasicModelCreator());
52     }
53
54     @Test
55     public void testModelConcepts() {
56         final AxModel model = testApexModel.getModel();
57         assertNotNull(model);
58         model.clean();
59         assertNotNull(model);
60
61         AxValidationResult result = new AxValidationResult();
62         result = model.validate(result);
63         assertEquals(ValidationResult.WARNING, result.getValidationResult());
64
65         model.register();
66         assertEquals(model.getKeyInformation(), ModelService.getModel(AxKeyInformation.class));
67
68         final AxModel clonedModel = new AxModel(model);
69         assertTrue(clonedModel.toString().startsWith("AxModel:(key=AxArtifactKey:(name=BasicModel"));
70
71         assertFalse(model.hashCode() == 0);
72
73         assertTrue(model.equals(model));
74         assertTrue(model.equals(clonedModel));
75         assertFalse(model.equals(null));
76         assertFalse(model.equals((Object) "Hello"));
77         clonedModel.getKey().setVersion("0.0.2");
78         assertFalse(model.equals(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         assertFalse(keyI.equals(null));
98         assertFalse(keyI.equals((Object) new AxArtifactKey()));
99         assertTrue(keyI.equals(clonedKeyI));
100
101         clonedKeyI.setKey(new AxArtifactKey());
102         assertFalse(keyI.equals(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 }