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.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfReferenceKey;
38 import org.onap.policy.models.base.PfValidationResult;
41 * DAO test for ToscaDatatype.
43 * @author Liam Fallon (liam.fallon@est.tech)
45 public class JpaToscaServiceTemplateTest {
47 private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
48 private static final String VERSION_001 = "0.0.1";
51 public void testServiceTemplatePojo() {
52 assertNotNull(new JpaToscaServiceTemplate());
53 assertNotNull(new JpaToscaServiceTemplate(new PfConceptKey()));
54 assertNotNull(new JpaToscaServiceTemplate(new PfConceptKey(), ""));
55 assertNotNull(new JpaToscaServiceTemplate(new JpaToscaServiceTemplate()));
57 assertThatThrownBy(() -> new JpaToscaServiceTemplate((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL);
59 assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, null)).hasMessageMatching(KEY_IS_NULL);
61 assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, "")).hasMessageMatching(KEY_IS_NULL);
63 assertThatThrownBy(() -> new JpaToscaServiceTemplate(new PfConceptKey(), null))
64 .hasMessageMatching("toscaDefinitionsVersion is marked .*on.*ull but is null");
66 assertThatThrownBy(() -> new JpaToscaServiceTemplate((JpaToscaServiceTemplate) null))
67 .isInstanceOf(NullPointerException.class);
69 PfConceptKey tstKey = new PfConceptKey("tst", VERSION_001);
70 JpaToscaServiceTemplate tst = new JpaToscaServiceTemplate(tstKey, "Tosca Version");
72 PfConceptKey dataTypeKey = new PfConceptKey("DataType", VERSION_001);
73 JpaToscaDataType dataType0 = new JpaToscaDataType(dataTypeKey);
74 PfConceptKey dtsKey = new PfConceptKey("dts", VERSION_001);
75 Map<PfConceptKey, JpaToscaDataType> dataTypeMap = new TreeMap<>();
76 dataTypeMap.put(dataTypeKey, dataType0);
77 JpaToscaDataTypes dataTypes = new JpaToscaDataTypes(dtsKey, dataTypeMap);
78 tst.setDataTypes(dataTypes);
79 assertEquals(dataTypes, tst.getDataTypes());
81 PfConceptKey policyTypeKey = new PfConceptKey("DataType", VERSION_001);
82 JpaToscaPolicyType policyType0 = new JpaToscaPolicyType(policyTypeKey);
83 PfConceptKey ptsKey = new PfConceptKey("dts", VERSION_001);
84 Map<PfConceptKey, JpaToscaPolicyType> policyTypeMap = new TreeMap<>();
85 policyTypeMap.put(policyTypeKey, policyType0);
86 JpaToscaPolicyTypes policyTypes = new JpaToscaPolicyTypes(ptsKey, policyTypeMap);
87 tst.setPolicyTypes(policyTypes);
88 assertEquals(policyTypes, tst.getPolicyTypes());
90 PfReferenceKey tttKey = new PfReferenceKey(tstKey, "TopologyTemplate");
91 JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
92 tst.setTopologyTemplate(ttt);
93 assertEquals(ttt, tst.getTopologyTemplate());
95 JpaToscaServiceTemplate tttClone0 = new JpaToscaServiceTemplate(tst);
96 assertEquals(tst, tttClone0);
97 assertEquals(0, tst.compareTo(tttClone0));
99 JpaToscaServiceTemplate tttClone1 = new JpaToscaServiceTemplate(tst);
100 assertEquals(tst, tttClone1);
101 assertEquals(0, tst.compareTo(tttClone1));
103 assertEquals(-1, tst.compareTo(null));
104 assertEquals(0, tst.compareTo(tst));
105 assertNotEquals(0, tst.compareTo(tst.getKey()));
107 PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
108 JpaToscaServiceTemplate otherDt = new JpaToscaServiceTemplate(otherDtKey);
110 assertNotEquals(0, tst.compareTo(otherDt));
111 otherDt.setKey(tstKey);
112 assertNotEquals(0, tst.compareTo(otherDt));
113 otherDt.setToscaDefinitionsVersion("Tosca Version");
114 assertNotEquals(0, tst.compareTo(otherDt));
115 otherDt.setDataTypes(dataTypes);
116 assertNotEquals(0, tst.compareTo(otherDt));
117 otherDt.setPolicyTypes(policyTypes);
118 assertNotEquals(0, tst.compareTo(otherDt));
119 otherDt.setTopologyTemplate(ttt);
120 assertEquals(0, tst.compareTo(otherDt));
122 assertEquals(6, tst.getKeys().size());
123 assertEquals(1, new JpaToscaServiceTemplate().getKeys().size());
125 new JpaToscaServiceTemplate().clean();
127 assertEquals(tttClone0, tst);
129 assertTrue(new JpaToscaServiceTemplate().validate(new PfValidationResult()).isValid());
130 assertTrue(tst.validate(new PfValidationResult()).isValid());
132 tst.setDescription(null);
133 assertTrue(tst.validate(new PfValidationResult()).isValid());
134 tst.setDescription("");
135 assertFalse(tst.validate(new PfValidationResult()).isValid());
136 tst.setDescription("A Description");
137 assertTrue(tst.validate(new PfValidationResult()).isValid());
139 assertThatThrownBy(() -> tst.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
141 tst.setToscaDefinitionsVersion(null);
142 PfValidationResult result = tst.validate(new PfValidationResult());
143 assertThat(result.toString()).contains("service template tosca definitions version may not be null");
145 tst.setToscaDefinitionsVersion(JpaToscaServiceTemplate.DEFAULT_TOSCA_DEFINTIONS_VERISON);
146 tst.setDataTypes(null);
147 result = tst.validate(new PfValidationResult());
148 assertTrue(result.isOk());
150 JpaToscaPolicyType pt0 = new JpaToscaPolicyType(new PfConceptKey("pt0:0.0.1"));
151 tst.getPolicyTypes().getConceptMap().put(pt0.getKey(), pt0);
152 result = tst.validate(new PfValidationResult());
153 assertTrue(result.isOk());
155 JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0:0.0.1"));
156 JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop0"));
157 prop0.setType(dt0.getKey());
159 pt0.setProperties(new LinkedHashMap<>());
160 pt0.getProperties().put(prop0.getKey().getLocalName(), prop0);
161 result = tst.validate(new PfValidationResult());
162 assertFalse(result.isOk());
163 assertThat(result.toString()).contains("referenced data type dt0:0.0.1 not found");
165 tst.setDataTypes(null);
166 result = tst.validate(new PfValidationResult());
167 assertFalse(result.isOk());
168 assertThat(result.toString()).contains("referenced data type dt0:0.0.1 not found");
170 tst.setDataTypes(new JpaToscaDataTypes());
171 result = tst.validate(new PfValidationResult());
172 assertFalse(result.isOk());
173 assertThat(result.toString()).contains("referenced data type dt0:0.0.1 not found");
175 tst.getDataTypes().getConceptMap().put(dt0.getKey(), dt0);
176 result = tst.validate(new PfValidationResult());
177 assertTrue(result.isOk());
179 tst.setTopologyTemplate(null);
180 result = tst.validate(new PfValidationResult());
181 assertTrue(result.isOk());
183 tst.setTopologyTemplate(new JpaToscaTopologyTemplate());
184 result = tst.validate(new PfValidationResult());
185 assertTrue(result.isOk());
187 tst.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
188 result = tst.validate(new PfValidationResult());
189 assertTrue(result.isOk());
191 tst.setPolicyTypes(null);
192 result = tst.validate(new PfValidationResult());
193 assertTrue(result.isOk());
195 JpaToscaPolicy pol0 = new JpaToscaPolicy(new PfConceptKey("pol0:0.0.1"));
196 tst.getTopologyTemplate().getPolicies().getConceptMap().put(pol0.getKey(), pol0);
197 result = tst.validate(new PfValidationResult());
198 assertFalse(result.isOk());
199 assertThat(result.toString()).contains("type is null or a null key");
201 pol0.setType(new PfConceptKey("i.dont.Exist:0.0.1"));
202 result = tst.validate(new PfValidationResult());
203 assertFalse(result.isOk());
204 assertThat(result.toString()).contains(
205 "no policy types are defined on the service template for the policies in the topology template");
207 tst.setPolicyTypes(policyTypes);
208 result = tst.validate(new PfValidationResult());
209 assertFalse(result.isOk());
210 assertThat(result.toString()).contains("policy type i.dont.Exist:0.0.1 referenced in policy not found");
212 pol0.setType(dt0.getKey());
213 result = tst.validate(new PfValidationResult());
214 assertFalse(result.isOk());
215 assertThat(result.toString()).contains("policy type dt0:0.0.1 referenced in policy not found");
217 pol0.setType(pt0.getKey());
218 result = tst.validate(new PfValidationResult());
219 assertTrue(result.isOk());
221 tst.setPolicyTypes(null);
222 result = tst.validate(new PfValidationResult());
223 assertFalse(result.isOk());
224 assertThat(result.toString()).contains(
225 "no policy types are defined on the service template for the policies in the topology template");
227 tst.setPolicyTypes(policyTypes);
228 pol0.setType(pt0.getKey());
229 result = tst.validate(new PfValidationResult());
230 assertTrue(result.isOk());
232 tst.setPolicyTypes(new JpaToscaPolicyTypes());
233 result = tst.validate(new PfValidationResult());
234 assertFalse(result.isOk());
235 assertThat(result.toString()).contains(
236 "no policy types are defined on the service template for the policies in the topology template");