9a91a955861a373facdfc57b74f827c4e6e4ff30
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaServiceTemplateTest.java
1 /*-
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
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.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;
31
32 import java.util.LinkedHashMap;
33 import java.util.Map;
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;
40
41 /**
42  * DAO test for ToscaDatatype.
43  *
44  * @author Liam Fallon (liam.fallon@est.tech)
45  */
46 public class JpaToscaServiceTemplateTest {
47
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";
50
51     @Test
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()));
57
58         assertThatThrownBy(() -> new JpaToscaServiceTemplate((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL);
59
60         assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, null)).hasMessageMatching(KEY_IS_NULL);
61
62         assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, "")).hasMessageMatching(KEY_IS_NULL);
63
64         assertThatThrownBy(() -> new JpaToscaServiceTemplate(new PfConceptKey(), null))
65                 .hasMessageMatching("toscaDefinitionsVersion is marked .*on.*ull but is null");
66
67         assertThatThrownBy(() -> new JpaToscaServiceTemplate((JpaToscaServiceTemplate) null))
68                 .isInstanceOf(NullPointerException.class);
69     }
70
71     @Test
72     public void testServiceTemplatePojo() {
73         PfConceptKey tstKey = new PfConceptKey("tst", VERSION_001);
74         JpaToscaServiceTemplate tst = new JpaToscaServiceTemplate(tstKey, "Tosca Version");
75
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());
84
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());
93
94         PfReferenceKey tttKey = new PfReferenceKey(tstKey, "TopologyTemplate");
95         JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
96         tst.setTopologyTemplate(ttt);
97         assertEquals(ttt, tst.getTopologyTemplate());
98
99         JpaToscaServiceTemplate tttClone0 = new JpaToscaServiceTemplate(tst);
100         assertEquals(tst, tttClone0);
101         assertEquals(0, tst.compareTo(tttClone0));
102
103         JpaToscaServiceTemplate tttClone1 = new JpaToscaServiceTemplate(tst);
104         assertEquals(tst, tttClone1);
105         assertEquals(0, tst.compareTo(tttClone1));
106
107         assertEquals(-1, tst.compareTo(null));
108         assertEquals(0, tst.compareTo(tst));
109         assertNotEquals(0, tst.compareTo(tst.getKey()));
110
111         PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
112         JpaToscaServiceTemplate otherDt = new JpaToscaServiceTemplate(otherDtKey);
113
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));
125
126         assertEquals(6, tst.getKeys().size());
127         assertEquals(1, new JpaToscaServiceTemplate().getKeys().size());
128
129         new JpaToscaServiceTemplate().clean();
130         tst.clean();
131         assertEquals(tttClone0, tst);
132
133         assertTrue(new JpaToscaServiceTemplate().validate("").isValid());
134         assertTrue(tst.validate("").isValid());
135
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());
142
143         assertThatThrownBy(() -> tst.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
144
145         tst.setToscaDefinitionsVersion(null);
146         BeanValidationResult result = tst.validate("");
147         assertThat(result.getResult()).contains("tosca_definitions_version").contains(Validated.IS_NULL);
148
149         tst.setToscaDefinitionsVersion(JpaToscaServiceTemplate.DEFAULT_TOSCA_DEFINTIONS_VERISON);
150         tst.setDataTypes(null);
151         result = tst.validate("");
152         assertTrue(result.isValid());
153
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());
158
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());
162
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);
168
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);
173
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);
178
179         tst.getDataTypes().getConceptMap().put(dt0.getKey(), dt0);
180         result = tst.validate("");
181         assertTrue(result.isValid());
182
183         tst.setTopologyTemplate(null);
184         result = tst.validate("");
185         assertTrue(result.isValid());
186
187         tst.setTopologyTemplate(new JpaToscaTopologyTemplate());
188         result = tst.validate("");
189         assertTrue(result.isValid());
190
191         tst.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
192         result = tst.validate("");
193         assertTrue(result.isValid());
194
195         tst.setPolicyTypes(null);
196         result = tst.validate("");
197         assertTrue(result.isValid());
198
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);
204
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");
210
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);
216
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);
221
222         pol0.setType(pt0.getKey());
223         result = tst.validate("");
224         assertTrue(result.isValid());
225
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");
231
232         tst.setPolicyTypes(policyTypes);
233         pol0.setType(pt0.getKey());
234         result = tst.validate("");
235         assertTrue(result.isValid());
236
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");
242
243     }
244 }