2d63343baa51758e96bacc38d039a38c1c48e0a8
[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());
78         assertDoesNotThrow(() -> new JpaAutomationCompositionElement(KEY, KEY));
79     }
80
81     @Test
82     void testJpaAutomationCompositionElement() {
83         var testJpaAcElement = createJpaAutomationCompositionElementInstance();
84
85         var ace = createAutomationCompositionElementInstance();
86         assertEquals(ace, testJpaAcElement.toAuthorative());
87
88         assertThatThrownBy(() -> {
89             testJpaAcElement.fromAuthorative(null);
90         }).hasMessageMatching("element" + NULL_ERROR);
91
92         assertThatThrownBy(() -> new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null))
93                 .isInstanceOf(NullPointerException.class);
94
95         var testJpaAcElementFa =
96                 new JpaAutomationCompositionElement(ace.getId().toString(), testJpaAcElement.getInstanceId());
97         testJpaAcElementFa.fromAuthorative(ace);
98         assertEquals(testJpaAcElement, testJpaAcElementFa);
99
100         assertEquals(ELEMENT_ID, testJpaAcElement.getElementId());
101
102         var testJpaAcElement2 = new JpaAutomationCompositionElement(testJpaAcElement);
103         assertEquals(testJpaAcElement, testJpaAcElement2);
104
105         testJpaAcElement2 = new JpaAutomationCompositionElement(testJpaAcElement.toAuthorative());
106         testJpaAcElement2.setElementId(ELEMENT_ID);
107         testJpaAcElement2.setInstanceId(INSTANCE_ID);
108         assertEquals(testJpaAcElement, testJpaAcElement2);
109     }
110
111     @Test
112     void testJpaAutomationCompositionElementValidation() {
113         var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
114
115         assertThatThrownBy(() -> testJpaAutomationCompositionElement.validate(null))
116                 .hasMessageMatching("fieldName" + NULL_ERROR);
117
118         assertTrue(testJpaAutomationCompositionElement.validate("").isValid());
119     }
120
121     @Test
122     void testJpaAcElementCompareTo() {
123         var testJpaAcElement = createJpaAutomationCompositionElementInstance();
124
125         var otherJpaAcElement =
126                 new JpaAutomationCompositionElement(testJpaAcElement);
127         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
128         assertEquals(-1, testJpaAcElement.compareTo(null));
129         assertEquals(0, testJpaAcElement.compareTo(testJpaAcElement));
130         assertNotEquals(0,
131                 testJpaAcElement.compareTo(new DummyJpaAutomationCompositionElementChild()));
132
133         assertEquals(testJpaAcElement, new JpaAutomationCompositionElement(testJpaAcElement));
134     }
135
136     @Test
137     void testJpaAutomationCompositionElementCompareTo() {
138         var testJpaAcElement = createJpaAutomationCompositionElementInstance();
139
140         var otherJpaAcElement =
141                 new JpaAutomationCompositionElement(testJpaAcElement);
142
143         testJpaAcElement.setElementId(BAD_VALUE);
144         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
145         testJpaAcElement.setElementId(ELEMENT_ID);
146         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
147
148         testJpaAcElement.setInstanceId(BAD_VALUE);
149         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
150         testJpaAcElement.setInstanceId(INSTANCE_ID);
151         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
152
153         testJpaAcElement.setDefinition(new PfConceptKey(BAD_VALUE, "0.0.1"));
154         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
155         testJpaAcElement.setDefinition(new PfConceptKey("aceDef", "0.0.1"));
156         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
157
158         testJpaAcElement.setDescription("Description");
159         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
160         testJpaAcElement.setDescription(null);
161         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
162
163         testJpaAcElement.setDeployState(DeployState.DEPLOYED);
164         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
165         testJpaAcElement.setDeployState(DeployState.UNDEPLOYED);
166         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
167
168         testJpaAcElement.setLockState(LockState.UNLOCKED);
169         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
170         testJpaAcElement.setLockState(LockState.LOCKED);
171         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
172
173         testJpaAcElement.setSubState(SubState.PREPARING);
174         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
175         testJpaAcElement.setSubState(SubState.NONE);
176         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
177
178         testJpaAcElement.setMigrationState(MigrationState.REMOVED);
179         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
180         testJpaAcElement.setMigrationState(MigrationState.DEFAULT);
181         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
182
183         testJpaAcElement.setUseState(BAD_VALUE);
184         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
185         testJpaAcElement.setUseState("IDLE");
186         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
187
188         testJpaAcElement.setOperationalState(BAD_VALUE);
189         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
190         testJpaAcElement.setOperationalState("DEFAULT");
191         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
192
193         testJpaAcElement.setStage(1);
194         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
195         testJpaAcElement.setStage(null);
196         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
197
198         testJpaAcElement.setMessage("Message");
199         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
200         testJpaAcElement.setMessage(null);
201         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
202
203         testJpaAcElement.setParticipantId(UUID.randomUUID().toString());
204         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
205
206     }
207
208     @Test
209     void testJpaAutomationCompositionElementLombok() {
210         var ace0 = new JpaAutomationCompositionElement();
211
212         assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
213         assertThat(ace0.hashCode()).isNotZero();
214         assertNotEquals(null, ace0);
215
216         var ace1 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
217
218         ace1.setDefinition(new PfConceptKey("defName", "0.0.1"));
219         ace1.setDescription("Description");
220         ace1.setParticipantId(CommonTestData.getJpaParticipantId());
221
222         assertThat(ace1.toString()).contains("AutomationCompositionElement(");
223         assertNotEquals(0, ace1.hashCode());
224         assertNotEquals(ace1, ace0);
225         assertNotEquals(null, ace1);
226
227         assertNotEquals(ace1, ace0);
228
229         var ace2 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
230         assertEquals(ace2, ace0);
231     }
232
233     private JpaAutomationCompositionElement createJpaAutomationCompositionElementInstance() {
234         var testAce = createAutomationCompositionElementInstance();
235         var testJpaAcElement =
236                 new JpaAutomationCompositionElement(testAce.getId().toString(), INSTANCE_ID);
237         testJpaAcElement.fromAuthorative(testAce);
238         testJpaAcElement.setProperties(Map.of(KEY, "{}"));
239
240         return testJpaAcElement;
241     }
242
243     private AutomationCompositionElement createAutomationCompositionElementInstance() {
244         var automationCompositionElement = new AutomationCompositionElement();
245         automationCompositionElement.setId(UUID.fromString(ELEMENT_ID));
246         automationCompositionElement.setDefinition(new ToscaConceptIdentifier("aceDef", "0.0.1"));
247         automationCompositionElement.setParticipantId(CommonTestData.getParticipantId());
248         automationCompositionElement.setProperties(Map.of(KEY, "{}"));
249         automationCompositionElement.setUseState("IDLE");
250         automationCompositionElement.setOperationalState("DEFAULT");
251
252         return automationCompositionElement;
253     }
254 }