d38aaf73523f115dd5eb82af4b25c0760eb6749e
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 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.Participant;
39 import org.onap.policy.common.utils.coder.CoderException;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.models.base.PfConceptKey;
42 import org.onap.policy.models.base.PfKey;
43 import org.onap.policy.models.base.PfReferenceKey;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
45
46 /**
47  * Test the {@link JpaAutomationCompositionElement} class.
48  */
49 class JpaAutomationCompositionElementTest {
50
51     private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
52
53     @Test
54     void testJpaAutomationCompositionElementConstructor() {
55         assertThatThrownBy(() -> {
56             new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null);
57         }).hasMessageMatching("copyConcept is marked .*ull but is null");
58
59         assertThatThrownBy(() -> {
60             new JpaAutomationCompositionElement((PfReferenceKey) null);
61         }).hasMessageMatching(NULL_KEY_ERROR);
62
63         assertThatThrownBy(() -> {
64             new JpaAutomationCompositionElement(null, null, null, null);
65         }).hasMessageMatching(NULL_KEY_ERROR);
66
67         assertThatThrownBy(() -> {
68             new JpaAutomationCompositionElement(null, null, null, AutomationCompositionState.UNINITIALISED);
69         }).hasMessageMatching(NULL_KEY_ERROR);
70
71         assertThatThrownBy(() -> {
72             new JpaAutomationCompositionElement(null, null, new PfConceptKey("participant", "0.0.1"), null);
73         }).hasMessageMatching(NULL_KEY_ERROR);
74
75         assertThatThrownBy(() -> {
76             new JpaAutomationCompositionElement(null, null, new PfConceptKey("participant", "0.0.1"),
77                 AutomationCompositionState.UNINITIALISED);
78         }).hasMessageMatching(NULL_KEY_ERROR);
79
80         assertThatThrownBy(() -> {
81             new JpaAutomationCompositionElement(null, new PfConceptKey(), null, null);
82         }).hasMessageMatching(NULL_KEY_ERROR);
83
84         assertThatThrownBy(() -> {
85             new JpaAutomationCompositionElement(null, new PfConceptKey(), null,
86                 AutomationCompositionState.UNINITIALISED);
87         }).hasMessageMatching(NULL_KEY_ERROR);
88
89         assertThatThrownBy(() -> {
90             new JpaAutomationCompositionElement(null, new PfConceptKey(), new PfConceptKey("participant", "0.0.1"),
91                 null);
92         }).hasMessageMatching(NULL_KEY_ERROR);
93
94         assertThatThrownBy(() -> {
95             new JpaAutomationCompositionElement(null, new PfConceptKey(), new PfConceptKey("participant", "0.0.1"),
96                 AutomationCompositionState.UNINITIALISED);
97         }).hasMessageMatching(NULL_KEY_ERROR);
98
99         assertThatThrownBy(() -> {
100             new JpaAutomationCompositionElement(new PfReferenceKey(), null, null, null);
101         }).hasMessageMatching("definition is marked .*ull but is null");
102
103         assertThatThrownBy(() -> {
104             new JpaAutomationCompositionElement(new PfReferenceKey(), null, null,
105                 AutomationCompositionState.UNINITIALISED);
106         }).hasMessageMatching("definition is marked .*ull but is null");
107
108         assertThatThrownBy(() -> {
109             new JpaAutomationCompositionElement(new PfReferenceKey(), null, new PfConceptKey("participant", "0.0.1"),
110                 null);
111         }).hasMessageMatching("definition is marked .*ull but is null");
112
113         assertThatThrownBy(() -> {
114             new JpaAutomationCompositionElement(new PfReferenceKey(), null, new PfConceptKey("participant", "0.0.1"),
115                 AutomationCompositionState.UNINITIALISED);
116         }).hasMessageMatching("definition is marked .*ull but is null");
117
118         assertThatThrownBy(() -> {
119             new JpaAutomationCompositionElement(new PfReferenceKey(), new PfConceptKey(), null, null);
120         }).hasMessageMatching("participantType is marked .*ull but is null");
121
122         assertThatThrownBy(() -> {
123             new JpaAutomationCompositionElement(new PfReferenceKey(), new PfConceptKey(), null,
124                 AutomationCompositionState.UNINITIALISED);
125         }).hasMessageMatching("participantType is marked .*ull but is null");
126
127         assertThatThrownBy(() -> {
128             new JpaAutomationCompositionElement(new PfReferenceKey(), new PfConceptKey(),
129                 new PfConceptKey("participant", "0.0.1"), null);
130         }).hasMessageMatching("state is marked .*ull but is null");
131
132         assertNotNull(new JpaAutomationCompositionElement());
133         assertNotNull(new JpaAutomationCompositionElement((new PfReferenceKey())));
134         assertNotNull(new JpaAutomationCompositionElement(new PfReferenceKey(), new PfConceptKey(),
135             new PfConceptKey("participant", "0.0.1"), AutomationCompositionState.UNINITIALISED));
136     }
137
138     @Test
139     void testJpaAutomationCompositionElement() {
140         var testJpaAutomationCompositionElement =
141             createJpaAutomationCompositionElementInstance();
142
143         var ace = createAutomationCompositionElementInstance();
144         assertEquals(ace, testJpaAutomationCompositionElement.toAuthorative());
145
146         assertThatThrownBy(() -> {
147             testJpaAutomationCompositionElement.fromAuthorative(null);
148         }).hasMessageMatching("element is marked .*ull but is null");
149
150         assertThatThrownBy(() -> new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null))
151             .isInstanceOf(NullPointerException.class);
152
153         var testJpaAutomationCompositionElementFa = new JpaAutomationCompositionElement();
154         testJpaAutomationCompositionElementFa.setKey(null);
155         testJpaAutomationCompositionElementFa.fromAuthorative(ace);
156         assertEquals(testJpaAutomationCompositionElement, testJpaAutomationCompositionElementFa);
157         testJpaAutomationCompositionElementFa.setKey(PfReferenceKey.getNullKey());
158         testJpaAutomationCompositionElementFa.fromAuthorative(ace);
159         assertEquals(testJpaAutomationCompositionElement, testJpaAutomationCompositionElementFa);
160         testJpaAutomationCompositionElementFa.setKey(
161             new PfReferenceKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, "a95757ba-b34a-4049-a2a8-46773abcbe5e"));
162         testJpaAutomationCompositionElementFa.fromAuthorative(ace);
163         assertEquals(testJpaAutomationCompositionElement, testJpaAutomationCompositionElementFa);
164
165         assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e",
166             testJpaAutomationCompositionElement.getKey().getLocalName());
167         assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e",
168             new JpaAutomationCompositionElement(createAutomationCompositionElementInstance()).getKey().getLocalName());
169         assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e",
170             ((PfReferenceKey) new JpaAutomationCompositionElement(createAutomationCompositionElementInstance())
171                 .getKeys().get(0)).getLocalName());
172
173         testJpaAutomationCompositionElement.clean();
174         assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e",
175             testJpaAutomationCompositionElement.getKey().getLocalName());
176
177         testJpaAutomationCompositionElement.setDescription(" A Message ");
178         testJpaAutomationCompositionElement.clean();
179         assertEquals("A Message", testJpaAutomationCompositionElement.getDescription());
180
181         var testJpaAutomationCompositionElement2 =
182             new JpaAutomationCompositionElement(testJpaAutomationCompositionElement);
183         assertEquals(testJpaAutomationCompositionElement, testJpaAutomationCompositionElement2);
184     }
185
186     @Test
187     void testJpaAutomationCompositionElementOrderedState() throws CoderException {
188         var testAutomationCompositionElement = createAutomationCompositionElementInstance();
189         var testJpaAutomationCompositionElement =
190             createJpaAutomationCompositionElementInstance();
191
192         testJpaAutomationCompositionElement.setOrderedState(null);
193         assertEquals(testAutomationCompositionElement, testJpaAutomationCompositionElement.toAuthorative());
194         testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
195
196         var noOrderedStateAce = new StandardCoder().decode(
197             new File("src/test/resources/json/AutomationCompositionElementNoOrderedState.json"),
198             AutomationCompositionElement.class);
199
200         var noOrderedStateJpaAce = new JpaAutomationCompositionElement(noOrderedStateAce);
201         assertNull(noOrderedStateJpaAce.getOrderedState());
202         noOrderedStateAce.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
203         noOrderedStateJpaAce = new JpaAutomationCompositionElement(noOrderedStateAce);
204         assertEquals(testJpaAutomationCompositionElement, noOrderedStateJpaAce);
205     }
206
207     @Test
208     void testJpaAutomationCompositionElementValidation() {
209         var testJpaAutomationCompositionElement =
210             createJpaAutomationCompositionElementInstance();
211
212         assertThatThrownBy(() -> testJpaAutomationCompositionElement.validate(null))
213             .hasMessageMatching("fieldName is marked .*ull but is null");
214
215         assertTrue(testJpaAutomationCompositionElement.validate("").isValid());
216     }
217
218     @Test
219     void testJpaAutomationCompositionElementCompareTo() {
220         var testJpaAutomationCompositionElement =
221             createJpaAutomationCompositionElementInstance();
222
223         var otherJpaAutomationCompositionElement =
224             new JpaAutomationCompositionElement(testJpaAutomationCompositionElement);
225         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
226         assertEquals(-1, testJpaAutomationCompositionElement.compareTo(null));
227         assertEquals(0, testJpaAutomationCompositionElement.compareTo(testJpaAutomationCompositionElement));
228         assertNotEquals(0,
229             testJpaAutomationCompositionElement.compareTo(new DummyJpaAutomationCompositionElementChild()));
230
231         testJpaAutomationCompositionElement
232             .setKey(new PfReferenceKey("BadValue", "0.0.1", "a95757ba-b34a-4049-a2a8-46773abcbe5e"));
233         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
234         testJpaAutomationCompositionElement.setKey(
235             new PfReferenceKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, "a95757ba-b34a-4049-a2a8-46773abcbe5e"));
236         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
237
238         testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("BadValue", "0.0.1"));
239         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
240         testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("aceDef", "0.0.1"));
241         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
242
243         testJpaAutomationCompositionElement.setDescription("Description");
244         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
245         testJpaAutomationCompositionElement.setDescription(null);
246         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
247
248         testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.PASSIVE);
249         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
250         testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
251         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
252
253         testJpaAutomationCompositionElement.setState(AutomationCompositionState.PASSIVE);
254         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
255         testJpaAutomationCompositionElement.setState(AutomationCompositionState.UNINITIALISED);
256         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
257
258         testJpaAutomationCompositionElement.setParticipantType(new PfConceptKey("dummy", "0.0.1"));
259         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
260         testJpaAutomationCompositionElement.setParticipantType(new PfConceptKey("participantType", "0.0.1"));
261         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
262
263         assertEquals(testJpaAutomationCompositionElement,
264             new JpaAutomationCompositionElement(testJpaAutomationCompositionElement));
265     }
266
267     @Test
268     void testJpaAutomationCompositionElementLombok() {
269         assertNotNull(new Participant());
270         var ace0 = new JpaAutomationCompositionElement();
271
272         assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
273         assertThat(ace0.hashCode()).isNotZero();
274         assertEquals(ace0, ace0);
275         assertNotEquals(null, ace0);
276
277         var ace1 = new JpaAutomationCompositionElement();
278
279         ace1.setDefinition(new PfConceptKey("defName", "0.0.1"));
280         ace1.setDescription("Description");
281         ace1.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
282         ace1.setState(AutomationCompositionState.UNINITIALISED);
283         ace1.setParticipantId(new PfConceptKey("participant", "0.0.1"));
284
285         assertThat(ace1.toString()).contains("AutomationCompositionElement(");
286         assertNotEquals(0, ace1.hashCode());
287         assertNotEquals(ace1, ace0);
288         assertNotEquals(null, ace1);
289
290         assertNotEquals(ace1, ace0);
291
292         var ace2 = new JpaAutomationCompositionElement();
293         assertEquals(ace2, ace0);
294     }
295
296     private JpaAutomationCompositionElement createJpaAutomationCompositionElementInstance() {
297         var testAce = createAutomationCompositionElementInstance();
298         var testJpaAutomationCompositionElement = new JpaAutomationCompositionElement();
299         testJpaAutomationCompositionElement.setKey(null);
300         testJpaAutomationCompositionElement.fromAuthorative(testAce);
301         testJpaAutomationCompositionElement.setKey(PfReferenceKey.getNullKey());
302         testJpaAutomationCompositionElement.fromAuthorative(testAce);
303         testJpaAutomationCompositionElement.setProperties(Map.of("key", "{}"));
304
305         return testJpaAutomationCompositionElement;
306     }
307
308     private AutomationCompositionElement createAutomationCompositionElementInstance() {
309         var automationCompositionElement = new AutomationCompositionElement();
310         automationCompositionElement.setId(UUID.fromString("a95757ba-b34a-4049-a2a8-46773abcbe5e"));
311         automationCompositionElement.setDefinition(new ToscaConceptIdentifier("aceDef", "0.0.1"));
312         automationCompositionElement.setParticipantType(new ToscaConceptIdentifier("participantType", "0.0.1"));
313         automationCompositionElement.setProperties(Map.of("key", "{}"));
314
315         return automationCompositionElement;
316     }
317 }