f4257bdd29279a67cca2278687f48c7b86a1be10
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30
31 import java.io.File;
32 import java.util.UUID;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
37 import org.onap.policy.clamp.models.acm.concepts.Participant;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.models.base.PfConceptKey;
41 import org.onap.policy.models.base.PfKey;
42 import org.onap.policy.models.base.PfReferenceKey;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
44
45 /**
46  * Test the {@link JpaAutomationCompositionElement} class.
47  */
48 class JpaAutomationCompositionElementTest {
49
50     private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
51
52     @Test
53     void testJpaAutomationCompositionElementConstructor() {
54         assertThatThrownBy(() -> {
55             new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null);
56         }).hasMessageMatching("copyConcept is marked .*ull but is null");
57
58         assertThatThrownBy(() -> {
59             new JpaAutomationCompositionElement((PfReferenceKey) null);
60         }).hasMessageMatching(NULL_KEY_ERROR);
61
62         assertThatThrownBy(() -> {
63             new JpaAutomationCompositionElement(null, null, null, null);
64         }).hasMessageMatching(NULL_KEY_ERROR);
65
66         assertThatThrownBy(() -> {
67             new JpaAutomationCompositionElement(null, null, null, AutomationCompositionState.UNINITIALISED);
68         }).hasMessageMatching(NULL_KEY_ERROR);
69
70         assertThatThrownBy(() -> {
71             new JpaAutomationCompositionElement(null, null, new PfConceptKey("participant", "0.0.1"), null);
72         }).hasMessageMatching(NULL_KEY_ERROR);
73
74         assertThatThrownBy(() -> {
75             new JpaAutomationCompositionElement(null, null, new PfConceptKey("participant", "0.0.1"),
76                 AutomationCompositionState.UNINITIALISED);
77         }).hasMessageMatching(NULL_KEY_ERROR);
78
79         assertThatThrownBy(() -> {
80             new JpaAutomationCompositionElement(null, new PfConceptKey(), null, null);
81         }).hasMessageMatching(NULL_KEY_ERROR);
82
83         assertThatThrownBy(() -> {
84             new JpaAutomationCompositionElement(null, new PfConceptKey(), null,
85                 AutomationCompositionState.UNINITIALISED);
86         }).hasMessageMatching(NULL_KEY_ERROR);
87
88         assertThatThrownBy(() -> {
89             new JpaAutomationCompositionElement(null, new PfConceptKey(), new PfConceptKey("participant", "0.0.1"),
90                 null);
91         }).hasMessageMatching(NULL_KEY_ERROR);
92
93         assertThatThrownBy(() -> {
94             new JpaAutomationCompositionElement(null, new PfConceptKey(), new PfConceptKey("participant", "0.0.1"),
95                 AutomationCompositionState.UNINITIALISED);
96         }).hasMessageMatching(NULL_KEY_ERROR);
97
98         assertThatThrownBy(() -> {
99             new JpaAutomationCompositionElement(new PfReferenceKey(), null, null, null);
100         }).hasMessageMatching("definition is marked .*ull but is null");
101
102         assertThatThrownBy(() -> {
103             new JpaAutomationCompositionElement(new PfReferenceKey(), null, null,
104                 AutomationCompositionState.UNINITIALISED);
105         }).hasMessageMatching("definition is marked .*ull but is null");
106
107         assertThatThrownBy(() -> {
108             new JpaAutomationCompositionElement(new PfReferenceKey(), null, new PfConceptKey("participant", "0.0.1"),
109                 null);
110         }).hasMessageMatching("definition is marked .*ull but is null");
111
112         assertThatThrownBy(() -> {
113             new JpaAutomationCompositionElement(new PfReferenceKey(), null, new PfConceptKey("participant", "0.0.1"),
114                 AutomationCompositionState.UNINITIALISED);
115         }).hasMessageMatching("definition is marked .*ull but is null");
116
117         assertThatThrownBy(() -> {
118             new JpaAutomationCompositionElement(new PfReferenceKey(), new PfConceptKey(), null, null);
119         }).hasMessageMatching("participantType is marked .*ull but is null");
120
121         assertThatThrownBy(() -> {
122             new JpaAutomationCompositionElement(new PfReferenceKey(), new PfConceptKey(), null,
123                 AutomationCompositionState.UNINITIALISED);
124         }).hasMessageMatching("participantType is marked .*ull but is null");
125
126         assertThatThrownBy(() -> {
127             new JpaAutomationCompositionElement(new PfReferenceKey(), new PfConceptKey(),
128                 new PfConceptKey("participant", "0.0.1"), null);
129         }).hasMessageMatching("state is marked .*ull but is null");
130
131         assertNotNull(new JpaAutomationCompositionElement());
132         assertNotNull(new JpaAutomationCompositionElement((new PfReferenceKey())));
133         assertNotNull(new JpaAutomationCompositionElement(new PfReferenceKey(), new PfConceptKey(),
134             new PfConceptKey("participant", "0.0.1"), AutomationCompositionState.UNINITIALISED));
135     }
136
137     @Test
138     void testJpaAutomationCompositionElement() {
139         JpaAutomationCompositionElement testJpaAutomationCompositionElement =
140             createJpaAutomationCompositionElementInstance();
141
142         AutomationCompositionElement ace = createAutomationCompositionElementInstance();
143         assertEquals(ace, testJpaAutomationCompositionElement.toAuthorative());
144
145         assertThatThrownBy(() -> {
146             testJpaAutomationCompositionElement.fromAuthorative(null);
147         }).hasMessageMatching("element is marked .*ull but is null");
148
149         assertThatThrownBy(() -> new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null))
150             .isInstanceOf(NullPointerException.class);
151
152         JpaAutomationCompositionElement testJpaAutomationCompositionElementFa = new JpaAutomationCompositionElement();
153         testJpaAutomationCompositionElementFa.setKey(null);
154         testJpaAutomationCompositionElementFa.fromAuthorative(ace);
155         assertEquals(testJpaAutomationCompositionElement, testJpaAutomationCompositionElementFa);
156         testJpaAutomationCompositionElementFa.setKey(PfReferenceKey.getNullKey());
157         testJpaAutomationCompositionElementFa.fromAuthorative(ace);
158         assertEquals(testJpaAutomationCompositionElement, testJpaAutomationCompositionElementFa);
159         testJpaAutomationCompositionElementFa.setKey(
160             new PfReferenceKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, "a95757ba-b34a-4049-a2a8-46773abcbe5e"));
161         testJpaAutomationCompositionElementFa.fromAuthorative(ace);
162         assertEquals(testJpaAutomationCompositionElement, testJpaAutomationCompositionElementFa);
163
164         assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e",
165             testJpaAutomationCompositionElement.getKey().getLocalName());
166         assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e",
167             new JpaAutomationCompositionElement(createAutomationCompositionElementInstance()).getKey().getLocalName());
168         assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e",
169             ((PfReferenceKey) new JpaAutomationCompositionElement(createAutomationCompositionElementInstance())
170                 .getKeys().get(0)).getLocalName());
171
172         testJpaAutomationCompositionElement.clean();
173         assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e",
174             testJpaAutomationCompositionElement.getKey().getLocalName());
175
176         testJpaAutomationCompositionElement.setDescription(" A Message ");
177         testJpaAutomationCompositionElement.clean();
178         assertEquals("A Message", testJpaAutomationCompositionElement.getDescription());
179
180         JpaAutomationCompositionElement testJpaAutomationCompositionElement2 =
181             new JpaAutomationCompositionElement(testJpaAutomationCompositionElement);
182         assertEquals(testJpaAutomationCompositionElement, testJpaAutomationCompositionElement2);
183     }
184
185     @Test
186     void testJpaAutomationCompositionElementOrderedState() throws CoderException {
187         AutomationCompositionElement testAutomationCompositionElement = createAutomationCompositionElementInstance();
188         JpaAutomationCompositionElement testJpaAutomationCompositionElement =
189             createJpaAutomationCompositionElementInstance();
190
191         testJpaAutomationCompositionElement.setOrderedState(null);
192         assertEquals(testAutomationCompositionElement, testJpaAutomationCompositionElement.toAuthorative());
193         testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
194
195         AutomationCompositionElement noOrderedStateAce = new StandardCoder().decode(
196             new File("src/test/resources/json/AutomationCompositionElementNoOrderedState.json"),
197             AutomationCompositionElement.class);
198
199         JpaAutomationCompositionElement noOrderedStateJpaAce = new JpaAutomationCompositionElement(noOrderedStateAce);
200         assertNull(noOrderedStateJpaAce.getOrderedState());
201         noOrderedStateAce.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
202         noOrderedStateJpaAce = new JpaAutomationCompositionElement(noOrderedStateAce);
203         assertEquals(testJpaAutomationCompositionElement, noOrderedStateJpaAce);
204     }
205
206     @Test
207     void testJpaAutomationCompositionElementValidation() {
208         JpaAutomationCompositionElement testJpaAutomationCompositionElement =
209             createJpaAutomationCompositionElementInstance();
210
211         assertThatThrownBy(() -> {
212             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         JpaAutomationCompositionElement testJpaAutomationCompositionElement =
221             createJpaAutomationCompositionElementInstance();
222
223         JpaAutomationCompositionElement 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         JpaAutomationCompositionElement ace0 = new JpaAutomationCompositionElement();
271
272         assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
273         assertThat(ace0.hashCode()).isNotZero();
274         assertEquals(true, ace0.equals(ace0));
275         assertEquals(false, ace0.equals(null));
276
277         JpaAutomationCompositionElement 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         assertEquals(false, ace1.hashCode() == 0);
287         assertEquals(false, ace1.equals(ace0));
288         assertEquals(false, ace1.equals(null));
289
290         assertNotEquals(ace1, ace0);
291
292         JpaAutomationCompositionElement ace2 = new JpaAutomationCompositionElement();
293         assertEquals(ace2, ace0);
294     }
295
296     private JpaAutomationCompositionElement createJpaAutomationCompositionElementInstance() {
297         AutomationCompositionElement testAce = createAutomationCompositionElementInstance();
298         JpaAutomationCompositionElement testJpaAutomationCompositionElement = new JpaAutomationCompositionElement();
299         testJpaAutomationCompositionElement.setKey(null);
300         testJpaAutomationCompositionElement.fromAuthorative(testAce);
301         testJpaAutomationCompositionElement.setKey(PfReferenceKey.getNullKey());
302         testJpaAutomationCompositionElement.fromAuthorative(testAce);
303
304         return testJpaAutomationCompositionElement;
305     }
306
307     private AutomationCompositionElement createAutomationCompositionElementInstance() {
308         AutomationCompositionElement automationCompositionElement = new AutomationCompositionElement();
309         automationCompositionElement.setId(UUID.fromString("a95757ba-b34a-4049-a2a8-46773abcbe5e"));
310         automationCompositionElement.setDefinition(new ToscaConceptIdentifier("aceDef", "0.0.1"));
311         automationCompositionElement.setParticipantType(new ToscaConceptIdentifier("participantType", "0.0.1"));
312
313         return automationCompositionElement;
314     }
315 }