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;
32 import java.util.UUID;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
37 import org.onap.policy.clamp.models.acm.concepts.Participant;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.models.base.PfConceptKey;
41 import org.onap.policy.models.base.PfKey;
42 import org.onap.policy.models.base.PfReferenceKey;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
46 * Test the {@link JpaAutomationCompositionElement} class.
48 class JpaAutomationCompositionElementTest {
50 private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
53 void testJpaAutomationCompositionElementConstructor() {
54 assertThatThrownBy(() -> {
55 new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null);
56 }).hasMessageMatching("copyConcept is marked .*ull but is null");
58 assertThatThrownBy(() -> {
59 new JpaAutomationCompositionElement((PfReferenceKey) null);
60 }).hasMessageMatching(NULL_KEY_ERROR);
62 assertThatThrownBy(() -> {
63 new JpaAutomationCompositionElement(null, null, null, null);
64 }).hasMessageMatching(NULL_KEY_ERROR);
66 assertThatThrownBy(() -> {
67 new JpaAutomationCompositionElement(null, null, null, AutomationCompositionState.UNINITIALISED);
68 }).hasMessageMatching(NULL_KEY_ERROR);
70 assertThatThrownBy(() -> {
71 new JpaAutomationCompositionElement(null, null, new PfConceptKey("participant", "0.0.1"), null);
72 }).hasMessageMatching(NULL_KEY_ERROR);
74 assertThatThrownBy(() -> {
75 new JpaAutomationCompositionElement(null, null, new PfConceptKey("participant", "0.0.1"),
76 AutomationCompositionState.UNINITIALISED);
77 }).hasMessageMatching(NULL_KEY_ERROR);
79 assertThatThrownBy(() -> {
80 new JpaAutomationCompositionElement(null, new PfConceptKey(), null, null);
81 }).hasMessageMatching(NULL_KEY_ERROR);
83 assertThatThrownBy(() -> {
84 new JpaAutomationCompositionElement(null, new PfConceptKey(), null,
85 AutomationCompositionState.UNINITIALISED);
86 }).hasMessageMatching(NULL_KEY_ERROR);
88 assertThatThrownBy(() -> {
89 new JpaAutomationCompositionElement(null, new PfConceptKey(), new PfConceptKey("participant", "0.0.1"),
91 }).hasMessageMatching(NULL_KEY_ERROR);
93 assertThatThrownBy(() -> {
94 new JpaAutomationCompositionElement(null, new PfConceptKey(), new PfConceptKey("participant", "0.0.1"),
95 AutomationCompositionState.UNINITIALISED);
96 }).hasMessageMatching(NULL_KEY_ERROR);
98 assertThatThrownBy(() -> {
99 new JpaAutomationCompositionElement(new PfReferenceKey(), null, null, null);
100 }).hasMessageMatching("definition is marked .*ull but is null");
102 assertThatThrownBy(() -> {
103 new JpaAutomationCompositionElement(new PfReferenceKey(), null, null,
104 AutomationCompositionState.UNINITIALISED);
105 }).hasMessageMatching("definition is marked .*ull but is null");
107 assertThatThrownBy(() -> {
108 new JpaAutomationCompositionElement(new PfReferenceKey(), null, new PfConceptKey("participant", "0.0.1"),
110 }).hasMessageMatching("definition is marked .*ull but is null");
112 assertThatThrownBy(() -> {
113 new JpaAutomationCompositionElement(new PfReferenceKey(), null, new PfConceptKey("participant", "0.0.1"),
114 AutomationCompositionState.UNINITIALISED);
115 }).hasMessageMatching("definition is marked .*ull but is null");
117 assertThatThrownBy(() -> {
118 new JpaAutomationCompositionElement(new PfReferenceKey(), new PfConceptKey(), null, null);
119 }).hasMessageMatching("participantType is marked .*ull but is null");
121 assertThatThrownBy(() -> {
122 new JpaAutomationCompositionElement(new PfReferenceKey(), new PfConceptKey(), null,
123 AutomationCompositionState.UNINITIALISED);
124 }).hasMessageMatching("participantType is marked .*ull but is null");
126 assertThatThrownBy(() -> {
127 new JpaAutomationCompositionElement(new PfReferenceKey(), new PfConceptKey(),
128 new PfConceptKey("participant", "0.0.1"), null);
129 }).hasMessageMatching("state is marked .*ull but is null");
131 assertNotNull(new JpaAutomationCompositionElement());
132 assertNotNull(new JpaAutomationCompositionElement((new PfReferenceKey())));
133 assertNotNull(new JpaAutomationCompositionElement(new PfReferenceKey(), new PfConceptKey(),
134 new PfConceptKey("participant", "0.0.1"), AutomationCompositionState.UNINITIALISED));
138 void testJpaAutomationCompositionElement() {
139 JpaAutomationCompositionElement testJpaAutomationCompositionElement =
140 createJpaAutomationCompositionElementInstance();
142 AutomationCompositionElement ace = createAutomationCompositionElementInstance();
143 assertEquals(ace, testJpaAutomationCompositionElement.toAuthorative());
145 assertThatThrownBy(() -> {
146 testJpaAutomationCompositionElement.fromAuthorative(null);
147 }).hasMessageMatching("element is marked .*ull but is null");
149 assertThatThrownBy(() -> new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null))
150 .isInstanceOf(NullPointerException.class);
152 JpaAutomationCompositionElement testJpaAutomationCompositionElementFa = new JpaAutomationCompositionElement();
153 testJpaAutomationCompositionElementFa.setKey(null);
154 testJpaAutomationCompositionElementFa.fromAuthorative(ace);
155 assertEquals(testJpaAutomationCompositionElement, testJpaAutomationCompositionElementFa);
156 testJpaAutomationCompositionElementFa.setKey(PfReferenceKey.getNullKey());
157 testJpaAutomationCompositionElementFa.fromAuthorative(ace);
158 assertEquals(testJpaAutomationCompositionElement, testJpaAutomationCompositionElementFa);
159 testJpaAutomationCompositionElementFa.setKey(
160 new PfReferenceKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, "a95757ba-b34a-4049-a2a8-46773abcbe5e"));
161 testJpaAutomationCompositionElementFa.fromAuthorative(ace);
162 assertEquals(testJpaAutomationCompositionElement, testJpaAutomationCompositionElementFa);
164 assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e",
165 testJpaAutomationCompositionElement.getKey().getLocalName());
166 assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e",
167 new JpaAutomationCompositionElement(createAutomationCompositionElementInstance()).getKey().getLocalName());
168 assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e",
169 ((PfReferenceKey) new JpaAutomationCompositionElement(createAutomationCompositionElementInstance())
170 .getKeys().get(0)).getLocalName());
172 testJpaAutomationCompositionElement.clean();
173 assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e",
174 testJpaAutomationCompositionElement.getKey().getLocalName());
176 testJpaAutomationCompositionElement.setDescription(" A Message ");
177 testJpaAutomationCompositionElement.clean();
178 assertEquals("A Message", testJpaAutomationCompositionElement.getDescription());
180 JpaAutomationCompositionElement testJpaAutomationCompositionElement2 =
181 new JpaAutomationCompositionElement(testJpaAutomationCompositionElement);
182 assertEquals(testJpaAutomationCompositionElement, testJpaAutomationCompositionElement2);
186 void testJpaAutomationCompositionElementOrderedState() throws CoderException {
187 AutomationCompositionElement testAutomationCompositionElement = createAutomationCompositionElementInstance();
188 JpaAutomationCompositionElement testJpaAutomationCompositionElement =
189 createJpaAutomationCompositionElementInstance();
191 testJpaAutomationCompositionElement.setOrderedState(null);
192 assertEquals(testAutomationCompositionElement, testJpaAutomationCompositionElement.toAuthorative());
193 testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
195 AutomationCompositionElement noOrderedStateAce = new StandardCoder().decode(
196 new File("src/test/resources/json/AutomationCompositionElementNoOrderedState.json"),
197 AutomationCompositionElement.class);
199 JpaAutomationCompositionElement noOrderedStateJpaAce = new JpaAutomationCompositionElement(noOrderedStateAce);
200 assertNull(noOrderedStateJpaAce.getOrderedState());
201 noOrderedStateAce.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
202 noOrderedStateJpaAce = new JpaAutomationCompositionElement(noOrderedStateAce);
203 assertEquals(testJpaAutomationCompositionElement, noOrderedStateJpaAce);
207 void testJpaAutomationCompositionElementValidation() {
208 JpaAutomationCompositionElement testJpaAutomationCompositionElement =
209 createJpaAutomationCompositionElementInstance();
211 assertThatThrownBy(() -> testJpaAutomationCompositionElement.validate(null))
212 .hasMessageMatching("fieldName is marked .*ull but is null");
214 assertTrue(testJpaAutomationCompositionElement.validate("").isValid());
218 void testJpaAutomationCompositionElementCompareTo() {
219 JpaAutomationCompositionElement testJpaAutomationCompositionElement =
220 createJpaAutomationCompositionElementInstance();
222 JpaAutomationCompositionElement otherJpaAutomationCompositionElement =
223 new JpaAutomationCompositionElement(testJpaAutomationCompositionElement);
224 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
225 assertEquals(-1, testJpaAutomationCompositionElement.compareTo(null));
226 assertEquals(0, testJpaAutomationCompositionElement.compareTo(testJpaAutomationCompositionElement));
228 testJpaAutomationCompositionElement.compareTo(new DummyJpaAutomationCompositionElementChild()));
230 testJpaAutomationCompositionElement
231 .setKey(new PfReferenceKey("BadValue", "0.0.1", "a95757ba-b34a-4049-a2a8-46773abcbe5e"));
232 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
233 testJpaAutomationCompositionElement.setKey(
234 new PfReferenceKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, "a95757ba-b34a-4049-a2a8-46773abcbe5e"));
235 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
237 testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("BadValue", "0.0.1"));
238 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
239 testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("aceDef", "0.0.1"));
240 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
242 testJpaAutomationCompositionElement.setDescription("Description");
243 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
244 testJpaAutomationCompositionElement.setDescription(null);
245 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
247 testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.PASSIVE);
248 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
249 testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
250 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
252 testJpaAutomationCompositionElement.setState(AutomationCompositionState.PASSIVE);
253 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
254 testJpaAutomationCompositionElement.setState(AutomationCompositionState.UNINITIALISED);
255 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
257 testJpaAutomationCompositionElement.setParticipantType(new PfConceptKey("dummy", "0.0.1"));
258 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
259 testJpaAutomationCompositionElement.setParticipantType(new PfConceptKey("participantType", "0.0.1"));
260 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
262 assertEquals(testJpaAutomationCompositionElement,
263 new JpaAutomationCompositionElement(testJpaAutomationCompositionElement));
267 void testJpaAutomationCompositionElementLombok() {
268 assertNotNull(new Participant());
269 JpaAutomationCompositionElement ace0 = new JpaAutomationCompositionElement();
271 assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
272 assertThat(ace0.hashCode()).isNotZero();
273 assertEquals(ace0, ace0);
274 assertNotEquals(null, ace0);
276 JpaAutomationCompositionElement ace1 = new JpaAutomationCompositionElement();
278 ace1.setDefinition(new PfConceptKey("defName", "0.0.1"));
279 ace1.setDescription("Description");
280 ace1.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
281 ace1.setState(AutomationCompositionState.UNINITIALISED);
282 ace1.setParticipantId(new PfConceptKey("participant", "0.0.1"));
284 assertThat(ace1.toString()).contains("AutomationCompositionElement(");
285 assertNotEquals(0, ace1.hashCode());
286 assertNotEquals(ace1, ace0);
287 assertNotEquals(null, ace1);
289 assertNotEquals(ace1, ace0);
291 JpaAutomationCompositionElement ace2 = new JpaAutomationCompositionElement();
292 assertEquals(ace2, ace0);
295 private JpaAutomationCompositionElement createJpaAutomationCompositionElementInstance() {
296 AutomationCompositionElement testAce = createAutomationCompositionElementInstance();
297 JpaAutomationCompositionElement testJpaAutomationCompositionElement = new JpaAutomationCompositionElement();
298 testJpaAutomationCompositionElement.setKey(null);
299 testJpaAutomationCompositionElement.fromAuthorative(testAce);
300 testJpaAutomationCompositionElement.setKey(PfReferenceKey.getNullKey());
301 testJpaAutomationCompositionElement.fromAuthorative(testAce);
303 return testJpaAutomationCompositionElement;
306 private AutomationCompositionElement createAutomationCompositionElementInstance() {
307 AutomationCompositionElement automationCompositionElement = new AutomationCompositionElement();
308 automationCompositionElement.setId(UUID.fromString("a95757ba-b34a-4049-a2a8-46773abcbe5e"));
309 automationCompositionElement.setDefinition(new ToscaConceptIdentifier("aceDef", "0.0.1"));
310 automationCompositionElement.setParticipantType(new ToscaConceptIdentifier("participantType", "0.0.1"));
312 return automationCompositionElement;