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