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