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