8f366b32183fbf85591fab6e6bb9a1158139c0b7
[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-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
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 testServiceTemplatePojo() {
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         PfConceptKey tstKey = new PfConceptKey("tst", VERSION_001);
71         JpaToscaServiceTemplate tst = new JpaToscaServiceTemplate(tstKey, "Tosca Version");
72
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());
81
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());
90
91         PfReferenceKey tttKey = new PfReferenceKey(tstKey, "TopologyTemplate");
92         JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
93         tst.setTopologyTemplate(ttt);
94         assertEquals(ttt, tst.getTopologyTemplate());
95
96         JpaToscaServiceTemplate tttClone0 = new JpaToscaServiceTemplate(tst);
97         assertEquals(tst, tttClone0);
98         assertEquals(0, tst.compareTo(tttClone0));
99
100         JpaToscaServiceTemplate tttClone1 = new JpaToscaServiceTemplate(tst);
101         assertEquals(tst, tttClone1);
102         assertEquals(0, tst.compareTo(tttClone1));
103
104         assertEquals(-1, tst.compareTo(null));
105         assertEquals(0, tst.compareTo(tst));
106         assertNotEquals(0, tst.compareTo(tst.getKey()));
107
108         PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
109         JpaToscaServiceTemplate otherDt = new JpaToscaServiceTemplate(otherDtKey);
110
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));
122
123         assertEquals(6, tst.getKeys().size());
124         assertEquals(1, new JpaToscaServiceTemplate().getKeys().size());
125
126         new JpaToscaServiceTemplate().clean();
127         tst.clean();
128         assertEquals(tttClone0, tst);
129
130         assertTrue(new JpaToscaServiceTemplate().validate("").isValid());
131         assertTrue(tst.validate("").isValid());
132
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());
139
140         assertThatThrownBy(() -> tst.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
141
142         tst.setToscaDefinitionsVersion(null);
143         BeanValidationResult result = tst.validate("");
144         assertThat(result.getResult()).contains("toscaDefinitionsVersion").contains(Validated.IS_NULL);
145
146         tst.setToscaDefinitionsVersion(JpaToscaServiceTemplate.DEFAULT_TOSCA_DEFINTIONS_VERISON);
147         tst.setDataTypes(null);
148         result = tst.validate("");
149         assertTrue(result.isValid());
150
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());
155
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());
159
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);
165
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);
170
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);
175
176         tst.getDataTypes().getConceptMap().put(dt0.getKey(), dt0);
177         result = tst.validate("");
178         assertTrue(result.isValid());
179
180         tst.setTopologyTemplate(null);
181         result = tst.validate("");
182         assertTrue(result.isValid());
183
184         tst.setTopologyTemplate(new JpaToscaTopologyTemplate());
185         result = tst.validate("");
186         assertTrue(result.isValid());
187
188         tst.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
189         result = tst.validate("");
190         assertTrue(result.isValid());
191
192         tst.setPolicyTypes(null);
193         result = tst.validate("");
194         assertTrue(result.isValid());
195
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);
201
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");
207
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);
213
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);
218
219         pol0.setType(pt0.getKey());
220         result = tst.validate("");
221         assertTrue(result.isValid());
222
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");
228
229         tst.setPolicyTypes(policyTypes);
230         pol0.setType(pt0.getKey());
231         result = tst.validate("");
232         assertTrue(result.isValid());
233
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");
239
240     }
241 }