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;
33 import java.util.TreeMap;
34 import org.junit.Test;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfReferenceKey;
37 import org.onap.policy.models.base.PfValidationResult;
40 * DAO test for ToscaDatatype.
42 * @author Liam Fallon (liam.fallon@est.tech)
44 public class JpaToscaServiceTemplateTest {
46 private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null";
47 private static final String VERSION_001 = "0.0.1";
50 public void testServiceTemplatePojo() {
51 assertNotNull(new JpaToscaServiceTemplate());
52 assertNotNull(new JpaToscaServiceTemplate(new PfConceptKey()));
53 assertNotNull(new JpaToscaServiceTemplate(new PfConceptKey(), ""));
54 assertNotNull(new JpaToscaServiceTemplate(new JpaToscaServiceTemplate()));
56 assertThatThrownBy(() -> new JpaToscaServiceTemplate((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL);
58 assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, null)).hasMessageMatching(KEY_IS_NULL);
60 assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, "")).hasMessageMatching(KEY_IS_NULL);
62 assertThatThrownBy(() -> new JpaToscaServiceTemplate(new PfConceptKey(), null))
63 .hasMessageMatching("toscaDefinitionsVersion is marked .*on.*ull but is null");
65 assertThatThrownBy(() -> new JpaToscaServiceTemplate((JpaToscaServiceTemplate) null))
66 .isInstanceOf(NullPointerException.class);
68 PfConceptKey tstKey = new PfConceptKey("tst", VERSION_001);
69 JpaToscaServiceTemplate tst = new JpaToscaServiceTemplate(tstKey, "Tosca Version");
71 PfConceptKey dataTypeKey = new PfConceptKey("DataType", VERSION_001);
72 JpaToscaDataType dataType0 = new JpaToscaDataType(dataTypeKey);
73 PfConceptKey dtsKey = new PfConceptKey("dts", VERSION_001);
74 Map<PfConceptKey, JpaToscaDataType> dataTypeMap = new TreeMap<>();
75 dataTypeMap.put(dataTypeKey, dataType0);
76 JpaToscaDataTypes dataTypes = new JpaToscaDataTypes(dtsKey, dataTypeMap);
77 tst.setDataTypes(dataTypes);
78 assertEquals(dataTypes, tst.getDataTypes());
80 PfConceptKey policyTypeKey = new PfConceptKey("DataType", VERSION_001);
81 JpaToscaPolicyType policyType0 = new JpaToscaPolicyType(policyTypeKey);
82 PfConceptKey ptsKey = new PfConceptKey("dts", VERSION_001);
83 Map<PfConceptKey, JpaToscaPolicyType> policyTypeMap = new TreeMap<>();
84 policyTypeMap.put(policyTypeKey, policyType0);
85 JpaToscaPolicyTypes policyTypes = new JpaToscaPolicyTypes(ptsKey, policyTypeMap);
86 tst.setPolicyTypes(policyTypes);
87 assertEquals(policyTypes, tst.getPolicyTypes());
89 PfReferenceKey tttKey = new PfReferenceKey(tstKey, "TopologyTemplate");
90 JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
91 tst.setTopologyTemplate(ttt);
92 assertEquals(ttt, tst.getTopologyTemplate());
94 JpaToscaServiceTemplate tttClone0 = new JpaToscaServiceTemplate(tst);
95 assertEquals(tst, tttClone0);
96 assertEquals(0, tst.compareTo(tttClone0));
98 JpaToscaServiceTemplate tttClone1 = new JpaToscaServiceTemplate(tst);
99 assertEquals(tst, tttClone1);
100 assertEquals(0, tst.compareTo(tttClone1));
102 assertEquals(-1, tst.compareTo(null));
103 assertEquals(0, tst.compareTo(tst));
104 assertNotEquals(0, tst.compareTo(tst.getKey()));
106 PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
107 JpaToscaServiceTemplate otherDt = new JpaToscaServiceTemplate(otherDtKey);
109 assertNotEquals(0, tst.compareTo(otherDt));
110 otherDt.setKey(tstKey);
111 assertNotEquals(0, tst.compareTo(otherDt));
112 otherDt.setToscaDefinitionsVersion("Tosca Version");
113 assertNotEquals(0, tst.compareTo(otherDt));
114 otherDt.setDataTypes(dataTypes);
115 assertNotEquals(0, tst.compareTo(otherDt));
116 otherDt.setPolicyTypes(policyTypes);
117 assertNotEquals(0, tst.compareTo(otherDt));
118 otherDt.setTopologyTemplate(ttt);
119 assertEquals(0, tst.compareTo(otherDt));
121 assertEquals(6, tst.getKeys().size());
122 assertEquals(1, new JpaToscaServiceTemplate().getKeys().size());
124 new JpaToscaServiceTemplate().clean();
126 assertEquals(tttClone0, tst);
128 assertTrue(new JpaToscaServiceTemplate().validate(new PfValidationResult()).isValid());
129 assertTrue(tst.validate(new PfValidationResult()).isValid());
131 tst.setDescription(null);
132 assertTrue(tst.validate(new PfValidationResult()).isValid());
133 tst.setDescription("");
134 assertFalse(tst.validate(new PfValidationResult()).isValid());
135 tst.setDescription("A Description");
136 assertTrue(tst.validate(new PfValidationResult()).isValid());
138 assertThatThrownBy(() -> tst.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
140 tst.setToscaDefinitionsVersion(null);
141 PfValidationResult result = tst.validate(new PfValidationResult());
142 assertThat(result.toString()).contains("service template tosca definitions version may not be null");
144 tst.setToscaDefinitionsVersion(JpaToscaServiceTemplate.DEFAULT_TOSCA_DEFINTIONS_VERISON);
145 tst.setDataTypes(null);
146 result = tst.validate(new PfValidationResult());
147 assertTrue(result.isOk());
149 JpaToscaPolicyType pt0 = new JpaToscaPolicyType(new PfConceptKey("pt0:0.0.1"));
150 tst.getPolicyTypes().getConceptMap().put(pt0.getKey(), pt0);
151 result = tst.validate(new PfValidationResult());
152 assertTrue(result.isOk());
154 JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0:0.0.1"));
155 JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop0"));
156 prop0.setType(dt0.getKey());
157 pt0.getProperties().put(prop0.getKey().getLocalName(), prop0);
158 result = tst.validate(new PfValidationResult());
159 assertFalse(result.isOk());
160 assertThat(result.toString()).contains("referenced data type dt0:0.0.1 not found");
162 tst.setDataTypes(null);
163 result = tst.validate(new PfValidationResult());
164 assertFalse(result.isOk());
165 assertThat(result.toString()).contains("referenced data type dt0:0.0.1 not found");
167 tst.setDataTypes(new JpaToscaDataTypes());
168 result = tst.validate(new PfValidationResult());
169 assertFalse(result.isOk());
170 assertThat(result.toString()).contains("referenced data type dt0:0.0.1 not found");
172 tst.getDataTypes().getConceptMap().put(dt0.getKey(), dt0);
173 result = tst.validate(new PfValidationResult());
174 assertTrue(result.isOk());
176 tst.setTopologyTemplate(null);
177 result = tst.validate(new PfValidationResult());
178 assertTrue(result.isOk());
180 tst.setTopologyTemplate(new JpaToscaTopologyTemplate());
181 result = tst.validate(new PfValidationResult());
182 assertTrue(result.isOk());
184 tst.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
185 result = tst.validate(new PfValidationResult());
186 assertTrue(result.isOk());
188 tst.setPolicyTypes(null);
189 result = tst.validate(new PfValidationResult());
190 assertTrue(result.isOk());
192 JpaToscaPolicy pol0 = new JpaToscaPolicy(new PfConceptKey("pol0:0.0.1"));
193 tst.getTopologyTemplate().getPolicies().getConceptMap().put(pol0.getKey(), pol0);
194 result = tst.validate(new PfValidationResult());
195 assertFalse(result.isOk());
196 assertThat(result.toString()).contains("type is null or a null key");
198 pol0.setType(new PfConceptKey("i.dont.Exist:0.0.1"));
199 result = tst.validate(new PfValidationResult());
200 assertFalse(result.isOk());
201 assertThat(result.toString()).contains(
202 "no policy types are defined on the service template for the policies in the topology template");
204 tst.setPolicyTypes(policyTypes);
205 result = tst.validate(new PfValidationResult());
206 assertFalse(result.isOk());
207 assertThat(result.toString()).contains("policy type i.dont.Exist:0.0.1 referenced in policy not found");
209 pol0.setType(dt0.getKey());
210 result = tst.validate(new PfValidationResult());
211 assertFalse(result.isOk());
212 assertThat(result.toString()).contains("policy type dt0:0.0.1 referenced in policy not found");
214 pol0.setType(pt0.getKey());
215 result = tst.validate(new PfValidationResult());
216 assertTrue(result.isOk());
218 tst.setPolicyTypes(null);
219 result = tst.validate(new PfValidationResult());
220 assertFalse(result.isOk());
221 assertThat(result.toString()).contains(
222 "no policy types are defined on the service template for the policies in the topology template");
224 tst.setPolicyTypes(policyTypes);
225 pol0.setType(pt0.getKey());
226 result = tst.validate(new PfValidationResult());
227 assertTrue(result.isOk());
229 tst.setPolicyTypes(new JpaToscaPolicyTypes());
230 result = tst.validate(new PfValidationResult());
231 assertFalse(result.isOk());
232 assertThat(result.toString()).contains(
233 "no policy types are defined on the service template for the policies in the topology template");