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