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