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