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.participant.intermediary.handler;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
29 import static org.mockito.Mockito.mock;
31 import java.time.Instant;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.UUID;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
41 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
42 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange;
43 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
44 import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
45 import org.onap.policy.clamp.controlloop.participant.intermediary.main.parameters.CommonTestData;
46 import org.onap.policy.common.utils.coder.CoderException;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
48 import org.springframework.test.context.junit.jupiter.SpringExtension;
50 @ExtendWith(SpringExtension.class)
51 class ControlLoopHandlerTest {
53 private CommonTestData commonTestData = new CommonTestData();
56 void controlLoopHandlerTest() {
57 var clh = commonTestData.getMockControlLoopHandler();
58 assertNotNull(clh.getControlLoops());
60 assertNotNull(clh.getControlLoopMap());
61 assertNotNull(clh.getElementsOnThisParticipant());
63 var elementId1 = UUID.randomUUID();
64 var element = new ControlLoopElement();
65 element.setId(elementId1);
66 element.setDefinition(new ToscaConceptIdentifier(
67 "org.onap.policy.controlloop.PolicyControlLoopParticipant", "1.0.1"));
69 element.setOrderedState(ControlLoopOrderedState.PASSIVE);
71 ControlLoopElementListener listener = mock(ControlLoopElementListener.class);
72 clh.registerControlLoopElementListener(listener);
73 assertThat(clh.getListeners()).contains(listener);
77 void updateNullControlLoopHandlerTest() {
78 var id = UUID.randomUUID();
80 var clh = commonTestData.getMockControlLoopHandler();
81 assertNull(clh.updateControlLoopElementState(null, null, ControlLoopOrderedState.UNINITIALISED,
82 ControlLoopState.PASSIVE));
84 assertNull(clh.updateControlLoopElementState(null, id, ControlLoopOrderedState.UNINITIALISED,
85 ControlLoopState.PASSIVE));
87 var clElementStatistics = new ClElementStatistics();
88 var controlLoopId = new ToscaConceptIdentifier("defName", "0.0.1");
89 clElementStatistics.setParticipantId(controlLoopId);
90 clElementStatistics.setControlLoopState(ControlLoopState.RUNNING);
91 clElementStatistics.setTimeStamp(Instant.now());
93 clh.updateControlLoopElementStatistics(id, clElementStatistics);
94 assertNull(clh.updateControlLoopElementState(controlLoopId, id, ControlLoopOrderedState.UNINITIALISED,
95 ControlLoopState.PASSIVE));
99 void updateControlLoopHandlerTest() throws CoderException {
100 var uuid = UUID.randomUUID();
101 var id = CommonTestData.getParticipantId();
103 var clh = commonTestData.setTestControlLoopHandler(id, uuid);
104 var key = clh.getElementsOnThisParticipant().keySet().iterator().next();
105 var value = clh.getElementsOnThisParticipant().get(key);
106 assertEquals(ControlLoopState.UNINITIALISED, value.getState());
107 clh.updateControlLoopElementState(id, uuid, ControlLoopOrderedState.UNINITIALISED,
108 ControlLoopState.PASSIVE);
109 assertEquals(ControlLoopState.PASSIVE, value.getState());
111 var clElementStatistics = new ClElementStatistics();
112 clElementStatistics.setParticipantId(id);
113 clElementStatistics.setControlLoopState(ControlLoopState.RUNNING);
114 clElementStatistics.setTimeStamp(Instant.now());
116 assertNotEquals(uuid, value.getClElementStatistics().getId());
117 clh.updateControlLoopElementStatistics(uuid, clElementStatistics);
118 assertEquals(uuid, value.getClElementStatistics().getId());
122 void handleControlLoopUpdateExceptionTest() throws CoderException {
123 var uuid = UUID.randomUUID();
124 var id = CommonTestData.getParticipantId();
126 var stateChange = getStateChange(id, uuid, ControlLoopOrderedState.RUNNING);
128 var clh = commonTestData.setTestControlLoopHandler(id, uuid);
129 clh.handleControlLoopStateChange(stateChange, List.of());
130 var newid = new ToscaConceptIdentifier("id", "1.2.3");
131 stateChange.setControlLoopId(newid);
132 stateChange.setParticipantId(newid);
133 assertDoesNotThrow(() -> clh.handleControlLoopStateChange(stateChange, List.of()));
135 List<ControlLoopElementDefinition> clElementDefinitions = new ArrayList<>();
136 var cld = new ControlLoopElementDefinition();
137 cld.setClElementDefinitionId(id);
138 clElementDefinitions.add(cld);
139 var updateMsg = new ControlLoopUpdate();
140 updateMsg.setControlLoopId(id);
141 updateMsg.setMessageId(uuid);
142 updateMsg.setParticipantId(id);
143 updateMsg.setStartPhase(0);
144 assertDoesNotThrow(() -> clh.handleControlLoopUpdate(updateMsg, clElementDefinitions));
145 updateMsg.setStartPhase(1);
146 assertDoesNotThrow(() -> clh.handleControlLoopUpdate(updateMsg, clElementDefinitions));
147 assertThat(clh.getClElementInstanceProperties(uuid)).isEmpty();
151 void controlLoopStateChangeUninitialisedTest() throws CoderException {
152 var uuid = UUID.randomUUID();
153 var id = CommonTestData.getParticipantId();
155 var stateChangeUninitialised = getStateChange(id, uuid, ControlLoopOrderedState.UNINITIALISED);
157 var clh = commonTestData.setTestControlLoopHandler(id, uuid);
158 clh.handleControlLoopStateChange(stateChangeUninitialised, List.of());
159 var newid = new ToscaConceptIdentifier("id", "1.2.3");
160 stateChangeUninitialised.setControlLoopId(newid);
161 stateChangeUninitialised.setParticipantId(newid);
162 assertDoesNotThrow(() -> clh.handleControlLoopStateChange(stateChangeUninitialised, List.of()));
166 void controlLoopStateChangePassiveTest() throws CoderException {
167 var uuid = UUID.randomUUID();
168 var id = CommonTestData.getParticipantId();
170 var stateChangePassive = getStateChange(id, uuid, ControlLoopOrderedState.PASSIVE);
172 var clh = commonTestData.setTestControlLoopHandler(id, uuid);
173 clh.handleControlLoopStateChange(stateChangePassive, List.of());
174 var newid = new ToscaConceptIdentifier("id", "1.2.3");
175 stateChangePassive.setControlLoopId(newid);
176 stateChangePassive.setParticipantId(newid);
177 assertDoesNotThrow(() -> clh.handleControlLoopStateChange(stateChangePassive, List.of()));
181 private ControlLoopStateChange getStateChange(ToscaConceptIdentifier id, UUID uuid, ControlLoopOrderedState state) {
182 var stateChange = new ControlLoopStateChange();
183 stateChange.setControlLoopId(id);
184 stateChange.setParticipantId(id);
185 stateChange.setMessageId(uuid);
186 stateChange.setOrderedState(state);
187 stateChange.setCurrentState(ControlLoopState.UNINITIALISED);
188 stateChange.setTimestamp(Instant.ofEpochMilli(3000));