460f3ab4b206d9d8e1fb0aaac3aa792c1b26da65
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2022 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, null);
76         }).hasMessageMatching(NULL_ELEMENT_ID_ERROR);
77
78         assertThatThrownBy(() -> {
79             new JpaAutomationCompositionElement("key", null, null, null, AutomationCompositionState.UNINITIALISED);
80         }).hasMessageMatching(NULL_INSTANCE_ID_ERROR);
81
82         assertThatThrownBy(() -> {
83             new JpaAutomationCompositionElement("key", "key", null, new PfConceptKey("participant", "0.0.1"), null);
84         }).hasMessageMatching("definition" + NULL_ERROR);
85
86         assertThatThrownBy(() -> {
87             new JpaAutomationCompositionElement("key", "key", new PfConceptKey(), null,
88                     AutomationCompositionState.UNINITIALISED);
89         }).hasMessageMatching("participantType" + NULL_ERROR);
90
91         assertThatThrownBy(() -> {
92             new JpaAutomationCompositionElement("key", "key", new PfConceptKey(), new PfConceptKey(), null);
93         }).hasMessageMatching("state" + NULL_ERROR);
94
95         assertNotNull(new JpaAutomationCompositionElement());
96         assertNotNull(new JpaAutomationCompositionElement("key", "key"));
97         assertNotNull(new JpaAutomationCompositionElement("key", "key", new PfConceptKey(),
98                 new PfConceptKey("participant", "0.0.1"), AutomationCompositionState.UNINITIALISED));
99     }
100
101     @Test
102     void testJpaAutomationCompositionElement() {
103         var testJpaAcElement = createJpaAutomationCompositionElementInstance();
104
105         var ace = createAutomationCompositionElementInstance();
106         assertEquals(ace, testJpaAcElement.toAuthorative());
107
108         assertThatThrownBy(() -> {
109             testJpaAcElement.fromAuthorative(null);
110         }).hasMessageMatching("element is marked .*ull but is null");
111
112         assertThatThrownBy(() -> new JpaAutomationCompositionElement((JpaAutomationCompositionElement) null))
113                 .isInstanceOf(NullPointerException.class);
114
115         var testJpaAcElementFa =
116                 new JpaAutomationCompositionElement(ace.getId().toString(), testJpaAcElement.getInstanceId());
117         testJpaAcElementFa.fromAuthorative(ace);
118         assertEquals(testJpaAcElement, testJpaAcElementFa);
119
120         assertEquals(ELEMENT_ID, testJpaAcElement.getElementId());
121
122         var testJpaAutomationCompositionElement2 = new JpaAutomationCompositionElement(testJpaAcElement);
123         assertEquals(testJpaAcElement, testJpaAutomationCompositionElement2);
124     }
125
126     @Test
127     void testJpaAutomationCompositionElementOrderedState() throws CoderException {
128         var testAutomationCompositionElement = createAutomationCompositionElementInstance();
129         var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
130
131         testJpaAutomationCompositionElement.setOrderedState(null);
132         assertEquals(testAutomationCompositionElement, testJpaAutomationCompositionElement.toAuthorative());
133         testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
134
135         var noOrderedStateAce = new StandardCoder().decode(
136                 new File("src/test/resources/json/AutomationCompositionElementNoOrderedState.json"),
137                 AutomationCompositionElement.class);
138
139         var noOrderedStateJpaAce = new JpaAutomationCompositionElement(noOrderedStateAce);
140         assertNull(noOrderedStateJpaAce.getOrderedState());
141         noOrderedStateAce.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
142         noOrderedStateJpaAce = new JpaAutomationCompositionElement(noOrderedStateAce);
143         noOrderedStateJpaAce.setInstanceId(testJpaAutomationCompositionElement.getInstanceId());
144         noOrderedStateJpaAce.setElementId(testJpaAutomationCompositionElement.getElementId());
145         noOrderedStateJpaAce.setParticipantId(testJpaAutomationCompositionElement.getParticipantId());
146         assertEquals(testJpaAutomationCompositionElement, noOrderedStateJpaAce);
147     }
148
149     @Test
150     void testJpaAutomationCompositionElementValidation() {
151         var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
152
153         assertThatThrownBy(() -> testJpaAutomationCompositionElement.validate(null))
154                 .hasMessageMatching("fieldName is marked .*ull but is null");
155
156         assertTrue(testJpaAutomationCompositionElement.validate("").isValid());
157     }
158
159     @Test
160     void testJpaAutomationCompositionElementCompareTo() {
161         var testJpaAutomationCompositionElement = createJpaAutomationCompositionElementInstance();
162
163         var otherJpaAutomationCompositionElement =
164                 new JpaAutomationCompositionElement(testJpaAutomationCompositionElement);
165         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
166         assertEquals(-1, testJpaAutomationCompositionElement.compareTo(null));
167         assertEquals(0, testJpaAutomationCompositionElement.compareTo(testJpaAutomationCompositionElement));
168         assertNotEquals(0,
169                 testJpaAutomationCompositionElement.compareTo(new DummyJpaAutomationCompositionElementChild()));
170
171         testJpaAutomationCompositionElement.setElementId("BadValue");
172         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
173         testJpaAutomationCompositionElement.setElementId(ELEMENT_ID);
174         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
175
176         testJpaAutomationCompositionElement.setInstanceId("BadValue");
177         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
178         testJpaAutomationCompositionElement.setInstanceId(INSTANCE_ID);
179         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
180
181         testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("BadValue", "0.0.1"));
182         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
183         testJpaAutomationCompositionElement.setDefinition(new PfConceptKey("aceDef", "0.0.1"));
184         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
185
186         testJpaAutomationCompositionElement.setDescription("Description");
187         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
188         testJpaAutomationCompositionElement.setDescription(null);
189         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
190
191         testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.PASSIVE);
192         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
193         testJpaAutomationCompositionElement.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
194         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
195
196         testJpaAutomationCompositionElement.setState(AutomationCompositionState.PASSIVE);
197         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
198         testJpaAutomationCompositionElement.setState(AutomationCompositionState.UNINITIALISED);
199         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
200
201         testJpaAutomationCompositionElement.setParticipantType(new PfConceptKey("dummy", "0.0.1"));
202         assertNotEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
203         testJpaAutomationCompositionElement.setParticipantType(new PfConceptKey("participantType", "0.0.1"));
204         assertEquals(0, testJpaAutomationCompositionElement.compareTo(otherJpaAutomationCompositionElement));
205
206         assertEquals(testJpaAutomationCompositionElement,
207                 new JpaAutomationCompositionElement(testJpaAutomationCompositionElement));
208     }
209
210     @Test
211     void testJpaAutomationCompositionElementLombok() {
212         assertNotNull(new Participant());
213         var ace0 = new JpaAutomationCompositionElement();
214
215         assertThat(ace0.toString()).contains("JpaAutomationCompositionElement(");
216         assertThat(ace0.hashCode()).isNotZero();
217         assertEquals(ace0, ace0);
218         assertNotEquals(null, ace0);
219
220         var ace1 = new JpaAutomationCompositionElement(ace0.getElementId(), ace0.getInstanceId());
221
222         ace1.setDefinition(new PfConceptKey("defName", "0.0.1"));
223         ace1.setDescription("Description");
224         ace1.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
225         ace1.setState(AutomationCompositionState.UNINITIALISED);
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.setParticipantType(new ToscaConceptIdentifier("participantType", "0.0.1"));
254         automationCompositionElement.setParticipantId(CommonTestData.getParticipantId());
255         automationCompositionElement.setProperties(Map.of("key", "{}"));
256
257         return automationCompositionElement;
258     }
259 }