bb0d2dcb59ce4b8a9564f0194592500c67d90a94
[policy/models.git] /
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.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfReferenceKey;
38 import org.onap.policy.models.base.PfValidationResult;
39
40 /**
41  * DAO test for ToscaDatatype.
42  *
43  * @author Liam Fallon (liam.fallon@est.tech)
44  */
45 public class JpaToscaServiceTemplateTest {
46
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";
49
50     @Test
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()));
56
57         assertThatThrownBy(() -> new JpaToscaServiceTemplate((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL);
58
59         assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, null)).hasMessageMatching(KEY_IS_NULL);
60
61         assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, "")).hasMessageMatching(KEY_IS_NULL);
62
63         assertThatThrownBy(() -> new JpaToscaServiceTemplate(new PfConceptKey(), null))
64                 .hasMessageMatching("toscaDefinitionsVersion is marked .*on.*ull but is null");
65
66         assertThatThrownBy(() -> new JpaToscaServiceTemplate((JpaToscaServiceTemplate) null))
67                 .isInstanceOf(NullPointerException.class);
68
69         PfConceptKey tstKey = new PfConceptKey("tst", VERSION_001);
70         JpaToscaServiceTemplate tst = new JpaToscaServiceTemplate(tstKey, "Tosca Version");
71
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());
80
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());
89
90         PfReferenceKey tttKey = new PfReferenceKey(tstKey, "TopologyTemplate");
91         JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
92         tst.setTopologyTemplate(ttt);
93         assertEquals(ttt, tst.getTopologyTemplate());
94
95         JpaToscaServiceTemplate tttClone0 = new JpaToscaServiceTemplate(tst);
96         assertEquals(tst, tttClone0);
97         assertEquals(0, tst.compareTo(tttClone0));
98
99         JpaToscaServiceTemplate tttClone1 = new JpaToscaServiceTemplate(tst);
100         assertEquals(tst, tttClone1);
101         assertEquals(0, tst.compareTo(tttClone1));
102
103         assertEquals(-1, tst.compareTo(null));
104         assertEquals(0, tst.compareTo(tst));
105         assertNotEquals(0, tst.compareTo(tst.getKey()));
106
107         PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
108         JpaToscaServiceTemplate otherDt = new JpaToscaServiceTemplate(otherDtKey);
109
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));
121
122         assertEquals(6, tst.getKeys().size());
123         assertEquals(1, new JpaToscaServiceTemplate().getKeys().size());
124
125         new JpaToscaServiceTemplate().clean();
126         tst.clean();
127         assertEquals(tttClone0, tst);
128
129         assertTrue(new JpaToscaServiceTemplate().validate(new PfValidationResult()).isValid());
130         assertTrue(tst.validate(new PfValidationResult()).isValid());
131
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());
138
139         assertThatThrownBy(() -> tst.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
140
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");
144
145         tst.setToscaDefinitionsVersion(JpaToscaServiceTemplate.DEFAULT_TOSCA_DEFINTIONS_VERISON);
146         tst.setDataTypes(null);
147         result = tst.validate(new PfValidationResult());
148         assertTrue(result.isOk());
149
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());
154
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());
158
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");
164
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");
169
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");
174
175         tst.getDataTypes().getConceptMap().put(dt0.getKey(), dt0);
176         result = tst.validate(new PfValidationResult());
177         assertTrue(result.isOk());
178
179         tst.setTopologyTemplate(null);
180         result = tst.validate(new PfValidationResult());
181         assertTrue(result.isOk());
182
183         tst.setTopologyTemplate(new JpaToscaTopologyTemplate());
184         result = tst.validate(new PfValidationResult());
185         assertTrue(result.isOk());
186
187         tst.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
188         result = tst.validate(new PfValidationResult());
189         assertTrue(result.isOk());
190
191         tst.setPolicyTypes(null);
192         result = tst.validate(new PfValidationResult());
193         assertTrue(result.isOk());
194
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");
200
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");
206
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");
211
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");
216
217         pol0.setType(pt0.getKey());
218         result = tst.validate(new PfValidationResult());
219         assertTrue(result.isOk());
220
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");
226
227         tst.setPolicyTypes(policyTypes);
228         pol0.setType(pt0.getKey());
229         result = tst.validate(new PfValidationResult());
230         assertTrue(result.isOk());
231
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");
237
238     }
239 }