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