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.Assert.assertNull;
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.assertNotNull;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
32 import java.util.ArrayList;
33 import java.util.LinkedHashMap;
34 import java.util.UUID;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
40 import org.onap.policy.clamp.models.acm.concepts.DeployState;
41 import org.onap.policy.clamp.models.acm.concepts.LockState;
42 import org.onap.policy.common.utils.coder.CoderException;
43 import org.onap.policy.common.utils.coder.StandardCoder;
44 import org.onap.policy.models.base.PfConceptKey;
47 * Test the{@link JpaAutomationCompositionTest} class.
49 class JpaAutomationCompositionTest {
51 private static final String NULL_INSTANCE_ID_ERROR = "instanceId is marked .*ull but is null";
52 private static final String NULL_TEXT_ERROR = " is marked .*ull but is null";
53 private static final String INSTANCE_ID = "709c62b3-8918-41b9-a747-d21eb79c6c20";
54 private static final String COMPOSITION_ID = "709c62b3-8918-41b9-a747-e21eb79c6c41";
57 void testJpaAutomationCompositionConstructor() {
58 assertThatThrownBy(() -> {
59 new JpaAutomationComposition((JpaAutomationComposition) null);
60 }).hasMessageMatching("copyConcept is marked .*ull but is null");
62 assertThatThrownBy(() -> {
63 new JpaAutomationComposition((AutomationComposition) null);
64 }).hasMessageMatching("authorativeConcept is marked .*ull but is null");
66 assertThatThrownBy(() -> {
67 new JpaAutomationComposition(null, null, null, null, null, null, null);
68 }).hasMessageMatching(NULL_INSTANCE_ID_ERROR);
70 assertThatThrownBy(() -> {
71 new JpaAutomationComposition(INSTANCE_ID, null, null, null, new ArrayList<>(),
72 DeployState.UNDEPLOYED, LockState.LOCKED);
73 }).hasMessageMatching("key" + NULL_TEXT_ERROR);
75 assertThatThrownBy(() -> {
76 new JpaAutomationComposition(INSTANCE_ID, new PfConceptKey(), null,
77 AutomationCompositionState.UNINITIALISED, new ArrayList<>(),
78 DeployState.UNDEPLOYED, LockState.LOCKED);
79 }).hasMessageMatching("compositionId" + NULL_TEXT_ERROR);
81 assertThatThrownBy(() -> {
82 new JpaAutomationComposition(INSTANCE_ID, new PfConceptKey(), COMPOSITION_ID.toString(), null,
83 new ArrayList<>(), DeployState.UNDEPLOYED, LockState.LOCKED);
84 }).hasMessageMatching("state" + NULL_TEXT_ERROR);
86 assertThatThrownBy(() -> {
87 new JpaAutomationComposition(INSTANCE_ID, new PfConceptKey(), COMPOSITION_ID.toString(),
88 AutomationCompositionState.UNINITIALISED, null, DeployState.UNDEPLOYED, LockState.LOCKED);
89 }).hasMessageMatching("elements" + NULL_TEXT_ERROR);
91 assertThatThrownBy(() -> {
92 new JpaAutomationComposition(INSTANCE_ID, new PfConceptKey(), COMPOSITION_ID.toString(),
93 AutomationCompositionState.UNINITIALISED, new ArrayList<>(), null, LockState.LOCKED);
94 }).hasMessageMatching("deployState" + NULL_TEXT_ERROR);
96 assertThatThrownBy(() -> {
97 new JpaAutomationComposition(INSTANCE_ID, new PfConceptKey(), COMPOSITION_ID.toString(),
98 AutomationCompositionState.UNINITIALISED, new ArrayList<>(), DeployState.UNDEPLOYED, null);
99 }).hasMessageMatching("lockState" + NULL_TEXT_ERROR);
101 assertNotNull(new JpaAutomationComposition());
102 assertNotNull(new JpaAutomationComposition(INSTANCE_ID, new PfConceptKey(), COMPOSITION_ID.toString(),
103 AutomationCompositionState.UNINITIALISED, new ArrayList<>(), DeployState.UNDEPLOYED, LockState.LOCKED));
107 void testJpaAutomationComposition() {
108 var testJpaAutomationComposition = createJpaAutomationCompositionInstance();
110 var participant = createAutomationCompositionInstance();
111 assertEquals(participant, testJpaAutomationComposition.toAuthorative());
113 assertThatThrownBy(() -> {
114 testJpaAutomationComposition.fromAuthorative(null);
115 }).hasMessageMatching("automationComposition is marked .*ull but is null");
117 assertThatThrownBy(() -> new JpaAutomationComposition((JpaAutomationComposition) null))
118 .isInstanceOf(NullPointerException.class);
120 var testJpaAutomationCompositionFa = new JpaAutomationComposition();
121 testJpaAutomationCompositionFa.setInstanceId(null);
122 testJpaAutomationCompositionFa.fromAuthorative(participant);
123 assertEquals(testJpaAutomationComposition, testJpaAutomationCompositionFa);
125 assertEquals("automation-composition", testJpaAutomationComposition.getName());
126 assertEquals("automation-composition",
127 new JpaAutomationComposition(createAutomationCompositionInstance()).getName());
129 var testJpaAutomationComposition2 = new JpaAutomationComposition(testJpaAutomationComposition);
130 assertEquals(testJpaAutomationComposition, testJpaAutomationComposition2);
134 void testJpaAutomationCompositionElementOrderedState() throws CoderException {
135 var testAutomationComposition = createAutomationCompositionInstance();
136 var testJpaAutomationComposition = createJpaAutomationCompositionInstance();
138 testJpaAutomationComposition.setOrderedState(null);
139 assertEquals(testAutomationComposition, testJpaAutomationComposition.toAuthorative());
140 testJpaAutomationComposition.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
142 var noOrderedStateAc =
143 new StandardCoder().decode(new File("src/test/resources/json/AutomationCompositionNoOrderedState.json"),
144 AutomationComposition.class);
146 noOrderedStateAc.setInstanceId(UUID.fromString(INSTANCE_ID));
147 var noOrderedStateJpaAc = new JpaAutomationComposition(noOrderedStateAc);
148 assertNull(noOrderedStateJpaAc.getOrderedState());
149 noOrderedStateAc.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
150 noOrderedStateJpaAc = new JpaAutomationComposition(noOrderedStateAc);
151 assertEquals(testJpaAutomationComposition, noOrderedStateJpaAc);
154 new StandardCoder().decode(new File("src/test/resources/providers/TestAutomationCompositions.json"),
155 AutomationCompositions.class).getAutomationCompositionList().get(0);
157 acWithElements.setInstanceId(UUID.fromString(INSTANCE_ID));
158 var jpaAutomationCompositionWithElements = new JpaAutomationComposition(acWithElements);
159 assertEquals(4, jpaAutomationCompositionWithElements.getElements().size());
160 assertEquals(acWithElements, jpaAutomationCompositionWithElements.toAuthorative());
164 void testJpaAutomationCompositionValidation() {
165 var testJpaAutomationComposition = createJpaAutomationCompositionInstance();
167 assertThatThrownBy(() -> testJpaAutomationComposition.validate(null))
168 .hasMessageMatching("fieldName is marked .*ull but is null");
170 assertTrue(testJpaAutomationComposition.validate("").isValid());
174 void testJpaAutomationCompositionCompareTo() {
175 var testJpaAutomationComposition = createJpaAutomationCompositionInstance();
177 var otherJpaAutomationComposition = new JpaAutomationComposition(testJpaAutomationComposition);
178 assertEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
179 assertEquals(-1, testJpaAutomationComposition.compareTo(null));
180 assertEquals(0, testJpaAutomationComposition.compareTo(testJpaAutomationComposition));
181 assertNotEquals(0, testJpaAutomationComposition.compareTo(new DummyJpaAutomationCompositionChild()));
183 testJpaAutomationComposition.setInstanceId("BadValue");
184 assertNotEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
185 testJpaAutomationComposition.setInstanceId(INSTANCE_ID);
186 assertEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
188 testJpaAutomationComposition.setCompositionId(UUID.randomUUID().toString());
189 assertNotEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
190 testJpaAutomationComposition.setCompositionId(COMPOSITION_ID);
191 assertEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
193 testJpaAutomationComposition.setName("BadValue");
194 assertNotEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
195 testJpaAutomationComposition.setName("automation-composition");
196 assertEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
198 testJpaAutomationComposition.setVersion("0.0.0");
199 assertNotEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
200 testJpaAutomationComposition.setVersion("0.0.1");
201 assertEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
203 testJpaAutomationComposition.setState(AutomationCompositionState.PASSIVE);
204 assertNotEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
205 testJpaAutomationComposition.setState(AutomationCompositionState.UNINITIALISED);
206 assertEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
208 testJpaAutomationComposition.setOrderedState(AutomationCompositionOrderedState.PASSIVE);
209 assertNotEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
210 testJpaAutomationComposition.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
211 assertEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
213 testJpaAutomationComposition.setDescription("A description");
214 assertNotEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
215 testJpaAutomationComposition.setDescription(null);
216 assertEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
218 assertEquals(testJpaAutomationComposition, new JpaAutomationComposition(testJpaAutomationComposition));
222 void testJpaAutomationCompositionLombok() {
223 assertNotNull(new AutomationComposition());
224 var ac0 = new JpaAutomationComposition();
225 ac0.setCompositionId(COMPOSITION_ID);
227 assertThat(ac0.toString()).contains("JpaAutomationComposition(");
228 assertThat(ac0.hashCode()).isNotZero();
229 assertEquals(ac0, ac0);
230 assertNotEquals(null, ac0);
232 var ac1 = new JpaAutomationComposition();
234 ac1.setCompositionId(UUID.randomUUID().toString());
235 ac1.setDescription("Description");
236 ac1.setElements(new ArrayList<>());
237 ac1.setInstanceId(INSTANCE_ID);
238 ac1.setState(AutomationCompositionState.UNINITIALISED);
240 assertThat(ac1.toString()).contains("AutomationComposition(");
241 assertNotEquals(0, ac1.hashCode());
242 assertNotEquals(ac1, ac0);
243 assertNotEquals(null, ac1);
245 assertNotEquals(ac1, ac0);
247 var ac2 = new JpaAutomationComposition();
248 ac2.setCompositionId(COMPOSITION_ID);
249 ac2.setInstanceId(ac0.getInstanceId());
250 assertEquals(ac2, ac0);
253 private JpaAutomationComposition createJpaAutomationCompositionInstance() {
254 var testAutomationComposition = createAutomationCompositionInstance();
255 var testJpaAutomationComposition = new JpaAutomationComposition();
256 testJpaAutomationComposition.fromAuthorative(testAutomationComposition);
258 return testJpaAutomationComposition;
261 private AutomationComposition createAutomationCompositionInstance() {
262 var testAutomationComposition = new AutomationComposition();
263 testAutomationComposition.setName("automation-composition");
264 testAutomationComposition.setInstanceId(UUID.fromString(INSTANCE_ID));
265 testAutomationComposition.setVersion("0.0.1");
266 testAutomationComposition.setCompositionId(UUID.fromString(COMPOSITION_ID));
267 testAutomationComposition.setElements(new LinkedHashMap<>());
269 return testAutomationComposition;