Java 17 Upgrade
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPropertyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021, 2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
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.models.tosca.simple.concepts;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNotSame;
30 import static org.junit.Assert.assertSame;
31 import static org.junit.Assert.assertTrue;
32
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.TreeMap;
37 import org.junit.Test;
38 import org.onap.policy.models.base.PfConceptKey;
39 import org.onap.policy.models.base.PfReferenceKey;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
41
42 /**
43  * DAO test for ToscaProperty.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class JpaToscaPropertyTest {
48
49     private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
50     private static final String DEFAULT_KEY = "defaultKey";
51     private static final String A_DESCRIPTION = "A Description";
52     private static final String VERSION_001 = "0.0.1";
53
54     @Test
55     public void testPropertyNull() {
56         assertNotNull(new JpaToscaProperty());
57         assertNotNull(new JpaToscaProperty(new PfReferenceKey()));
58         assertNotNull(new JpaToscaProperty(new PfReferenceKey(), new PfConceptKey()));
59         assertNotNull(new JpaToscaProperty(new JpaToscaProperty()));
60
61         assertThatThrownBy(() -> new JpaToscaProperty((PfReferenceKey) null)).hasMessageMatching(KEY_IS_NULL);
62
63         assertThatThrownBy(() -> new JpaToscaProperty(null, null)).hasMessageMatching(KEY_IS_NULL);
64
65         assertThatThrownBy(() -> new JpaToscaProperty(null, new PfConceptKey())).hasMessageMatching(KEY_IS_NULL);
66
67         assertThatThrownBy(() -> new JpaToscaProperty(new PfReferenceKey(), null))
68                 .hasMessageMatching("type is marked .*on.*ull but is null");
69     }
70
71     @Test
72     public void testProperty() {
73         PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
74         PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
75         PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
76         JpaToscaProperty tp = new JpaToscaProperty(pkey, ptypeKey);
77
78         assertEquals(tp, new JpaToscaProperty(tp));
79
80         tp.setDescription(A_DESCRIPTION);
81         assertEquals(A_DESCRIPTION, tp.getDescription());
82
83         tp.setRequired(false);
84         assertFalse(tp.isRequired());
85
86         tp.setDefaultValue(DEFAULT_KEY);
87
88         tp.setStatus(ToscaProperty.Status.SUPPORTED);
89
90         List<JpaToscaConstraint> constraints = new ArrayList<>();
91         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
92         constraints.add(lsc);
93         tp.setConstraints(constraints);
94         assertEquals(constraints, tp.getConstraints());
95
96         PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
97         JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(typeKey);
98         tp.setEntrySchema(tes);
99
100         TreeMap<String, String> metadata = new TreeMap<>();
101         metadata.put("metaA", "dataA");
102         metadata.put("metaB", "dataB");
103         tp.setMetadata(metadata);
104         assertSame(metadata, tp.getMetadata());
105
106         JpaToscaProperty tdtClone0 = new JpaToscaProperty(tp);
107         assertEquals(tp, tdtClone0);
108         assertEquals(0, tp.compareTo(tdtClone0));
109
110         assertNotSame(tdtClone0.getMetadata(), tp.getMetadata());
111
112         JpaToscaProperty tdtClone1 = new JpaToscaProperty(tp);
113         assertEquals(tp, tdtClone1);
114         assertEquals(0, tp.compareTo(tdtClone1));
115
116         assertThatThrownBy(() -> tp.compareTo(null)).isInstanceOf(NullPointerException.class);
117         assertEquals(0, tp.compareTo(tp));
118         assertNotEquals(0, tp.compareTo(tp.getKey()));
119     }
120
121     @Test
122     public void testPropertyCompareData() {
123         JpaToscaProperty tp = setUpJpaToscaProperty();
124
125         PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", VERSION_001, "OtherProperty");
126         JpaToscaProperty otherDt = new JpaToscaProperty(otherDtKey);
127
128         assertNotEquals(0, tp.compareTo(otherDt));
129
130         PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
131         PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
132         otherDt.setKey(pkey);
133         assertNotEquals(0, tp.compareTo(otherDt));
134
135         PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
136         otherDt.setType(ptypeKey);
137         assertNotEquals(0, tp.compareTo(otherDt));
138         otherDt.setDescription(A_DESCRIPTION);
139         assertNotEquals(0, tp.compareTo(otherDt));
140         otherDt.setRequired(false);
141         assertNotEquals(0, tp.compareTo(otherDt));
142         otherDt.setDefaultValue(DEFAULT_KEY);
143         assertNotEquals(0, tp.compareTo(otherDt));
144         otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
145         assertNotEquals(0, tp.compareTo(otherDt));
146         assertNotEquals(0, tp.compareTo(otherDt));
147
148         List<JpaToscaConstraint> constraints = new ArrayList<>();
149         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
150         constraints.add(lsc);
151         otherDt.setConstraints(constraints);
152         assertNotEquals(0, tp.compareTo(otherDt));
153
154         PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
155         JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(typeKey);
156         otherDt.setEntrySchema(tes);
157         assertNotEquals(0, tp.compareTo(otherDt));
158
159         TreeMap<String, String> metadata = new TreeMap<>();
160         metadata.put("metaA", "dataA");
161         metadata.put("metaB", "dataB");
162         otherDt.setMetadata(metadata);
163         assertEquals(0, tp.compareTo(otherDt));
164
165         otherDt.setRequired(true);
166         assertNotEquals(0, tp.compareTo(otherDt));
167         otherDt.setRequired(false);
168         assertEquals(0, tp.compareTo(otherDt));
169
170         otherDt.setStatus(ToscaProperty.Status.UNSUPPORTED);
171         assertNotEquals(0, tp.compareTo(otherDt));
172         otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
173         assertEquals(0, tp.compareTo(otherDt));
174
175         assertThatThrownBy(() -> new JpaToscaProperty((JpaToscaProperty) null))
176                 .isInstanceOf(NullPointerException.class);
177
178         assertEquals(3, tp.getKeys().size());
179         assertEquals(2, new JpaToscaProperty().getKeys().size());
180     }
181
182     @Test
183     public void testPropertyCompareDescription() {
184         JpaToscaProperty tp = setUpJpaToscaProperty();
185         JpaToscaProperty tdtClone0 = new JpaToscaProperty(tp);
186
187         new JpaToscaProperty().clean();
188         tp.clean();
189         assertEquals(tdtClone0, tp);
190
191         assertFalse(new JpaToscaProperty().validate("").isValid());
192         assertTrue(tp.validate("").isValid());
193
194         tp.setDescription(null);
195         assertTrue(tp.validate("").isValid());
196         tp.setDescription("");
197         assertFalse(tp.validate("").isValid());
198         tp.setDescription(A_DESCRIPTION);
199         assertTrue(tp.validate("").isValid());
200
201         tp.setType(null);
202         assertFalse(tp.validate("").isValid());
203     }
204
205     @Test
206     public void testPropertyCompareMetadata() {
207         JpaToscaProperty tp = setUpJpaToscaProperty();
208         PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
209         tp.setType(typeKey);
210         assertTrue(tp.validate("").isValid());
211
212         tp.setType(PfConceptKey.getNullKey());
213         assertFalse(tp.validate("").isValid());
214         tp.setType(typeKey);
215         assertTrue(tp.validate("").isValid());
216
217         tp.setDefaultValue(null);
218         assertTrue(tp.validate("").isValid());
219         tp.setDefaultValue("");
220         assertFalse(tp.validate("").isValid());
221         tp.setDefaultValue(DEFAULT_KEY);
222         assertTrue(tp.validate("").isValid());
223
224         tp.getConstraints().add(null);
225         assertFalse(tp.validate("").isValid());
226         tp.getConstraints().remove(null);
227         assertTrue(tp.validate("").isValid());
228
229         tp.setMetadata(null);
230         assertTrue(tp.validate("").isValid());
231
232         assertThatThrownBy(() -> tp.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
233     }
234
235     @Test
236     public void testToAuthorative_testFromAuthorative() {
237         // check with empty structure
238         JpaToscaProperty tp = new JpaToscaProperty();
239         ToscaProperty auth = tp.toAuthorative();
240         JpaToscaProperty tp2 = new JpaToscaProperty();
241         tp2.fromAuthorative(auth);
242         assertEquals(tp, tp2);
243
244         // populate and try again
245         PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
246         PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
247         PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
248         tp = new JpaToscaProperty(pkey, ptypeKey);
249
250         tp.setDescription(A_DESCRIPTION);
251         tp.setRequired(true);
252         tp.setDefaultValue(DEFAULT_KEY);
253         tp.setStatus(ToscaProperty.Status.SUPPORTED);
254
255         List<JpaToscaConstraint> constraints = new ArrayList<>();
256         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
257         constraints.add(lsc);
258         tp.setConstraints(constraints);
259
260         PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
261         JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(typeKey);
262         tp.setEntrySchema(tes);
263
264         TreeMap<String, String> metadata = new TreeMap<>();
265         metadata.put("metaA", "dataA");
266         metadata.put("metaB", "dataB");
267         tp.setMetadata(metadata);
268
269         auth = tp.toAuthorative();
270         tp2 = new JpaToscaProperty();
271         tp2.fromAuthorative(auth);
272
273         // note: parent key info is not copied, so we manually copy it
274         tp2.getKey().setParentConceptKey(tp.getKey().getParentConceptKey());
275
276         assertEquals(tp.toString(), tp2.toString());
277         assertEquals(tp, tp2);
278     }
279
280     private JpaToscaProperty setUpJpaToscaProperty() {
281         PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
282         PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
283         PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
284
285         JpaToscaProperty tp = new JpaToscaProperty(pkey, ptypeKey);
286         tp.setDescription(A_DESCRIPTION);
287         tp.setRequired(false);
288         tp.setDefaultValue(DEFAULT_KEY);
289         tp.setStatus(ToscaProperty.Status.SUPPORTED);
290
291         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
292         // Maps and Lists need to be modifiable
293         List<JpaToscaConstraint> constraints = new ArrayList<>(List.of(lsc));
294         tp.setConstraints(constraints);
295
296         PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
297         JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(typeKey);
298         tp.setEntrySchema(tes);
299         TreeMap<String, String> metadata = new TreeMap<>(Map.of("metaA", "dataA", "metaB", "dataB"));
300         tp.setMetadata(metadata);
301
302         return tp;
303     }
304 }