83e13c74af6523ee2c0fcf7109843b1528a4a3d5
[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.assertNull;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30
31 import java.io.File;
32 import java.util.Map;
33 import java.util.UUID;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
38 import org.onap.policy.clamp.models.acm.concepts.Participant;
39 import org.onap.policy.clamp.models.acm.utils.CommonTestData;
40 import org.onap.policy.common.utils.coder.CoderException;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.onap.policy.models.base.PfConceptKey;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
44
45 /**
46  * Test the{@link JpaAutomationCompositionElement} class.
47  */
48 class JpaAutomationCompositionElementTest {
49
50     private static final String NULL_INSTANCE_ID_ERROR = "instanceId is marked .*ull but is null";
51     private static final String NULL_ELEMENT_ID_ERROR = "elementId is marked .*ull but is null";
52     private static final String NULL_ERROR = " is marked .*ull but is null";
53     private static final String ELEMENT_ID = "a95757ba-b34a-4049-a2a8-46773abcbe5e";
54     private static final String INSTANCE_ID = "a78757co-b34a-8949-a2a8-46773abcbe2a";
55
56     @Test
57     void testJpaAutomationCompositionElementConstructor() {
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);
76         }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
77
78         assertThatThrownBy(() -> {
79             new JpaAutomationCompositionElement("key", null, null, AutomationCompositionState.UNINITIALISED);
80         }).hasMessageMatching(NULL_INSTANCE_ID_ERROR);
81
82         assertThatThrownBy(() -> {
83             new JpaAutomationCompositionElement("key", "key", new PfConceptKey(), null);
84         }).hasMessageMatching("state" + NULL_ERROR);
85
86         assertNotNull(new JpaAutomationCompositionElement());
87         assertNotNull(new JpaAutomationCompositionElement("key", "key"));
88         assertNotNull(new JpaAutomationCompositionElement("key", "key",
89             new PfConceptKey(), AutomationCompositionState.UNINITIALISED));
90     }
91
92     @Test
93     void testJpaAutomationCompositionElement() {
94         var testJpaAcElement = createJpaAutomationCompositionElementInstance();
95
96         var ace = createAutomationCompositionElementInstance();
97         assertEquals(ace, testJpaAcElement.toAuthorative());
98
99         assertThatThrownBy(() -> {
100             testJpaAcElement.fromAuthorative(null);
101         }).hasMessageMatching("element is marked .*ull but is null");
102
103         assertThatThrownBy(() -> new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null))
104                 .isInstanceOf(NullPointerException.class);
105
106         var testJpaAcElementFa =
107                 new JpaAutomationCompositionElement(ace.getId().toString(), testJpaAcElement.getInstanceId());
108         testJpaAcElementFa.fromAuthorative(ace);
109         assertEquals(testJpaAcElement, testJpaAcElementFa);
110
111         assertEquals(ELEMENT_ID, testJpaAcElement.getElementId());
112
113         var testJpaAutomationCompositionElement2 = new JpaAutomationCompositionElement(testJpaAcElement);
114         assertEquals(testJpaAcElement, testJpaAutomationCompositionElement2);
115     }
116
117     @Test
118     void testJpaAutomationCompositionElementOrderedState() throws CoderException {
119         var testAutomationCompositionElement = createAutomationCompositionElementInstance();
120         var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
121
122         testJpaAutomationCompositionElement.setOrderedState(null);
123         assertEquals(testAutomationCompositionElement, testJpaAutomationCompositionElement.toAuthorative());
124         testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
125
126         var noOrderedStateAce = new StandardCoder().decode(
127                 new File("src/test/resources/json/AutomationCompositionElementNoOrderedState.json"),
128                 AutomationCompositionElement.class);
129
130         var noOrderedStateJpaAce = new JpaAutomationCompositionElement(noOrderedStateAce);
131         assertNull(noOrderedStateJpaAce.getOrderedState());
132         noOrderedStateAce.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
133         noOrderedStateJpaAce = new JpaAutomationCompositionElement(noOrderedStateAce);
134         noOrderedStateJpaAce.setInstanceId(testJpaAutomationCompositionElement.getInstanceId());
135         noOrderedStateJpaAce.setElementId(testJpaAutomationCompositionElement.getElementId());
136         noOrderedStateJpaAce.setParticipantId(testJpaAutomationCompositionElement.getParticipantId());
137         assertEquals(testJpaAutomationCompositionElement, noOrderedStateJpaAce);
138     }
139
140     @Test
141     void testJpaAutomationCompositionElementValidation() {
142         var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
143
144         assertThatThrownBy(() -> testJpaAutomationCompositionElement.validate(null))
145                 .hasMessageMatching("fieldName is marked .*ull but is null");
146
147         assertTrue(testJpaAutomationCompositionElement.validate("").isValid());
148     }
149
150     @Test
151     void testJpaAutomationCompositionElementCompareTo() {
152         var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
153
154         var otherJpaAutomationCompositionElement =
155                 new JpaAutomationCompositionElement(testJpaAutomationCompositionElement);
156         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
157         assertEquals(-1, testJpaAutomationCompositionElement.compareTo(null));
158         assertEquals(0, testJpaAutomationCompositionElement.compareTo(testJpaAutomationCompositionElement));
159         assertNotEquals(0,
160                 testJpaAutomationCompositionElement.compareTo(new DummyJpaAutomationCompositionElementChild()));
161
162         testJpaAutomationCompositionElement.setElementId("BadValue");
163         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
164         testJpaAutomationCompositionElement.setElementId(ELEMENT_ID);
165         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
166
167         testJpaAutomationCompositionElement.setInstanceId("BadValue");
168         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
169         testJpaAutomationCompositionElement.setInstanceId(INSTANCE_ID);
170         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
171
172         testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("BadValue", "0.0.1"));
173         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
174         testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("aceDef", "0.0.1"));
175         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
176
177         testJpaAutomationCompositionElement.setDescription("Description");
178         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
179         testJpaAutomationCompositionElement.setDescription(null);
180         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
181
182         testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.PASSIVE);
183         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
184         testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
185         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
186
187         testJpaAutomationCompositionElement.setState(AutomationCompositionState.PASSIVE);
188         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
189         testJpaAutomationCompositionElement.setState(AutomationCompositionState.UNINITIALISED);
190         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
191
192         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
193         testJpaAutomationCompositionElement.setParticipantId(UUID.randomUUID().toString());
194         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
195
196         assertEquals(testJpaAutomationCompositionElement,
197                 new JpaAutomationCompositionElement(testJpaAutomationCompositionElement));
198     }
199
200     @Test
201     void testJpaAutomationCompositionElementLombok() {
202         assertNotNull(new Participant());
203         var ace0 = new JpaAutomationCompositionElement();
204
205         assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
206         assertThat(ace0.hashCode()).isNotZero();
207         assertEquals(ace0, ace0);
208         assertNotEquals(null, ace0);
209
210         var ace1 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
211
212         ace1.setDefinition(new PfConceptKey("defName", "0.0.1"));
213         ace1.setDescription("Description");
214         ace1.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
215         ace1.setState(AutomationCompositionState.UNINITIALISED);
216         ace1.setParticipantId(CommonTestData.getJpaParticipantId());
217
218         assertThat(ace1.toString()).contains("AutomationCompositionElement(");
219         assertNotEquals(0, ace1.hashCode());
220         assertNotEquals(ace1, ace0);
221         assertNotEquals(null, ace1);
222
223         assertNotEquals(ace1, ace0);
224
225         var ace2 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
226         assertEquals(ace2, ace0);
227     }
228
229     private JpaAutomationCompositionElement createJpaAutomationCompositionElementInstance() {
230         var testAce = createAutomationCompositionElementInstance();
231         var testJpaAcElement =
232                 new JpaAutomationCompositionElement(testAce.getId().toString(), INSTANCE_ID);
233         testJpaAcElement.fromAuthorative(testAce);
234         testJpaAcElement.setProperties(Map.of("key", "{}"));
235
236         return testJpaAcElement;
237     }
238
239     private AutomationCompositionElement createAutomationCompositionElementInstance() {
240         var automationCompositionElement = new AutomationCompositionElement();
241         automationCompositionElement.setId(UUID.fromString(ELEMENT_ID));
242         automationCompositionElement.setDefinition(new ToscaConceptIdentifier("aceDef", "0.0.1"));
243         automationCompositionElement.setParticipantId(CommonTestData.getParticipantId());
244         automationCompositionElement.setProperties(Map.of("key", "{}"));
245
246         return automationCompositionElement;
247     }
248 }