Fix trailing space in Info.yaml models
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaDataTypeTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2024 Nordix Foundation
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.models.tosca.simple.concepts;
24
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertFalse;
28 import static org.junit.jupiter.api.Assertions.assertNotEquals;
29 import static org.junit.jupiter.api.Assertions.assertNotNull;
30 import static org.junit.jupiter.api.Assertions.assertTrue;
31
32 import java.util.ArrayList;
33 import java.util.LinkedHashMap;
34 import java.util.List;
35 import java.util.Map;
36 import org.junit.jupiter.api.Test;
37 import org.onap.policy.models.base.PfConceptKey;
38 import org.onap.policy.models.base.PfKey;
39 import org.onap.policy.models.base.PfReferenceKey;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
42
43 /**
44  * DAO test for JpaToscaDatatype.
45  *
46  * @author Liam Fallon (liam.fallon@est.tech)
47  */
48 class JpaToscaDataTypeTest {
49
50     private static final String VERSION_001 = "0.0.1";
51
52     @Test
53     void testDataTypePojo() {
54         assertNotNull(new JpaToscaDataType());
55         assertNotNull(new JpaToscaDataType(new PfConceptKey()));
56         assertNotNull(new JpaToscaDataType(new JpaToscaDataType()));
57         assertNotNull(new JpaToscaDataType(new ToscaDataType()));
58
59         assertThatThrownBy(() -> {
60             new JpaToscaDataType((PfConceptKey) null);
61         }).hasMessageMatching("key is marked .*on.*ull but is null");
62
63         assertThatThrownBy(() -> new JpaToscaDataType((JpaToscaDataType) null))
64                 .isInstanceOf(NullPointerException.class);
65     }
66
67     @Test
68     void testDataTypeProperties() {
69         PfConceptKey dtKey = new PfConceptKey("tdt", VERSION_001);
70         JpaToscaDataType tdt = new JpaToscaDataType(dtKey);
71
72         List<JpaToscaConstraint> constraints = new ArrayList<>();
73         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
74         constraints.add(lsc);
75         tdt.setConstraints(constraints);
76         assertEquals(constraints, tdt.getConstraints());
77
78         Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
79         JpaToscaProperty tp =
80                 new JpaToscaProperty(new PfReferenceKey(dtKey, "pr"), new PfConceptKey("type", VERSION_001));
81         properties.put(tp.getKey().getLocalName(), tp);
82         tdt.setProperties(properties);
83         assertEquals(properties, tdt.getProperties());
84
85         JpaToscaDataType tdtClone0 = new JpaToscaDataType(tdt);
86         assertEquals(tdt, tdtClone0);
87         assertEquals(0, tdt.compareTo(tdtClone0));
88
89         JpaToscaDataType tdtClone1 = new JpaToscaDataType(tdt);
90         assertEquals(tdt, tdtClone1);
91         assertEquals(0, tdt.compareTo(tdtClone1));
92
93         assertEquals(-1, tdt.compareTo(null));
94         assertEquals(0, tdt.compareTo(tdt));
95         assertNotEquals(0, tdt.compareTo(tdt.getKey()));
96
97         PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
98         JpaToscaDataType otherDt = new JpaToscaDataType(otherDtKey);
99
100         assertNotEquals(0, tdt.compareTo(otherDt));
101         otherDt.setKey(dtKey);
102         assertNotEquals(0, tdt.compareTo(otherDt));
103         otherDt.setConstraints(constraints);
104         assertNotEquals(0, tdt.compareTo(otherDt));
105         otherDt.setProperties(properties);
106         assertEquals(0, tdt.compareTo(otherDt));
107
108         assertEquals(3, tdt.getKeys().size());
109         assertEquals(1, new JpaToscaDataType().getKeys().size());
110
111         new JpaToscaDataType().clean();
112         tdt.clean();
113         assertEquals(tdtClone0, tdt);
114
115         assertFalse(new JpaToscaDataType().validate("").isValid());
116         validateJpaToscaDataTypeOperations(tdt);
117
118         assertThatThrownBy(() -> {
119             tdt.validate(null);
120         }).hasMessageMatching("fieldName is marked .*on.*ull but is null");
121     }
122
123     private void validateJpaToscaDataTypeOperations(JpaToscaDataType tdt) {
124         assertTrue(tdt.validate("").isValid());
125
126         tdt.getConstraints().add(null);
127         assertFalse(tdt.validate("").isValid());
128         tdt.getConstraints().remove(null);
129         assertTrue(tdt.validate("").isValid());
130
131         tdt.getProperties().put(null, null);
132         assertFalse(tdt.validate("").isValid());
133         tdt.getProperties().remove(null);
134         assertTrue(tdt.validate("").isValid());
135     }
136
137     @Test
138     void testDataTypeConstraints() {
139         ToscaDataType dat = new ToscaDataType();
140         dat.setName("name");
141         dat.setVersion("1.2.3");
142         dat.setConstraints(new ArrayList<>());
143         ToscaConstraint constraint = new ToscaConstraint();
144         constraint.setEqual("EqualTo");
145         dat.getConstraints().add(constraint);
146
147         JpaToscaDataType tdta = new JpaToscaDataType();
148         tdta.fromAuthorative(dat);
149         assertEquals("name", tdta.getKey().getName());
150
151         ToscaDataType datOut = tdta.toAuthorative();
152         assertNotNull(datOut);
153     }
154
155     @Test
156     void testGetReferencedDataTypes() {
157         JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0", "0.0.1"));
158
159         assertTrue(dt0.getReferencedDataTypes().isEmpty());
160
161         dt0.setProperties(new LinkedHashMap<>());
162         assertTrue(dt0.getReferencedDataTypes().isEmpty());
163
164         JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop0"));
165         prop0.setType(new PfConceptKey("string", PfKey.NULL_KEY_VERSION));
166         assertTrue(prop0.validate("").isValid());
167
168         dt0.getProperties().put(prop0.getKey().getLocalName(), prop0);
169         assertTrue(dt0.getReferencedDataTypes().isEmpty());
170
171         JpaToscaProperty prop1 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop1"));
172         prop1.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
173         assertTrue(prop1.validate("").isValid());
174
175         dt0.getProperties().put(prop1.getKey().getLocalName(), prop1);
176         assertEquals(1, dt0.getReferencedDataTypes().size());
177
178         JpaToscaProperty prop2 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop2"));
179         prop2.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
180         assertTrue(prop2.validate("").isValid());
181
182         dt0.getProperties().put(prop2.getKey().getLocalName(), prop2);
183         assertEquals(1, dt0.getReferencedDataTypes().size());
184
185         JpaToscaProperty prop3 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop4"));
186         prop3.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
187         prop3.setEntrySchema(new JpaToscaSchemaDefinition());
188         prop3.getEntrySchema().setType(new PfConceptKey("the.property.Type3", "0.0.1"));
189         assertTrue(prop3.validate("").isValid());
190
191         dt0.getProperties().put(prop3.getKey().getLocalName(), prop3);
192         assertEquals(3, dt0.getReferencedDataTypes().size());
193
194         JpaToscaProperty prop4 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop4"));
195         prop4.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
196         prop4.setEntrySchema(new JpaToscaSchemaDefinition());
197         prop4.getEntrySchema().setType(new PfConceptKey("the.property.Type2", "0.0.1"));
198         assertTrue(prop4.validate("").isValid());
199
200         dt0.getProperties().put(prop4.getKey().getLocalName(), prop4);
201         assertEquals(3, dt0.getReferencedDataTypes().size());
202     }
203 }