3274833ca97524938e43d5176241f973bb5657e9
[policy/clamp.git] /
1 /*-
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
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.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;
30
31 import java.io.File;
32 import java.util.Map;
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.DeployState;
39 import org.onap.policy.clamp.models.acm.concepts.LockState;
40 import org.onap.policy.clamp.models.acm.concepts.Participant;
41 import org.onap.policy.clamp.models.acm.utils.CommonTestData;
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;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
46
47 /**
48  * Test the{@link JpaAutomationCompositionElement} class.
49  */
50 class JpaAutomationCompositionElementTest {
51
52     private static final String NULL_INSTANCE_ID_ERROR = "instanceId is marked .*ull but is null";
53     private static final String NULL_ELEMENT_ID_ERROR = "elementId is marked .*ull but is null";
54     private static final String NULL_ERROR = " is marked .*ull but is null";
55     private static final String ELEMENT_ID = "a95757ba-b34a-4049-a2a8-46773abcbe5e";
56     private static final String INSTANCE_ID = "a78757co-b34a-8949-a2a8-46773abcbe2a";
57
58     @Test
59     void testJpaAutomationCompositionElementConstructor() {
60         assertThatThrownBy(() -> {
61             new JpaAutomationCompositionElement((AutomationCompositionElement) null);
62         }).hasMessageMatching("authorativeConcept is marked .*ull but is null");
63
64         assertThatThrownBy(() -> {
65             new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null);
66         }).hasMessageMatching("copyConcept is marked .*ull but is null");
67
68         assertThatThrownBy(() -> {
69             new JpaAutomationCompositionElement("key", null);
70         }).hasMessageMatching(NULL_INSTANCE_ID_ERROR);
71
72         assertThatThrownBy(() -> {
73             new JpaAutomationCompositionElement(null, "key");
74         }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
75
76         assertThatThrownBy(() -> {
77             new JpaAutomationCompositionElement(null, null);
78         }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
79
80         assertThatThrownBy(() -> {
81             new JpaAutomationCompositionElement(null, null, null, null, null, null);
82         }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
83
84         assertThatThrownBy(() -> {
85             new JpaAutomationCompositionElement("key", null, null,
86                 AutomationCompositionState.UNINITIALISED, DeployState.UNDEPLOYED, LockState.LOCKED);
87         }).hasMessageMatching(NULL_INSTANCE_ID_ERROR);
88
89         assertThatThrownBy(() -> {
90             new JpaAutomationCompositionElement("key", "key", null,
91                 AutomationCompositionState.UNINITIALISED, DeployState.UNDEPLOYED, LockState.LOCKED);
92         }).hasMessageMatching("definition" + NULL_ERROR);
93
94         assertThatThrownBy(() -> {
95             new JpaAutomationCompositionElement("key", "key", new PfConceptKey(), null,
96                 DeployState.UNDEPLOYED, LockState.LOCKED);
97         }).hasMessageMatching("state" + NULL_ERROR);
98
99         assertThatThrownBy(() -> {
100             new JpaAutomationCompositionElement("key", "key", new PfConceptKey(),
101                 AutomationCompositionState.UNINITIALISED, null, LockState.LOCKED);
102         }).hasMessageMatching("deployState" + NULL_ERROR);
103
104         assertThatThrownBy(() -> {
105             new JpaAutomationCompositionElement("key", "key", new PfConceptKey(),
106                 AutomationCompositionState.UNINITIALISED, DeployState.UNDEPLOYED, null);
107         }).hasMessageMatching("lockState" + NULL_ERROR);
108
109         assertNotNull(new JpaAutomationCompositionElement());
110         assertNotNull(new JpaAutomationCompositionElement("key", "key"));
111         assertNotNull(new JpaAutomationCompositionElement("key", "key",
112             new PfConceptKey(), AutomationCompositionState.UNINITIALISED, DeployState.UNDEPLOYED, LockState.LOCKED));
113     }
114
115     @Test
116     void testJpaAutomationCompositionElement() {
117         var testJpaAcElement = createJpaAutomationCompositionElementInstance();
118
119         var ace = createAutomationCompositionElementInstance();
120         assertEquals(ace, testJpaAcElement.toAuthorative());
121
122         assertThatThrownBy(() -> {
123             testJpaAcElement.fromAuthorative(null);
124         }).hasMessageMatching("element is marked .*ull but is null");
125
126         assertThatThrownBy(() -> new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null))
127                 .isInstanceOf(NullPointerException.class);
128
129         var testJpaAcElementFa =
130                 new JpaAutomationCompositionElement(ace.getId().toString(), testJpaAcElement.getInstanceId());
131         testJpaAcElementFa.fromAuthorative(ace);
132         assertEquals(testJpaAcElement, testJpaAcElementFa);
133
134         assertEquals(ELEMENT_ID, testJpaAcElement.getElementId());
135
136         var testJpaAutomationCompositionElement2 = new JpaAutomationCompositionElement(testJpaAcElement);
137         assertEquals(testJpaAcElement, testJpaAutomationCompositionElement2);
138     }
139
140     @Test
141     void testJpaAutomationCompositionElementOrderedState() throws CoderException {
142         var testAutomationCompositionElement = createAutomationCompositionElementInstance();
143         var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
144
145         testJpaAutomationCompositionElement.setOrderedState(null);
146         assertEquals(testAutomationCompositionElement, testJpaAutomationCompositionElement.toAuthorative());
147         testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
148
149         var noOrderedStateAce = new StandardCoder().decode(
150                 new File("src/test/resources/json/AutomationCompositionElementNoOrderedState.json"),
151                 AutomationCompositionElement.class);
152
153         var noOrderedStateJpaAce = new JpaAutomationCompositionElement(noOrderedStateAce);
154         assertNull(noOrderedStateJpaAce.getOrderedState());
155         noOrderedStateAce.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
156         noOrderedStateJpaAce = new JpaAutomationCompositionElement(noOrderedStateAce);
157         noOrderedStateJpaAce.setInstanceId(testJpaAutomationCompositionElement.getInstanceId());
158         noOrderedStateJpaAce.setElementId(testJpaAutomationCompositionElement.getElementId());
159         noOrderedStateJpaAce.setParticipantId(testJpaAutomationCompositionElement.getParticipantId());
160         assertEquals(testJpaAutomationCompositionElement, noOrderedStateJpaAce);
161     }
162
163     @Test
164     void testJpaAutomationCompositionElementValidation() {
165         var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
166
167         assertThatThrownBy(() -> testJpaAutomationCompositionElement.validate(null))
168                 .hasMessageMatching("fieldName is marked .*ull but is null");
169
170         assertTrue(testJpaAutomationCompositionElement.validate("").isValid());
171     }
172
173     @Test
174     void testJpaAutomationCompositionElementCompareTo() {
175         var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
176
177         var otherJpaAutomationCompositionElement =
178                 new JpaAutomationCompositionElement(testJpaAutomationCompositionElement);
179         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
180         assertEquals(-1, testJpaAutomationCompositionElement.compareTo(null));
181         assertEquals(0, testJpaAutomationCompositionElement.compareTo(testJpaAutomationCompositionElement));
182         assertNotEquals(0,
183                 testJpaAutomationCompositionElement.compareTo(new DummyJpaAutomationCompositionElementChild()));
184
185         testJpaAutomationCompositionElement.setElementId("BadValue");
186         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
187         testJpaAutomationCompositionElement.setElementId(ELEMENT_ID);
188         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
189
190         testJpaAutomationCompositionElement.setInstanceId("BadValue");
191         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
192         testJpaAutomationCompositionElement.setInstanceId(INSTANCE_ID);
193         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
194
195         testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("BadValue", "0.0.1"));
196         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
197         testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("aceDef", "0.0.1"));
198         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
199
200         testJpaAutomationCompositionElement.setDescription("Description");
201         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
202         testJpaAutomationCompositionElement.setDescription(null);
203         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
204
205         testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.PASSIVE);
206         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
207         testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
208         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
209
210         testJpaAutomationCompositionElement.setState(AutomationCompositionState.PASSIVE);
211         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
212         testJpaAutomationCompositionElement.setState(AutomationCompositionState.UNINITIALISED);
213         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
214
215         testJpaAutomationCompositionElement.setDeployState(DeployState.DEPLOYED);
216         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
217         testJpaAutomationCompositionElement.setDeployState(DeployState.UNDEPLOYED);
218         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
219
220         testJpaAutomationCompositionElement.setLockState(LockState.UNLOCKED);
221         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
222         testJpaAutomationCompositionElement.setLockState(LockState.LOCKED);
223         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
224
225         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
226         testJpaAutomationCompositionElement.setParticipantId(UUID.randomUUID().toString());
227         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
228
229         assertEquals(testJpaAutomationCompositionElement,
230                 new JpaAutomationCompositionElement(testJpaAutomationCompositionElement));
231     }
232
233     @Test
234     void testJpaAutomationCompositionElementLombok() {
235         assertNotNull(new Participant());
236         var ace0 = new JpaAutomationCompositionElement();
237
238         assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
239         assertThat(ace0.hashCode()).isNotZero();
240         assertEquals(ace0, ace0);
241         assertNotEquals(null, ace0);
242
243         var ace1 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
244
245         ace1.setDefinition(new PfConceptKey("defName", "0.0.1"));
246         ace1.setDescription("Description");
247         ace1.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
248         ace1.setState(AutomationCompositionState.UNINITIALISED);
249         ace1.setParticipantId(CommonTestData.getJpaParticipantId());
250
251         assertThat(ace1.toString()).contains("AutomationCompositionElement(");
252         assertNotEquals(0, ace1.hashCode());
253         assertNotEquals(ace1, ace0);
254         assertNotEquals(null, ace1);
255
256         assertNotEquals(ace1, ace0);
257
258         var ace2 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
259         assertEquals(ace2, ace0);
260     }
261
262     private JpaAutomationCompositionElement createJpaAutomationCompositionElementInstance() {
263         var testAce = createAutomationCompositionElementInstance();
264         var testJpaAcElement =
265                 new JpaAutomationCompositionElement(testAce.getId().toString(), INSTANCE_ID);
266         testJpaAcElement.fromAuthorative(testAce);
267         testJpaAcElement.setProperties(Map.of("key", "{}"));
268
269         return testJpaAcElement;
270     }
271
272     private AutomationCompositionElement createAutomationCompositionElementInstance() {
273         var automationCompositionElement = new AutomationCompositionElement();
274         automationCompositionElement.setId(UUID.fromString(ELEMENT_ID));
275         automationCompositionElement.setDefinition(new ToscaConceptIdentifier("aceDef", "0.0.1"));
276         automationCompositionElement.setParticipantId(CommonTestData.getParticipantId());
277         automationCompositionElement.setProperties(Map.of("key", "{}"));
278
279         return automationCompositionElement;
280     }
281 }