Fix Sonar Issues models-tosca-simple
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPolicyTest.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.PfUtils;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
41
42 /**
43  * DAO test for ToscaPolicy.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class JpaToscaPolicyTest {
48
49     private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
50     private static final String VERSION_001 = "0.0.1";
51
52     @Test
53     public void testPolicyPojo() {
54         assertNotNull(new JpaToscaPolicy());
55         assertNotNull(new JpaToscaPolicy(new PfConceptKey()));
56         assertNotNull(new JpaToscaPolicy(new PfConceptKey(), new PfConceptKey()));
57         assertNotNull(new JpaToscaPolicy(new JpaToscaPolicy()));
58
59         final ToscaPolicy pol = new ToscaPolicy();
60         pol.setType("type");
61         assertThatThrownBy(() -> {
62             new JpaToscaPolicy(pol);
63         }).hasMessage(
64                 "Version not specified, the version of this TOSCA entity must be specified in the type_version field");
65
66         assertThatThrownBy(() -> {
67             new JpaToscaPolicy((PfConceptKey) null);
68         }).hasMessageMatching(KEY_IS_NULL);
69
70         assertThatThrownBy(() -> {
71             new JpaToscaPolicy(null, null);
72         }).hasMessageMatching(KEY_IS_NULL);
73
74         assertThatThrownBy(() -> {
75             new JpaToscaPolicy(new PfConceptKey(), null);
76         }).hasMessageMatching("type is marked .*on.*ull but is null");
77
78         assertThatThrownBy(() -> {
79             new JpaToscaPolicy(null, new PfConceptKey());
80         }).hasMessageMatching(KEY_IS_NULL);
81
82         assertThatThrownBy(() -> new JpaToscaPolicy((JpaToscaPolicy) null)).isInstanceOf(NullPointerException.class);
83     }
84
85     @Test
86     public void testJpaToscaPolicy() {
87         PfConceptKey tpKey = new PfConceptKey("tdt", VERSION_001);
88         PfConceptKey ptKey = new PfConceptKey("policyType", VERSION_001);
89         JpaToscaPolicy tp = new JpaToscaPolicy(tpKey, ptKey);
90
91         Map<String, String> propertyMap = new HashMap<>();
92         propertyMap.put("Property", "\"Property Value\"");
93         tp.setProperties(propertyMap);
94         assertEquals(propertyMap, tp.getProperties());
95
96         List<PfConceptKey> targets = new ArrayList<>();
97         PfConceptKey target = new PfConceptKey("target", VERSION_001);
98         targets.add(target);
99         tp.setTargets(targets);
100         assertEquals(targets, tp.getTargets());
101
102         JpaToscaPolicy tdtClone0 = new JpaToscaPolicy(tp);
103         assertEquals(tp, tdtClone0);
104         assertEquals(0, tp.compareTo(tdtClone0));
105
106         JpaToscaPolicy tdtClone1 = new JpaToscaPolicy(tp);
107         assertEquals(tp, tdtClone1);
108         assertEquals(0, tp.compareTo(tdtClone1));
109
110         assertEquals(-1, tp.compareTo(null));
111         assertEquals(0, tp.compareTo(tp));
112         assertNotEquals(0, tp.compareTo(tp.getKey()));
113
114         PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
115         JpaToscaPolicy otherDt = new JpaToscaPolicy(otherDtKey);
116
117         assertNotEquals(0, tp.compareTo(otherDt));
118         otherDt.setKey(tpKey);
119         assertNotEquals(0, tp.compareTo(otherDt));
120         otherDt.setType(ptKey);
121         assertNotEquals(0, tp.compareTo(otherDt));
122         otherDt.setProperties(propertyMap);
123         assertNotEquals(0, tp.compareTo(otherDt));
124         otherDt.setTargets(targets);
125         assertEquals(0, tp.compareTo(otherDt));
126
127         assertEquals(3, tp.getKeys().size());
128         assertEquals(2, new JpaToscaPolicy().getKeys().size());
129
130         new JpaToscaPolicy().clean();
131         tp.clean();
132         assertEquals(tdtClone0, tp);
133     }
134
135     @Test
136     public void testJpaToscaPolicyValidation() {
137         JpaToscaPolicy tp = setUpJpaToscaPolicy();
138         assertFalse(new JpaToscaPolicy().validate("").isValid());
139         assertTrue(tp.validate("").isValid());
140
141         tp.getProperties().put(null, null);
142         assertFalse(tp.validate("").isValid());
143         tp.getProperties().remove(null);
144         assertTrue(tp.validate("").isValid());
145
146         tp.getProperties().put("Key", null);
147         assertFalse(tp.validate("").isValid());
148         tp.getProperties().remove("Key");
149         assertTrue(tp.validate("").isValid());
150
151         tp.getProperties().put(null, "Value");
152         assertFalse(tp.validate("").isValid());
153         tp.getProperties().remove(null);
154         assertTrue(tp.validate("").isValid());
155
156         tp.getTargets().add(null);
157         assertFalse(tp.validate("").isValid());
158         tp.getTargets().remove(null);
159         assertTrue(tp.validate("").isValid());
160     }
161
162     @Test
163     public void testJpaToscaPolicyAuthorative() {
164         JpaToscaPolicy tp = setUpJpaToscaPolicy();
165         PfConceptKey tpTypeKey = tp.getKey();
166         assertNotNull(tpTypeKey);
167         tp.setType(null);
168         assertFalse(tp.validate("").isValid());
169         tp.setType(PfConceptKey.getNullKey());
170         assertFalse(tp.validate("").isValid());
171         tp.setType(tpTypeKey);
172         assertTrue(tp.validate("").isValid());
173
174         assertThatThrownBy(() -> {
175             tp.validate(null);
176         }).hasMessageMatching("fieldName is marked .*on.*ull but is null");
177
178         assertNotNull(tp.toAuthorative());
179         tp.getType().setVersion(PfKey.NULL_KEY_VERSION);
180         assertNotNull(tp.toAuthorative());
181         tp.setProperties(null);
182         assertNotNull(tp.toAuthorative());
183
184         assertThatThrownBy(() -> {
185             tp.fromAuthorative(null);
186         }).hasMessageMatching("toscaPolicy is marked .*on.*ull but is null");
187
188         ToscaPolicy pol1 = new ToscaPolicy();
189         pol1.setName("policy");
190         pol1.setVersion("1.2.3");
191         pol1.setType("poltype");
192         pol1.setTypeVersion("2.2.3");
193         tp.fromAuthorative(pol1);
194         assertEquals("2.2.3", tp.getType().getVersion());
195     }
196
197     @Test
198     public void testPolicyProperties() {
199
200         Map<String, Object> properties = new LinkedHashMap<>();
201
202         // @formatter:off
203         properties.put("byte",    Byte.valueOf("2"));
204         properties.put("short",   Short.valueOf("1234"));
205         properties.put("int",     Integer.valueOf("12345678"));
206         properties.put("long",    Long.valueOf("1234567890"));
207         properties.put("float",   Float.valueOf("12345.678"));
208         properties.put("double",  Double.valueOf("-12345.6789"));
209         properties.put("char",    '%');
210         properties.put("string",  "hello");
211         properties.put("boolean", false);
212         // @formatter:on
213
214         ToscaPolicy tp = new ToscaPolicy();
215         tp.setType("org.onap.Policy");
216         tp.setTypeVersion("1.2.3");
217         tp.setProperties(properties);
218
219         JpaToscaPolicy jtp = new JpaToscaPolicy(tp);
220         assertEquals(0, PfUtils.compareCollections(tp.getProperties().keySet(), jtp.getProperties().keySet()));
221
222         ToscaPolicy tpFromTo = jtp.toAuthorative();
223
224         // @formatter:off
225         assertEquals(2,           tpFromTo.getProperties().get("byte"));
226         assertEquals(1234,        tpFromTo.getProperties().get("short"));
227         assertEquals(12345678,    tpFromTo.getProperties().get("int"));
228         assertEquals(1234567890,  tpFromTo.getProperties().get("long"));
229         assertEquals(12345.678,   tpFromTo.getProperties().get("float"));
230         assertEquals(-12345.6789, tpFromTo.getProperties().get("double"));
231         assertEquals("%",         tpFromTo.getProperties().get("char"));
232         assertEquals("hello",     tpFromTo.getProperties().get("string"));
233         assertEquals(false,       tpFromTo.getProperties().get("boolean"));
234         // @formatter:on
235     }
236
237     private JpaToscaPolicy setUpJpaToscaPolicy() {
238         PfConceptKey tpKey = new PfConceptKey("tdt", VERSION_001);
239         PfConceptKey ptKey = new PfConceptKey("policyType", VERSION_001);
240         JpaToscaPolicy tp = new JpaToscaPolicy(tpKey, ptKey);
241
242         // Maps and Lists need to be modifiable
243         Map<String, String> propertyMap = new HashMap<>(Map.of("Property", "\"Property Value\""));
244         tp.setProperties(propertyMap);
245
246         PfConceptKey target = new PfConceptKey("target", VERSION_001);
247         List<PfConceptKey> targets = new ArrayList<>(List.of(target));
248         tp.setTargets(targets);
249         return tp;
250     }
251 }