2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 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";
52 void testJpaAutomationCompositionElementConstructor() {
53 assertThatThrownBy(() -> {
54 new JpaAutomationCompositionElement((AutomationCompositionElement) null);
55 }).hasMessageMatching("authorativeConcept is marked .*ull but is null");
57 assertThatThrownBy(() -> {
58 new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null);
59 }).hasMessageMatching("copyConcept is marked .*ull but is null");
61 assertThatThrownBy(() -> {
62 new JpaAutomationCompositionElement("key", null);
63 }).hasMessageMatching(NULL_INSTANCE_ID_ERROR);
65 assertThatThrownBy(() -> {
66 new JpaAutomationCompositionElement(null, "key");
67 }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
69 assertThatThrownBy(() -> {
70 new JpaAutomationCompositionElement(null, null);
71 }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
73 assertThatThrownBy(() -> {
74 new JpaAutomationCompositionElement(null, null, null, null, null);
75 }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
77 assertThatThrownBy(() -> {
78 new JpaAutomationCompositionElement("key", null, null,
79 DeployState.UNDEPLOYED, LockState.LOCKED);
80 }).hasMessageMatching(NULL_INSTANCE_ID_ERROR);
82 assertThatThrownBy(() -> {
83 new JpaAutomationCompositionElement("key", "key", null,
84 DeployState.UNDEPLOYED, LockState.LOCKED);
85 }).hasMessageMatching("definition" + NULL_ERROR);
87 assertThatThrownBy(() -> {
88 new JpaAutomationCompositionElement("key", "key", new PfConceptKey(),
89 null, LockState.LOCKED);
90 }).hasMessageMatching("deployState" + NULL_ERROR);
92 assertThatThrownBy(() -> {
93 new JpaAutomationCompositionElement("key", "key", new PfConceptKey(),
94 DeployState.UNDEPLOYED, null);
95 }).hasMessageMatching("lockState" + NULL_ERROR);
97 assertDoesNotThrow(() -> new JpaAutomationCompositionElement());
98 assertDoesNotThrow(() -> new JpaAutomationCompositionElement("key", "key"));
99 assertDoesNotThrow(() -> new JpaAutomationCompositionElement("key", "key",
100 new PfConceptKey(), DeployState.UNDEPLOYED, LockState.LOCKED));
104 void testJpaAutomationCompositionElement() {
105 var testJpaAcElement = createJpaAutomationCompositionElementInstance();
107 var ace = createAutomationCompositionElementInstance();
108 assertEquals(ace, testJpaAcElement.toAuthorative());
110 assertThatThrownBy(() -> {
111 testJpaAcElement.fromAuthorative(null);
112 }).hasMessageMatching("element is marked .*ull but is null");
114 assertThatThrownBy(() -> new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null))
115 .isInstanceOf(NullPointerException.class);
117 var testJpaAcElementFa =
118 new JpaAutomationCompositionElement(ace.getId().toString(), testJpaAcElement.getInstanceId());
119 testJpaAcElementFa.fromAuthorative(ace);
120 assertEquals(testJpaAcElement, testJpaAcElementFa);
122 assertEquals(ELEMENT_ID, testJpaAcElement.getElementId());
124 var testJpaAcElement2 = new JpaAutomationCompositionElement(testJpaAcElement);
125 assertEquals(testJpaAcElement, testJpaAcElement2);
127 testJpaAcElement2 = new JpaAutomationCompositionElement(testJpaAcElement.toAuthorative());
128 testJpaAcElement2.setElementId(ELEMENT_ID);
129 testJpaAcElement2.setInstanceId(INSTANCE_ID);
130 assertEquals(testJpaAcElement, testJpaAcElement2);
134 void testJpaAutomationCompositionElementValidation() {
135 var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
137 assertThatThrownBy(() -> testJpaAutomationCompositionElement.validate(null))
138 .hasMessageMatching("fieldName is marked .*ull but is null");
140 assertTrue(testJpaAutomationCompositionElement.validate("").isValid());
144 void testJpaAutomationCompositionElementCompareTo() {
145 var testJpaAcElement = createJpaAutomationCompositionElementInstance();
147 var otherJpaAcElement =
148 new JpaAutomationCompositionElement(testJpaAcElement);
149 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
150 assertEquals(-1, testJpaAcElement.compareTo(null));
151 assertEquals(0, testJpaAcElement.compareTo(testJpaAcElement));
153 testJpaAcElement.compareTo(new DummyJpaAutomationCompositionElementChild()));
155 testJpaAcElement.setElementId("BadValue");
156 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
157 testJpaAcElement.setElementId(ELEMENT_ID);
158 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
160 testJpaAcElement.setInstanceId("BadValue");
161 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
162 testJpaAcElement.setInstanceId(INSTANCE_ID);
163 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
165 testJpaAcElement.setDefinition(new PfConceptKey("BadValue", "0.0.1"));
166 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
167 testJpaAcElement.setDefinition(new PfConceptKey("aceDef", "0.0.1"));
168 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
170 testJpaAcElement.setDescription("Description");
171 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
172 testJpaAcElement.setDescription(null);
173 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
175 testJpaAcElement.setDeployState(DeployState.DEPLOYED);
176 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
177 testJpaAcElement.setDeployState(DeployState.UNDEPLOYED);
178 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
180 testJpaAcElement.setLockState(LockState.UNLOCKED);
181 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
182 testJpaAcElement.setLockState(LockState.LOCKED);
183 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
185 testJpaAcElement.setUseState("BadValue");
186 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
187 testJpaAcElement.setUseState("IDLE");
188 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
190 testJpaAcElement.setOperationalState("BadValue");
191 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
192 testJpaAcElement.setOperationalState("DEFAULT");
193 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
195 testJpaAcElement.setMessage("Message");
196 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
197 testJpaAcElement.setMessage(null);
198 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
200 testJpaAcElement.setRestarting(true);
201 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
202 testJpaAcElement.setRestarting(null);
203 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
205 testJpaAcElement.setParticipantId(UUID.randomUUID().toString());
206 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
208 assertEquals(testJpaAcElement, new JpaAutomationCompositionElement(testJpaAcElement));
212 void testJpaAutomationCompositionElementLombok() {
213 var ace0 = new JpaAutomationCompositionElement();
215 assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
216 assertThat(ace0.hashCode()).isNotZero();
217 assertNotEquals(null, ace0);
219 var ace1 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
221 ace1.setDefinition(new PfConceptKey("defName", "0.0.1"));
222 ace1.setDescription("Description");
223 ace1.setParticipantId(CommonTestData.getJpaParticipantId());
225 assertThat(ace1.toString()).contains("AutomationCompositionElement(");
226 assertNotEquals(0, ace1.hashCode());
227 assertNotEquals(ace1, ace0);
228 assertNotEquals(null, ace1);
230 assertNotEquals(ace1, ace0);
232 var ace2 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
233 assertEquals(ace2, ace0);
236 private JpaAutomationCompositionElement createJpaAutomationCompositionElementInstance() {
237 var testAce = createAutomationCompositionElementInstance();
238 var testJpaAcElement =
239 new JpaAutomationCompositionElement(testAce.getId().toString(), INSTANCE_ID);
240 testJpaAcElement.fromAuthorative(testAce);
241 testJpaAcElement.setProperties(Map.of("key", "{}"));
243 return testJpaAcElement;
246 private AutomationCompositionElement createAutomationCompositionElementInstance() {
247 var automationCompositionElement = new AutomationCompositionElement();
248 automationCompositionElement.setId(UUID.fromString(ELEMENT_ID));
249 automationCompositionElement.setDefinition(new ToscaConceptIdentifier("aceDef", "0.0.1"));
250 automationCompositionElement.setParticipantId(CommonTestData.getParticipantId());
251 automationCompositionElement.setProperties(Map.of("key", "{}"));
252 automationCompositionElement.setUseState("IDLE");
253 automationCompositionElement.setOperationalState("DEFAULT");
255 return automationCompositionElement;