Changes for Checkstyle 8.32
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPolicyTypeTest.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.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.LinkedHashMap;
33 import java.util.List;
34 import java.util.Map;
35 import org.junit.Test;
36 import org.onap.policy.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfKey;
38 import org.onap.policy.models.base.PfReferenceKey;
39 import org.onap.policy.models.base.PfValidationResult;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
42
43 /**
44  * DAO test for ToscaPolicyType.
45  *
46  * @author Liam Fallon (liam.fallon@est.tech)
47  */
48 public class JpaToscaPolicyTypeTest {
49     private static final String A_DESCRIPTION = "A Description";
50     private static final String VERSION_001 = "0.0.1";
51
52     @Test
53     public void testPolicyTypePojo() {
54         assertNotNull(new JpaToscaPolicyType());
55         assertNotNull(new JpaToscaPolicyType(new PfConceptKey()));
56         assertNotNull(new JpaToscaPolicyType(new JpaToscaPolicyType()));
57
58         assertThatThrownBy(() -> new JpaToscaPolicyType((PfConceptKey) null))
59                 .hasMessageMatching("key is marked .*on.*ull but is null");
60
61         assertThatThrownBy(() -> new JpaToscaPolicyType((JpaToscaPolicyType) null))
62                 .isInstanceOf(NullPointerException.class);
63
64         PfConceptKey ptKey = new PfConceptKey("tdt", VERSION_001);
65         JpaToscaPolicyType tpt = new JpaToscaPolicyType(ptKey);
66
67         PfConceptKey derivedFromKey = new PfConceptKey("deriveFrom", VERSION_001);
68         tpt.setDerivedFrom(derivedFromKey);
69
70         Map<String, String> metadata = new HashMap<>();
71         metadata.put("key", "value");
72         tpt.setMetadata(metadata);
73         assertEquals(metadata, tpt.getMetadata());
74
75         tpt.setDescription(A_DESCRIPTION);
76
77         PfConceptKey propTypeKey = new PfConceptKey("propType", VERSION_001);
78         Map<String, JpaToscaProperty> properties = new LinkedHashMap<>();
79         JpaToscaProperty tp = new JpaToscaProperty(new PfReferenceKey(ptKey, "aProp"), propTypeKey);
80         properties.put(tp.getKey().getLocalName(), tp);
81         tpt.setProperties(properties);
82         assertEquals(properties, tpt.getProperties());
83
84         List<PfConceptKey> targets = new ArrayList<>();
85         PfConceptKey target = new PfConceptKey("target", VERSION_001);
86         targets.add(target);
87         tpt.setTargets(targets);
88         assertEquals(targets, tpt.getTargets());
89
90         List<JpaToscaTrigger> triggers = new ArrayList<>();
91         JpaToscaTrigger trigger = new JpaToscaTrigger(new PfReferenceKey(ptKey, "aTrigger"), "EventType", "Action");
92         triggers.add(trigger);
93         tpt.setTriggers(triggers);
94         assertEquals(triggers, tpt.getTriggers());
95
96         JpaToscaPolicyType tdtClone0 = new JpaToscaPolicyType(tpt);
97         assertEquals(tpt, tdtClone0);
98         assertEquals(0, tpt.compareTo(tdtClone0));
99
100         JpaToscaPolicyType tdtClone1 = new JpaToscaPolicyType(tpt);
101         assertEquals(tpt, tdtClone1);
102         assertEquals(0, tpt.compareTo(tdtClone1));
103
104         assertEquals(-1, tpt.compareTo(null));
105         assertEquals(0, tpt.compareTo(tpt));
106         assertFalse(tpt.compareTo(tpt.getKey()) == 0);
107
108         PfConceptKey otherDtKey = new PfConceptKey("otherDt", VERSION_001);
109         JpaToscaPolicyType otherDt = new JpaToscaPolicyType(otherDtKey);
110
111         assertFalse(tpt.compareTo(otherDt) == 0);
112         otherDt.setKey(ptKey);
113         assertFalse(tpt.compareTo(otherDt) == 0);
114         otherDt.setDerivedFrom(derivedFromKey);
115         assertFalse(tpt.compareTo(otherDt) == 0);
116         otherDt.setMetadata(metadata);
117         assertFalse(tpt.compareTo(otherDt) == 0);
118         otherDt.setDescription(A_DESCRIPTION);
119         assertFalse(tpt.compareTo(otherDt) == 0);
120         otherDt.setProperties(properties);
121         assertFalse(tpt.compareTo(otherDt) == 0);
122         otherDt.setTargets(targets);
123         assertFalse(tpt.compareTo(otherDt) == 0);
124         otherDt.setTriggers(triggers);
125         assertEquals(0, tpt.compareTo(otherDt));
126
127         assertEquals(6, tpt.getKeys().size());
128         assertEquals(1, new JpaToscaPolicyType().getKeys().size());
129
130         new JpaToscaPolicyType().clean();
131         tpt.clean();
132         assertEquals(tdtClone0, tpt);
133
134         assertFalse(new JpaToscaPolicyType().validate(new PfValidationResult()).isValid());
135         assertTrue(tpt.validate(new PfValidationResult()).isValid());
136
137         tpt.getProperties().put(null, null);
138         assertFalse(tpt.validate(new PfValidationResult()).isValid());
139         tpt.getProperties().remove(null);
140         assertTrue(tpt.validate(new PfValidationResult()).isValid());
141
142         tpt.getTargets().add(null);
143         assertFalse(tpt.validate(new PfValidationResult()).isValid());
144         tpt.getTargets().remove(null);
145         assertTrue(tpt.validate(new PfValidationResult()).isValid());
146
147         tpt.getTriggers().add(null);
148         assertFalse(tpt.validate(new PfValidationResult()).isValid());
149         tpt.getTriggers().remove(null);
150         assertTrue(tpt.validate(new PfValidationResult()).isValid());
151
152         tpt.getMetadata().put(null, null);
153         assertFalse(tpt.validate(new PfValidationResult()).isValid());
154         tpt.getMetadata().remove(null);
155         assertTrue(tpt.validate(new PfValidationResult()).isValid());
156
157         tpt.getMetadata().put("nullKey", null);
158         assertFalse(tpt.validate(new PfValidationResult()).isValid());
159         tpt.getMetadata().remove("nullKey");
160         assertTrue(tpt.validate(new PfValidationResult()).isValid());
161
162         tpt.setDescription("");
163
164         assertFalse(tpt.validate(new PfValidationResult()).isValid());
165         tpt.setDescription(A_DESCRIPTION);
166         assertTrue(tpt.validate(new PfValidationResult()).isValid());
167
168         tpt.setDerivedFrom(PfConceptKey.getNullKey());
169         assertFalse(tpt.validate(new PfValidationResult()).isValid());
170         tpt.setDerivedFrom(derivedFromKey);
171         assertTrue(tpt.validate(new PfValidationResult()).isValid());
172
173         assertThatThrownBy(() -> tpt.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null");
174
175         assertThatThrownBy(() -> new JpaToscaEntityType<ToscaPolicy>((PfConceptKey) null))
176                 .hasMessageMatching("key is marked .*on.*ull but is null");
177
178         assertThatThrownBy(() -> new JpaToscaEntityType<ToscaPolicy>((JpaToscaEntityType<ToscaPolicy>) null))
179                 .isInstanceOf(NullPointerException.class);
180
181         JpaToscaEntityType<ToscaPolicy> tet = new JpaToscaEntityType<>(tpt.getKey());
182         assertEquals(-1, tet.compareTo(null));
183         assertEquals(0, tet.compareTo(tet));
184         assertFalse(tet.compareTo(tet.getKey()) == 0);
185
186         assertNotNull(new JpaToscaPolicyType(new ToscaPolicyType()));
187
188         assertNotNull(new JpaToscaEntityType<ToscaPolicyType>(new ToscaPolicyType()));
189     }
190
191     @Test
192     public void testGetReferencedDataTypes() {
193         JpaToscaPolicyType pt0 = new JpaToscaPolicyType(new PfConceptKey("pt0", "0.0.1"));
194
195         assertTrue(pt0.getReferencedDataTypes().isEmpty());
196
197         pt0.setProperties(new LinkedHashMap<>());
198         assertTrue(pt0.getReferencedDataTypes().isEmpty());
199
200         JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop0"));
201         prop0.setType(new PfConceptKey("string", PfKey.NULL_KEY_VERSION));
202         assertTrue(prop0.validate(new PfValidationResult()).isValid());
203
204         pt0.getProperties().put(prop0.getKey().getLocalName(), prop0);
205         assertTrue(pt0.getReferencedDataTypes().isEmpty());
206
207         JpaToscaProperty prop1 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop1"));
208         prop1.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
209         assertTrue(prop1.validate(new PfValidationResult()).isValid());
210
211         pt0.getProperties().put(prop1.getKey().getLocalName(), prop1);
212         assertEquals(1, pt0.getReferencedDataTypes().size());
213
214         JpaToscaProperty prop2 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop2"));
215         prop2.setType(new PfConceptKey("the.property.Type0", "0.0.1"));
216         assertTrue(prop2.validate(new PfValidationResult()).isValid());
217
218         pt0.getProperties().put(prop2.getKey().getLocalName(), prop2);
219         assertEquals(1, pt0.getReferencedDataTypes().size());
220
221         JpaToscaProperty prop3 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop4"));
222         prop3.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
223         prop3.setEntrySchema(new JpaToscaEntrySchema());
224         prop3.getEntrySchema().setType(new PfConceptKey("the.property.Type3", "0.0.1"));
225         assertTrue(prop3.validate(new PfValidationResult()).isValid());
226
227         pt0.getProperties().put(prop3.getKey().getLocalName(), prop3);
228         assertEquals(3, pt0.getReferencedDataTypes().size());
229
230         JpaToscaProperty prop4 = new JpaToscaProperty(new PfReferenceKey(pt0.getKey(), "prop4"));
231         prop4.setType(new PfConceptKey("the.property.Type1", "0.0.1"));
232         prop4.setEntrySchema(new JpaToscaEntrySchema());
233         prop4.getEntrySchema().setType(new PfConceptKey("the.property.Type2", "0.0.1"));
234         assertTrue(prop4.validate(new PfValidationResult()).isValid());
235
236         pt0.getProperties().put(prop4.getKey().getLocalName(), prop4);
237         assertEquals(3, pt0.getReferencedDataTypes().size());
238     }
239 }