5cbec008b1633491b392b9cf33e24c7e4b84f88f
[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 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.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30
31 import java.util.Map;
32 import java.util.TreeMap;
33
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;
38
39 /**
40  * DAO test for ToscaDatatype.
41  *
42  * @author Liam Fallon (liam.fallon@est.tech)
43  */
44 public class JpaToscaServiceTemplateTest {
45
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";
48
49     @Test
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()));
55
56         assertThatThrownBy(() -> new JpaToscaServiceTemplate((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL);
57
58         assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, null)).hasMessageMatching(KEY_IS_NULL);
59
60         assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, "")).hasMessageMatching(KEY_IS_NULL);
61
62         assertThatThrownBy(() -> new JpaToscaServiceTemplate(new PfConceptKey(), null))
63                 .hasMessageMatching("toscaDefinitionsVersion is marked .*on.*ull but is null");
64
65         assertThatThrownBy(() -> new JpaToscaServiceTemplate((JpaToscaServiceTemplate) null))
66                 .isInstanceOf(NullPointerException.class);
67
68         PfConceptKey tstKey = new PfConceptKey("tst", VERSION_001);
69         JpaToscaServiceTemplate tst = new JpaToscaServiceTemplate(tstKey, "Tosca Version");
70
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());
79
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());
88
89         PfReferenceKey tttKey = new PfReferenceKey(tstKey, "TopologyTemplate");
90         JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey);
91         tst.setTopologyTemplate(ttt);
92         assertEquals(ttt, tst.getTopologyTemplate());
93
94         JpaToscaServiceTemplate tttClone0 = new JpaToscaServiceTemplate(tst);
95         assertEquals(tst, tttClone0);
96         assertEquals(0, tst.compareTo(tttClone0));
97
98         JpaToscaServiceTemplate tttClone1 = new JpaToscaServiceTemplate(tst);
99         assertEquals(tst, tttClone1);
100         assertEquals(0, tst.compareTo(tttClone1));
101
102         assertEquals(-1, tst.compareTo(null));
103         assertEquals(0, tst.compareTo(tst));
104         assertFalse(tst.compareTo(tst.getKey()) == 0);
105
106         PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
107         JpaToscaServiceTemplate otherDt = new JpaToscaServiceTemplate(otherDtKey);
108
109         assertFalse(tst.compareTo(otherDt) == 0);
110         otherDt.setKey(tstKey);
111         assertFalse(tst.compareTo(otherDt) == 0);
112         otherDt.setToscaDefinitionsVersion("Tosca Version");
113         assertFalse(tst.compareTo(otherDt) == 0);
114         otherDt.setDataTypes(dataTypes);
115         assertFalse(tst.compareTo(otherDt) == 0);
116         otherDt.setPolicyTypes(policyTypes);
117         assertFalse(tst.compareTo(otherDt) == 0);
118         otherDt.setTopologyTemplate(ttt);
119         assertEquals(0, tst.compareTo(otherDt));
120
121         assertEquals(6, tst.getKeys().size());
122         assertEquals(1, new JpaToscaServiceTemplate().getKeys().size());
123
124         new JpaToscaServiceTemplate().clean();
125         tst.clean();
126         assertEquals(tttClone0, tst);
127
128         assertTrue(new JpaToscaServiceTemplate().validate(new PfValidationResult()).isValid());
129         assertTrue(tst.validate(new PfValidationResult()).isValid());
130
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());
137
138         assertThatThrownBy(() -> tst.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
139
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");
143
144         tst.setToscaDefinitionsVersion(JpaToscaServiceTemplate.DEFAULT_TOSCA_DEFINTIONS_VERISON);
145         tst.setDataTypes(null);
146         result = tst.validate(new PfValidationResult());
147         assertTrue(result.isOk());
148
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());
153
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");
161
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");
166
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");
171
172         tst.getDataTypes().getConceptMap().put(dt0.getKey(), dt0);
173         result = tst.validate(new PfValidationResult());
174         assertTrue(result.isOk());
175
176         tst.setTopologyTemplate(null);
177         result = tst.validate(new PfValidationResult());
178         assertTrue(result.isOk());
179
180         tst.setTopologyTemplate(new JpaToscaTopologyTemplate());
181         result = tst.validate(new PfValidationResult());
182         assertTrue(result.isOk());
183
184         tst.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
185         result = tst.validate(new PfValidationResult());
186         assertTrue(result.isOk());
187
188         tst.setPolicyTypes(null);
189         result = tst.validate(new PfValidationResult());
190         assertTrue(result.isOk());
191
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");
197
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");
203
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");
208
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");
213
214         pol0.setType(pt0.getKey());
215         result = tst.validate(new PfValidationResult());
216         assertTrue(result.isOk());
217
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");
223
224         tst.setPolicyTypes(policyTypes);
225         pol0.setType(pt0.getKey());
226         result = tst.validate(new PfValidationResult());
227         assertTrue(result.isOk());
228
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");
234
235     }
236 }