Changes for Checkstyle 8.32
[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-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.assertSame;
29 import static org.junit.Assert.assertTrue;
30
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.TreeMap;
34 import org.junit.Test;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfReferenceKey;
37 import org.onap.policy.models.base.PfValidationResult;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
39
40 /**
41  * DAO test for ToscaProperty.
42  *
43  * @author Liam Fallon (liam.fallon@est.tech)
44  */
45 public class JpaToscaPropertyTest {
46
47     private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
48     private static final String DEFAULT_KEY = "defaultKey";
49     private static final String A_DESCRIPTION = "A Description";
50     private static final String VERSION_001 = "0.0.1";
51
52     @Test
53     public void testPropertyPojo() {
54         assertNotNull(new JpaToscaProperty());
55         assertNotNull(new JpaToscaProperty(new PfReferenceKey()));
56         assertNotNull(new JpaToscaProperty(new PfReferenceKey(), new PfConceptKey()));
57         assertNotNull(new JpaToscaProperty(new JpaToscaProperty()));
58
59         assertThatThrownBy(() -> new JpaToscaProperty((PfReferenceKey) null)).hasMessageMatching(KEY_IS_NULL);
60
61         assertThatThrownBy(() -> new JpaToscaProperty(null, null)).hasMessageMatching(KEY_IS_NULL);
62
63         assertThatThrownBy(() -> new JpaToscaProperty(null, new PfConceptKey())).hasMessageMatching(KEY_IS_NULL);
64
65         assertThatThrownBy(() -> new JpaToscaProperty(new PfReferenceKey(), null))
66                 .hasMessageMatching("type is marked .*on.*ull but is null");
67
68         PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
69         PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
70         PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
71         JpaToscaProperty tp = new JpaToscaProperty(pkey, ptypeKey);
72
73         assertEquals(tp, new JpaToscaProperty(tp));
74
75         tp.setDescription(A_DESCRIPTION);
76         assertEquals(A_DESCRIPTION, tp.getDescription());
77
78         tp.setRequired(false);
79         assertFalse(tp.isRequired());
80
81         tp.setDefaultValue(DEFAULT_KEY);
82
83         tp.setStatus(ToscaProperty.Status.SUPPORTED);
84
85         List<JpaToscaConstraint> constraints = new ArrayList<>();
86         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
87         constraints.add(lsc);
88         tp.setConstraints(constraints);
89         assertEquals(constraints, tp.getConstraints());
90
91         PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
92         JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey);
93         tp.setEntrySchema(tes);
94
95         TreeMap<String, String> metadata = new TreeMap<>();
96         metadata.put("metaA", "dataA");
97         metadata.put("metaB", "dataB");
98         tp.setMetadata(metadata);
99         assertSame(metadata, tp.getMetadata());
100
101         JpaToscaProperty tdtClone0 = new JpaToscaProperty(tp);
102         assertEquals(tp, tdtClone0);
103         assertEquals(0, tp.compareTo(tdtClone0));
104
105         assertTrue(tdtClone0.getMetadata() != tp.getMetadata());
106
107         JpaToscaProperty tdtClone1 = new JpaToscaProperty(tp);
108         assertEquals(tp, tdtClone1);
109         assertEquals(0, tp.compareTo(tdtClone1));
110
111         assertEquals(-1, tp.compareTo(null));
112         assertEquals(0, tp.compareTo(tp));
113         assertFalse(tp.compareTo(tp.getKey()) == 0);
114
115         PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", VERSION_001, "OtherProperty");
116         JpaToscaProperty otherDt = new JpaToscaProperty(otherDtKey);
117
118         assertFalse(tp.compareTo(otherDt) == 0);
119         otherDt.setKey(pkey);
120         assertFalse(tp.compareTo(otherDt) == 0);
121         otherDt.setType(ptypeKey);
122         assertFalse(tp.compareTo(otherDt) == 0);
123         otherDt.setDescription(A_DESCRIPTION);
124         assertFalse(tp.compareTo(otherDt) == 0);
125         otherDt.setRequired(false);
126         assertFalse(tp.compareTo(otherDt) == 0);
127         otherDt.setDefaultValue(DEFAULT_KEY);
128         assertFalse(tp.compareTo(otherDt) == 0);
129         otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
130         assertFalse(tp.compareTo(otherDt) == 0);
131         assertFalse(tp.compareTo(otherDt) == 0);
132         otherDt.setConstraints(constraints);
133         assertFalse(tp.compareTo(otherDt) == 0);
134         otherDt.setEntrySchema(tes);
135         assertEquals(0, tp.compareTo(otherDt));
136
137         otherDt.setRequired(true);
138         assertFalse(tp.compareTo(otherDt) == 0);
139         otherDt.setRequired(false);
140         assertEquals(0, tp.compareTo(otherDt));
141
142         otherDt.setStatus(ToscaProperty.Status.UNSUPPORTED);
143         assertFalse(tp.compareTo(otherDt) == 0);
144         otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
145         assertEquals(0, tp.compareTo(otherDt));
146
147         assertThatThrownBy(() -> new JpaToscaProperty((JpaToscaProperty) null))
148                 .isInstanceOf(NullPointerException.class);
149
150         assertEquals(3, tp.getKeys().size());
151         assertEquals(2, new JpaToscaProperty().getKeys().size());
152
153         new JpaToscaProperty().clean();
154         tp.clean();
155         assertEquals(tdtClone0, tp);
156
157         assertFalse(new JpaToscaProperty().validate(new PfValidationResult()).isValid());
158         assertTrue(tp.validate(new PfValidationResult()).isValid());
159
160         tp.setDescription(null);
161         assertTrue(tp.validate(new PfValidationResult()).isValid());
162         tp.setDescription("");
163         assertFalse(tp.validate(new PfValidationResult()).isValid());
164         tp.setDescription(A_DESCRIPTION);
165         assertTrue(tp.validate(new PfValidationResult()).isValid());
166
167         tp.setType(null);
168         assertFalse(tp.validate(new PfValidationResult()).isValid());
169         tp.setType(typeKey);
170         assertTrue(tp.validate(new PfValidationResult()).isValid());
171
172         tp.setType(PfConceptKey.getNullKey());
173         assertFalse(tp.validate(new PfValidationResult()).isValid());
174         tp.setType(typeKey);
175         assertTrue(tp.validate(new PfValidationResult()).isValid());
176
177         tp.setDefaultValue(null);
178         assertTrue(tp.validate(new PfValidationResult()).isValid());
179         tp.setDefaultValue("");
180         assertFalse(tp.validate(new PfValidationResult()).isValid());
181         tp.setDefaultValue(DEFAULT_KEY);
182         assertTrue(tp.validate(new PfValidationResult()).isValid());
183
184         tp.getConstraints().add(null);
185         assertFalse(tp.validate(new PfValidationResult()).isValid());
186         tp.getConstraints().remove(null);
187         assertTrue(tp.validate(new PfValidationResult()).isValid());
188
189         tp.setMetadata(null);
190         assertTrue(tp.validate(new PfValidationResult()).isValid());
191
192         assertThatThrownBy(() -> tp.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
193     }
194
195     @Test
196     public void testToAuthorative_testFromAuthorative() {
197         // check with empty structure
198         JpaToscaProperty tp = new JpaToscaProperty();
199         ToscaProperty auth = tp.toAuthorative();
200         JpaToscaProperty tp2 = new JpaToscaProperty();
201         tp2.fromAuthorative(auth);
202         assertEquals(tp, tp2);
203
204         // populate and try again
205         PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
206         PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
207         PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
208         tp = new JpaToscaProperty(pkey, ptypeKey);
209
210         tp.setDescription(A_DESCRIPTION);
211         tp.setRequired(true);
212         tp.setDefaultValue(DEFAULT_KEY);
213         tp.setStatus(ToscaProperty.Status.SUPPORTED);
214
215         List<JpaToscaConstraint> constraints = new ArrayList<>();
216         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
217         constraints.add(lsc);
218         tp.setConstraints(constraints);
219
220         PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
221         JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey);
222         tp.setEntrySchema(tes);
223
224         TreeMap<String, String> metadata = new TreeMap<>();
225         metadata.put("metaA", "dataA");
226         metadata.put("metaB", "dataB");
227         tp.setMetadata(metadata);
228
229         auth = tp.toAuthorative();
230         tp2 = new JpaToscaProperty();
231         tp2.fromAuthorative(auth);
232
233         // note: parent key info is not copied, so we manually copy it
234         tp2.getKey().setParentConceptKey(tp.getKey().getParentConceptKey());
235
236         assertEquals(tp.toString(), tp2.toString());
237         assertEquals(tp, tp2);
238     }
239 }