20030e91e651d823bdd6878d4bdc7d03269ec8c6
[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.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(() -> testJpaAutomationCompositionElement.validate(null))
212             .hasMessageMatching("fieldName is marked .*ull but is null");
213
214         assertTrue(testJpaAutomationCompositionElement.validate("").isValid());
215     }
216
217     @Test
218     void testJpaAutomationCompositionElementCompareTo() {
219         JpaAutomationCompositionElement testJpaAutomationCompositionElement =
220             createJpaAutomationCompositionElementInstance();
221
222         JpaAutomationCompositionElement otherJpaAutomationCompositionElement =
223             new JpaAutomationCompositionElement(testJpaAutomationCompositionElement);
224         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
225         assertEquals(-1, testJpaAutomationCompositionElement.compareTo(null));
226         assertEquals(0, testJpaAutomationCompositionElement.compareTo(testJpaAutomationCompositionElement));
227         assertNotEquals(0,
228             testJpaAutomationCompositionElement.compareTo(new DummyJpaAutomationCompositionElementChild()));
229
230         testJpaAutomationCompositionElement
231             .setKey(new PfReferenceKey("BadValue", "0.0.1", "a95757ba-b34a-4049-a2a8-46773abcbe5e"));
232         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
233         testJpaAutomationCompositionElement.setKey(
234             new PfReferenceKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, "a95757ba-b34a-4049-a2a8-46773abcbe5e"));
235         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
236
237         testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("BadValue", "0.0.1"));
238         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
239         testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("aceDef", "0.0.1"));
240         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
241
242         testJpaAutomationCompositionElement.setDescription("Description");
243         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
244         testJpaAutomationCompositionElement.setDescription(null);
245         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
246
247         testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.PASSIVE);
248         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
249         testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
250         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
251
252         testJpaAutomationCompositionElement.setState(AutomationCompositionState.PASSIVE);
253         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
254         testJpaAutomationCompositionElement.setState(AutomationCompositionState.UNINITIALISED);
255         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
256
257         testJpaAutomationCompositionElement.setParticipantType(new PfConceptKey("dummy", "0.0.1"));
258         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
259         testJpaAutomationCompositionElement.setParticipantType(new PfConceptKey("participantType", "0.0.1"));
260         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
261
262         assertEquals(testJpaAutomationCompositionElement,
263             new JpaAutomationCompositionElement(testJpaAutomationCompositionElement));
264     }
265
266     @Test
267     void testJpaAutomationCompositionElementLombok() {
268         assertNotNull(new Participant());
269         JpaAutomationCompositionElement ace0 = new JpaAutomationCompositionElement();
270
271         assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
272         assertThat(ace0.hashCode()).isNotZero();
273         assertEquals(ace0, ace0);
274         assertNotEquals(null, ace0);
275
276         JpaAutomationCompositionElement ace1 = new JpaAutomationCompositionElement();
277
278         ace1.setDefinition(new PfConceptKey("defName", "0.0.1"));
279         ace1.setDescription("Description");
280         ace1.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
281         ace1.setState(AutomationCompositionState.UNINITIALISED);
282         ace1.setParticipantId(new PfConceptKey("participant", "0.0.1"));
283
284         assertThat(ace1.toString()).contains("AutomationCompositionElement(");
285         assertNotEquals(0, ace1.hashCode());
286         assertNotEquals(ace1, ace0);
287         assertNotEquals(null, ace1);
288
289         assertNotEquals(ace1, ace0);
290
291         JpaAutomationCompositionElement ace2 = new JpaAutomationCompositionElement();
292         assertEquals(ace2, ace0);
293     }
294
295     private JpaAutomationCompositionElement createJpaAutomationCompositionElementInstance() {
296         AutomationCompositionElement testAce = createAutomationCompositionElementInstance();
297         JpaAutomationCompositionElement testJpaAutomationCompositionElement = new JpaAutomationCompositionElement();
298         testJpaAutomationCompositionElement.setKey(null);
299         testJpaAutomationCompositionElement.fromAuthorative(testAce);
300         testJpaAutomationCompositionElement.setKey(PfReferenceKey.getNullKey());
301         testJpaAutomationCompositionElement.fromAuthorative(testAce);
302
303         return testJpaAutomationCompositionElement;
304     }
305
306     private AutomationCompositionElement createAutomationCompositionElementInstance() {
307         AutomationCompositionElement automationCompositionElement = new AutomationCompositionElement();
308         automationCompositionElement.setId(UUID.fromString("a95757ba-b34a-4049-a2a8-46773abcbe5e"));
309         automationCompositionElement.setDefinition(new ToscaConceptIdentifier("aceDef", "0.0.1"));
310         automationCompositionElement.setParticipantType(new ToscaConceptIdentifier("participantType", "0.0.1"));
311
312         return automationCompositionElement;
313     }
314 }