d705c49b52791529372116328e0b9b600faea3cf
[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.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertTrue;
31
32 import java.io.File;
33 import java.util.LinkedHashMap;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
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 JpaControlLoopTest} class.
46  */
47 class JpaControlLoopTest {
48
49     private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
50
51     @Test
52     void testJpaControlLoopConstructor() {
53         assertThatThrownBy(() -> {
54             new JpaControlLoop((JpaControlLoop) null);
55         }).hasMessageMatching("copyConcept is marked .*ull but is null");
56
57         assertThatThrownBy(() -> {
58             new JpaControlLoop((PfConceptKey) null);
59         }).hasMessageMatching(NULL_KEY_ERROR);
60
61         assertThatThrownBy(() -> {
62             new JpaControlLoop(null, null, null, null);
63         }).hasMessageMatching(NULL_KEY_ERROR);
64
65         assertThatThrownBy(() -> {
66             new JpaControlLoop(null, null, null, new LinkedHashMap<>());
67         }).hasMessageMatching(NULL_KEY_ERROR);
68
69         assertThatThrownBy(() -> {
70             new JpaControlLoop(null, null, ControlLoopState.UNINITIALISED, null);
71         }).hasMessageMatching(NULL_KEY_ERROR);
72
73         assertThatThrownBy(() -> {
74             new JpaControlLoop(null, null, ControlLoopState.UNINITIALISED, new LinkedHashMap<>());
75         }).hasMessageMatching(NULL_KEY_ERROR);
76
77         assertThatThrownBy(() -> {
78             new JpaControlLoop(null, new PfConceptKey(), null, null);
79         }).hasMessageMatching(NULL_KEY_ERROR);
80
81         assertThatThrownBy(() -> {
82             new JpaControlLoop(null, new PfConceptKey(), null, new LinkedHashMap<>());
83         }).hasMessageMatching(NULL_KEY_ERROR);
84
85         assertThatThrownBy(() -> {
86             new JpaControlLoop(null, new PfConceptKey(), ControlLoopState.UNINITIALISED, null);
87         }).hasMessageMatching(NULL_KEY_ERROR);
88
89         assertThatThrownBy(() -> {
90             new JpaControlLoop(null, new PfConceptKey(), ControlLoopState.UNINITIALISED, new LinkedHashMap<>());
91         }).hasMessageMatching(NULL_KEY_ERROR);
92
93         assertThatThrownBy(() -> {
94             new JpaControlLoop(new PfConceptKey(), null, null, null);
95         }).hasMessageMatching("definition is marked .*ull but is null");
96
97         assertThatThrownBy(() -> {
98             new JpaControlLoop(new PfConceptKey(), null, null, new LinkedHashMap<>());
99         }).hasMessageMatching("definition is marked .*ull but is null");
100
101         assertThatThrownBy(() -> {
102             new JpaControlLoop(new PfConceptKey(), null, ControlLoopState.UNINITIALISED, null);
103         }).hasMessageMatching("definition is marked .*ull but is null");
104
105         assertThatThrownBy(() -> {
106             new JpaControlLoop(new PfConceptKey(), null, ControlLoopState.UNINITIALISED, new LinkedHashMap<>());
107         }).hasMessageMatching("definition is marked .*ull but is null");
108
109         assertThatThrownBy(() -> {
110             new JpaControlLoop(new PfConceptKey(), new PfConceptKey(), null, null);
111         }).hasMessageMatching("state is marked .*ull but is null");
112
113         assertThatThrownBy(() -> {
114             new JpaControlLoop(new PfConceptKey(), new PfConceptKey(), null, new LinkedHashMap<>());
115         }).hasMessageMatching("state is marked .*ull but is null");
116
117         assertThatThrownBy(() -> {
118             new JpaControlLoop(new PfConceptKey(), new PfConceptKey(), ControlLoopState.UNINITIALISED, null);
119         }).hasMessageMatching("elements is marked .*ull but is null");
120
121         assertNotNull(new JpaControlLoop());
122         assertNotNull(new JpaControlLoop((new PfConceptKey())));
123         assertNotNull(new JpaControlLoop(new PfConceptKey(), new PfConceptKey(), ControlLoopState.UNINITIALISED,
124                 new LinkedHashMap<>()));
125     }
126
127     @Test
128     void testJpaControlLoop() {
129         JpaControlLoop testJpaControlLoop = createJpaControlLoopInstance();
130
131         ControlLoop participant = createControlLoopInstance();
132         assertEquals(participant, testJpaControlLoop.toAuthorative());
133
134         assertThatThrownBy(() -> {
135             testJpaControlLoop.fromAuthorative(null);
136         }).hasMessageMatching("controlLoop is marked .*ull but is null");
137
138         assertThatThrownBy(() -> new JpaControlLoop((JpaControlLoop) null)).isInstanceOf(NullPointerException.class);
139
140         JpaControlLoop testJpaControlLoopFa = new JpaControlLoop();
141         testJpaControlLoopFa.setKey(null);
142         testJpaControlLoopFa.fromAuthorative(participant);
143         assertEquals(testJpaControlLoop, testJpaControlLoopFa);
144         testJpaControlLoopFa.setKey(PfConceptKey.getNullKey());
145         testJpaControlLoopFa.fromAuthorative(participant);
146         assertEquals(testJpaControlLoop, testJpaControlLoopFa);
147         testJpaControlLoopFa.setKey(new PfConceptKey("control-loop", "0.0.1"));
148         testJpaControlLoopFa.fromAuthorative(participant);
149         assertEquals(testJpaControlLoop, testJpaControlLoopFa);
150
151         assertEquals("control-loop", testJpaControlLoop.getKey().getName());
152         assertEquals("control-loop", new JpaControlLoop(createControlLoopInstance()).getKey().getName());
153         assertEquals("control-loop",
154                 ((PfConceptKey) new JpaControlLoop(createControlLoopInstance()).getKeys().get(0)).getName());
155
156         testJpaControlLoop.clean();
157         assertEquals("control-loop", testJpaControlLoop.getKey().getName());
158
159         testJpaControlLoop.setDescription("   A Message   ");
160         testJpaControlLoop.clean();
161         assertEquals("A Message", testJpaControlLoop.getDescription());
162
163         JpaControlLoop testJpaControlLoop2 = new JpaControlLoop(testJpaControlLoop);
164         assertEquals(testJpaControlLoop, testJpaControlLoop2);
165     }
166
167     @Test
168     void testJpaControlLoopElementOrderedState() throws CoderException {
169         ControlLoop testControlLoop = createControlLoopInstance();
170         JpaControlLoop testJpaControlLoop = createJpaControlLoopInstance();
171
172         testJpaControlLoop.setOrderedState(null);
173         assertEquals(testControlLoop, testJpaControlLoop.toAuthorative());
174         testJpaControlLoop.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
175
176         ControlLoop noOrderedStateCl = new StandardCoder()
177                 .decode(new File("src/test/resources/json/ControlLoopNoOrderedState.json"), ControlLoop.class);
178
179         JpaControlLoop noOrderedStateJpaCl = new JpaControlLoop(noOrderedStateCl);
180         assertNull(noOrderedStateJpaCl.getOrderedState());
181         noOrderedStateCl.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
182         noOrderedStateJpaCl = new JpaControlLoop(noOrderedStateCl);
183         assertEquals(testJpaControlLoop, noOrderedStateJpaCl);
184
185         ControlLoops controlLoopsWithElements = new StandardCoder()
186                 .decode(new File("src/test/resources/providers/TestControlLoops.json"), ControlLoops.class);
187
188         JpaControlLoop jpaControlLoopWithElements =
189                 new JpaControlLoop(controlLoopsWithElements.getControlLoopList().get(0));
190         assertEquals(4, jpaControlLoopWithElements.getElements().size());
191         assertEquals(18, jpaControlLoopWithElements.getKeys().size());
192         assertThatCode(() -> jpaControlLoopWithElements.clean()).doesNotThrowAnyException();
193
194         assertEquals(controlLoopsWithElements.getControlLoopList().get(0), jpaControlLoopWithElements.toAuthorative());
195     }
196
197     @Test
198     void testJpaControlLoopValidation() {
199         JpaControlLoop testJpaControlLoop = createJpaControlLoopInstance();
200
201         assertThatThrownBy(() -> {
202             testJpaControlLoop.validate(null);
203         }).hasMessageMatching("fieldName is marked .*ull but is null");
204
205         assertTrue(testJpaControlLoop.validate("").isValid());
206     }
207
208     @Test
209     void testJpaControlLoopCompareTo() {
210         JpaControlLoop testJpaControlLoop = createJpaControlLoopInstance();
211
212         JpaControlLoop otherJpaControlLoop = new JpaControlLoop(testJpaControlLoop);
213         assertEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
214         assertEquals(-1, testJpaControlLoop.compareTo(null));
215         assertEquals(0, testJpaControlLoop.compareTo(testJpaControlLoop));
216         assertNotEquals(0, testJpaControlLoop.compareTo(new DummyJpaControlLoopChild()));
217
218         testJpaControlLoop.setKey(new PfConceptKey("BadValue", "0.0.1"));
219         assertNotEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
220         testJpaControlLoop.setKey(new PfConceptKey("control-loop", "0.0.1"));
221         assertEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
222
223         testJpaControlLoop.setDefinition(new PfConceptKey("BadValue", "0.0.1"));
224         assertNotEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
225         testJpaControlLoop.setDefinition(new PfConceptKey("controlLoopDefinitionName", "0.0.1"));
226         assertEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
227
228         testJpaControlLoop.setState(ControlLoopState.PASSIVE);
229         assertNotEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
230         testJpaControlLoop.setState(ControlLoopState.UNINITIALISED);
231         assertEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
232
233         testJpaControlLoop.setOrderedState(ControlLoopOrderedState.PASSIVE);
234         assertNotEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
235         testJpaControlLoop.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
236         assertEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
237
238         testJpaControlLoop.setDescription("A description");
239         assertNotEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
240         testJpaControlLoop.setDescription(null);
241         assertEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
242
243         testJpaControlLoop.setPrimed(true);
244         assertNotEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
245         testJpaControlLoop.setPrimed(false);
246         assertEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
247
248         assertEquals(testJpaControlLoop, new JpaControlLoop(testJpaControlLoop));
249     }
250
251     @Test
252     void testJpaControlLoopLombok() {
253         assertNotNull(new ControlLoop());
254         JpaControlLoop cl0 = new JpaControlLoop();
255
256         assertThat(cl0.toString()).contains("JpaControlLoop(");
257         assertThat(cl0.hashCode()).isNotZero();
258         assertEquals(true, cl0.equals(cl0));
259         assertEquals(false, cl0.equals(null));
260
261
262         JpaControlLoop cl1 = new JpaControlLoop();
263
264         cl1.setDefinition(new PfConceptKey("defName", "0.0.1"));
265         cl1.setDescription("Description");
266         cl1.setElements(new LinkedHashMap<>());
267         cl1.setKey(new PfConceptKey("participant", "0.0.1"));
268         cl1.setState(ControlLoopState.UNINITIALISED);
269
270         assertThat(cl1.toString()).contains("ControlLoop(");
271         assertEquals(false, cl1.hashCode() == 0);
272         assertEquals(false, cl1.equals(cl0));
273         assertEquals(false, cl1.equals(null));
274
275         assertNotEquals(cl1, cl0);
276
277         JpaControlLoop cl2 = new JpaControlLoop();
278         assertEquals(cl2, cl0);
279     }
280
281     private JpaControlLoop createJpaControlLoopInstance() {
282         ControlLoop testControlLoop = createControlLoopInstance();
283         JpaControlLoop testJpaControlLoop = new JpaControlLoop();
284         testJpaControlLoop.setKey(null);
285         testJpaControlLoop.fromAuthorative(testControlLoop);
286         testJpaControlLoop.setKey(PfConceptKey.getNullKey());
287         testJpaControlLoop.fromAuthorative(testControlLoop);
288
289         return testJpaControlLoop;
290     }
291
292     private ControlLoop createControlLoopInstance() {
293         ControlLoop testControlLoop = new ControlLoop();
294         testControlLoop.setName("control-loop");
295         testControlLoop.setVersion("0.0.1");
296         testControlLoop.setDefinition(new ToscaConceptIdentifier("controlLoopDefinitionName", "0.0.1"));
297         testControlLoop.setElements(new LinkedHashMap<>());
298
299         return testControlLoop;
300     }
301 }