2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.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(() -> {
212 testJpaAutomationCompositionElement.validate(null);
213 }).hasMessageMatching("fieldName is marked .*ull but is null");
215 assertTrue(testJpaAutomationCompositionElement.validate("").isValid());
219 void testJpaAutomationCompositionElementCompareTo() {
220 JpaAutomationCompositionElement testJpaAutomationCompositionElement =
221 createJpaAutomationCompositionElementInstance();
223 JpaAutomationCompositionElement 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 JpaAutomationCompositionElement ace0 = new JpaAutomationCompositionElement();
272 assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
273 assertThat(ace0.hashCode()).isNotZero();
274 assertEquals(true, ace0.equals(ace0));
275 assertEquals(false, ace0.equals(null));
277 JpaAutomationCompositionElement 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 assertEquals(false, ace1.hashCode() == 0);
287 assertEquals(false, ace1.equals(ace0));
288 assertEquals(false, ace1.equals(null));
290 assertNotEquals(ace1, ace0);
292 JpaAutomationCompositionElement ace2 = new JpaAutomationCompositionElement();
293 assertEquals(ace2, ace0);
296 private JpaAutomationCompositionElement createJpaAutomationCompositionElementInstance() {
297 AutomationCompositionElement testAce = createAutomationCompositionElementInstance();
298 JpaAutomationCompositionElement testJpaAutomationCompositionElement = new JpaAutomationCompositionElement();
299 testJpaAutomationCompositionElement.setKey(null);
300 testJpaAutomationCompositionElement.fromAuthorative(testAce);
301 testJpaAutomationCompositionElement.setKey(PfReferenceKey.getNullKey());
302 testJpaAutomationCompositionElement.fromAuthorative(testAce);
304 return testJpaAutomationCompositionElement;
307 private AutomationCompositionElement createAutomationCompositionElementInstance() {
308 AutomationCompositionElement automationCompositionElement = new AutomationCompositionElement();
309 automationCompositionElement.setId(UUID.fromString("a95757ba-b34a-4049-a2a8-46773abcbe5e"));
310 automationCompositionElement.setDefinition(new ToscaConceptIdentifier("aceDef", "0.0.1"));
311 automationCompositionElement.setParticipantType(new ToscaConceptIdentifier("participantType", "0.0.1"));
313 return automationCompositionElement;