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 testJpaAcElement2 = new JpaAutomationCompositionElement(testJpaAcElement);
126 assertEquals(testJpaAcElement, testJpaAcElement2);
128 testJpaAcElement2 = new JpaAutomationCompositionElement(testJpaAcElement.toAuthorative());
129 testJpaAcElement2.setElementId(ELEMENT_ID);
130 testJpaAcElement2.setInstanceId(INSTANCE_ID);
131 assertEquals(testJpaAcElement, testJpaAcElement2);
135 void testJpaAutomationCompositionElementValidation() {
136 var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
138 assertThatThrownBy(() -> testJpaAutomationCompositionElement.validate(null))
139 .hasMessageMatching("fieldName is marked .*ull but is null");
141 assertTrue(testJpaAutomationCompositionElement.validate("").isValid());
145 void testJpaAutomationCompositionElementCompareTo() {
146 var testJpaAcElement = createJpaAutomationCompositionElementInstance();
148 var otherJpaAcElement =
149 new JpaAutomationCompositionElement(testJpaAcElement);
150 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
151 assertEquals(-1, testJpaAcElement.compareTo(null));
152 assertEquals(0, testJpaAcElement.compareTo(testJpaAcElement));
154 testJpaAcElement.compareTo(new DummyJpaAutomationCompositionElementChild()));
156 testJpaAcElement.setElementId("BadValue");
157 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
158 testJpaAcElement.setElementId(ELEMENT_ID);
159 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
161 testJpaAcElement.setInstanceId("BadValue");
162 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
163 testJpaAcElement.setInstanceId(INSTANCE_ID);
164 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
166 testJpaAcElement.setDefinition(new PfConceptKey("BadValue", "0.0.1"));
167 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
168 testJpaAcElement.setDefinition(new PfConceptKey("aceDef", "0.0.1"));
169 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
171 testJpaAcElement.setDescription("Description");
172 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
173 testJpaAcElement.setDescription(null);
174 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
176 testJpaAcElement.setDeployState(DeployState.DEPLOYED);
177 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
178 testJpaAcElement.setDeployState(DeployState.UNDEPLOYED);
179 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
181 testJpaAcElement.setLockState(LockState.UNLOCKED);
182 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
183 testJpaAcElement.setLockState(LockState.LOCKED);
184 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
186 testJpaAcElement.setUseState("BadValue");
187 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
188 testJpaAcElement.setUseState("IDLE");
189 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
191 testJpaAcElement.setOperationalState("BadValue");
192 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
193 testJpaAcElement.setOperationalState("DEFAULT");
194 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
196 testJpaAcElement.setMessage("Message");
197 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
198 testJpaAcElement.setMessage(null);
199 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
201 testJpaAcElement.setRestarting(true);
202 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
203 testJpaAcElement.setRestarting(null);
204 assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
206 testJpaAcElement.setParticipantId(UUID.randomUUID().toString());
207 assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
209 assertEquals(testJpaAcElement, new JpaAutomationCompositionElement(testJpaAcElement));
213 void testJpaAutomationCompositionElementLombok() {
214 assertNotNull(new Participant());
215 var ace0 = new JpaAutomationCompositionElement();
217 assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
218 assertThat(ace0.hashCode()).isNotZero();
219 assertEquals(ace0, ace0);
220 assertNotEquals(null, ace0);
222 var ace1 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
224 ace1.setDefinition(new PfConceptKey("defName", "0.0.1"));
225 ace1.setDescription("Description");
226 ace1.setParticipantId(CommonTestData.getJpaParticipantId());
228 assertThat(ace1.toString()).contains("AutomationCompositionElement(");
229 assertNotEquals(0, ace1.hashCode());
230 assertNotEquals(ace1, ace0);
231 assertNotEquals(null, ace1);
233 assertNotEquals(ace1, ace0);
235 var ace2 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
236 assertEquals(ace2, ace0);
239 private JpaAutomationCompositionElement createJpaAutomationCompositionElementInstance() {
240 var testAce = createAutomationCompositionElementInstance();
241 var testJpaAcElement =
242 new JpaAutomationCompositionElement(testAce.getId().toString(), INSTANCE_ID);
243 testJpaAcElement.fromAuthorative(testAce);
244 testJpaAcElement.setProperties(Map.of("key", "{}"));
246 return testJpaAcElement;
249 private AutomationCompositionElement createAutomationCompositionElementInstance() {
250 var automationCompositionElement = new AutomationCompositionElement();
251 automationCompositionElement.setId(UUID.fromString(ELEMENT_ID));
252 automationCompositionElement.setDefinition(new ToscaConceptIdentifier("aceDef", "0.0.1"));
253 automationCompositionElement.setParticipantId(CommonTestData.getParticipantId());
254 automationCompositionElement.setProperties(Map.of("key", "{}"));
255 automationCompositionElement.setUseState("IDLE");
256 automationCompositionElement.setOperationalState("DEFAULT");
258 return automationCompositionElement;