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.assertNull;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
33 import java.util.UUID;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
38 import org.onap.policy.clamp.models.acm.concepts.Participant;
39 import org.onap.policy.clamp.models.acm.utils.CommonTestData;
40 import org.onap.policy.common.utils.coder.CoderException;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.onap.policy.models.base.PfConceptKey;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
46 * Test the{@link JpaAutomationCompositionElement} class.
48 class JpaAutomationCompositionElementTest {
50 private static final String NULL_INSTANCE_ID_ERROR = "instanceId is marked .*ull but is null";
51 private static final String NULL_ELEMENT_ID_ERROR = "elementId is marked .*ull but is null";
52 private static final String NULL_ERROR = " is marked .*ull but is null";
53 private static final String ELEMENT_ID = "a95757ba-b34a-4049-a2a8-46773abcbe5e";
54 private static final String INSTANCE_ID = "a78757co-b34a-8949-a2a8-46773abcbe2a";
57 void testJpaAutomationCompositionElementConstructor() {
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);
76 }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
78 assertThatThrownBy(() -> {
79 new JpaAutomationCompositionElement("key", null, null, AutomationCompositionState.UNINITIALISED);
80 }).hasMessageMatching(NULL_INSTANCE_ID_ERROR);
82 assertThatThrownBy(() -> {
83 new JpaAutomationCompositionElement("key", "key", new PfConceptKey(), null);
84 }).hasMessageMatching("state" + NULL_ERROR);
86 assertNotNull(new JpaAutomationCompositionElement());
87 assertNotNull(new JpaAutomationCompositionElement("key", "key"));
88 assertNotNull(new JpaAutomationCompositionElement("key", "key",
89 new PfConceptKey(), AutomationCompositionState.UNINITIALISED));
93 void testJpaAutomationCompositionElement() {
94 var testJpaAcElement = createJpaAutomationCompositionElementInstance();
96 var ace = createAutomationCompositionElementInstance();
97 assertEquals(ace, testJpaAcElement.toAuthorative());
99 assertThatThrownBy(() -> {
100 testJpaAcElement.fromAuthorative(null);
101 }).hasMessageMatching("element is marked .*ull but is null");
103 assertThatThrownBy(() -> new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null))
104 .isInstanceOf(NullPointerException.class);
106 var testJpaAcElementFa =
107 new JpaAutomationCompositionElement(ace.getId().toString(), testJpaAcElement.getInstanceId());
108 testJpaAcElementFa.fromAuthorative(ace);
109 assertEquals(testJpaAcElement, testJpaAcElementFa);
111 assertEquals(ELEMENT_ID, testJpaAcElement.getElementId());
113 var testJpaAutomationCompositionElement2 = new JpaAutomationCompositionElement(testJpaAcElement);
114 assertEquals(testJpaAcElement, testJpaAutomationCompositionElement2);
118 void testJpaAutomationCompositionElementOrderedState() throws CoderException {
119 var testAutomationCompositionElement = createAutomationCompositionElementInstance();
120 var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
122 testJpaAutomationCompositionElement.setOrderedState(null);
123 assertEquals(testAutomationCompositionElement, testJpaAutomationCompositionElement.toAuthorative());
124 testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
126 var noOrderedStateAce = new StandardCoder().decode(
127 new File("src/test/resources/json/AutomationCompositionElementNoOrderedState.json"),
128 AutomationCompositionElement.class);
130 var noOrderedStateJpaAce = new JpaAutomationCompositionElement(noOrderedStateAce);
131 assertNull(noOrderedStateJpaAce.getOrderedState());
132 noOrderedStateAce.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
133 noOrderedStateJpaAce = new JpaAutomationCompositionElement(noOrderedStateAce);
134 noOrderedStateJpaAce.setInstanceId(testJpaAutomationCompositionElement.getInstanceId());
135 noOrderedStateJpaAce.setElementId(testJpaAutomationCompositionElement.getElementId());
136 noOrderedStateJpaAce.setParticipantId(testJpaAutomationCompositionElement.getParticipantId());
137 assertEquals(testJpaAutomationCompositionElement, noOrderedStateJpaAce);
141 void testJpaAutomationCompositionElementValidation() {
142 var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
144 assertThatThrownBy(() -> testJpaAutomationCompositionElement.validate(null))
145 .hasMessageMatching("fieldName is marked .*ull but is null");
147 assertTrue(testJpaAutomationCompositionElement.validate("").isValid());
151 void testJpaAutomationCompositionElementCompareTo() {
152 var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
154 var otherJpaAutomationCompositionElement =
155 new JpaAutomationCompositionElement(testJpaAutomationCompositionElement);
156 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
157 assertEquals(-1, testJpaAutomationCompositionElement.compareTo(null));
158 assertEquals(0, testJpaAutomationCompositionElement.compareTo(testJpaAutomationCompositionElement));
160 testJpaAutomationCompositionElement.compareTo(new DummyJpaAutomationCompositionElementChild()));
162 testJpaAutomationCompositionElement.setElementId("BadValue");
163 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
164 testJpaAutomationCompositionElement.setElementId(ELEMENT_ID);
165 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
167 testJpaAutomationCompositionElement.setInstanceId("BadValue");
168 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
169 testJpaAutomationCompositionElement.setInstanceId(INSTANCE_ID);
170 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
172 testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("BadValue", "0.0.1"));
173 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
174 testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("aceDef", "0.0.1"));
175 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
177 testJpaAutomationCompositionElement.setDescription("Description");
178 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
179 testJpaAutomationCompositionElement.setDescription(null);
180 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
182 testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.PASSIVE);
183 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
184 testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
185 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
187 testJpaAutomationCompositionElement.setState(AutomationCompositionState.PASSIVE);
188 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
189 testJpaAutomationCompositionElement.setState(AutomationCompositionState.UNINITIALISED);
190 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
192 assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
193 testJpaAutomationCompositionElement.setParticipantId(UUID.randomUUID().toString());
194 assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
196 assertEquals(testJpaAutomationCompositionElement,
197 new JpaAutomationCompositionElement(testJpaAutomationCompositionElement));
201 void testJpaAutomationCompositionElementLombok() {
202 assertNotNull(new Participant());
203 var ace0 = new JpaAutomationCompositionElement();
205 assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
206 assertThat(ace0.hashCode()).isNotZero();
207 assertEquals(ace0, ace0);
208 assertNotEquals(null, ace0);
210 var ace1 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
212 ace1.setDefinition(new PfConceptKey("defName", "0.0.1"));
213 ace1.setDescription("Description");
214 ace1.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
215 ace1.setState(AutomationCompositionState.UNINITIALISED);
216 ace1.setParticipantId(CommonTestData.getJpaParticipantId());
218 assertThat(ace1.toString()).contains("AutomationCompositionElement(");
219 assertNotEquals(0, ace1.hashCode());
220 assertNotEquals(ace1, ace0);
221 assertNotEquals(null, ace1);
223 assertNotEquals(ace1, ace0);
225 var ace2 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
226 assertEquals(ace2, ace0);
229 private JpaAutomationCompositionElement createJpaAutomationCompositionElementInstance() {
230 var testAce = createAutomationCompositionElementInstance();
231 var testJpaAcElement =
232 new JpaAutomationCompositionElement(testAce.getId().toString(), INSTANCE_ID);
233 testJpaAcElement.fromAuthorative(testAce);
234 testJpaAcElement.setProperties(Map.of("key", "{}"));
236 return testJpaAcElement;
239 private AutomationCompositionElement createAutomationCompositionElementInstance() {
240 var automationCompositionElement = new AutomationCompositionElement();
241 automationCompositionElement.setId(UUID.fromString(ELEMENT_ID));
242 automationCompositionElement.setDefinition(new ToscaConceptIdentifier("aceDef", "0.0.1"));
243 automationCompositionElement.setParticipantId(CommonTestData.getParticipantId());
244 automationCompositionElement.setProperties(Map.of("key", "{}"));
246 return automationCompositionElement;