Set all cross references of policy/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  * ================================================================================
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.assertTrue;
30
31 import java.util.ArrayList;
32 import java.util.LinkedHashMap;
33 import java.util.List;
34 import java.util.Map;
35 import org.junit.Test;
36 import org.onap.policy.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfKey;
38 import org.onap.policy.models.base.PfReferenceKey;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
41
42 /**
43  * DAO test for JpaToscaDatatype.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class JpaToscaDataTypeTest {
48
49     private static final String VERSION_001 = "0.0.1";
50
51     @Test
52     public void testDataTypePojo() {
53         assertNotNull(new JpaToscaDataType());
54         assertNotNull(new JpaToscaDataType(new PfConceptKey()));
55         assertNotNull(new JpaToscaDataType(new JpaToscaDataType()));
56         assertNotNull(new JpaToscaDataType(new ToscaDataType()));
57
58         assertThatThrownBy(() -> {
59             new JpaToscaDataType((PfConceptKey) null);
60         }).hasMessageMatching("key is marked .*on.*ull but is null");
61
62         assertThatThrownBy(() -> new JpaToscaDataType((JpaToscaDataType) null))
63                 .isInstanceOf(NullPointerException.class);
64     }
65
66     @Test
67     public void testDataTypeProperties() {
68         PfConceptKey dtKey = new PfConceptKey("tdt", VERSION_001);
69         JpaToscaDataType tdt = new JpaToscaDataType(dtKey);
70
71         List<JpaToscaConstraint> constraints = new ArrayList<>();
72         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
73         constraints.add(lsc);
74         tdt.setConstraints(constraints);
75         assertEquals(constraints, tdt.getConstraints());
76
77         Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
78         JpaToscaProperty tp =
79                 new JpaToscaProperty(new PfReferenceKey(dtKey, "pr"), new PfConceptKey("type", VERSION_001));
80         properties.put(tp.getKey().getLocalName(), tp);
81         tdt.setProperties(properties);
82         assertEquals(properties, tdt.getProperties());
83
84         JpaToscaDataType tdtClone0 = new JpaToscaDataType(tdt);
85         assertEquals(tdt, tdtClone0);
86         assertEquals(0, tdt.compareTo(tdtClone0));
87
88         JpaToscaDataType tdtClone1 = new JpaToscaDataType(tdt);
89         assertEquals(tdt, tdtClone1);
90         assertEquals(0, tdt.compareTo(tdtClone1));
91
92         assertEquals(-1, tdt.compareTo(null));
93         assertEquals(0, tdt.compareTo(tdt));
94         assertNotEquals(0, tdt.compareTo(tdt.getKey()));
95
96         PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
97         JpaToscaDataType otherDt = new JpaToscaDataType(otherDtKey);
98
99         assertNotEquals(0, tdt.compareTo(otherDt));
100         otherDt.setKey(dtKey);
101         assertNotEquals(0, tdt.compareTo(otherDt));
102         otherDt.setConstraints(constraints);
103         assertNotEquals(0, tdt.compareTo(otherDt));
104         otherDt.setProperties(properties);
105         assertEquals(0, tdt.compareTo(otherDt));
106
107         assertEquals(3, tdt.getKeys().size());
108         assertEquals(1, new JpaToscaDataType().getKeys().size());
109
110         new JpaToscaDataType().clean();
111         tdt.clean();
112         assertEquals(tdtClone0, tdt);
113
114         assertFalse(new JpaToscaDataType().validate("").isValid());
115         validateJpaToscaDataTypeOperations(tdt);
116
117         assertThatThrownBy(() -> {
118             tdt.validate(null);
119         }).hasMessageMatching("fieldName is marked .*on.*ull but is null");
120     }
121
122     private void validateJpaToscaDataTypeOperations(JpaToscaDataType tdt) {
123         assertTrue(tdt.validate("").isValid());
124
125         tdt.getConstraints().add(null);
126         assertFalse(tdt.validate("").isValid());
127         tdt.getConstraints().remove(null);
128         assertTrue(tdt.validate("").isValid());
129
130         tdt.getProperties().put(null, null);
131         assertFalse(tdt.validate("").isValid());
132         tdt.getProperties().remove(null);
133         assertTrue(tdt.validate("").isValid());
134     }
135
136     @Test
137     public void testDataTypeConstraints() {
138         ToscaDataType dat = new ToscaDataType();
139         dat.setName("name");
140         dat.setVersion("1.2.3");
141         dat.setConstraints(new ArrayList<>());
142         ToscaConstraint constraint = new ToscaConstraint();
143         constraint.setEqual("EqualTo");
144         dat.getConstraints().add(constraint);
145
146         JpaToscaDataType tdta = new JpaToscaDataType();
147         tdta.fromAuthorative(dat);
148         assertEquals("name", tdta.getKey().getName());
149
150         ToscaDataType datOut = tdta.toAuthorative();
151         assertNotNull(datOut);
152     }
153
154     @Test
155     public void testGetReferencedDataTypes() {
156         JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0", "0.0.1"));
157
158         assertTrue(dt0.getReferencedDataTypes().isEmpty());
159
160         dt0.setProperties(new LinkedHashMap<>());
161         assertTrue(dt0.getReferencedDataTypes().isEmpty());
162
163         JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop0"));
164         prop0.setType(new PfConceptKey("string", PfKey.NULL_KEY_VERSION));
165         assertTrue(prop0.validate("").isValid());
166
167         dt0.getProperties().put(prop0.getKey().getLocalName(), prop0);
168         assertTrue(dt0.getReferencedDataTypes().isEmpty());
169
170         JpaToscaProperty prop1 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop1"));
171         prop1.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
172         assertTrue(prop1.validate("").isValid());
173
174         dt0.getProperties().put(prop1.getKey().getLocalName(), prop1);
175         assertEquals(1, dt0.getReferencedDataTypes().size());
176
177         JpaToscaProperty prop2 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop2"));
178         prop2.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
179         assertTrue(prop2.validate("").isValid());
180
181         dt0.getProperties().put(prop2.getKey().getLocalName(), prop2);
182         assertEquals(1, dt0.getReferencedDataTypes().size());
183
184         JpaToscaProperty prop3 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop4"));
185         prop3.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
186         prop3.setEntrySchema(new JpaToscaSchemaDefinition());
187         prop3.getEntrySchema().setType(new PfConceptKey("the.property.Type3", "0.0.1"));
188         assertTrue(prop3.validate("").isValid());
189
190         dt0.getProperties().put(prop3.getKey().getLocalName(), prop3);
191         assertEquals(3, dt0.getReferencedDataTypes().size());
192
193         JpaToscaProperty prop4 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop4"));
194         prop4.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
195         prop4.setEntrySchema(new JpaToscaSchemaDefinition());
196         prop4.getEntrySchema().setType(new PfConceptKey("the.property.Type2", "0.0.1"));
197         assertTrue(prop4.validate("").isValid());
198
199         dt0.getProperties().put(prop4.getKey().getLocalName(), prop4);
200         assertEquals(3, dt0.getReferencedDataTypes().size());
201     }
202 }