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