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