Fix Sonar Issues models-tosca-simple
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPolicyTypeTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 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.assertTrue;
30
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.LinkedHashMap;
34 import java.util.List;
35 import java.util.Map;
36 import org.junit.Test;
37 import org.onap.policy.models.base.PfConceptKey;
38 import org.onap.policy.models.base.PfKey;
39 import org.onap.policy.models.base.PfReferenceKey;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
42
43 /**
44  * DAO test for ToscaPolicyType.
45  *
46  * @author Liam Fallon (liam.fallon@est.tech)
47  */
48 public class JpaToscaPolicyTypeTest {
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 testPolicyTypeNull() {
54         assertNotNull(new JpaToscaPolicyType());
55         assertNotNull(new JpaToscaPolicyType(new PfConceptKey()));
56         assertNotNull(new JpaToscaPolicyType(new JpaToscaPolicyType()));
57
58         assertThatThrownBy(() -> new JpaToscaPolicyType((PfConceptKey) null))
59                 .hasMessageMatching("key is marked .*on.*ull but is null");
60
61         assertThatThrownBy(() -> new JpaToscaPolicyType((JpaToscaPolicyType) null))
62                 .isInstanceOf(NullPointerException.class);
63     }
64
65     @Test
66     public void testPolicyTypePojo() {
67         PfConceptKey ptKey = new PfConceptKey("tdt", VERSION_001);
68         JpaToscaPolicyType tpt = new JpaToscaPolicyType(ptKey);
69
70         PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", VERSION_001);
71         tpt.setDerivedFrom(derivedFromKey);
72
73         Map<String, String> metadata = new HashMap<>();
74         metadata.put("key", "value");
75         tpt.setMetadata(metadata);
76         assertEquals(metadata, tpt.getMetadata());
77
78         tpt.setDescription(A_DESCRIPTION);
79
80         PfConceptKey propTypeKey = new PfConceptKey("propType", VERSION_001);
81         Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
82         JpaToscaProperty tp = new JpaToscaProperty(new PfReferenceKey(ptKey, "aProp"), propTypeKey);
83         properties.put(tp.getKey().getLocalName(), tp);
84         tpt.setProperties(properties);
85         assertEquals(properties, tpt.getProperties());
86
87         List<PfConceptKey> targets = new ArrayList<>();
88         PfConceptKey target = new PfConceptKey("target", VERSION_001);
89         targets.add(target);
90         tpt.setTargets(targets);
91         assertEquals(targets, tpt.getTargets());
92
93         List<JpaToscaTrigger> triggers = new ArrayList<>();
94         JpaToscaTrigger trigger = new JpaToscaTrigger(new PfReferenceKey(ptKey, "aTrigger"), "EventType", "Action");
95         triggers.add(trigger);
96         tpt.setTriggers(triggers);
97         assertEquals(triggers, tpt.getTriggers());
98
99         JpaToscaPolicyType tdtClone0 = new JpaToscaPolicyType(tpt);
100         assertEquals(tpt, tdtClone0);
101         assertEquals(0, tpt.compareTo(tdtClone0));
102
103         JpaToscaPolicyType tdtClone1 = new JpaToscaPolicyType(tpt);
104         assertEquals(tpt, tdtClone1);
105         assertEquals(0, tpt.compareTo(tdtClone1));
106
107         assertEquals(-1, tpt.compareTo(null));
108         assertEquals(0, tpt.compareTo(tpt));
109         assertNotEquals(0, tpt.compareTo(tpt.getKey()));
110
111         PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
112         JpaToscaPolicyType otherDt = new JpaToscaPolicyType(otherDtKey);
113
114         assertNotEquals(0, tpt.compareTo(otherDt));
115         otherDt.setKey(ptKey);
116         assertNotEquals(0, tpt.compareTo(otherDt));
117         otherDt.setDerivedFrom(derivedFromKey);
118         assertNotEquals(0, tpt.compareTo(otherDt));
119         otherDt.setMetadata(metadata);
120         assertNotEquals(0, tpt.compareTo(otherDt));
121         otherDt.setDescription(A_DESCRIPTION);
122         assertNotEquals(0, tpt.compareTo(otherDt));
123         otherDt.setProperties(properties);
124         assertNotEquals(0, tpt.compareTo(otherDt));
125         otherDt.setTargets(targets);
126         assertNotEquals(0, tpt.compareTo(otherDt));
127         otherDt.setTriggers(triggers);
128         assertEquals(0, tpt.compareTo(otherDt));
129
130         new JpaToscaPolicyType().clean();
131         tpt.clean();
132         assertEquals(tdtClone0, tpt);
133     }
134
135     @Test
136     public void testPolicyTypeValidation() {
137         JpaToscaPolicyType tpt = setUpJpaToscaPolicyType();
138
139         assertEquals(6, tpt.getKeys().size());
140         assertEquals(1, new JpaToscaPolicyType().getKeys().size());
141
142         assertFalse(new JpaToscaPolicyType().validate("").isValid());
143         assertTrue(tpt.validate("").isValid());
144
145         tpt.getProperties().put(null, null);
146         assertFalse(tpt.validate("").isValid());
147         tpt.getProperties().remove(null);
148         assertTrue(tpt.validate("").isValid());
149
150         tpt.getTargets().add(null);
151         assertFalse(tpt.validate("").isValid());
152         tpt.getTargets().remove(null);
153         assertTrue(tpt.validate("").isValid());
154
155         tpt.getTriggers().add(null);
156         assertFalse(tpt.validate("").isValid());
157         tpt.getTriggers().remove(null);
158         assertTrue(tpt.validate("").isValid());
159     }
160
161     @Test
162     public void testPolicyTypeValidation2() {
163         JpaToscaPolicyType tpt = setUpJpaToscaPolicyType();
164
165         tpt.getMetadata().put(null, null);
166         assertFalse(tpt.validate("").isValid());
167         tpt.getMetadata().remove(null);
168         assertTrue(tpt.validate("").isValid());
169
170         tpt.getMetadata().put("nullKey", null);
171         assertFalse(tpt.validate("").isValid());
172         tpt.getMetadata().remove("nullKey");
173         assertTrue(tpt.validate("").isValid());
174
175         tpt.setDescription("");
176
177         assertFalse(tpt.validate("").isValid());
178         tpt.setDescription(A_DESCRIPTION);
179         assertTrue(tpt.validate("").isValid());
180
181         tpt.setDerivedFrom(PfConceptKey.getNullKey());
182         assertFalse(tpt.validate("").isValid());
183
184         PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", VERSION_001);
185         tpt.setDerivedFrom(derivedFromKey);
186         assertTrue(tpt.validate("").isValid());
187     }
188
189     @Test
190     public void testPolicyTypeEntity() {
191         JpaToscaPolicyType tpt = setUpJpaToscaPolicyType();
192
193         assertThatThrownBy(() -> tpt.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
194
195         assertThatThrownBy(() -> new JpaToscaEntityType<ToscaPolicy>((PfConceptKey) null))
196                 .hasMessageMatching("key is marked .*on.*ull but is null");
197
198         assertThatThrownBy(() -> new JpaToscaEntityType<ToscaPolicy>((JpaToscaEntityType<ToscaPolicy>) null))
199                 .isInstanceOf(NullPointerException.class);
200
201         JpaToscaEntityType<ToscaPolicy> tet = new JpaToscaEntityType<>(tpt.getKey());
202         assertEquals(-1, tet.compareTo(null));
203         assertEquals(0, tet.compareTo(tet));
204         assertNotEquals(0, tet.compareTo(tet.getKey()));
205
206         assertNotNull(new JpaToscaPolicyType(new ToscaPolicyType()));
207
208         assertNotNull(new JpaToscaEntityType<ToscaPolicyType>(new ToscaPolicyType()));
209     }
210
211     @Test
212     public void testGetReferencedDataTypes() {
213         JpaToscaPolicyType pt0 = new JpaToscaPolicyType(new PfConceptKey("pt0", "0.0.1"));
214
215         assertTrue(pt0.getReferencedDataTypes().isEmpty());
216
217         pt0.setProperties(new LinkedHashMap<>());
218         assertTrue(pt0.getReferencedDataTypes().isEmpty());
219
220         JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop0"));
221         prop0.setType(new PfConceptKey("string", PfKey.NULL_KEY_VERSION));
222         assertTrue(prop0.validate("").isValid());
223
224         pt0.getProperties().put(prop0.getKey().getLocalName(), prop0);
225         assertTrue(pt0.getReferencedDataTypes().isEmpty());
226
227         JpaToscaProperty prop1 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop1"));
228         prop1.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
229         assertTrue(prop1.validate("").isValid());
230
231         pt0.getProperties().put(prop1.getKey().getLocalName(), prop1);
232         assertEquals(1, pt0.getReferencedDataTypes().size());
233
234         JpaToscaProperty prop2 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop2"));
235         prop2.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
236         assertTrue(prop2.validate("").isValid());
237
238         pt0.getProperties().put(prop2.getKey().getLocalName(), prop2);
239         assertEquals(1, pt0.getReferencedDataTypes().size());
240
241         JpaToscaProperty prop3 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop4"));
242         prop3.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
243         prop3.setEntrySchema(new JpaToscaSchemaDefinition());
244         prop3.getEntrySchema().setType(new PfConceptKey("the.property.Type3", "0.0.1"));
245         assertTrue(prop3.validate("").isValid());
246
247         pt0.getProperties().put(prop3.getKey().getLocalName(), prop3);
248         assertEquals(3, pt0.getReferencedDataTypes().size());
249
250         JpaToscaProperty prop4 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop4"));
251         prop4.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
252         prop4.setEntrySchema(new JpaToscaSchemaDefinition());
253         prop4.getEntrySchema().setType(new PfConceptKey("the.property.Type2", "0.0.1"));
254         assertTrue(prop4.validate("").isValid());
255
256         pt0.getProperties().put(prop4.getKey().getLocalName(), prop4);
257         assertEquals(3, pt0.getReferencedDataTypes().size());
258     }
259
260     private JpaToscaPolicyType setUpJpaToscaPolicyType() {
261         PfConceptKey ptKey = new PfConceptKey("tdt", VERSION_001);
262         JpaToscaPolicyType tpt = new JpaToscaPolicyType(ptKey);
263
264         PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", VERSION_001);
265         tpt.setDerivedFrom(derivedFromKey);
266         // Maps and Lists need to be modifiable
267         Map<String, String> metadata = new HashMap<>(Map.of("key", "value"));
268         tpt.setMetadata(metadata);
269
270         tpt.setDescription(A_DESCRIPTION);
271
272         PfConceptKey propTypeKey = new PfConceptKey("propType", VERSION_001);
273         JpaToscaProperty tp = new JpaToscaProperty(new PfReferenceKey(ptKey, "aProp"), propTypeKey);
274         Map<String, JpaToscaProperty> properties = new LinkedHashMap<>(Map.of(tp.getKey().getLocalName(), tp));
275         tpt.setProperties(properties);
276
277         PfConceptKey target = new PfConceptKey("target", VERSION_001);
278         List<PfConceptKey> targets = new ArrayList<>(List.of(target));
279         tpt.setTargets(targets);
280
281         JpaToscaTrigger trigger = new JpaToscaTrigger(new PfReferenceKey(ptKey, "aTrigger"), "EventType", "Action");
282         List<JpaToscaTrigger> triggers = new ArrayList<>(List.of(trigger));
283         tpt.setTriggers(triggers);
284
285         return tpt;
286     }
287 }