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