JPA concepts for TOSCA
[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         JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(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         assertNotEquals(0, tp.compareTo(otherDt));
138         otherDt.setMetadata(metadata);
139         assertEquals(0, tp.compareTo(otherDt));
140
141         otherDt.setRequired(true);
142         assertNotEquals(0, tp.compareTo(otherDt));
143         otherDt.setRequired(false);
144         assertEquals(0, tp.compareTo(otherDt));
145
146         otherDt.setStatus(ToscaProperty.Status.UNSUPPORTED);
147         assertNotEquals(0, tp.compareTo(otherDt));
148         otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
149         assertEquals(0, tp.compareTo(otherDt));
150
151         assertThatThrownBy(() -> new JpaToscaProperty((JpaToscaProperty) null))
152                 .isInstanceOf(NullPointerException.class);
153
154         assertEquals(3, tp.getKeys().size());
155         assertEquals(2, new JpaToscaProperty().getKeys().size());
156
157         new JpaToscaProperty().clean();
158         tp.clean();
159         assertEquals(tdtClone0, tp);
160
161         assertFalse(new JpaToscaProperty().validate(new PfValidationResult()).isValid());
162         assertTrue(tp.validate(new PfValidationResult()).isValid());
163
164         tp.setDescription(null);
165         assertTrue(tp.validate(new PfValidationResult()).isValid());
166         tp.setDescription("");
167         assertFalse(tp.validate(new PfValidationResult()).isValid());
168         tp.setDescription(A_DESCRIPTION);
169         assertTrue(tp.validate(new PfValidationResult()).isValid());
170
171         tp.setType(null);
172         assertFalse(tp.validate(new PfValidationResult()).isValid());
173         tp.setType(typeKey);
174         assertTrue(tp.validate(new PfValidationResult()).isValid());
175
176         tp.setType(PfConceptKey.getNullKey());
177         assertFalse(tp.validate(new PfValidationResult()).isValid());
178         tp.setType(typeKey);
179         assertTrue(tp.validate(new PfValidationResult()).isValid());
180
181         tp.setDefaultValue(null);
182         assertTrue(tp.validate(new PfValidationResult()).isValid());
183         tp.setDefaultValue("");
184         assertFalse(tp.validate(new PfValidationResult()).isValid());
185         tp.setDefaultValue(DEFAULT_KEY);
186         assertTrue(tp.validate(new PfValidationResult()).isValid());
187
188         tp.getConstraints().add(null);
189         assertFalse(tp.validate(new PfValidationResult()).isValid());
190         tp.getConstraints().remove(null);
191         assertTrue(tp.validate(new PfValidationResult()).isValid());
192
193         tp.setMetadata(null);
194         assertTrue(tp.validate(new PfValidationResult()).isValid());
195
196         assertThatThrownBy(() -> tp.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
197     }
198
199     @Test
200     public void testToAuthorative_testFromAuthorative() {
201         // check with empty structure
202         JpaToscaProperty tp = new JpaToscaProperty();
203         ToscaProperty auth = tp.toAuthorative();
204         JpaToscaProperty tp2 = new JpaToscaProperty();
205         tp2.fromAuthorative(auth);
206         assertEquals(tp, tp2);
207
208         // populate and try again
209         PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
210         PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
211         PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
212         tp = new JpaToscaProperty(pkey, ptypeKey);
213
214         tp.setDescription(A_DESCRIPTION);
215         tp.setRequired(true);
216         tp.setDefaultValue(DEFAULT_KEY);
217         tp.setStatus(ToscaProperty.Status.SUPPORTED);
218
219         List<JpaToscaConstraint> constraints = new ArrayList<>();
220         JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
221         constraints.add(lsc);
222         tp.setConstraints(constraints);
223
224         PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
225         JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(typeKey);
226         tp.setEntrySchema(tes);
227
228         TreeMap<String, String> metadata = new TreeMap<>();
229         metadata.put("metaA", "dataA");
230         metadata.put("metaB", "dataB");
231         tp.setMetadata(metadata);
232
233         auth = tp.toAuthorative();
234         tp2 = new JpaToscaProperty();
235         tp2.fromAuthorative(auth);
236
237         // note: parent key info is not copied, so we manually copy it
238         tp2.getKey().setParentConceptKey(tp.getKey().getParentConceptKey());
239
240         assertEquals(tp.toString(), tp2.toString());
241         assertEquals(tp, tp2);
242     }
243 }