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