a9b69f8a85929ed5c82238890cbdfe741eda19aa
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2026 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.common.parameters.BeanValidator;
40 import org.onap.policy.models.base.PfConceptKey;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42
43 /**
44  * Test the{@link JpaAutomationCompositionElement} class.
45  */
46 class JpaAutomationCompositionElementTest {
47
48     private static final String NULL_INSTANCE_ID_ERROR = "instanceId is marked non-null but is null";
49     private static final String NULL_ELEMENT_ID_ERROR = "elementId is marked non-null but is null";
50     private static final String NULL_ERROR = " is marked non-null but is null";
51     private static final String ELEMENT_ID = "a95757ba-b34a-4049-a2a8-46773abcbe5e";
52     private static final String INSTANCE_ID = "a78757co-b34a-8949-a2a8-46773abcbe2a";
53     private static final String KEY = "key";
54     private static final String BAD_VALUE = "BadValue";
55
56     @Test
57     void testJpaAutomationCompositionElementConstructor() {
58         assertThatThrownBy(() -> {
59             new JpaAutomationCompositionElement((AutomationCompositionElement) null);
60         }).hasMessageMatching("authorativeConcept" + NULL_ERROR);
61
62         assertThatThrownBy(() -> {
63             new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null);
64         }).hasMessageMatching("copyConcept" + NULL_ERROR);
65
66         assertThatThrownBy(() -> {
67             new JpaAutomationCompositionElement(KEY, null);
68         }).hasMessageMatching(NULL_INSTANCE_ID_ERROR);
69
70         assertThatThrownBy(() -> {
71             new JpaAutomationCompositionElement(null, KEY);
72         }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
73
74         assertThatThrownBy(() -> {
75             new JpaAutomationCompositionElement(null, null);
76         }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
77
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         assertTrue(BeanValidator.isValid(testJpaAutomationCompositionElement));
115     }
116
117     @Test
118     void testJpaAcElementCompareTo() {
119         var testJpaAcElement = createJpaAutomationCompositionElementInstance();
120
121         var otherJpaAcElement =
122                 new JpaAutomationCompositionElement(testJpaAcElement);
123         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
124         assertEquals(-1, testJpaAcElement.compareTo(null));
125         assertEquals(0, testJpaAcElement.compareTo(testJpaAcElement));
126         assertNotEquals(0,
127                 testJpaAcElement.compareTo(new DummyJpaAutomationCompositionElementChild()));
128
129         assertEquals(testJpaAcElement, new JpaAutomationCompositionElement(testJpaAcElement));
130     }
131
132     @Test
133     void testJpaAutomationCompositionElementCompareTo() {
134         var testJpaAcElement = createJpaAutomationCompositionElementInstance();
135
136         var otherJpaAcElement =
137                 new JpaAutomationCompositionElement(testJpaAcElement);
138
139         testJpaAcElement.setElementId(BAD_VALUE);
140         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
141         testJpaAcElement.setElementId(ELEMENT_ID);
142         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
143
144         testJpaAcElement.setInstanceId(BAD_VALUE);
145         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
146         testJpaAcElement.setInstanceId(INSTANCE_ID);
147         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
148
149         testJpaAcElement.setDefinition(new PfConceptKey(BAD_VALUE, "0.0.1"));
150         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
151         testJpaAcElement.setDefinition(new PfConceptKey("aceDef", "0.0.1"));
152         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
153
154         testJpaAcElement.setDescription("Description");
155         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
156         testJpaAcElement.setDescription(null);
157         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
158
159         testJpaAcElement.setDeployState(DeployState.DEPLOYED);
160         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
161         testJpaAcElement.setDeployState(DeployState.UNDEPLOYED);
162         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
163
164         testJpaAcElement.setLockState(LockState.UNLOCKED);
165         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
166         testJpaAcElement.setLockState(LockState.LOCKED);
167         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
168
169         testJpaAcElement.setSubState(SubState.PREPARING);
170         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
171         testJpaAcElement.setSubState(SubState.NONE);
172         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
173
174         testJpaAcElement.setMigrationState(MigrationState.REMOVED);
175         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
176         testJpaAcElement.setMigrationState(MigrationState.DEFAULT);
177         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
178
179         testJpaAcElement.setUseState(BAD_VALUE);
180         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
181         testJpaAcElement.setUseState("IDLE");
182         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
183
184         testJpaAcElement.setOperationalState(BAD_VALUE);
185         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
186         testJpaAcElement.setOperationalState("DEFAULT");
187         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
188
189         testJpaAcElement.setStage(1);
190         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
191         testJpaAcElement.setStage(null);
192         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
193
194         testJpaAcElement.setMessage("Message");
195         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
196         testJpaAcElement.setMessage(null);
197         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
198
199         testJpaAcElement.setParticipantId(UUID.randomUUID().toString());
200         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
201
202     }
203
204     @Test
205     void testJpaAutomationCompositionElementLombok() {
206         var ace0 = new JpaAutomationCompositionElement();
207
208         assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
209         assertThat(ace0.hashCode()).isNotZero();
210         assertNotEquals(null, ace0);
211
212         var ace1 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
213
214         ace1.setDefinition(new PfConceptKey("defName", "0.0.1"));
215         ace1.setDescription("Description");
216         ace1.setParticipantId(CommonTestData.getJpaParticipantId());
217
218         assertThat(ace1.toString()).contains("AutomationCompositionElement(");
219         assertNotEquals(0, ace1.hashCode());
220         assertNotEquals(ace1, ace0);
221         assertNotEquals(null, ace1);
222
223         assertNotEquals(ace1, ace0);
224
225         var ace2 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
226         assertEquals(ace2, ace0);
227     }
228
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, "{}"));
235
236         return testJpaAcElement;
237     }
238
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, "{}"));
245         automationCompositionElement.setUseState("IDLE");
246         automationCompositionElement.setOperationalState("DEFAULT");
247
248         return automationCompositionElement;
249     }
250 }