a6b3c0f806a0c215c3dc1f9318700e17298f4e3e
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2024 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.assertDoesNotThrow;
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.assertTrue;
29
30 import java.util.Map;
31 import java.util.UUID;
32 import org.junit.jupiter.api.Test;
33 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
34 import org.onap.policy.clamp.models.acm.concepts.DeployState;
35 import org.onap.policy.clamp.models.acm.concepts.LockState;
36 import org.onap.policy.clamp.models.acm.utils.CommonTestData;
37 import org.onap.policy.models.base.PfConceptKey;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
39
40 /**
41  * Test the{@link JpaAutomationCompositionElement} class.
42  */
43 class JpaAutomationCompositionElementTest {
44
45     private static final String NULL_INSTANCE_ID_ERROR = "instanceId is marked .*ull but is null";
46     private static final String NULL_ELEMENT_ID_ERROR = "elementId is marked .*ull but is null";
47     private static final String NULL_ERROR = " is marked .*ull but is null";
48     private static final String ELEMENT_ID = "a95757ba-b34a-4049-a2a8-46773abcbe5e";
49     private static final String INSTANCE_ID = "a78757co-b34a-8949-a2a8-46773abcbe2a";
50
51     private static final PfConceptKey CONCEPT_KEY = new PfConceptKey();
52
53     @Test
54     void testJpaAutomationCompositionElementConstructor() {
55         assertThatThrownBy(() -> {
56             new JpaAutomationCompositionElement((AutomationCompositionElement) null);
57         }).hasMessageMatching("authorativeConcept is marked .*ull but is null");
58
59         assertThatThrownBy(() -> {
60             new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null);
61         }).hasMessageMatching("copyConcept is marked .*ull but is null");
62
63         assertThatThrownBy(() -> {
64             new JpaAutomationCompositionElement("key", null);
65         }).hasMessageMatching(NULL_INSTANCE_ID_ERROR);
66
67         assertThatThrownBy(() -> {
68             new JpaAutomationCompositionElement(null, "key");
69         }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
70
71         assertThatThrownBy(() -> {
72             new JpaAutomationCompositionElement(null, null);
73         }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
74
75         assertThatThrownBy(() -> {
76             new JpaAutomationCompositionElement(null, null, null, null, null);
77         }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
78
79         assertThatThrownBy(() -> {
80             new JpaAutomationCompositionElement("key", null, null,
81                 DeployState.UNDEPLOYED, LockState.LOCKED);
82         }).hasMessageMatching(NULL_INSTANCE_ID_ERROR);
83
84         assertThatThrownBy(() -> {
85             new JpaAutomationCompositionElement("key", "key", null,
86                 DeployState.UNDEPLOYED, LockState.LOCKED);
87         }).hasMessageMatching("definition" + NULL_ERROR);
88
89         assertThatThrownBy(() -> {
90             new JpaAutomationCompositionElement("key", "key", CONCEPT_KEY,
91                 null, LockState.LOCKED);
92         }).hasMessageMatching("deployState" + NULL_ERROR);
93
94         assertThatThrownBy(() -> {
95             new JpaAutomationCompositionElement("key", "key", CONCEPT_KEY,
96                 DeployState.UNDEPLOYED, null);
97         }).hasMessageMatching("lockState" + NULL_ERROR);
98
99         assertDoesNotThrow(() -> new JpaAutomationCompositionElement());
100         assertDoesNotThrow(() -> new JpaAutomationCompositionElement("key", "key"));
101         assertDoesNotThrow(() -> new JpaAutomationCompositionElement("key", "key",
102                 CONCEPT_KEY, DeployState.UNDEPLOYED, LockState.LOCKED));
103     }
104
105     @Test
106     void testJpaAutomationCompositionElement() {
107         var testJpaAcElement = createJpaAutomationCompositionElementInstance();
108
109         var ace = createAutomationCompositionElementInstance();
110         assertEquals(ace, testJpaAcElement.toAuthorative());
111
112         assertThatThrownBy(() -> {
113             testJpaAcElement.fromAuthorative(null);
114         }).hasMessageMatching("element is marked .*ull but is null");
115
116         assertThatThrownBy(() -> new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null))
117                 .isInstanceOf(NullPointerException.class);
118
119         var testJpaAcElementFa =
120                 new JpaAutomationCompositionElement(ace.getId().toString(), testJpaAcElement.getInstanceId());
121         testJpaAcElementFa.fromAuthorative(ace);
122         assertEquals(testJpaAcElement, testJpaAcElementFa);
123
124         assertEquals(ELEMENT_ID, testJpaAcElement.getElementId());
125
126         var testJpaAcElement2 = new JpaAutomationCompositionElement(testJpaAcElement);
127         assertEquals(testJpaAcElement, testJpaAcElement2);
128
129         testJpaAcElement2 = new JpaAutomationCompositionElement(testJpaAcElement.toAuthorative());
130         testJpaAcElement2.setElementId(ELEMENT_ID);
131         testJpaAcElement2.setInstanceId(INSTANCE_ID);
132         assertEquals(testJpaAcElement, testJpaAcElement2);
133     }
134
135     @Test
136     void testJpaAutomationCompositionElementValidation() {
137         var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
138
139         assertThatThrownBy(() -> testJpaAutomationCompositionElement.validate(null))
140                 .hasMessageMatching("fieldName is marked .*ull but is null");
141
142         assertTrue(testJpaAutomationCompositionElement.validate("").isValid());
143     }
144
145     @Test
146     void testJpaAcElementCompareTo() {
147         var testJpaAcElement = createJpaAutomationCompositionElementInstance();
148
149         var otherJpaAcElement =
150                 new JpaAutomationCompositionElement(testJpaAcElement);
151         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
152         assertEquals(-1, testJpaAcElement.compareTo(null));
153         assertEquals(0, testJpaAcElement.compareTo(testJpaAcElement));
154         assertNotEquals(0,
155                 testJpaAcElement.compareTo(new DummyJpaAutomationCompositionElementChild()));
156
157         assertEquals(testJpaAcElement, new JpaAutomationCompositionElement(testJpaAcElement));
158     }
159
160     @Test
161     void testJpaAutomationCompositionElementCompareTo() {
162         var testJpaAcElement = createJpaAutomationCompositionElementInstance();
163
164         var otherJpaAcElement =
165                 new JpaAutomationCompositionElement(testJpaAcElement);
166
167         testJpaAcElement.setElementId("BadValue");
168         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
169         testJpaAcElement.setElementId(ELEMENT_ID);
170         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
171
172         testJpaAcElement.setInstanceId("BadValue");
173         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
174         testJpaAcElement.setInstanceId(INSTANCE_ID);
175         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
176
177         testJpaAcElement.setDefinition(new PfConceptKey("BadValue", "0.0.1"));
178         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
179         testJpaAcElement.setDefinition(new PfConceptKey("aceDef", "0.0.1"));
180         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
181
182         testJpaAcElement.setDescription("Description");
183         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
184         testJpaAcElement.setDescription(null);
185         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
186
187         testJpaAcElement.setDeployState(DeployState.DEPLOYED);
188         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
189         testJpaAcElement.setDeployState(DeployState.UNDEPLOYED);
190         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
191
192         testJpaAcElement.setLockState(LockState.UNLOCKED);
193         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
194         testJpaAcElement.setLockState(LockState.LOCKED);
195         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
196
197         testJpaAcElement.setUseState("BadValue");
198         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
199         testJpaAcElement.setUseState("IDLE");
200         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
201
202         testJpaAcElement.setOperationalState("BadValue");
203         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
204         testJpaAcElement.setOperationalState("DEFAULT");
205         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
206
207         testJpaAcElement.setMessage("Message");
208         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
209         testJpaAcElement.setMessage(null);
210         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
211
212         testJpaAcElement.setRestarting(true);
213         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
214         testJpaAcElement.setRestarting(null);
215         assertEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
216
217         testJpaAcElement.setParticipantId(UUID.randomUUID().toString());
218         assertNotEquals(0, testJpaAcElement.compareTo(otherJpaAcElement));
219
220     }
221
222     @Test
223     void testJpaAutomationCompositionElementLombok() {
224         var ace0 = new JpaAutomationCompositionElement();
225
226         assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
227         assertThat(ace0.hashCode()).isNotZero();
228         assertNotEquals(null, ace0);
229
230         var ace1 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
231
232         ace1.setDefinition(new PfConceptKey("defName", "0.0.1"));
233         ace1.setDescription("Description");
234         ace1.setParticipantId(CommonTestData.getJpaParticipantId());
235
236         assertThat(ace1.toString()).contains("AutomationCompositionElement(");
237         assertNotEquals(0, ace1.hashCode());
238         assertNotEquals(ace1, ace0);
239         assertNotEquals(null, ace1);
240
241         assertNotEquals(ace1, ace0);
242
243         var ace2 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
244         assertEquals(ace2, ace0);
245     }
246
247     private JpaAutomationCompositionElement createJpaAutomationCompositionElementInstance() {
248         var testAce = createAutomationCompositionElementInstance();
249         var testJpaAcElement =
250                 new JpaAutomationCompositionElement(testAce.getId().toString(), INSTANCE_ID);
251         testJpaAcElement.fromAuthorative(testAce);
252         testJpaAcElement.setProperties(Map.of("key", "{}"));
253
254         return testJpaAcElement;
255     }
256
257     private AutomationCompositionElement createAutomationCompositionElementInstance() {
258         var automationCompositionElement = new AutomationCompositionElement();
259         automationCompositionElement.setId(UUID.fromString(ELEMENT_ID));
260         automationCompositionElement.setDefinition(new ToscaConceptIdentifier("aceDef", "0.0.1"));
261         automationCompositionElement.setParticipantId(CommonTestData.getParticipantId());
262         automationCompositionElement.setProperties(Map.of("key", "{}"));
263         automationCompositionElement.setUseState("IDLE");
264         automationCompositionElement.setOperationalState("DEFAULT");
265
266         return automationCompositionElement;
267     }
268 }