032d655e3598788e5ff486294c9ff704154113d2
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.controlloop.models.controlloop.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.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30
31 import java.io.File;
32 import java.util.UUID;
33 import org.junit.Test;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.models.base.PfConceptKey;
41 import org.onap.policy.models.base.PfKey;
42 import org.onap.policy.models.base.PfReferenceKey;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
44
45 /**
46  * Test the {@link JpaControlLoopElement} class.
47  */
48 public class JpaControlLoopElementTest {
49
50     private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
51
52     @Test
53     public void testJpaControlLoopElementConstructor() {
54         assertThatThrownBy(() -> {
55             new JpaControlLoopElement((JpaControlLoopElement) null);
56         }).hasMessageMatching("copyConcept is marked .*ull but is null");
57
58         assertThatThrownBy(() -> {
59             new JpaControlLoopElement((PfReferenceKey) null);
60         }).hasMessageMatching(NULL_KEY_ERROR);
61
62         assertThatThrownBy(() -> {
63             new JpaControlLoopElement(null, null, null, null);
64         }).hasMessageMatching(NULL_KEY_ERROR);
65
66         assertThatThrownBy(() -> {
67             new JpaControlLoopElement(null, null, null, ControlLoopState.UNINITIALISED);
68         }).hasMessageMatching(NULL_KEY_ERROR);
69
70         assertThatThrownBy(() -> {
71             new JpaControlLoopElement(null, null, new PfConceptKey("participant", "0.0.1"), null);
72         }).hasMessageMatching(NULL_KEY_ERROR);
73
74         assertThatThrownBy(() -> {
75             new JpaControlLoopElement(null, null, new PfConceptKey("participant", "0.0.1"),
76                     ControlLoopState.UNINITIALISED);
77         }).hasMessageMatching(NULL_KEY_ERROR);
78
79         assertThatThrownBy(() -> {
80             new JpaControlLoopElement(null, new PfConceptKey(), null, null);
81         }).hasMessageMatching(NULL_KEY_ERROR);
82
83         assertThatThrownBy(() -> {
84             new JpaControlLoopElement(null, new PfConceptKey(), null, ControlLoopState.UNINITIALISED);
85         }).hasMessageMatching(NULL_KEY_ERROR);
86
87         assertThatThrownBy(() -> {
88             new JpaControlLoopElement(null, new PfConceptKey(), new PfConceptKey("participant", "0.0.1"), null);
89         }).hasMessageMatching(NULL_KEY_ERROR);
90
91         assertThatThrownBy(() -> {
92             new JpaControlLoopElement(null, new PfConceptKey(), new PfConceptKey("participant", "0.0.1"),
93                     ControlLoopState.UNINITIALISED);
94         }).hasMessageMatching(NULL_KEY_ERROR);
95
96         assertThatThrownBy(() -> {
97             new JpaControlLoopElement(new PfReferenceKey(), null, null, null);
98         }).hasMessageMatching("definition is marked .*ull but is null");
99
100         assertThatThrownBy(() -> {
101             new JpaControlLoopElement(new PfReferenceKey(), null, null, ControlLoopState.UNINITIALISED);
102         }).hasMessageMatching("definition is marked .*ull but is null");
103
104         assertThatThrownBy(() -> {
105             new JpaControlLoopElement(new PfReferenceKey(), null, new PfConceptKey("participant", "0.0.1"), null);
106         }).hasMessageMatching("definition is marked .*ull but is null");
107
108         assertThatThrownBy(() -> {
109             new JpaControlLoopElement(new PfReferenceKey(), null, new PfConceptKey("participant", "0.0.1"),
110                     ControlLoopState.UNINITIALISED);
111         }).hasMessageMatching("definition is marked .*ull but is null");
112
113         assertThatThrownBy(() -> {
114             new JpaControlLoopElement(new PfReferenceKey(), new PfConceptKey(), null, null);
115         }).hasMessageMatching("participantType is marked .*ull but is null");
116
117         assertThatThrownBy(() -> {
118             new JpaControlLoopElement(new PfReferenceKey(), new PfConceptKey(), null, ControlLoopState.UNINITIALISED);
119         }).hasMessageMatching("participantType is marked .*ull but is null");
120
121         assertThatThrownBy(() -> {
122             new JpaControlLoopElement(new PfReferenceKey(), new PfConceptKey(),
123                     new PfConceptKey("participant", "0.0.1"), null);
124         }).hasMessageMatching("state is marked .*ull but is null");
125
126         assertNotNull(new JpaControlLoopElement());
127         assertNotNull(new JpaControlLoopElement((new PfReferenceKey())));
128         assertNotNull(new JpaControlLoopElement(new PfReferenceKey(), new PfConceptKey(),
129                 new PfConceptKey("participant", "0.0.1"), ControlLoopState.UNINITIALISED));
130     }
131
132     @Test
133     public void testJpaControlLoopElement() {
134         JpaControlLoopElement testJpaControlLoopElement = createJpaControlLoopElementInstance();
135
136         ControlLoopElement cle = createControlLoopElementInstance();
137         assertEquals(cle, testJpaControlLoopElement.toAuthorative());
138
139         assertThatThrownBy(() -> {
140             testJpaControlLoopElement.fromAuthorative(null);
141         }).hasMessageMatching("element is marked .*ull but is null");
142
143         assertThatThrownBy(() -> new JpaControlLoopElement((JpaControlLoopElement) null))
144                 .isInstanceOf(NullPointerException.class);
145
146         JpaControlLoopElement testJpaControlLoopElementFa = new JpaControlLoopElement();
147         testJpaControlLoopElementFa.setKey(null);
148         testJpaControlLoopElementFa.fromAuthorative(cle);
149         assertEquals(testJpaControlLoopElement, testJpaControlLoopElementFa);
150         testJpaControlLoopElementFa.setKey(PfReferenceKey.getNullKey());
151         testJpaControlLoopElementFa.fromAuthorative(cle);
152         assertEquals(testJpaControlLoopElement, testJpaControlLoopElementFa);
153         testJpaControlLoopElementFa.setKey(new PfReferenceKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION,
154                 "a95757ba-b34a-4049-a2a8-46773abcbe5e"));
155         testJpaControlLoopElementFa.fromAuthorative(cle);
156         assertEquals(testJpaControlLoopElement, testJpaControlLoopElementFa);
157
158         assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e", testJpaControlLoopElement.getKey().getLocalName());
159         assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e",
160                 new JpaControlLoopElement(createControlLoopElementInstance()).getKey().getLocalName());
161         assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e",
162                 ((PfReferenceKey) new JpaControlLoopElement(createControlLoopElementInstance()).getKeys().get(0))
163                         .getLocalName());
164
165         testJpaControlLoopElement.clean();
166         assertEquals("a95757ba-b34a-4049-a2a8-46773abcbe5e", testJpaControlLoopElement.getKey().getLocalName());
167
168         testJpaControlLoopElement.setDescription(" A Message ");
169         testJpaControlLoopElement.clean();
170         assertEquals("A Message", testJpaControlLoopElement.getDescription());
171
172         JpaControlLoopElement testJpaControlLoopElement2 = new JpaControlLoopElement(testJpaControlLoopElement);
173         assertEquals(testJpaControlLoopElement, testJpaControlLoopElement2);
174     }
175
176     @Test
177     public void testJpaControlLoopElementOrderedState() throws CoderException {
178         ControlLoopElement testControlLoopElement = createControlLoopElementInstance();
179         JpaControlLoopElement testJpaControlLoopElement = createJpaControlLoopElementInstance();
180
181         testJpaControlLoopElement.setOrderedState(null);
182         assertEquals(testControlLoopElement, testJpaControlLoopElement.toAuthorative());
183         testJpaControlLoopElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
184
185         ControlLoopElement noOrderedStateCle = new StandardCoder().decode(
186                 new File("src/test/resources/json/ControlLoopElementNoOrderedState.json"), ControlLoopElement.class);
187
188         JpaControlLoopElement noOrderedStateJpaCle = new JpaControlLoopElement(noOrderedStateCle);
189         assertNull(noOrderedStateJpaCle.getOrderedState());
190         noOrderedStateCle.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
191         noOrderedStateJpaCle = new JpaControlLoopElement(noOrderedStateCle);
192         assertEquals(testJpaControlLoopElement, noOrderedStateJpaCle);
193     }
194
195     @Test
196     public void testJpaControlLoopElementValidation() {
197         JpaControlLoopElement testJpaControlLoopElement = createJpaControlLoopElementInstance();
198
199         assertThatThrownBy(() -> {
200             testJpaControlLoopElement.validate(null);
201         }).hasMessageMatching("fieldName is marked .*ull but is null");
202
203         assertTrue(testJpaControlLoopElement.validate("").isValid());
204     }
205
206     @Test
207     public void testJpaControlLoopElementCompareTo() {
208         JpaControlLoopElement testJpaControlLoopElement = createJpaControlLoopElementInstance();
209
210         JpaControlLoopElement otherJpaControlLoopElement = new JpaControlLoopElement(testJpaControlLoopElement);
211         assertEquals(0, testJpaControlLoopElement.compareTo(otherJpaControlLoopElement));
212         assertEquals(-1, testJpaControlLoopElement.compareTo(null));
213         assertEquals(0, testJpaControlLoopElement.compareTo(testJpaControlLoopElement));
214         assertNotEquals(0, testJpaControlLoopElement.compareTo(new DummyJpaControlLoopElementChild()));
215
216         testJpaControlLoopElement
217                 .setKey(new PfReferenceKey("BadValue", "0.0.1", "a95757ba-b34a-4049-a2a8-46773abcbe5e"));
218         assertNotEquals(0, testJpaControlLoopElement.compareTo(otherJpaControlLoopElement));
219         testJpaControlLoopElement.setKey(new PfReferenceKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION,
220                 "a95757ba-b34a-4049-a2a8-46773abcbe5e"));
221         assertEquals(0, testJpaControlLoopElement.compareTo(otherJpaControlLoopElement));
222
223         testJpaControlLoopElement.setDefinition(new PfConceptKey("BadValue", "0.0.1"));
224         assertNotEquals(0, testJpaControlLoopElement.compareTo(otherJpaControlLoopElement));
225         testJpaControlLoopElement.setDefinition(new PfConceptKey("cleDef", "0.0.1"));
226         assertEquals(0, testJpaControlLoopElement.compareTo(otherJpaControlLoopElement));
227
228         testJpaControlLoopElement.setDescription("Description");
229         assertNotEquals(0, testJpaControlLoopElement.compareTo(otherJpaControlLoopElement));
230         testJpaControlLoopElement.setDescription(null);
231         assertEquals(0, testJpaControlLoopElement.compareTo(otherJpaControlLoopElement));
232
233         testJpaControlLoopElement.setOrderedState(ControlLoopOrderedState.PASSIVE);
234         assertNotEquals(0, testJpaControlLoopElement.compareTo(otherJpaControlLoopElement));
235         testJpaControlLoopElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
236         assertEquals(0, testJpaControlLoopElement.compareTo(otherJpaControlLoopElement));
237
238         testJpaControlLoopElement.setState(ControlLoopState.PASSIVE);
239         assertNotEquals(0, testJpaControlLoopElement.compareTo(otherJpaControlLoopElement));
240         testJpaControlLoopElement.setState(ControlLoopState.UNINITIALISED);
241         assertEquals(0, testJpaControlLoopElement.compareTo(otherJpaControlLoopElement));
242
243         testJpaControlLoopElement.setParticipantType(new PfConceptKey("dummy", "0.0.1"));
244         assertNotEquals(0, testJpaControlLoopElement.compareTo(otherJpaControlLoopElement));
245         testJpaControlLoopElement.setParticipantType(new PfConceptKey("participantType", "0.0.1"));
246         assertEquals(0, testJpaControlLoopElement.compareTo(otherJpaControlLoopElement));
247
248         assertEquals(testJpaControlLoopElement, new JpaControlLoopElement(testJpaControlLoopElement));
249     }
250
251     @Test
252     public void testJpaControlLoopElementLombok() {
253         assertNotNull(new Participant());
254         JpaControlLoopElement cle0 = new JpaControlLoopElement();
255
256         assertThat(cle0.toString()).contains("JpaControlLoopElement(");
257         assertThat(cle0.hashCode()).isNotZero();
258         assertEquals(true, cle0.equals(cle0));
259         assertEquals(false, cle0.equals(null));
260
261
262         JpaControlLoopElement cle1 = new JpaControlLoopElement();
263
264         cle1.setDefinition(new PfConceptKey("defName", "0.0.1"));
265         cle1.setDescription("Description");
266         cle1.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
267         cle1.setState(ControlLoopState.UNINITIALISED);
268         cle1.setParticipantId(new PfConceptKey("participant", "0.0.1"));
269
270         assertThat(cle1.toString()).contains("ControlLoopElement(");
271         assertEquals(false, cle1.hashCode() == 0);
272         assertEquals(false, cle1.equals(cle0));
273         assertEquals(false, cle1.equals(null));
274
275         assertNotEquals(cle1, cle0);
276
277         JpaControlLoopElement cle2 = new JpaControlLoopElement();
278         assertEquals(cle2, cle0);
279     }
280
281     private JpaControlLoopElement createJpaControlLoopElementInstance() {
282         ControlLoopElement testCle = createControlLoopElementInstance();
283         JpaControlLoopElement testJpaControlLoopElement = new JpaControlLoopElement();
284         testJpaControlLoopElement.setKey(null);
285         testJpaControlLoopElement.fromAuthorative(testCle);
286         testJpaControlLoopElement.setKey(PfReferenceKey.getNullKey());
287         testJpaControlLoopElement.fromAuthorative(testCle);
288
289         return testJpaControlLoopElement;
290     }
291
292     private ControlLoopElement createControlLoopElementInstance() {
293         ControlLoopElement controlLoopElement = new ControlLoopElement();
294         controlLoopElement.setId(UUID.fromString("a95757ba-b34a-4049-a2a8-46773abcbe5e"));
295         controlLoopElement.setDefinition(new ToscaConceptIdentifier("cleDef", "0.0.1"));
296         controlLoopElement.setParticipantType(new ToscaConceptIdentifier("participantType", "0.0.1"));
297
298         return controlLoopElement;
299     }
300 }