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.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.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.Participant;
37 import org.onap.policy.clamp.models.acm.utils.CommonTestData;
38 import org.onap.policy.models.base.PfConceptKey;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42 * Test the{@link JpaAutomationCompositionElement} class.
44 class JpaAutomationCompositionElementTest {
46 private static final String NULL_INSTANCE_ID_ERROR = "instanceId is marked .*ull but is null";
47 private static final String NULL_ELEMENT_ID_ERROR = "elementId is marked .*ull but is null";
48 private static final String NULL_ERROR = " is marked .*ull but is null";
49 private static final String ELEMENT_ID = "a95757ba-b34a-4049-a2a8-46773abcbe5e";
50 private static final String INSTANCE_ID = "a78757co-b34a-8949-a2a8-46773abcbe2a";
53 void testJpaAutomationCompositionElementConstructor() {
54 assertThatThrownBy(() -> {
55 new JpaAutomationCompositionElement((AutomationCompositionElement) null);
56 }).hasMessageMatching("authorativeConcept is marked .*ull but is null");
58 assertThatThrownBy(() -> {
59 new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null);
60 }).hasMessageMatching("copyConcept is marked .*ull but is null");
62 assertThatThrownBy(() -> {
63 new JpaAutomationCompositionElement("key", null);
64 }).hasMessageMatching(NULL_INSTANCE_ID_ERROR);
66 assertThatThrownBy(() -> {
67 new JpaAutomationCompositionElement(null, "key");
68 }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
70 assertThatThrownBy(() -> {
71 new JpaAutomationCompositionElement(null, null);
72 }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
74 assertThatThrownBy(() -> {
75 new JpaAutomationCompositionElement(null, null, null, null, null);
76 }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
78 assertThatThrownBy(() -> {
79 new JpaAutomationCompositionElement("key", null, null,
80 DeployState.UNDEPLOYED, LockState.LOCKED);
81 }).hasMessageMatching(NULL_INSTANCE_ID_ERROR);
83 assertThatThrownBy(() -> {
84 new JpaAutomationCompositionElement("key", "key", null,
85 DeployState.UNDEPLOYED, LockState.LOCKED);
86 }).hasMessageMatching("definition" + NULL_ERROR);
88 assertThatThrownBy(() -> {
89 new JpaAutomationCompositionElement("key", "key", new PfConceptKey(),
90 null, LockState.LOCKED);
91 }).hasMessageMatching("deployState" + NULL_ERROR);
93 assertThatThrownBy(() -> {
94 new JpaAutomationCompositionElement("key", "key", new PfConceptKey(),
95 DeployState.UNDEPLOYED, null);
96 }).hasMessageMatching("lockState" + NULL_ERROR);
98 assertNotNull(new JpaAutomationCompositionElement());
99 assertNotNull(new JpaAutomationCompositionElement("key", "key"));
100 assertNotNull(new JpaAutomationCompositionElement("key", "key",
101 new PfConceptKey(), DeployState.UNDEPLOYED, LockState.LOCKED));
105 void testJpaAutomationCompositionElement() {
106 var testJpaAcElement = createJpaAutomationCompositionElementInstance();
108 var ace = createAutomationCompositionElementInstance();
109 assertEquals(ace, testJpaAcElement.toAuthorative());
111 assertThatThrownBy(() -> {
112 testJpaAcElement.fromAuthorative(null);
113 }).hasMessageMatching("element is marked .*ull but is null");
115 assertThatThrownBy(() -> new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null))
116 .isInstanceOf(NullPointerException.class);
118 var testJpaAcElementFa =
119 new JpaAutomationCompositionElement(ace.getId().toString(), testJpaAcElement.getInstanceId());
120 testJpaAcElementFa.fromAuthorative(ace);
121 assertEquals(testJpaAcElement, testJpaAcElementFa);
123 assertEquals(ELEMENT_ID, testJpaAcElement.getElementId());
125 var testJpaAutomationCompositionElement2 = new JpaAutomationCompositionElement(testJpaAcElement);
126 assertEquals(testJpaAcElement, testJpaAutomationCompositionElement2);
130 void testJpaAutomationCompositionElementValidation() {
131 var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
133 assertThatThrownBy(() -> testJpaAutomationCompositionElement.validate(null))
134 .hasMessageMatching("fieldName is marked .*ull but is null");
136 assertTrue(testJpaAutomationCompositionElement.validate("").isValid());
140 void testJpaAutomationCompositionElementCompareTo() {
141 var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
143 var otherJpaAutomationCompositionElement =
144 new JpaAutomationCompositionElement(testJpaAutomationCompositionElement);
145 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
146 assertEquals(-1, testJpaAutomationCompositionElement.compareTo(null));
147 assertEquals(0, testJpaAutomationCompositionElement.compareTo(testJpaAutomationCompositionElement));
149 testJpaAutomationCompositionElement.compareTo(new DummyJpaAutomationCompositionElementChild()));
151 testJpaAutomationCompositionElement.setElementId("BadValue");
152 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
153 testJpaAutomationCompositionElement.setElementId(ELEMENT_ID);
154 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
156 testJpaAutomationCompositionElement.setInstanceId("BadValue");
157 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
158 testJpaAutomationCompositionElement.setInstanceId(INSTANCE_ID);
159 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
161 testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("BadValue", "0.0.1"));
162 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
163 testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("aceDef", "0.0.1"));
164 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
166 testJpaAutomationCompositionElement.setDescription("Description");
167 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
168 testJpaAutomationCompositionElement.setDescription(null);
169 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
171 testJpaAutomationCompositionElement.setDeployState(DeployState.DEPLOYED);
172 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
173 testJpaAutomationCompositionElement.setDeployState(DeployState.UNDEPLOYED);
174 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
176 testJpaAutomationCompositionElement.setLockState(LockState.UNLOCKED);
177 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
178 testJpaAutomationCompositionElement.setLockState(LockState.LOCKED);
179 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
181 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
182 testJpaAutomationCompositionElement.setParticipantId(UUID.randomUUID().toString());
183 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
185 assertEquals(testJpaAutomationCompositionElement,
186 new JpaAutomationCompositionElement(testJpaAutomationCompositionElement));
190 void testJpaAutomationCompositionElementLombok() {
191 assertNotNull(new Participant());
192 var ace0 = new JpaAutomationCompositionElement();
194 assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
195 assertThat(ace0.hashCode()).isNotZero();
196 assertEquals(ace0, ace0);
197 assertNotEquals(null, ace0);
199 var ace1 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
201 ace1.setDefinition(new PfConceptKey("defName", "0.0.1"));
202 ace1.setDescription("Description");
203 ace1.setParticipantId(CommonTestData.getJpaParticipantId());
205 assertThat(ace1.toString()).contains("AutomationCompositionElement(");
206 assertNotEquals(0, ace1.hashCode());
207 assertNotEquals(ace1, ace0);
208 assertNotEquals(null, ace1);
210 assertNotEquals(ace1, ace0);
212 var ace2 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
213 assertEquals(ace2, ace0);
216 private JpaAutomationCompositionElement createJpaAutomationCompositionElementInstance() {
217 var testAce = createAutomationCompositionElementInstance();
218 var testJpaAcElement =
219 new JpaAutomationCompositionElement(testAce.getId().toString(), INSTANCE_ID);
220 testJpaAcElement.fromAuthorative(testAce);
221 testJpaAcElement.setProperties(Map.of("key", "{}"));
223 return testJpaAcElement;
226 private AutomationCompositionElement createAutomationCompositionElementInstance() {
227 var automationCompositionElement = new AutomationCompositionElement();
228 automationCompositionElement.setId(UUID.fromString(ELEMENT_ID));
229 automationCompositionElement.setDefinition(new ToscaConceptIdentifier("aceDef", "0.0.1"));
230 automationCompositionElement.setParticipantId(CommonTestData.getParticipantId());
231 automationCompositionElement.setProperties(Map.of("key", "{}"));
233 return automationCompositionElement;