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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts;
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;
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;
45 * Test the {@link JpaControlLoopTest} class.
47 class JpaControlLoopTest {
49 private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
52 void testJpaControlLoopConstructor() {
53 assertThatThrownBy(() -> {
54 new JpaControlLoop((JpaControlLoop) null);
55 }).hasMessageMatching("copyConcept is marked .*ull but is null");
57 assertThatThrownBy(() -> {
58 new JpaControlLoop((PfConceptKey) null);
59 }).hasMessageMatching(NULL_KEY_ERROR);
61 assertThatThrownBy(() -> {
62 new JpaControlLoop(null, null, null, null);
63 }).hasMessageMatching(NULL_KEY_ERROR);
65 assertThatThrownBy(() -> {
66 new JpaControlLoop(null, null, null, new LinkedHashMap<>());
67 }).hasMessageMatching(NULL_KEY_ERROR);
69 assertThatThrownBy(() -> {
70 new JpaControlLoop(null, null, ControlLoopState.UNINITIALISED, null);
71 }).hasMessageMatching(NULL_KEY_ERROR);
73 assertThatThrownBy(() -> {
74 new JpaControlLoop(null, null, ControlLoopState.UNINITIALISED, new LinkedHashMap<>());
75 }).hasMessageMatching(NULL_KEY_ERROR);
77 assertThatThrownBy(() -> {
78 new JpaControlLoop(null, new PfConceptKey(), null, null);
79 }).hasMessageMatching(NULL_KEY_ERROR);
81 assertThatThrownBy(() -> {
82 new JpaControlLoop(null, new PfConceptKey(), null, new LinkedHashMap<>());
83 }).hasMessageMatching(NULL_KEY_ERROR);
85 assertThatThrownBy(() -> {
86 new JpaControlLoop(null, new PfConceptKey(), ControlLoopState.UNINITIALISED, null);
87 }).hasMessageMatching(NULL_KEY_ERROR);
89 assertThatThrownBy(() -> {
90 new JpaControlLoop(null, new PfConceptKey(), ControlLoopState.UNINITIALISED, new LinkedHashMap<>());
91 }).hasMessageMatching(NULL_KEY_ERROR);
93 assertThatThrownBy(() -> {
94 new JpaControlLoop(new PfConceptKey(), null, null, null);
95 }).hasMessageMatching("definition is marked .*ull but is null");
97 assertThatThrownBy(() -> {
98 new JpaControlLoop(new PfConceptKey(), null, null, new LinkedHashMap<>());
99 }).hasMessageMatching("definition is marked .*ull but is null");
101 assertThatThrownBy(() -> {
102 new JpaControlLoop(new PfConceptKey(), null, ControlLoopState.UNINITIALISED, null);
103 }).hasMessageMatching("definition is marked .*ull but is null");
105 assertThatThrownBy(() -> {
106 new JpaControlLoop(new PfConceptKey(), null, ControlLoopState.UNINITIALISED, new LinkedHashMap<>());
107 }).hasMessageMatching("definition is marked .*ull but is null");
109 assertThatThrownBy(() -> {
110 new JpaControlLoop(new PfConceptKey(), new PfConceptKey(), null, null);
111 }).hasMessageMatching("state is marked .*ull but is null");
113 assertThatThrownBy(() -> {
114 new JpaControlLoop(new PfConceptKey(), new PfConceptKey(), null, new LinkedHashMap<>());
115 }).hasMessageMatching("state is marked .*ull but is null");
117 assertThatThrownBy(() -> {
118 new JpaControlLoop(new PfConceptKey(), new PfConceptKey(), ControlLoopState.UNINITIALISED, null);
119 }).hasMessageMatching("elements is marked .*ull but is null");
121 assertNotNull(new JpaControlLoop());
122 assertNotNull(new JpaControlLoop((new PfConceptKey())));
123 assertNotNull(new JpaControlLoop(new PfConceptKey(), new PfConceptKey(), ControlLoopState.UNINITIALISED,
124 new LinkedHashMap<>()));
128 void testJpaControlLoop() {
129 JpaControlLoop testJpaControlLoop = createJpaControlLoopInstance();
131 ControlLoop participant = createControlLoopInstance();
132 assertEquals(participant, testJpaControlLoop.toAuthorative());
134 assertThatThrownBy(() -> {
135 testJpaControlLoop.fromAuthorative(null);
136 }).hasMessageMatching("controlLoop is marked .*ull but is null");
138 assertThatThrownBy(() -> new JpaControlLoop((JpaControlLoop) null)).isInstanceOf(NullPointerException.class);
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);
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());
156 testJpaControlLoop.clean();
157 assertEquals("control-loop", testJpaControlLoop.getKey().getName());
159 testJpaControlLoop.setDescription(" A Message ");
160 testJpaControlLoop.clean();
161 assertEquals("A Message", testJpaControlLoop.getDescription());
163 JpaControlLoop testJpaControlLoop2 = new JpaControlLoop(testJpaControlLoop);
164 assertEquals(testJpaControlLoop, testJpaControlLoop2);
168 void testJpaControlLoopElementOrderedState() throws CoderException {
169 ControlLoop testControlLoop = createControlLoopInstance();
170 JpaControlLoop testJpaControlLoop = createJpaControlLoopInstance();
172 testJpaControlLoop.setOrderedState(null);
173 assertEquals(testControlLoop, testJpaControlLoop.toAuthorative());
174 testJpaControlLoop.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
176 ControlLoop noOrderedStateCl = new StandardCoder()
177 .decode(new File("src/test/resources/json/ControlLoopNoOrderedState.json"), ControlLoop.class);
179 JpaControlLoop noOrderedStateJpaCl = new JpaControlLoop(noOrderedStateCl);
180 assertNull(noOrderedStateJpaCl.getOrderedState());
181 noOrderedStateCl.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
182 noOrderedStateJpaCl = new JpaControlLoop(noOrderedStateCl);
183 assertEquals(testJpaControlLoop, noOrderedStateJpaCl);
185 ControlLoops controlLoopsWithElements = new StandardCoder()
186 .decode(new File("src/test/resources/providers/TestControlLoops.json"), ControlLoops.class);
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();
194 assertEquals(controlLoopsWithElements.getControlLoopList().get(0), jpaControlLoopWithElements.toAuthorative());
198 void testJpaControlLoopValidation() {
199 JpaControlLoop testJpaControlLoop = createJpaControlLoopInstance();
201 assertThatThrownBy(() -> {
202 testJpaControlLoop.validate(null);
203 }).hasMessageMatching("fieldName is marked .*ull but is null");
205 assertTrue(testJpaControlLoop.validate("").isValid());
209 void testJpaControlLoopCompareTo() {
210 JpaControlLoop testJpaControlLoop = createJpaControlLoopInstance();
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()));
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));
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));
228 testJpaControlLoop.setState(ControlLoopState.PASSIVE);
229 assertNotEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
230 testJpaControlLoop.setState(ControlLoopState.UNINITIALISED);
231 assertEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
233 testJpaControlLoop.setOrderedState(ControlLoopOrderedState.PASSIVE);
234 assertNotEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
235 testJpaControlLoop.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
236 assertEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
238 testJpaControlLoop.setDescription("A description");
239 assertNotEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
240 testJpaControlLoop.setDescription(null);
241 assertEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
243 testJpaControlLoop.setPrimed(true);
244 assertNotEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
245 testJpaControlLoop.setPrimed(false);
246 assertEquals(0, testJpaControlLoop.compareTo(otherJpaControlLoop));
248 assertEquals(testJpaControlLoop, new JpaControlLoop(testJpaControlLoop));
252 void testJpaControlLoopLombok() {
253 assertNotNull(new ControlLoop());
254 JpaControlLoop cl0 = new JpaControlLoop();
256 assertThat(cl0.toString()).contains("JpaControlLoop(");
257 assertThat(cl0.hashCode()).isNotZero();
258 assertEquals(true, cl0.equals(cl0));
259 assertEquals(false, cl0.equals(null));
262 JpaControlLoop cl1 = new JpaControlLoop();
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);
270 assertThat(cl1.toString()).contains("ControlLoop(");
271 assertEquals(false, cl1.hashCode() == 0);
272 assertEquals(false, cl1.equals(cl0));
273 assertEquals(false, cl1.equals(null));
275 assertNotEquals(cl1, cl0);
277 JpaControlLoop cl2 = new JpaControlLoop();
278 assertEquals(cl2, cl0);
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);
289 return testJpaControlLoop;
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<>());
299 return testControlLoop;