2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2024 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.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.utils.CommonTestData;
37 import org.onap.policy.models.base.PfConceptKey;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 * Test the{@link JpaAutomationCompositionElement} class.
43 class JpaAutomationCompositionElementTest {
45 private static final String NULL_INSTANCE_ID_ERROR = "instanceId is marked .*ull but is null";
46 private static final String NULL_ELEMENT_ID_ERROR = "elementId is marked .*ull but is null";
47 private static final String NULL_ERROR = " is marked .*ull but is null";
48 private static final String ELEMENT_ID = "a95757ba-b34a-4049-a2a8-46773abcbe5e";
49 private static final String INSTANCE_ID = "a78757co-b34a-8949-a2a8-46773abcbe2a";
51 private static final PfConceptKey CONCEPT_KEY = new PfConceptKey();
54 void testJpaAutomationCompositionElementConstructor() {
55 assertThatThrownBy(() -> {
56 new JpaAutomationCompositionElement((AutomationCompositionElement) null);
57 }).hasMessageMatching("authorativeConcept is marked .*ull but is null");
59 assertThatThrownBy(() -> {
60 new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null);
61 }).hasMessageMatching("copyConcept is marked .*ull but is null");
63 assertThatThrownBy(() -> {
64 new JpaAutomationCompositionElement("key", null);
65 }).hasMessageMatching(NULL_INSTANCE_ID_ERROR);
67 assertThatThrownBy(() -> {
68 new JpaAutomationCompositionElement(null, "key");
69 }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
71 assertThatThrownBy(() -> {
72 new JpaAutomationCompositionElement(null, null);
73 }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
75 assertThatThrownBy(() -> {
76 new JpaAutomationCompositionElement(null, null, null, null, null);
77 }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
79 assertThatThrownBy(() -> {
80 new JpaAutomationCompositionElement("key", null, null,
81 DeployState.UNDEPLOYED, LockState.LOCKED);
82 }).hasMessageMatching(NULL_INSTANCE_ID_ERROR);
84 assertThatThrownBy(() -> {
85 new JpaAutomationCompositionElement("key", "key", null,
86 DeployState.UNDEPLOYED, LockState.LOCKED);
87 }).hasMessageMatching("definition" + NULL_ERROR);
89 assertThatThrownBy(() -> {
90 new JpaAutomationCompositionElement("key", "key", CONCEPT_KEY,
91 null, LockState.LOCKED);
92 }).hasMessageMatching("deployState" + NULL_ERROR);
94 assertThatThrownBy(() -> {
95 new JpaAutomationCompositionElement("key", "key", CONCEPT_KEY,
96 DeployState.UNDEPLOYED, null);
97 }).hasMessageMatching("lockState" + NULL_ERROR);
99 assertDoesNotThrow(() -> new JpaAutomationCompositionElement());
100 assertDoesNotThrow(() -> new JpaAutomationCompositionElement("key", "key"));
101 assertDoesNotThrow(() -> new JpaAutomationCompositionElement("key", "key",
102 CONCEPT_KEY, DeployState.UNDEPLOYED, LockState.LOCKED));
106 void testJpaAutomationCompositionElement() {
107 var testJpaAcElement = createJpaAutomationCompositionElementInstance();
109 var ace = createAutomationCompositionElementInstance();
110 assertEquals(ace, testJpaAcElement.toAuthorative());
112 assertThatThrownBy(() -> {
113 testJpaAcElement.fromAuthorative(null);
114 }).hasMessageMatching("element is marked .*ull but is null");
116 assertThatThrownBy(() -> new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null))
117 .isInstanceOf(NullPointerException.class);
119 var testJpaAcElementFa =
120 new JpaAutomationCompositionElement(ace.getId().toString(), testJpaAcElement.getInstanceId());
121 testJpaAcElementFa.fromAuthorative(ace);
122 assertEquals(testJpaAcElement, testJpaAcElementFa);
124 assertEquals(ELEMENT_ID, testJpaAcElement.getElementId());
126 var testJpaAcElement2 = new JpaAutomationCompositionElement(testJpaAcElement);
127 assertEquals(testJpaAcElement, testJpaAcElement2);
129 testJpaAcElement2 = new JpaAutomationCompositionElement(testJpaAcElement.toAuthorative());
130 testJpaAcElement2.setElementId(ELEMENT_ID);
131 testJpaAcElement2.setInstanceId(INSTANCE_ID);
132 assertEquals(testJpaAcElement, testJpaAcElement2);
136 void testJpaAutomationCompositionElementValidation() {
137 var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
139 assertThatThrownBy(() -> testJpaAutomationCompositionElement.validate(null))
140 .hasMessageMatching("fieldName is marked .*ull but is null");
142 assertTrue(testJpaAutomationCompositionElement.validate("").isValid());
146 void testJpaAcElementCompareTo() {
147 var testJpaAcElement = createJpaAutomationCompositionElementInstance();
149 var otherJpaAcElement =
150 new JpaAutomationCompositionElement(testJpaAcElement);
151 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
152 assertEquals(-1, testJpaAcElement.compareTo(null));
153 assertEquals(0, testJpaAcElement.compareTo(testJpaAcElement));
155 testJpaAcElement.compareTo(new DummyJpaAutomationCompositionElementChild()));
157 assertEquals(testJpaAcElement, new JpaAutomationCompositionElement(testJpaAcElement));
161 void testJpaAutomationCompositionElementCompareTo() {
162 var testJpaAcElement = createJpaAutomationCompositionElementInstance();
164 var otherJpaAcElement =
165 new JpaAutomationCompositionElement(testJpaAcElement);
167 testJpaAcElement.setElementId("BadValue");
168 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
169 testJpaAcElement.setElementId(ELEMENT_ID);
170 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
172 testJpaAcElement.setInstanceId("BadValue");
173 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
174 testJpaAcElement.setInstanceId(INSTANCE_ID);
175 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
177 testJpaAcElement.setDefinition(new PfConceptKey("BadValue", "0.0.1"));
178 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
179 testJpaAcElement.setDefinition(new PfConceptKey("aceDef", "0.0.1"));
180 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
182 testJpaAcElement.setDescription("Description");
183 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
184 testJpaAcElement.setDescription(null);
185 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
187 testJpaAcElement.setDeployState(DeployState.DEPLOYED);
188 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
189 testJpaAcElement.setDeployState(DeployState.UNDEPLOYED);
190 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
192 testJpaAcElement.setLockState(LockState.UNLOCKED);
193 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
194 testJpaAcElement.setLockState(LockState.LOCKED);
195 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
197 testJpaAcElement.setUseState("BadValue");
198 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
199 testJpaAcElement.setUseState("IDLE");
200 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
202 testJpaAcElement.setOperationalState("BadValue");
203 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
204 testJpaAcElement.setOperationalState("DEFAULT");
205 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
207 testJpaAcElement.setMessage("Message");
208 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
209 testJpaAcElement.setMessage(null);
210 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
212 testJpaAcElement.setRestarting(true);
213 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
214 testJpaAcElement.setRestarting(null);
215 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
217 testJpaAcElement.setParticipantId(UUID.randomUUID().toString());
218 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
223 void testJpaAutomationCompositionElementLombok() {
224 var ace0 = new JpaAutomationCompositionElement();
226 assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
227 assertThat(ace0.hashCode()).isNotZero();
228 assertNotEquals(null, ace0);
230 var ace1 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
232 ace1.setDefinition(new PfConceptKey("defName", "0.0.1"));
233 ace1.setDescription("Description");
234 ace1.setParticipantId(CommonTestData.getJpaParticipantId());
236 assertThat(ace1.toString()).contains("AutomationCompositionElement(");
237 assertNotEquals(0, ace1.hashCode());
238 assertNotEquals(ace1, ace0);
239 assertNotEquals(null, ace1);
241 assertNotEquals(ace1, ace0);
243 var ace2 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
244 assertEquals(ace2, ace0);
247 private JpaAutomationCompositionElement createJpaAutomationCompositionElementInstance() {
248 var testAce = createAutomationCompositionElementInstance();
249 var testJpaAcElement =
250 new JpaAutomationCompositionElement(testAce.getId().toString(), INSTANCE_ID);
251 testJpaAcElement.fromAuthorative(testAce);
252 testJpaAcElement.setProperties(Map.of("key", "{}"));
254 return testJpaAcElement;
257 private AutomationCompositionElement createAutomationCompositionElementInstance() {
258 var automationCompositionElement = new AutomationCompositionElement();
259 automationCompositionElement.setId(UUID.fromString(ELEMENT_ID));
260 automationCompositionElement.setDefinition(new ToscaConceptIdentifier("aceDef", "0.0.1"));
261 automationCompositionElement.setParticipantId(CommonTestData.getParticipantId());
262 automationCompositionElement.setProperties(Map.of("key", "{}"));
263 automationCompositionElement.setUseState("IDLE");
264 automationCompositionElement.setOperationalState("DEFAULT");
266 return automationCompositionElement;