2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2021 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.concepts;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
32 import java.util.LinkedHashMap;
34 import java.util.TreeMap;
35 import org.junit.Test;
36 import org.onap.policy.common.parameters.BeanValidationResult;
37 import org.onap.policy.models.base.PfConceptKey;
38 import org.onap.policy.models.base.PfReferenceKey;
39 import org.onap.policy.models.base.Validated;
42 * DAO test for ToscaDatatype.
44 * @author Liam Fallon (liam.fallon@est.tech)
46 public class JpaToscaServiceTemplateTest {
48 private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
49 private static final String VERSION_001 = "0.0.1";
52 public void testServiceTemplateNull() {
53 assertNotNull(new JpaToscaServiceTemplate());
54 assertNotNull(new JpaToscaServiceTemplate(new PfConceptKey()));
55 assertNotNull(new JpaToscaServiceTemplate(new PfConceptKey(), ""));
56 assertNotNull(new JpaToscaServiceTemplate(new JpaToscaServiceTemplate()));
58 assertThatThrownBy(() -> new JpaToscaServiceTemplate((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL);
60 assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, null)).hasMessageMatching(KEY_IS_NULL);
62 assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, "")).hasMessageMatching(KEY_IS_NULL);
64 assertThatThrownBy(() -> new JpaToscaServiceTemplate(new PfConceptKey(), null))
65 .hasMessageMatching("toscaDefinitionsVersion is marked .*on.*ull but is null");
67 assertThatThrownBy(() -> new JpaToscaServiceTemplate((JpaToscaServiceTemplate) null))
68 .isInstanceOf(NullPointerException.class);
72 public void testServiceTemplatePojo() {
73 PfConceptKey tstKey = new PfConceptKey("tst", VERSION_001);
74 JpaToscaServiceTemplate tst = new JpaToscaServiceTemplate(tstKey, "Tosca Version");
76 PfConceptKey dataTypeKey = new PfConceptKey("DataType", VERSION_001);
77 JpaToscaDataType dataType0 = new JpaToscaDataType(dataTypeKey);
78 PfConceptKey dtsKey = new PfConceptKey("dts", VERSION_001);
79 Map<PfConceptKey, JpaToscaDataType> dataTypeMap = new TreeMap<>();
80 dataTypeMap.put(dataTypeKey, dataType0);
81 JpaToscaDataTypes dataTypes = new JpaToscaDataTypes(dtsKey, dataTypeMap);
82 tst.setDataTypes(dataTypes);
83 assertEquals(dataTypes, tst.getDataTypes());
85 PfConceptKey policyTypeKey = new PfConceptKey("DataType", VERSION_001);
86 JpaToscaPolicyType policyType0 = new JpaToscaPolicyType(policyTypeKey);
87 PfConceptKey ptsKey = new PfConceptKey("dts", VERSION_001);
88 Map<PfConceptKey, JpaToscaPolicyType> policyTypeMap = new TreeMap<>();
89 policyTypeMap.put(policyTypeKey, policyType0);
90 JpaToscaPolicyTypes policyTypes = new JpaToscaPolicyTypes(ptsKey, policyTypeMap);
91 tst.setPolicyTypes(policyTypes);
92 assertEquals(policyTypes, tst.getPolicyTypes());
94 PfReferenceKey tttKey = new PfReferenceKey(tstKey, "TopologyTemplate");
95 JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
96 tst.setTopologyTemplate(ttt);
97 assertEquals(ttt, tst.getTopologyTemplate());
99 JpaToscaServiceTemplate tttClone0 = new JpaToscaServiceTemplate(tst);
100 assertEquals(tst, tttClone0);
101 assertEquals(0, tst.compareTo(tttClone0));
103 JpaToscaServiceTemplate tttClone1 = new JpaToscaServiceTemplate(tst);
104 assertEquals(tst, tttClone1);
105 assertEquals(0, tst.compareTo(tttClone1));
107 assertEquals(-1, tst.compareTo(null));
108 assertEquals(0, tst.compareTo(tst));
109 assertNotEquals(0, tst.compareTo(tst.getKey()));
111 PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
112 JpaToscaServiceTemplate otherDt = new JpaToscaServiceTemplate(otherDtKey);
114 assertNotEquals(0, tst.compareTo(otherDt));
115 otherDt.setKey(tstKey);
116 assertNotEquals(0, tst.compareTo(otherDt));
117 otherDt.setToscaDefinitionsVersion("Tosca Version");
118 assertNotEquals(0, tst.compareTo(otherDt));
119 otherDt.setDataTypes(dataTypes);
120 assertNotEquals(0, tst.compareTo(otherDt));
121 otherDt.setPolicyTypes(policyTypes);
122 assertNotEquals(0, tst.compareTo(otherDt));
123 otherDt.setTopologyTemplate(ttt);
124 assertEquals(0, tst.compareTo(otherDt));
126 assertEquals(6, tst.getKeys().size());
127 assertEquals(1, new JpaToscaServiceTemplate().getKeys().size());
129 new JpaToscaServiceTemplate().clean();
131 assertEquals(tttClone0, tst);
133 assertTrue(new JpaToscaServiceTemplate().validate("").isValid());
134 assertTrue(tst.validate("").isValid());
136 tst.setDescription(null);
137 assertTrue(tst.validate("").isValid());
138 tst.setDescription("");
139 assertFalse(tst.validate("").isValid());
140 tst.setDescription("A Description");
141 assertTrue(tst.validate("").isValid());
143 assertThatThrownBy(() -> tst.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
145 tst.setToscaDefinitionsVersion(null);
146 BeanValidationResult result = tst.validate("");
147 assertThat(result.getResult()).contains("tosca_definitions_version").contains(Validated.IS_NULL);
149 tst.setToscaDefinitionsVersion(JpaToscaServiceTemplate.DEFAULT_TOSCA_DEFINTIONS_VERISON);
150 tst.setDataTypes(null);
151 result = tst.validate("");
152 assertTrue(result.isValid());
154 JpaToscaPolicyType pt0 = new JpaToscaPolicyType(new PfConceptKey("pt0:0.0.1"));
155 tst.getPolicyTypes().getConceptMap().put(pt0.getKey(), pt0);
156 result = tst.validate("");
157 assertTrue(result.isValid());
159 JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0:0.0.1"));
160 JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop0"));
161 prop0.setType(dt0.getKey());
163 pt0.setProperties(new LinkedHashMap<>());
164 pt0.getProperties().put(prop0.getKey().getLocalName(), prop0);
165 result = tst.validate("");
166 assertFalse(result.isValid());
167 assertThat(result.getResult()).contains("data type").contains("dt0:0.0.1").contains(Validated.NOT_FOUND);
169 tst.setDataTypes(null);
170 result = tst.validate("");
171 assertFalse(result.isValid());
172 assertThat(result.getResult()).contains("data type").contains("dt0:0.0.1").contains(Validated.NOT_FOUND);
174 tst.setDataTypes(new JpaToscaDataTypes());
175 result = tst.validate("");
176 assertFalse(result.isValid());
177 assertThat(result.getResult()).contains("data type").contains("dt0:0.0.1").contains(Validated.NOT_FOUND);
179 tst.getDataTypes().getConceptMap().put(dt0.getKey(), dt0);
180 result = tst.validate("");
181 assertTrue(result.isValid());
183 tst.setTopologyTemplate(null);
184 result = tst.validate("");
185 assertTrue(result.isValid());
187 tst.setTopologyTemplate(new JpaToscaTopologyTemplate());
188 result = tst.validate("");
189 assertTrue(result.isValid());
191 tst.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
192 result = tst.validate("");
193 assertTrue(result.isValid());
195 tst.setPolicyTypes(null);
196 result = tst.validate("");
197 assertTrue(result.isValid());
199 JpaToscaPolicy pol0 = new JpaToscaPolicy(new PfConceptKey("pol0:0.0.1"));
200 tst.getTopologyTemplate().getPolicies().getConceptMap().put(pol0.getKey(), pol0);
201 result = tst.validate("");
202 assertFalse(result.isValid());
203 assertThat(result.getResult()).contains("type").contains(Validated.IS_A_NULL_KEY);
205 pol0.setType(new PfConceptKey("i.dont.Exist:0.0.1"));
206 result = tst.validate("");
207 assertFalse(result.isValid());
208 assertThat(result.getResult()).contains(
209 "no policy types are defined on the service template for the policies in the topology template");
211 tst.setPolicyTypes(policyTypes);
212 result = tst.validate("");
213 assertFalse(result.isValid());
214 assertThat(result.getResult()).contains("policy type").contains("i.dont.Exist:0.0.1")
215 .contains(Validated.NOT_FOUND);
217 pol0.setType(dt0.getKey());
218 result = tst.validate("");
219 assertFalse(result.isValid());
220 assertThat(result.getResult()).contains("policy type").contains("dt0:0.0.1").contains(Validated.NOT_FOUND);
222 pol0.setType(pt0.getKey());
223 result = tst.validate("");
224 assertTrue(result.isValid());
226 tst.setPolicyTypes(null);
227 result = tst.validate("");
228 assertFalse(result.isValid());
229 assertThat(result.getResult()).contains(
230 "no policy types are defined on the service template for the policies in the topology template");
232 tst.setPolicyTypes(policyTypes);
233 pol0.setType(pt0.getKey());
234 result = tst.validate("");
235 assertTrue(result.isValid());
237 tst.setPolicyTypes(new JpaToscaPolicyTypes());
238 result = tst.validate("");
239 assertFalse(result.isValid());
240 assertThat(result.getResult()).contains(
241 "no policy types are defined on the service template for the policies in the topology template");