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