62d3ada22950fe4cfbbc42493a85fef6ff922771
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved.
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.models.acm.persistence.concepts;
22
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;
29
30 import java.util.Map;
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.MigrationState;
37 import org.onap.policy.clamp.models.acm.concepts.SubState;
38 import org.onap.policy.clamp.models.acm.utils.CommonTestData;
39 import org.onap.policy.models.base.PfConceptKey;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41
42 /**
43  * Test the{@link JpaAutomationCompositionElement} class.
44  */
45 class JpaAutomationCompositionElementTest {
46
47     private static final String NULL_INSTANCE_ID_ERROR = "instanceId is marked .*ull but is null";
48     private static final String NULL_ELEMENT_ID_ERROR = "elementId is marked .*ull but is null";
49     private static final String NULL_ERROR = " is marked .*ull but is null";
50     private static final String ELEMENT_ID = "a95757ba-b34a-4049-a2a8-46773abcbe5e";
51     private static final String INSTANCE_ID = "a78757co-b34a-8949-a2a8-46773abcbe2a";
52     private static final String KEY = "key";
53     private static final String BAD_VALUE = "BadValue";
54
55     @Test
56     void testJpaAutomationCompositionElementConstructor() {
57         assertThatThrownBy(() -> {
58             new JpaAutomationCompositionElement((AutomationCompositionElement) null);
59         }).hasMessageMatching("authorativeConcept" + NULL_ERROR);
60
61         assertThatThrownBy(() -> {
62             new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null);
63         }).hasMessageMatching("copyConcept" + NULL_ERROR);
64
65         assertThatThrownBy(() -> {
66             new JpaAutomationCompositionElement(KEY, null);
67         }).hasMessageMatching(NULL_INSTANCE_ID_ERROR);
68
69         assertThatThrownBy(() -> {
70             new JpaAutomationCompositionElement(null, KEY);
71         }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
72
73         assertThatThrownBy(() -> {
74             new JpaAutomationCompositionElement(null, null);
75         }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
76
77         assertDoesNotThrow(() -> new JpaAutomationCompositionElement(KEY, KEY));
78     }
79
80     @Test
81     void testJpaAutomationCompositionElement() {
82         var testJpaAcElement = createJpaAutomationCompositionElementInstance();
83
84         var ace = createAutomationCompositionElementInstance();
85         assertEquals(ace, testJpaAcElement.toAuthorative());
86
87         assertThatThrownBy(() -> {
88             testJpaAcElement.fromAuthorative(null);
89         }).hasMessageMatching("element" + NULL_ERROR);
90
91         assertThatThrownBy(() -> new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null))
92                 .isInstanceOf(NullPointerException.class);
93
94         var testJpaAcElementFa =
95                 new JpaAutomationCompositionElement(ace.getId().toString(), testJpaAcElement.getInstanceId());
96         testJpaAcElementFa.fromAuthorative(ace);
97         assertEquals(testJpaAcElement, testJpaAcElementFa);
98
99         assertEquals(ELEMENT_ID, testJpaAcElement.getElementId());
100
101         var testJpaAcElement2 = new JpaAutomationCompositionElement(testJpaAcElement);
102         assertEquals(testJpaAcElement, testJpaAcElement2);
103
104         testJpaAcElement2 = new JpaAutomationCompositionElement(testJpaAcElement.toAuthorative());
105         testJpaAcElement2.setElementId(ELEMENT_ID);
106         testJpaAcElement2.setInstanceId(INSTANCE_ID);
107         assertEquals(testJpaAcElement, testJpaAcElement2);
108     }
109
110     @Test
111     void testJpaAutomationCompositionElementValidation() {
112         var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
113
114         assertThatThrownBy(() -> testJpaAutomationCompositionElement.validate(null))
115                 .hasMessageMatching("fieldName" + NULL_ERROR);
116
117         assertTrue(testJpaAutomationCompositionElement.validate("").isValid());
118     }
119
120     @Test
121     void testJpaAcElementCompareTo() {
122         var testJpaAcElement = createJpaAutomationCompositionElementInstance();
123
124         var otherJpaAcElement =
125                 new JpaAutomationCompositionElement(testJpaAcElement);
126         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
127         assertEquals(-1, testJpaAcElement.compareTo(null));
128         assertEquals(0, testJpaAcElement.compareTo(testJpaAcElement));
129         assertNotEquals(0,
130                 testJpaAcElement.compareTo(new DummyJpaAutomationCompositionElementChild()));
131
132         assertEquals(testJpaAcElement, new JpaAutomationCompositionElement(testJpaAcElement));
133     }
134
135     @Test
136     void testJpaAutomationCompositionElementCompareTo() {
137         var testJpaAcElement = createJpaAutomationCompositionElementInstance();
138
139         var otherJpaAcElement =
140                 new JpaAutomationCompositionElement(testJpaAcElement);
141
142         testJpaAcElement.setElementId(BAD_VALUE);
143         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
144         testJpaAcElement.setElementId(ELEMENT_ID);
145         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
146
147         testJpaAcElement.setInstanceId(BAD_VALUE);
148         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
149         testJpaAcElement.setInstanceId(INSTANCE_ID);
150         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
151
152         testJpaAcElement.setDefinition(new PfConceptKey(BAD_VALUE, "0.0.1"));
153         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
154         testJpaAcElement.setDefinition(new PfConceptKey("aceDef", "0.0.1"));
155         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
156
157         testJpaAcElement.setDescription("Description");
158         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
159         testJpaAcElement.setDescription(null);
160         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
161
162         testJpaAcElement.setDeployState(DeployState.DEPLOYED);
163         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
164         testJpaAcElement.setDeployState(DeployState.UNDEPLOYED);
165         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
166
167         testJpaAcElement.setLockState(LockState.UNLOCKED);
168         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
169         testJpaAcElement.setLockState(LockState.LOCKED);
170         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
171
172         testJpaAcElement.setSubState(SubState.PREPARING);
173         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
174         testJpaAcElement.setSubState(SubState.NONE);
175         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
176
177         testJpaAcElement.setMigrationState(MigrationState.REMOVED);
178         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
179         testJpaAcElement.setMigrationState(MigrationState.DEFAULT);
180         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
181
182         testJpaAcElement.setUseState(BAD_VALUE);
183         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
184         testJpaAcElement.setUseState("IDLE");
185         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
186
187         testJpaAcElement.setOperationalState(BAD_VALUE);
188         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
189         testJpaAcElement.setOperationalState("DEFAULT");
190         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
191
192         testJpaAcElement.setStage(1);
193         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
194         testJpaAcElement.setStage(null);
195         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
196
197         testJpaAcElement.setMessage("Message");
198         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
199         testJpaAcElement.setMessage(null);
200         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
201
202         testJpaAcElement.setParticipantId(UUID.randomUUID().toString());
203         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
204
205     }
206
207     @Test
208     void testJpaAutomationCompositionElementLombok() {
209         var ace0 = new JpaAutomationCompositionElement();
210
211         assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
212         assertThat(ace0.hashCode()).isNotZero();
213         assertNotEquals(null, ace0);
214
215         var ace1 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
216
217         ace1.setDefinition(new PfConceptKey("defName", "0.0.1"));
218         ace1.setDescription("Description");
219         ace1.setParticipantId(CommonTestData.getJpaParticipantId());
220
221         assertThat(ace1.toString()).contains("AutomationCompositionElement(");
222         assertNotEquals(0, ace1.hashCode());
223         assertNotEquals(ace1, ace0);
224         assertNotEquals(null, ace1);
225
226         assertNotEquals(ace1, ace0);
227
228         var ace2 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
229         assertEquals(ace2, ace0);
230     }
231
232     private JpaAutomationCompositionElement createJpaAutomationCompositionElementInstance() {
233         var testAce = createAutomationCompositionElementInstance();
234         var testJpaAcElement =
235                 new JpaAutomationCompositionElement(testAce.getId().toString(), INSTANCE_ID);
236         testJpaAcElement.fromAuthorative(testAce);
237         testJpaAcElement.setProperties(Map.of(KEY, "{}"));
238
239         return testJpaAcElement;
240     }
241
242     private AutomationCompositionElement createAutomationCompositionElementInstance() {
243         var automationCompositionElement = new AutomationCompositionElement();
244         automationCompositionElement.setId(UUID.fromString(ELEMENT_ID));
245         automationCompositionElement.setDefinition(new ToscaConceptIdentifier("aceDef", "0.0.1"));
246         automationCompositionElement.setParticipantId(CommonTestData.getParticipantId());
247         automationCompositionElement.setProperties(Map.of(KEY, "{}"));
248         automationCompositionElement.setUseState("IDLE");
249         automationCompositionElement.setOperationalState("DEFAULT");
250
251         return automationCompositionElement;
252     }
253 }