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