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