Implement validation and hierarchical get
[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-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 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.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.util.ArrayList;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import java.util.Map;
34
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.base.PfValidationResult;
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 public class JpaToscaDataTypeTest {
49
50     private static final String VERSION_001 = "0.0.1";
51
52     @Test
53     public 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         PfConceptKey dtKey = new PfConceptKey("tdt", VERSION_001);
67         JpaToscaDataType tdt = new JpaToscaDataType(dtKey);
68
69         List<JpaToscaConstraint> constraints = new ArrayList<>();
70         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
71         constraints.add(lsc);
72         tdt.setConstraints(constraints);
73         assertEquals(constraints, tdt.getConstraints());
74
75         Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
76         JpaToscaProperty tp =
77                 new JpaToscaProperty(new PfReferenceKey(dtKey, "pr"), new PfConceptKey("type", VERSION_001));
78         properties.put(tp.getKey().getLocalName(), tp);
79         tdt.setProperties(properties);
80         assertEquals(properties, tdt.getProperties());
81
82         JpaToscaDataType tdtClone0 = new JpaToscaDataType(tdt);
83         assertEquals(tdt, tdtClone0);
84         assertEquals(0, tdt.compareTo(tdtClone0));
85
86         JpaToscaDataType tdtClone1 = new JpaToscaDataType(tdt);
87         assertEquals(tdt, tdtClone1);
88         assertEquals(0, tdt.compareTo(tdtClone1));
89
90         assertEquals(-1, tdt.compareTo(null));
91         assertEquals(0, tdt.compareTo(tdt));
92         assertFalse(tdt.compareTo(tdt.getKey()) == 0);
93
94         PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
95         JpaToscaDataType otherDt = new JpaToscaDataType(otherDtKey);
96
97         assertFalse(tdt.compareTo(otherDt) == 0);
98         otherDt.setKey(dtKey);
99         assertFalse(tdt.compareTo(otherDt) == 0);
100         otherDt.setConstraints(constraints);
101         assertFalse(tdt.compareTo(otherDt) == 0);
102         otherDt.setProperties(properties);
103         assertEquals(0, tdt.compareTo(otherDt));
104
105         assertEquals(3, tdt.getKeys().size());
106         assertEquals(1, new JpaToscaDataType().getKeys().size());
107
108         new JpaToscaDataType().clean();
109         tdt.clean();
110         assertEquals(tdtClone0, tdt);
111
112         assertFalse(new JpaToscaDataType().validate(new PfValidationResult()).isValid());
113         assertTrue(tdt.validate(new PfValidationResult()).isValid());
114
115         tdt.getConstraints().add(null);
116         assertFalse(tdt.validate(new PfValidationResult()).isValid());
117         tdt.getConstraints().remove(null);
118         assertTrue(tdt.validate(new PfValidationResult()).isValid());
119
120         tdt.getProperties().put(null, null);
121         assertFalse(tdt.validate(new PfValidationResult()).isValid());
122         tdt.getProperties().remove(null);
123         assertTrue(tdt.validate(new PfValidationResult()).isValid());
124
125         assertThatThrownBy(() -> {
126             tdt.validate(null);
127         }).hasMessageMatching("resultIn is marked .*on.*ull but is null");
128
129         ToscaDataType dat = new ToscaDataType();
130         dat.setName("name");
131         dat.setVersion("1.2.3");
132         dat.setConstraints(new ArrayList<>());
133         ToscaConstraint constraint = new ToscaConstraint();
134         constraint.setEqual("EqualTo");
135         dat.getConstraints().add(constraint);
136
137         JpaToscaDataType tdta = new JpaToscaDataType();
138         tdta.fromAuthorative(dat);
139         assertEquals("name", tdta.getKey().getName());
140
141         ToscaDataType datOut = tdta.toAuthorative();
142         assertNotNull(datOut);
143     }
144
145     @Test
146     public void testGetReferencedDataTypes() {
147         JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0", "0.0.1"));
148
149         assertTrue(dt0.getReferencedDataTypes().isEmpty());
150
151         dt0.setProperties(new LinkedHashMap<>());
152         assertTrue(dt0.getReferencedDataTypes().isEmpty());
153
154         JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop0"));
155         prop0.setType(new PfConceptKey("string", PfKey.NULL_KEY_VERSION));
156         assertTrue(prop0.validate(new PfValidationResult()).isValid());
157
158         dt0.getProperties().put(prop0.getKey().getLocalName(), prop0);
159         assertTrue(dt0.getReferencedDataTypes().isEmpty());
160
161         JpaToscaProperty prop1 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop1"));
162         prop1.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
163         assertTrue(prop1.validate(new PfValidationResult()).isValid());
164
165         dt0.getProperties().put(prop1.getKey().getLocalName(), prop1);
166         assertEquals(1, dt0.getReferencedDataTypes().size());
167
168         JpaToscaProperty prop2 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop2"));
169         prop2.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
170         assertTrue(prop2.validate(new PfValidationResult()).isValid());
171
172         dt0.getProperties().put(prop2.getKey().getLocalName(), prop2);
173         assertEquals(1, dt0.getReferencedDataTypes().size());
174
175         JpaToscaProperty prop3 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop4"));
176         prop3.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
177         prop3.setEntrySchema(new JpaToscaEntrySchema());
178         prop3.getEntrySchema().setType(new PfConceptKey("the.property.Type3", "0.0.1"));
179         assertTrue(prop3.validate(new PfValidationResult()).isValid());
180
181         dt0.getProperties().put(prop3.getKey().getLocalName(), prop3);
182         assertEquals(3, dt0.getReferencedDataTypes().size());
183
184         JpaToscaProperty prop4 = new JpaToscaProperty(new PfReferenceKey(dt0.getKey(), "prop4"));
185         prop4.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
186         prop4.setEntrySchema(new JpaToscaEntrySchema());
187         prop4.getEntrySchema().setType(new PfConceptKey("the.property.Type2", "0.0.1"));
188         assertTrue(prop4.validate(new PfValidationResult()).isValid());
189
190         dt0.getProperties().put(prop4.getKey().getLocalName(), prop4);
191         assertEquals(3, dt0.getReferencedDataTypes().size());
192     }
193 }