2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.models.acm.persistence.concepts;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertNotEquals;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.assertNull;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
33 import java.util.UUID;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
38 import org.onap.policy.clamp.models.acm.concepts.Participant;
39 import org.onap.policy.common.utils.coder.CoderException;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.models.base.PfConceptKey;
42 import org.onap.policy.models.base.PfKey;
43 import org.onap.policy.models.base.PfReferenceKey;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47 * Test the {@link JpaAutomationCompositionElement} class.
49 class JpaAutomationCompositionElementTest {
51 private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
54 void testJpaAutomationCompositionElementConstructor() {
55 assertThatThrownBy(() -> {
56 new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null);
57 }).hasMessageMatching("copyConcept is marked .*ull but is null");
59 assertThatThrownBy(() -> {
60 new JpaAutomationCompositionElement((PfReferenceKey) null);
61 }).hasMessageMatching(NULL_KEY_ERROR);
63 assertThatThrownBy(() -> {
64 new JpaAutomationCompositionElement(null, null, null, null);
65 }).hasMessageMatching(NULL_KEY_ERROR);
67 assertThatThrownBy(() -> {
68 new JpaAutomationCompositionElement(null, null, null, AutomationCompositionState.UNINITIALISED);
69 }).hasMessageMatching(NULL_KEY_ERROR);
71 assertThatThrownBy(() -> {
72 new JpaAutomationCompositionElement(null, null, new PfConceptKey("participant", "0.0.1"), null);
73 }).hasMessageMatching(NULL_KEY_ERROR);
75 assertThatThrownBy(() -> {
76 new JpaAutomationCompositionElement(null, null, new PfConceptKey("participant", "0.0.1"),
77 AutomationCompositionState.UNINITIALISED);
78 }).hasMessageMatching(NULL_KEY_ERROR);
80 assertThatThrownBy(() -> {
81 new JpaAutomationCompositionElement(null, new PfConceptKey(), null, null);
82 }).hasMessageMatching(NULL_KEY_ERROR);
84 assertThatThrownBy(() -> {
85 new JpaAutomationCompositionElement(null, new PfConceptKey(), null,
86 AutomationCompositionState.UNINITIALISED);
87 }).hasMessageMatching(NULL_KEY_ERROR);
89 assertThatThrownBy(() -> {
90 new JpaAutomationCompositionElement(null, new PfConceptKey(), new PfConceptKey("participant", "0.0.1"),
92 }).hasMessageMatching(NULL_KEY_ERROR);
94 assertThatThrownBy(() -> {
95 new JpaAutomationCompositionElement(null, new PfConceptKey(), new PfConceptKey("participant", "0.0.1"),
96 AutomationCompositionState.UNINITIALISED);
97 }).hasMessageMatching(NULL_KEY_ERROR);
99 assertThatThrownBy(() -> {
100 new JpaAutomationCompositionElement(new PfReferenceKey(), null, null, null);
101 }).hasMessageMatching("definition is marked .*ull but is null");
103 assertThatThrownBy(() -> {
104 new JpaAutomationCompositionElement(new PfReferenceKey(), null, null,
105 AutomationCompositionState.UNINITIALISED);
106 }).hasMessageMatching("definition is marked .*ull but is null");
108 assertThatThrownBy(() -> {
109 new JpaAutomationCompositionElement(new PfReferenceKey(), null, new PfConceptKey("participant", "0.0.1"),
111 }).hasMessageMatching("definition is marked .*ull but is null");
113 assertThatThrownBy(() -> {
114 new JpaAutomationCompositionElement(new PfReferenceKey(), null, new PfConceptKey("participant", "0.0.1"),
115 AutomationCompositionState.UNINITIALISED);
116 }).hasMessageMatching("definition is marked .*ull but is null");
118 assertThatThrownBy(() -> {
119 new JpaAutomationCompositionElement(new PfReferenceKey(), new PfConceptKey(), null, null);
120 }).hasMessageMatching("participantType is marked .*ull but is null");
122 assertThatThrownBy(() -> {
123 new JpaAutomationCompositionElement(new PfReferenceKey(), new PfConceptKey(), null,
124 AutomationCompositionState.UNINITIALISED);
125 }).hasMessageMatching("participantType is marked .*ull but is null");
127 assertThatThrownBy(() -> {
128 new JpaAutomationCompositionElement(new PfReferenceKey(), new PfConceptKey(),
129 new PfConceptKey("participant", "0.0.1"), null);
130 }).hasMessageMatching("state is marked .*ull but is null");
132 assertNotNull(new JpaAutomationCompositionElement());
133 assertNotNull(new JpaAutomationCompositionElement((new PfReferenceKey())));
134 assertNotNull(new JpaAutomationCompositionElement(new PfReferenceKey(), new PfConceptKey(),
135 new PfConceptKey("participant", "0.0.1"), AutomationCompositionState.UNINITIALISED));
139 void testJpaAutomationCompositionElement() {
140 var testJpaAutomationCompositionElement =
141 createJpaAutomationCompositionElementInstance();
143 var ace = createAutomationCompositionElementInstance();
144 assertEquals(ace, testJpaAutomationCompositionElement.toAuthorative());
146 assertThatThrownBy(() -> {
147 testJpaAutomationCompositionElement.fromAuthorative(null);
148 }).hasMessageMatching("element is marked .*ull but is null");
150 assertThatThrownBy(() -> new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null))
151 .isInstanceOf(NullPointerException.class);
153 var testJpaAutomationCompositionElementFa = new JpaAutomationCompositionElement();
154 testJpaAutomationCompositionElementFa.setKey(null);
155 testJpaAutomationCompositionElementFa.fromAuthorative(ace);
156 assertEquals(testJpaAutomationCompositionElement, testJpaAutomationCompositionElementFa);
157 testJpaAutomationCompositionElementFa.setKey(PfReferenceKey.getNullKey());
158 testJpaAutomationCompositionElementFa.fromAuthorative(ace);
159 assertEquals(testJpaAutomationCompositionElement, testJpaAutomationCompositionElementFa);
160 testJpaAutomationCompositionElementFa.setKey(
161 new PfReferenceKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, "a95757ba-b34a-4049-a2a8-46773abcbe5e"));
162 testJpaAutomationCompositionElementFa.fromAuthorative(ace);
163 assertEquals(testJpaAutomationCompositionElement, testJpaAutomationCompositionElementFa);
165 assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e",
166 testJpaAutomationCompositionElement.getKey().getLocalName());
167 assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e",
168 new JpaAutomationCompositionElement(createAutomationCompositionElementInstance()).getKey().getLocalName());
169 assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e",
170 ((PfReferenceKey) new JpaAutomationCompositionElement(createAutomationCompositionElementInstance())
171 .getKeys().get(0)).getLocalName());
173 testJpaAutomationCompositionElement.clean();
174 assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e",
175 testJpaAutomationCompositionElement.getKey().getLocalName());
177 testJpaAutomationCompositionElement.setDescription(" A Message ");
178 testJpaAutomationCompositionElement.clean();
179 assertEquals("A Message", testJpaAutomationCompositionElement.getDescription());
181 var testJpaAutomationCompositionElement2 =
182 new JpaAutomationCompositionElement(testJpaAutomationCompositionElement);
183 assertEquals(testJpaAutomationCompositionElement, testJpaAutomationCompositionElement2);
187 void testJpaAutomationCompositionElementOrderedState() throws CoderException {
188 var testAutomationCompositionElement = createAutomationCompositionElementInstance();
189 var testJpaAutomationCompositionElement =
190 createJpaAutomationCompositionElementInstance();
192 testJpaAutomationCompositionElement.setOrderedState(null);
193 assertEquals(testAutomationCompositionElement, testJpaAutomationCompositionElement.toAuthorative());
194 testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
196 var noOrderedStateAce = new StandardCoder().decode(
197 new File("src/test/resources/json/AutomationCompositionElementNoOrderedState.json"),
198 AutomationCompositionElement.class);
200 var noOrderedStateJpaAce = new JpaAutomationCompositionElement(noOrderedStateAce);
201 assertNull(noOrderedStateJpaAce.getOrderedState());
202 noOrderedStateAce.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
203 noOrderedStateJpaAce = new JpaAutomationCompositionElement(noOrderedStateAce);
204 assertEquals(testJpaAutomationCompositionElement, noOrderedStateJpaAce);
208 void testJpaAutomationCompositionElementValidation() {
209 var testJpaAutomationCompositionElement =
210 createJpaAutomationCompositionElementInstance();
212 assertThatThrownBy(() -> testJpaAutomationCompositionElement.validate(null))
213 .hasMessageMatching("fieldName is marked .*ull but is null");
215 assertTrue(testJpaAutomationCompositionElement.validate("").isValid());
219 void testJpaAutomationCompositionElementCompareTo() {
220 var testJpaAutomationCompositionElement =
221 createJpaAutomationCompositionElementInstance();
223 var otherJpaAutomationCompositionElement =
224 new JpaAutomationCompositionElement(testJpaAutomationCompositionElement);
225 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
226 assertEquals(-1, testJpaAutomationCompositionElement.compareTo(null));
227 assertEquals(0, testJpaAutomationCompositionElement.compareTo(testJpaAutomationCompositionElement));
229 testJpaAutomationCompositionElement.compareTo(new DummyJpaAutomationCompositionElementChild()));
231 testJpaAutomationCompositionElement
232 .setKey(new PfReferenceKey("BadValue", "0.0.1", "a95757ba-b34a-4049-a2a8-46773abcbe5e"));
233 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
234 testJpaAutomationCompositionElement.setKey(
235 new PfReferenceKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, "a95757ba-b34a-4049-a2a8-46773abcbe5e"));
236 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
238 testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("BadValue", "0.0.1"));
239 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
240 testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("aceDef", "0.0.1"));
241 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
243 testJpaAutomationCompositionElement.setDescription("Description");
244 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
245 testJpaAutomationCompositionElement.setDescription(null);
246 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
248 testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.PASSIVE);
249 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
250 testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
251 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
253 testJpaAutomationCompositionElement.setState(AutomationCompositionState.PASSIVE);
254 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
255 testJpaAutomationCompositionElement.setState(AutomationCompositionState.UNINITIALISED);
256 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
258 testJpaAutomationCompositionElement.setParticipantType(new PfConceptKey("dummy", "0.0.1"));
259 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
260 testJpaAutomationCompositionElement.setParticipantType(new PfConceptKey("participantType", "0.0.1"));
261 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
263 assertEquals(testJpaAutomationCompositionElement,
264 new JpaAutomationCompositionElement(testJpaAutomationCompositionElement));
268 void testJpaAutomationCompositionElementLombok() {
269 assertNotNull(new Participant());
270 var ace0 = new JpaAutomationCompositionElement();
272 assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
273 assertThat(ace0.hashCode()).isNotZero();
274 assertEquals(ace0, ace0);
275 assertNotEquals(null, ace0);
277 var ace1 = new JpaAutomationCompositionElement();
279 ace1.setDefinition(new PfConceptKey("defName", "0.0.1"));
280 ace1.setDescription("Description");
281 ace1.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
282 ace1.setState(AutomationCompositionState.UNINITIALISED);
283 ace1.setParticipantId(new PfConceptKey("participant", "0.0.1"));
285 assertThat(ace1.toString()).contains("AutomationCompositionElement(");
286 assertNotEquals(0, ace1.hashCode());
287 assertNotEquals(ace1, ace0);
288 assertNotEquals(null, ace1);
290 assertNotEquals(ace1, ace0);
292 var ace2 = new JpaAutomationCompositionElement();
293 assertEquals(ace2, ace0);
296 private JpaAutomationCompositionElement createJpaAutomationCompositionElementInstance() {
297 var testAce = createAutomationCompositionElementInstance();
298 var testJpaAutomationCompositionElement = new JpaAutomationCompositionElement();
299 testJpaAutomationCompositionElement.setKey(null);
300 testJpaAutomationCompositionElement.fromAuthorative(testAce);
301 testJpaAutomationCompositionElement.setKey(PfReferenceKey.getNullKey());
302 testJpaAutomationCompositionElement.fromAuthorative(testAce);
303 testJpaAutomationCompositionElement.setProperties(Map.of("key", "{}"));
305 return testJpaAutomationCompositionElement;
308 private AutomationCompositionElement createAutomationCompositionElementInstance() {
309 var automationCompositionElement = new AutomationCompositionElement();
310 automationCompositionElement.setId(UUID.fromString("a95757ba-b34a-4049-a2a8-46773abcbe5e"));
311 automationCompositionElement.setDefinition(new ToscaConceptIdentifier("aceDef", "0.0.1"));
312 automationCompositionElement.setParticipantType(new ToscaConceptIdentifier("participantType", "0.0.1"));
313 automationCompositionElement.setProperties(Map.of("key", "{}"));
315 return automationCompositionElement;