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.concepts;
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;
30 import java.time.Instant;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import java.util.UUID;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.models.base.PfKey;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
38 class ControlLoopTest {
40 void testControlLoop() {
41 var cl0 = new ControlLoop();
42 cl0.setDefinition(new ToscaConceptIdentifier("dfName", "1.2.3"));
43 assertEquals("dfName", cl0.getType());
44 assertEquals("1.2.3", cl0.getTypeVersion());
46 var cl1 = new ControlLoop(cl0);
47 assertEquals(cl0, cl1);
49 assertEquals(0, cl0.compareTo(cl1));
53 void testControlLoopLombok() {
54 assertNotNull(new ControlLoop());
55 var cl0 = new ControlLoop();
56 cl0.setElements(new LinkedHashMap<>());
58 assertThat(cl0.toString()).contains("ControlLoop(");
59 assertThat(cl0.hashCode()).isNotZero();
60 assertEquals(true, cl0.equals(cl0));
61 assertEquals(false, cl0.equals(null));
63 var cl1 = new ControlLoop();
65 cl1.setDefinition(new ToscaConceptIdentifier("defName", "0.0.1"));
66 cl1.setDescription("Description");
67 cl1.setElements(new LinkedHashMap<>());
69 cl1.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
70 cl1.setState(ControlLoopState.UNINITIALISED);
71 cl1.setVersion("0.0.1");
73 assertThat(cl1.toString()).contains("ControlLoop(");
74 assertEquals(false, cl1.hashCode() == 0);
75 assertEquals(false, cl1.equals(cl0));
76 assertEquals(false, cl1.equals(null));
78 assertNotEquals(cl1, cl0);
80 var cl2 = new ControlLoop();
81 cl2.setElements(new LinkedHashMap<>());
84 assertThatThrownBy(() -> cl2.setDefinition(null)). isInstanceOf(NullPointerException.class);
85 assertThatThrownBy(() -> cl2.setOrderedState(null)).isInstanceOf(NullPointerException.class);
86 assertThatThrownBy(() -> cl2.setState(null)). isInstanceOf(NullPointerException.class);
89 assertEquals(cl2, cl0);
91 cl1.setCascadedOrderedState(ControlLoopOrderedState.PASSIVE);
92 assertEquals(ControlLoopOrderedState.PASSIVE, cl1.getOrderedState());
94 cl1.getElements().put(UUID.randomUUID(), new ControlLoopElement());
95 cl1.setCascadedOrderedState(ControlLoopOrderedState.RUNNING);
96 assertEquals(ControlLoopOrderedState.RUNNING, cl1.getOrderedState());
97 assertEquals(ControlLoopOrderedState.RUNNING, cl1.getElements().values().iterator().next().getOrderedState());
99 assertNull(cl0.getElements().get(UUID.randomUUID()));
100 assertNull(cl1.getElements().get(UUID.randomUUID()));
102 assertEquals(PfKey.NULL_KEY_NAME, cl0.getDefinition().getName());
107 void testControlLoopElementStatisticsList() {
108 var cl = new ControlLoop();
109 List<ClElementStatistics> emptylist = cl.getControlLoopElementStatisticsList(cl);
110 assertEquals(List.of(), emptylist);
112 var cl1 = getControlLoopTest();
113 List<ClElementStatistics> list = cl1.getControlLoopElementStatisticsList(cl1);
115 assertEquals(2, list.size());
116 assertEquals(ControlLoopState.UNINITIALISED, list.get(0).getControlLoopState());
119 private ControlLoop getControlLoopTest() {
120 var cl = new ControlLoop();
121 cl.setDefinition(new ToscaConceptIdentifier("defName", "1.2.3"));
122 cl.setDescription("Description");
123 cl.setElements(new LinkedHashMap<>());
125 cl.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
126 cl.setState(ControlLoopState.UNINITIALISED);
127 cl.setVersion("0.0.1");
129 var uuid = UUID.randomUUID();
130 var id = new ToscaConceptIdentifier(
131 "org.onap.policy.controlloop.PolicyControlLoopParticipant", "1.0.1");
132 var clElement = getControlLoopElementTest(uuid, id);
134 var uuid2 = UUID.randomUUID();
135 var id2 = new ToscaConceptIdentifier(
136 "org.onap.policy.controlloop.PolicyControlLoopParticipantIntermediary", "0.0.1");
137 var clElement2 = getControlLoopElementTest(uuid2, id2);
139 cl.getElements().put(uuid, clElement);
140 cl.getElements().put(uuid2, clElement2);
144 private ControlLoopElement getControlLoopElementTest(UUID uuid, ToscaConceptIdentifier id) {
145 var clElement = new ControlLoopElement();
146 clElement.setId(uuid);
147 clElement.setParticipantId(id);
148 clElement.setDefinition(id);
149 clElement.setOrderedState(ControlLoopOrderedState.UNINITIALISED);
151 var clElementStatistics = new ClElementStatistics();
152 clElementStatistics.setParticipantId(id);
153 clElementStatistics.setControlLoopState(ControlLoopState.UNINITIALISED);
154 clElementStatistics.setTimeStamp(Instant.now());
156 clElement.setClElementStatistics(clElementStatistics);