e9df9b7e511884e747fc1e29b3ff631e13631354
[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 testJpaAutomationCompositionElement2 = new JpaAutomationCompositionElement(testJpaAcElement);
126         assertEquals(testJpaAcElement, testJpaAutomationCompositionElement2);
127     }
128
129     @Test
130     void testJpaAutomationCompositionElementValidation() {
131         var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
132
133         assertThatThrownBy(() -> testJpaAutomationCompositionElement.validate(null))
134                 .hasMessageMatching("fieldName is marked .*ull but is null");
135
136         assertTrue(testJpaAutomationCompositionElement.validate("").isValid());
137     }
138
139     @Test
140     void testJpaAutomationCompositionElementCompareTo() {
141         var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
142
143         var otherJpaAutomationCompositionElement =
144                 new JpaAutomationCompositionElement(testJpaAutomationCompositionElement);
145         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
146         assertEquals(-1, testJpaAutomationCompositionElement.compareTo(null));
147         assertEquals(0, testJpaAutomationCompositionElement.compareTo(testJpaAutomationCompositionElement));
148         assertNotEquals(0,
149                 testJpaAutomationCompositionElement.compareTo(new DummyJpaAutomationCompositionElementChild()));
150
151         testJpaAutomationCompositionElement.setElementId("BadValue");
152         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
153         testJpaAutomationCompositionElement.setElementId(ELEMENT_ID);
154         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
155
156         testJpaAutomationCompositionElement.setInstanceId("BadValue");
157         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
158         testJpaAutomationCompositionElement.setInstanceId(INSTANCE_ID);
159         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
160
161         testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("BadValue", "0.0.1"));
162         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
163         testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("aceDef", "0.0.1"));
164         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
165
166         testJpaAutomationCompositionElement.setDescription("Description");
167         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
168         testJpaAutomationCompositionElement.setDescription(null);
169         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
170
171         testJpaAutomationCompositionElement.setDeployState(DeployState.DEPLOYED);
172         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
173         testJpaAutomationCompositionElement.setDeployState(DeployState.UNDEPLOYED);
174         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
175
176         testJpaAutomationCompositionElement.setLockState(LockState.UNLOCKED);
177         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
178         testJpaAutomationCompositionElement.setLockState(LockState.LOCKED);
179         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
180
181         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
182         testJpaAutomationCompositionElement.setParticipantId(UUID.randomUUID().toString());
183         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
184
185         assertEquals(testJpaAutomationCompositionElement,
186                 new JpaAutomationCompositionElement(testJpaAutomationCompositionElement));
187     }
188
189     @Test
190     void testJpaAutomationCompositionElementLombok() {
191         assertNotNull(new Participant());
192         var ace0 = new JpaAutomationCompositionElement();
193
194         assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
195         assertThat(ace0.hashCode()).isNotZero();
196         assertEquals(ace0, ace0);
197         assertNotEquals(null, ace0);
198
199         var ace1 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
200
201         ace1.setDefinition(new PfConceptKey("defName", "0.0.1"));
202         ace1.setDescription("Description");
203         ace1.setParticipantId(CommonTestData.getJpaParticipantId());
204
205         assertThat(ace1.toString()).contains("AutomationCompositionElement(");
206         assertNotEquals(0, ace1.hashCode());
207         assertNotEquals(ace1, ace0);
208         assertNotEquals(null, ace1);
209
210         assertNotEquals(ace1, ace0);
211
212         var ace2 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
213         assertEquals(ace2, ace0);
214     }
215
216     private JpaAutomationCompositionElement createJpaAutomationCompositionElementInstance() {
217         var testAce = createAutomationCompositionElementInstance();
218         var testJpaAcElement =
219                 new JpaAutomationCompositionElement(testAce.getId().toString(), INSTANCE_ID);
220         testJpaAcElement.fromAuthorative(testAce);
221         testJpaAcElement.setProperties(Map.of("key", "{}"));
222
223         return testJpaAcElement;
224     }
225
226     private AutomationCompositionElement createAutomationCompositionElementInstance() {
227         var automationCompositionElement = new AutomationCompositionElement();
228         automationCompositionElement.setId(UUID.fromString(ELEMENT_ID));
229         automationCompositionElement.setDefinition(new ToscaConceptIdentifier("aceDef", "0.0.1"));
230         automationCompositionElement.setParticipantId(CommonTestData.getParticipantId());
231         automationCompositionElement.setProperties(Map.of("key", "{}"));
232
233         return automationCompositionElement;
234     }
235 }