2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved.
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.assertDoesNotThrow;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertNotEquals;
28 import static org.junit.jupiter.api.Assertions.assertTrue;
31 import java.util.UUID;
32 import org.junit.jupiter.api.Test;
33 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
34 import org.onap.policy.clamp.models.acm.concepts.DeployState;
35 import org.onap.policy.clamp.models.acm.concepts.LockState;
36 import org.onap.policy.clamp.models.acm.concepts.MigrationState;
37 import org.onap.policy.clamp.models.acm.concepts.SubState;
38 import org.onap.policy.clamp.models.acm.utils.CommonTestData;
39 import org.onap.policy.models.base.PfConceptKey;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 * Test the{@link JpaAutomationCompositionElement} class.
45 class JpaAutomationCompositionElementTest {
47 private static final String NULL_INSTANCE_ID_ERROR = "instanceId is marked non-null but is null";
48 private static final String NULL_ELEMENT_ID_ERROR = "elementId is marked non-null but is null";
49 private static final String NULL_ERROR = " is marked non-null but is null";
50 private static final String ELEMENT_ID = "a95757ba-b34a-4049-a2a8-46773abcbe5e";
51 private static final String INSTANCE_ID = "a78757co-b34a-8949-a2a8-46773abcbe2a";
52 private static final String KEY = "key";
53 private static final String BAD_VALUE = "BadValue";
56 void testJpaAutomationCompositionElementConstructor() {
57 assertThatThrownBy(() -> {
58 new JpaAutomationCompositionElement((AutomationCompositionElement) null);
59 }).hasMessageMatching("authorativeConcept" + NULL_ERROR);
61 assertThatThrownBy(() -> {
62 new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null);
63 }).hasMessageMatching("copyConcept" + NULL_ERROR);
65 assertThatThrownBy(() -> {
66 new JpaAutomationCompositionElement(KEY, null);
67 }).hasMessageMatching(NULL_INSTANCE_ID_ERROR);
69 assertThatThrownBy(() -> {
70 new JpaAutomationCompositionElement(null, KEY);
71 }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
73 assertThatThrownBy(() -> {
74 new JpaAutomationCompositionElement(null, null);
75 }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
77 assertDoesNotThrow(() -> new JpaAutomationCompositionElement(KEY, KEY));
81 void testJpaAutomationCompositionElement() {
82 var testJpaAcElement = createJpaAutomationCompositionElementInstance();
84 var ace = createAutomationCompositionElementInstance();
85 assertEquals(ace, testJpaAcElement.toAuthorative());
87 assertThatThrownBy(() -> {
88 testJpaAcElement.fromAuthorative(null);
89 }).hasMessageMatching("element" + NULL_ERROR);
91 assertThatThrownBy(() -> new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null))
92 .isInstanceOf(NullPointerException.class);
94 var testJpaAcElementFa =
95 new JpaAutomationCompositionElement(ace.getId().toString(), testJpaAcElement.getInstanceId());
96 testJpaAcElementFa.fromAuthorative(ace);
97 assertEquals(testJpaAcElement, testJpaAcElementFa);
99 assertEquals(ELEMENT_ID, testJpaAcElement.getElementId());
101 var testJpaAcElement2 = new JpaAutomationCompositionElement(testJpaAcElement);
102 assertEquals(testJpaAcElement, testJpaAcElement2);
104 testJpaAcElement2 = new JpaAutomationCompositionElement(testJpaAcElement.toAuthorative());
105 testJpaAcElement2.setElementId(ELEMENT_ID);
106 testJpaAcElement2.setInstanceId(INSTANCE_ID);
107 assertEquals(testJpaAcElement, testJpaAcElement2);
111 void testJpaAutomationCompositionElementValidation() {
112 var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
114 assertThatThrownBy(() -> testJpaAutomationCompositionElement.validate(null))
115 .hasMessageMatching("fieldName" + NULL_ERROR);
117 assertTrue(testJpaAutomationCompositionElement.validate("").isValid());
121 void testJpaAcElementCompareTo() {
122 var testJpaAcElement = createJpaAutomationCompositionElementInstance();
124 var otherJpaAcElement =
125 new JpaAutomationCompositionElement(testJpaAcElement);
126 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
127 assertEquals(-1, testJpaAcElement.compareTo(null));
128 assertEquals(0, testJpaAcElement.compareTo(testJpaAcElement));
130 testJpaAcElement.compareTo(new DummyJpaAutomationCompositionElementChild()));
132 assertEquals(testJpaAcElement, new JpaAutomationCompositionElement(testJpaAcElement));
136 void testJpaAutomationCompositionElementCompareTo() {
137 var testJpaAcElement = createJpaAutomationCompositionElementInstance();
139 var otherJpaAcElement =
140 new JpaAutomationCompositionElement(testJpaAcElement);
142 testJpaAcElement.setElementId(BAD_VALUE);
143 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
144 testJpaAcElement.setElementId(ELEMENT_ID);
145 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
147 testJpaAcElement.setInstanceId(BAD_VALUE);
148 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
149 testJpaAcElement.setInstanceId(INSTANCE_ID);
150 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
152 testJpaAcElement.setDefinition(new PfConceptKey(BAD_VALUE, "0.0.1"));
153 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
154 testJpaAcElement.setDefinition(new PfConceptKey("aceDef", "0.0.1"));
155 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
157 testJpaAcElement.setDescription("Description");
158 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
159 testJpaAcElement.setDescription(null);
160 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
162 testJpaAcElement.setDeployState(DeployState.DEPLOYED);
163 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
164 testJpaAcElement.setDeployState(DeployState.UNDEPLOYED);
165 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
167 testJpaAcElement.setLockState(LockState.UNLOCKED);
168 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
169 testJpaAcElement.setLockState(LockState.LOCKED);
170 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
172 testJpaAcElement.setSubState(SubState.PREPARING);
173 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
174 testJpaAcElement.setSubState(SubState.NONE);
175 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
177 testJpaAcElement.setMigrationState(MigrationState.REMOVED);
178 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
179 testJpaAcElement.setMigrationState(MigrationState.DEFAULT);
180 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
182 testJpaAcElement.setUseState(BAD_VALUE);
183 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
184 testJpaAcElement.setUseState("IDLE");
185 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
187 testJpaAcElement.setOperationalState(BAD_VALUE);
188 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
189 testJpaAcElement.setOperationalState("DEFAULT");
190 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
192 testJpaAcElement.setStage(1);
193 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
194 testJpaAcElement.setStage(null);
195 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
197 testJpaAcElement.setMessage("Message");
198 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
199 testJpaAcElement.setMessage(null);
200 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
202 testJpaAcElement.setParticipantId(UUID.randomUUID().toString());
203 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
208 void testJpaAutomationCompositionElementLombok() {
209 var ace0 = new JpaAutomationCompositionElement();
211 assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
212 assertThat(ace0.hashCode()).isNotZero();
213 assertNotEquals(null, ace0);
215 var ace1 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
217 ace1.setDefinition(new PfConceptKey("defName", "0.0.1"));
218 ace1.setDescription("Description");
219 ace1.setParticipantId(CommonTestData.getJpaParticipantId());
221 assertThat(ace1.toString()).contains("AutomationCompositionElement(");
222 assertNotEquals(0, ace1.hashCode());
223 assertNotEquals(ace1, ace0);
224 assertNotEquals(null, ace1);
226 assertNotEquals(ace1, ace0);
228 var ace2 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
229 assertEquals(ace2, ace0);
232 private JpaAutomationCompositionElement createJpaAutomationCompositionElementInstance() {
233 var testAce = createAutomationCompositionElementInstance();
234 var testJpaAcElement =
235 new JpaAutomationCompositionElement(testAce.getId().toString(), INSTANCE_ID);
236 testJpaAcElement.fromAuthorative(testAce);
237 testJpaAcElement.setProperties(Map.of(KEY, "{}"));
239 return testJpaAcElement;
242 private AutomationCompositionElement createAutomationCompositionElementInstance() {
243 var automationCompositionElement = new AutomationCompositionElement();
244 automationCompositionElement.setId(UUID.fromString(ELEMENT_ID));
245 automationCompositionElement.setDefinition(new ToscaConceptIdentifier("aceDef", "0.0.1"));
246 automationCompositionElement.setParticipantId(CommonTestData.getParticipantId());
247 automationCompositionElement.setProperties(Map.of(KEY, "{}"));
248 automationCompositionElement.setUseState("IDLE");
249 automationCompositionElement.setOperationalState("DEFAULT");
251 return automationCompositionElement;