f5a2149b4796d6f827d50abf437a6443f5b5a627
[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.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
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.assertNotNull;
29 import static org.junit.jupiter.api.Assertions.assertNull;
30 import static org.junit.jupiter.api.Assertions.assertTrue;
31
32 import java.io.File;
33 import java.util.LinkedHashMap;
34 import java.util.UUID;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
40 import org.onap.policy.common.utils.coder.CoderException;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.onap.policy.models.base.PfConceptKey;
43
44 /**
45  * Test the {@link JpaAutomationCompositionTest} class.
46  */
47 class JpaAutomationCompositionTest {
48
49     private static final String NULL_KEY_ERROR = "instanceId is marked .*ull but is null";
50     private static final UUID INSTANCE_ID = UUID.fromString("709c62b3-8918-41b9-a747-d21eb79c6c20");
51     private static final String COMPOSITION_ID = "709c62b3-8918-41b9-a747-e21eb79c6c41";
52
53     @Test
54     void testJpaAutomationCompositionConstructor() {
55         assertThatThrownBy(() -> {
56             new JpaAutomationComposition((JpaAutomationComposition) null);
57         }).hasMessageMatching("copyConcept is marked .*ull but is null");
58
59         assertThatThrownBy(() -> {
60             new JpaAutomationComposition(null, null, null, null, null);
61         }).hasMessageMatching(NULL_KEY_ERROR);
62
63         assertThatThrownBy(() -> {
64             new JpaAutomationComposition(null, null, null, null, new LinkedHashMap<>());
65         }).hasMessageMatching(NULL_KEY_ERROR);
66
67         assertThatThrownBy(() -> {
68             new JpaAutomationComposition(null, null, null, AutomationCompositionState.UNINITIALISED, null);
69         }).hasMessageMatching(NULL_KEY_ERROR);
70
71         assertThatThrownBy(() -> {
72             new JpaAutomationComposition(null, null, null, AutomationCompositionState.UNINITIALISED,
73                     new LinkedHashMap<>());
74         }).hasMessageMatching(NULL_KEY_ERROR);
75
76         assertThatThrownBy(() -> {
77             new JpaAutomationComposition(null, null, "key", null, new LinkedHashMap<>());
78         }).hasMessageMatching(NULL_KEY_ERROR);
79
80         assertThatThrownBy(() -> {
81             new JpaAutomationComposition(null, null, "key", AutomationCompositionState.UNINITIALISED, null);
82         }).hasMessageMatching(NULL_KEY_ERROR);
83
84         assertThatThrownBy(() -> {
85             new JpaAutomationComposition(null, null, "key", AutomationCompositionState.UNINITIALISED,
86                     new LinkedHashMap<>());
87         }).hasMessageMatching(NULL_KEY_ERROR);
88
89         assertThatThrownBy(() -> {
90             new JpaAutomationComposition(INSTANCE_ID.toString(), new PfConceptKey(), null, null, null);
91         }).hasMessageMatching("compositionId is marked .*ull but is null");
92
93         assertThatThrownBy(() -> {
94             new JpaAutomationComposition(INSTANCE_ID.toString(), new PfConceptKey(), null, null, new LinkedHashMap<>());
95         }).hasMessageMatching("compositionId is marked .*ull but is null");
96
97         assertThatThrownBy(() -> {
98             new JpaAutomationComposition(INSTANCE_ID.toString(), new PfConceptKey(), null,
99                     AutomationCompositionState.UNINITIALISED, null);
100         }).hasMessageMatching("compositionId is marked .*ull but is null");
101
102         assertThatThrownBy(() -> {
103             new JpaAutomationComposition(INSTANCE_ID.toString(), new PfConceptKey(), null,
104                     AutomationCompositionState.UNINITIALISED, new LinkedHashMap<>());
105         }).hasMessageMatching("compositionId is marked .*ull but is null");
106
107         assertThatThrownBy(() -> {
108             new JpaAutomationComposition(INSTANCE_ID.toString(), new PfConceptKey(), "key", null, null);
109         }).hasMessageMatching("state is marked .*ull but is null");
110
111         assertThatThrownBy(() -> {
112             new JpaAutomationComposition(INSTANCE_ID.toString(), new PfConceptKey(), "key", null,
113                     new LinkedHashMap<>());
114         }).hasMessageMatching("state is marked .*ull but is null");
115
116         assertThatThrownBy(() -> {
117             new JpaAutomationComposition(INSTANCE_ID.toString(), new PfConceptKey(), "key",
118                     AutomationCompositionState.UNINITIALISED, null);
119         }).hasMessageMatching("elements is marked .*ull but is null");
120
121         assertNotNull(new JpaAutomationComposition());
122         assertNotNull(new JpaAutomationComposition(INSTANCE_ID.toString(), new PfConceptKey(), "key",
123                 AutomationCompositionState.UNINITIALISED, new LinkedHashMap<>()));
124     }
125
126     @Test
127     void testJpaAutomationComposition() {
128         var testJpaAutomationComposition = createJpaAutomationCompositionInstance();
129
130         var participant = createAutomationCompositionInstance();
131         assertEquals(participant, testJpaAutomationComposition.toAuthorative());
132
133         assertThatThrownBy(() -> {
134             testJpaAutomationComposition.fromAuthorative(null);
135         }).hasMessageMatching("automationComposition is marked .*ull but is null");
136
137         assertThatThrownBy(() -> new JpaAutomationComposition((JpaAutomationComposition) null))
138                 .isInstanceOf(NullPointerException.class);
139
140         var testJpaAutomationCompositionFa = new JpaAutomationComposition();
141         testJpaAutomationCompositionFa.setKey(null);
142         testJpaAutomationCompositionFa.fromAuthorative(participant);
143         assertEquals(testJpaAutomationComposition, testJpaAutomationCompositionFa);
144         testJpaAutomationCompositionFa.setKey(PfConceptKey.getNullKey());
145         testJpaAutomationCompositionFa.fromAuthorative(participant);
146         assertEquals(testJpaAutomationComposition, testJpaAutomationCompositionFa);
147         testJpaAutomationCompositionFa.setKey(new PfConceptKey("automation-composition", "0.0.1"));
148         testJpaAutomationCompositionFa.fromAuthorative(participant);
149         assertEquals(testJpaAutomationComposition, testJpaAutomationCompositionFa);
150
151         assertEquals("automation-composition", testJpaAutomationComposition.getKey().getName());
152         assertEquals("automation-composition",
153                 new JpaAutomationComposition(createAutomationCompositionInstance()).getKey().getName());
154         assertEquals("automation-composition",
155                 ((PfConceptKey) new JpaAutomationComposition(createAutomationCompositionInstance()).getKeys().get(0))
156                         .getName());
157
158         testJpaAutomationComposition.clean();
159         assertEquals("automation-composition", testJpaAutomationComposition.getKey().getName());
160
161         testJpaAutomationComposition.setDescription("   A Message   ");
162         testJpaAutomationComposition.clean();
163         assertEquals("A Message", testJpaAutomationComposition.getDescription());
164
165         var testJpaAutomationComposition2 = new JpaAutomationComposition(testJpaAutomationComposition);
166         assertEquals(testJpaAutomationComposition, testJpaAutomationComposition2);
167     }
168
169     @Test
170     void testJpaAutomationCompositionElementOrderedState() throws CoderException {
171         var testAutomationComposition = createAutomationCompositionInstance();
172         var testJpaAutomationComposition = createJpaAutomationCompositionInstance();
173
174         testJpaAutomationComposition.setOrderedState(null);
175         assertEquals(testAutomationComposition, testJpaAutomationComposition.toAuthorative());
176         testJpaAutomationComposition.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
177
178         var noOrderedStateAc =
179                 new StandardCoder().decode(new File("src/test/resources/json/AutomationCompositionNoOrderedState.json"),
180                         AutomationComposition.class);
181
182         noOrderedStateAc.setInstanceId(INSTANCE_ID);
183         var noOrderedStateJpaAc = new JpaAutomationComposition(noOrderedStateAc);
184         assertNull(noOrderedStateJpaAc.getOrderedState());
185         noOrderedStateAc.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
186         noOrderedStateJpaAc = new JpaAutomationComposition(noOrderedStateAc);
187         assertEquals(testJpaAutomationComposition, noOrderedStateJpaAc);
188
189         var acWithElements =
190                 new StandardCoder().decode(new File("src/test/resources/providers/TestAutomationCompositions.json"),
191                         AutomationCompositions.class).getAutomationCompositionList().get(0);
192
193         acWithElements.setInstanceId(INSTANCE_ID);
194         var jpaAutomationCompositionWithElements = new JpaAutomationComposition(acWithElements);
195         assertEquals(4, jpaAutomationCompositionWithElements.getElements().size());
196         assertEquals(17, jpaAutomationCompositionWithElements.getKeys().size());
197         assertThatCode(jpaAutomationCompositionWithElements::clean).doesNotThrowAnyException();
198
199         assertEquals(acWithElements, jpaAutomationCompositionWithElements.toAuthorative());
200     }
201
202     @Test
203     void testJpaAutomationCompositionValidation() {
204         var testJpaAutomationComposition = createJpaAutomationCompositionInstance();
205
206         assertThatThrownBy(() -> testJpaAutomationComposition.validate(null))
207                 .hasMessageMatching("fieldName is marked .*ull but is null");
208
209         assertTrue(testJpaAutomationComposition.validate("").isValid());
210     }
211
212     @Test
213     void testJpaAutomationCompositionCompareTo() {
214         var testJpaAutomationComposition = createJpaAutomationCompositionInstance();
215
216         var otherJpaAutomationComposition = new JpaAutomationComposition(testJpaAutomationComposition);
217         assertEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
218         assertEquals(-1, testJpaAutomationComposition.compareTo(null));
219         assertEquals(0, testJpaAutomationComposition.compareTo(testJpaAutomationComposition));
220         assertNotEquals(0, testJpaAutomationComposition.compareTo(new DummyJpaAutomationCompositionChild()));
221
222         testJpaAutomationComposition.setKey(new PfConceptKey("BadValue", "0.0.1"));
223         assertNotEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
224         testJpaAutomationComposition.setKey(new PfConceptKey("automation-composition", "0.0.1"));
225         assertEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
226
227         testJpaAutomationComposition.setCompositionId(UUID.randomUUID().toString());
228         assertNotEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
229         testJpaAutomationComposition.setCompositionId(COMPOSITION_ID);
230         assertEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
231
232         testJpaAutomationComposition.setState(AutomationCompositionState.PASSIVE);
233         assertNotEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
234         testJpaAutomationComposition.setState(AutomationCompositionState.UNINITIALISED);
235         assertEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
236
237         testJpaAutomationComposition.setOrderedState(AutomationCompositionOrderedState.PASSIVE);
238         assertNotEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
239         testJpaAutomationComposition.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
240         assertEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
241
242         testJpaAutomationComposition.setDescription("A description");
243         assertNotEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
244         testJpaAutomationComposition.setDescription(null);
245         assertEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
246
247         testJpaAutomationComposition.setPrimed(true);
248         assertNotEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
249         testJpaAutomationComposition.setPrimed(false);
250         assertEquals(0, testJpaAutomationComposition.compareTo(otherJpaAutomationComposition));
251
252         assertEquals(testJpaAutomationComposition, new JpaAutomationComposition(testJpaAutomationComposition));
253     }
254
255     @Test
256     void testJpaAutomationCompositionLombok() {
257         assertNotNull(new AutomationComposition());
258         var ac0 = new JpaAutomationComposition();
259         ac0.setCompositionId(COMPOSITION_ID);
260
261         assertThat(ac0.toString()).contains("JpaAutomationComposition(");
262         assertThat(ac0.hashCode()).isNotZero();
263         assertEquals(ac0, ac0);
264         assertNotEquals(null, ac0);
265
266         var ac1 = new JpaAutomationComposition();
267
268         ac1.setCompositionId(UUID.randomUUID().toString());
269         ac1.setDescription("Description");
270         ac1.setElements(new LinkedHashMap<>());
271         ac1.setKey(new PfConceptKey("participant", "0.0.1"));
272         ac1.setState(AutomationCompositionState.UNINITIALISED);
273
274         assertThat(ac1.toString()).contains("AutomationComposition(");
275         assertNotEquals(0, ac1.hashCode());
276         assertNotEquals(ac1, ac0);
277         assertNotEquals(null, ac1);
278
279         assertNotEquals(ac1, ac0);
280
281         var ac2 = new JpaAutomationComposition();
282         ac2.setCompositionId(COMPOSITION_ID);
283         ac2.setInstanceId(ac0.getInstanceId());
284         assertEquals(ac2, ac0);
285     }
286
287     private JpaAutomationComposition createJpaAutomationCompositionInstance() {
288         var testAutomationComposition = createAutomationCompositionInstance();
289         var testJpaAutomationComposition = new JpaAutomationComposition();
290         testJpaAutomationComposition.setKey(null);
291         testJpaAutomationComposition.fromAuthorative(testAutomationComposition);
292         testJpaAutomationComposition.setKey(PfConceptKey.getNullKey());
293         testJpaAutomationComposition.fromAuthorative(testAutomationComposition);
294
295         return testJpaAutomationComposition;
296     }
297
298     private AutomationComposition createAutomationCompositionInstance() {
299         var testAutomationComposition = new AutomationComposition();
300         testAutomationComposition.setName("automation-composition");
301         testAutomationComposition.setInstanceId(INSTANCE_ID);
302         testAutomationComposition.setVersion("0.0.1");
303         testAutomationComposition.setCompositionId(UUID.fromString(COMPOSITION_ID));
304         testAutomationComposition.setElements(new LinkedHashMap<>());
305
306         return testAutomationComposition;
307     }
308 }