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
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 testServiceTemplatePojo() {
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);
70 PfConceptKey tstKey = new PfConceptKey("tst", VERSION_001);
71 JpaToscaServiceTemplate tst = new JpaToscaServiceTemplate(tstKey, "Tosca Version");
73 PfConceptKey dataTypeKey = new PfConceptKey("DataType", VERSION_001);
74 JpaToscaDataType dataType0 = new JpaToscaDataType(dataTypeKey);
75 PfConceptKey dtsKey = new PfConceptKey("dts", VERSION_001);
76 Map<PfConceptKey, JpaToscaDataType> dataTypeMap = new TreeMap<>();
77 dataTypeMap.put(dataTypeKey, dataType0);
78 JpaToscaDataTypes dataTypes = new JpaToscaDataTypes(dtsKey, dataTypeMap);
79 tst.setDataTypes(dataTypes);
80 assertEquals(dataTypes, tst.getDataTypes());
82 PfConceptKey policyTypeKey = new PfConceptKey("DataType", VERSION_001);
83 JpaToscaPolicyType policyType0 = new JpaToscaPolicyType(policyTypeKey);
84 PfConceptKey ptsKey = new PfConceptKey("dts", VERSION_001);
85 Map<PfConceptKey, JpaToscaPolicyType> policyTypeMap = new TreeMap<>();
86 policyTypeMap.put(policyTypeKey, policyType0);
87 JpaToscaPolicyTypes policyTypes = new JpaToscaPolicyTypes(ptsKey, policyTypeMap);
88 tst.setPolicyTypes(policyTypes);
89 assertEquals(policyTypes, tst.getPolicyTypes());
91 PfReferenceKey tttKey = new PfReferenceKey(tstKey, "TopologyTemplate");
92 JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
93 tst.setTopologyTemplate(ttt);
94 assertEquals(ttt, tst.getTopologyTemplate());
96 JpaToscaServiceTemplate tttClone0 = new JpaToscaServiceTemplate(tst);
97 assertEquals(tst, tttClone0);
98 assertEquals(0, tst.compareTo(tttClone0));
100 JpaToscaServiceTemplate tttClone1 = new JpaToscaServiceTemplate(tst);
101 assertEquals(tst, tttClone1);
102 assertEquals(0, tst.compareTo(tttClone1));
104 assertEquals(-1, tst.compareTo(null));
105 assertEquals(0, tst.compareTo(tst));
106 assertNotEquals(0, tst.compareTo(tst.getKey()));
108 PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
109 JpaToscaServiceTemplate otherDt = new JpaToscaServiceTemplate(otherDtKey);
111 assertNotEquals(0, tst.compareTo(otherDt));
112 otherDt.setKey(tstKey);
113 assertNotEquals(0, tst.compareTo(otherDt));
114 otherDt.setToscaDefinitionsVersion("Tosca Version");
115 assertNotEquals(0, tst.compareTo(otherDt));
116 otherDt.setDataTypes(dataTypes);
117 assertNotEquals(0, tst.compareTo(otherDt));
118 otherDt.setPolicyTypes(policyTypes);
119 assertNotEquals(0, tst.compareTo(otherDt));
120 otherDt.setTopologyTemplate(ttt);
121 assertEquals(0, tst.compareTo(otherDt));
123 assertEquals(6, tst.getKeys().size());
124 assertEquals(1, new JpaToscaServiceTemplate().getKeys().size());
126 new JpaToscaServiceTemplate().clean();
128 assertEquals(tttClone0, tst);
130 assertTrue(new JpaToscaServiceTemplate().validate("").isValid());
131 assertTrue(tst.validate("").isValid());
133 tst.setDescription(null);
134 assertTrue(tst.validate("").isValid());
135 tst.setDescription("");
136 assertFalse(tst.validate("").isValid());
137 tst.setDescription("A Description");
138 assertTrue(tst.validate("").isValid());
140 assertThatThrownBy(() -> tst.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
142 tst.setToscaDefinitionsVersion(null);
143 BeanValidationResult result = tst.validate("");
144 assertThat(result.getResult()).contains("toscaDefinitionsVersion").contains(Validated.IS_NULL);
146 tst.setToscaDefinitionsVersion(JpaToscaServiceTemplate.DEFAULT_TOSCA_DEFINTIONS_VERISON);
147 tst.setDataTypes(null);
148 result = tst.validate("");
149 assertTrue(result.isValid());
151 JpaToscaPolicyType pt0 = new JpaToscaPolicyType(new PfConceptKey("pt0:0.0.1"));
152 tst.getPolicyTypes().getConceptMap().put(pt0.getKey(), pt0);
153 result = tst.validate("");
154 assertTrue(result.isValid());
156 JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0:0.0.1"));
157 JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop0"));
158 prop0.setType(dt0.getKey());
160 pt0.setProperties(new LinkedHashMap<>());
161 pt0.getProperties().put(prop0.getKey().getLocalName(), prop0);
162 result = tst.validate("");
163 assertFalse(result.isValid());
164 assertThat(result.getResult()).contains("data type").contains("dt0:0.0.1").contains(Validated.NOT_FOUND);
166 tst.setDataTypes(null);
167 result = tst.validate("");
168 assertFalse(result.isValid());
169 assertThat(result.getResult()).contains("data type").contains("dt0:0.0.1").contains(Validated.NOT_FOUND);
171 tst.setDataTypes(new JpaToscaDataTypes());
172 result = tst.validate("");
173 assertFalse(result.isValid());
174 assertThat(result.getResult()).contains("data type").contains("dt0:0.0.1").contains(Validated.NOT_FOUND);
176 tst.getDataTypes().getConceptMap().put(dt0.getKey(), dt0);
177 result = tst.validate("");
178 assertTrue(result.isValid());
180 tst.setTopologyTemplate(null);
181 result = tst.validate("");
182 assertTrue(result.isValid());
184 tst.setTopologyTemplate(new JpaToscaTopologyTemplate());
185 result = tst.validate("");
186 assertTrue(result.isValid());
188 tst.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
189 result = tst.validate("");
190 assertTrue(result.isValid());
192 tst.setPolicyTypes(null);
193 result = tst.validate("");
194 assertTrue(result.isValid());
196 JpaToscaPolicy pol0 = new JpaToscaPolicy(new PfConceptKey("pol0:0.0.1"));
197 tst.getTopologyTemplate().getPolicies().getConceptMap().put(pol0.getKey(), pol0);
198 result = tst.validate("");
199 assertFalse(result.isValid());
200 assertThat(result.getResult()).contains("type").contains(Validated.IS_A_NULL_KEY);
202 pol0.setType(new PfConceptKey("i.dont.Exist:0.0.1"));
203 result = tst.validate("");
204 assertFalse(result.isValid());
205 assertThat(result.getResult()).contains(
206 "no policy types are defined on the service template for the policies in the topology template");
208 tst.setPolicyTypes(policyTypes);
209 result = tst.validate("");
210 assertFalse(result.isValid());
211 assertThat(result.getResult()).contains("policy type").contains("i.dont.Exist:0.0.1")
212 .contains(Validated.NOT_FOUND);
214 pol0.setType(dt0.getKey());
215 result = tst.validate("");
216 assertFalse(result.isValid());
217 assertThat(result.getResult()).contains("policy type").contains("dt0:0.0.1").contains(Validated.NOT_FOUND);
219 pol0.setType(pt0.getKey());
220 result = tst.validate("");
221 assertTrue(result.isValid());
223 tst.setPolicyTypes(null);
224 result = tst.validate("");
225 assertFalse(result.isValid());
226 assertThat(result.getResult()).contains(
227 "no policy types are defined on the service template for the policies in the topology template");
229 tst.setPolicyTypes(policyTypes);
230 pol0.setType(pt0.getKey());
231 result = tst.validate("");
232 assertTrue(result.isValid());
234 tst.setPolicyTypes(new JpaToscaPolicyTypes());
235 result = tst.validate("");
236 assertFalse(result.isValid());
237 assertThat(result.getResult()).contains(
238 "no policy types are defined on the service template for the policies in the topology template");