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.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 public class JpaControlLoopTest {
49 private static final String NULL_KEY_ERROR = "key is marked .*ull but is null";
52 public 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 public 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 public 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 public 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 public 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 assertEquals(testJpaControlLoop, new JpaControlLoop(testJpaControlLoop));
247 public void testJpaControlLoopLombok() {
248 assertNotNull(new ControlLoop());
249 JpaControlLoop cl0 = new JpaControlLoop();
251 assertThat(cl0.toString()).contains("JpaControlLoop(");
252 assertThat(cl0.hashCode()).isNotZero();
253 assertEquals(true, cl0.equals(cl0));
254 assertEquals(false, cl0.equals(null));
257 JpaControlLoop cl1 = new JpaControlLoop();
259 cl1.setDefinition(new PfConceptKey("defName", "0.0.1"));
260 cl1.setDescription("Description");
261 cl1.setElements(new LinkedHashMap<>());
262 cl1.setKey(new PfConceptKey("participant", "0.0.1"));
263 cl1.setState(ControlLoopState.UNINITIALISED);
265 assertThat(cl1.toString()).contains("ControlLoop(");
266 assertEquals(false, cl1.hashCode() == 0);
267 assertEquals(false, cl1.equals(cl0));
268 assertEquals(false, cl1.equals(null));
270 assertNotEquals(cl1, cl0);
272 JpaControlLoop cl2 = new JpaControlLoop();
273 assertEquals(cl2, cl0);
276 private JpaControlLoop createJpaControlLoopInstance() {
277 ControlLoop testControlLoop = createControlLoopInstance();
278 JpaControlLoop testJpaControlLoop = new JpaControlLoop();
279 testJpaControlLoop.setKey(null);
280 testJpaControlLoop.fromAuthorative(testControlLoop);
281 testJpaControlLoop.setKey(PfConceptKey.getNullKey());
282 testJpaControlLoop.fromAuthorative(testControlLoop);
284 return testJpaControlLoop;
287 private ControlLoop createControlLoopInstance() {
288 ControlLoop testControlLoop = new ControlLoop();
289 testControlLoop.setName("control-loop");
290 testControlLoop.setVersion("0.0.1");
291 testControlLoop.setDefinition(new ToscaConceptIdentifier("controlLoopDefinitionName", "0.0.1"));
292 testControlLoop.setElements(new LinkedHashMap<>());
294 return testControlLoop;