43b43e3bf612de78bd71ba991659d1cd1a736c01
[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.participant.intermediary.handler;
22
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;
30
31 import java.time.Instant;
32 import java.util.List;
33 import java.util.UUID;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.extension.ExtendWith;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
37 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
41 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUpdates;
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.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
49 import org.springframework.test.context.junit.jupiter.SpringExtension;
50
51 @ExtendWith(SpringExtension.class)
52 class ControlLoopHandlerTest {
53
54     private CommonTestData commonTestData = new CommonTestData();
55
56     @Test
57     void controlLoopHandlerTest() {
58         var clh = commonTestData.getMockControlLoopHandler();
59         assertNotNull(clh.getControlLoops());
60
61         assertNotNull(clh.getControlLoopMap());
62         assertNotNull(clh.getElementsOnThisParticipant());
63
64         var elementId1 = UUID.randomUUID();
65         var element = new ControlLoopElement();
66         element.setId(elementId1);
67         element.setDefinition(new ToscaConceptIdentifier(
68                 "org.onap.policy.controlloop.PolicyControlLoopParticipant", "1.0.1"));
69
70         element.setOrderedState(ControlLoopOrderedState.PASSIVE);
71
72         ControlLoopElementListener listener = mock(ControlLoopElementListener.class);
73         clh.registerControlLoopElementListener(listener);
74         assertThat(clh.getListeners()).contains(listener);
75     }
76
77     @Test
78     void updateNullControlLoopHandlerTest() {
79         var id = UUID.randomUUID();
80
81         var clh = commonTestData.getMockControlLoopHandler();
82         assertNull(clh.updateControlLoopElementState(null, null, ControlLoopOrderedState.UNINITIALISED,
83                 ControlLoopState.PASSIVE));
84
85         assertNull(clh.updateControlLoopElementState(null, id, ControlLoopOrderedState.UNINITIALISED,
86                 ControlLoopState.PASSIVE));
87
88         var clElementStatistics = new ClElementStatistics();
89         var controlLoopId = new ToscaConceptIdentifier("defName", "0.0.1");
90         clElementStatistics.setParticipantId(controlLoopId);
91         clElementStatistics.setControlLoopState(ControlLoopState.RUNNING);
92         clElementStatistics.setTimeStamp(Instant.now());
93
94         clh.updateControlLoopElementStatistics(id, clElementStatistics);
95         assertNull(clh.updateControlLoopElementState(controlLoopId, id, ControlLoopOrderedState.UNINITIALISED,
96                 ControlLoopState.PASSIVE));
97     }
98
99     @Test
100     void updateControlLoopHandlerTest() throws CoderException {
101         var uuid = UUID.randomUUID();
102         var id = CommonTestData.getParticipantId();
103
104         var clh = commonTestData.setTestControlLoopHandler(id, uuid);
105         var key = clh.getElementsOnThisParticipant().keySet().iterator().next();
106         var value = clh.getElementsOnThisParticipant().get(key);
107         assertEquals(ControlLoopState.UNINITIALISED, value.getState());
108         clh.updateControlLoopElementState(id, uuid, ControlLoopOrderedState.UNINITIALISED,
109                 ControlLoopState.PASSIVE);
110         assertEquals(ControlLoopState.PASSIVE, value.getState());
111
112         clh.getControlLoopMap().values().iterator().next().getElements().putIfAbsent(key, value);
113         clh.updateControlLoopElementState(id, key, ControlLoopOrderedState.PASSIVE,
114                 ControlLoopState.RUNNING);
115         assertEquals(ControlLoopState.RUNNING, value.getState());
116
117         var clElementStatistics = new ClElementStatistics();
118         clElementStatistics.setParticipantId(id);
119         clElementStatistics.setControlLoopState(ControlLoopState.RUNNING);
120         clElementStatistics.setTimeStamp(Instant.now());
121
122         assertNotEquals(uuid, value.getClElementStatistics().getId());
123         clh.updateControlLoopElementStatistics(uuid, clElementStatistics);
124         assertEquals(uuid, value.getClElementStatistics().getId());
125
126         clh.getElementsOnThisParticipant().remove(key, value);
127         clh.getControlLoopMap().values().iterator().next().getElements().clear();
128         assertNull(clh.updateControlLoopElementState(id, key, ControlLoopOrderedState.PASSIVE,
129                 ControlLoopState.RUNNING));
130
131     }
132
133     @Test
134     void handleControlLoopUpdateExceptionTest() throws CoderException {
135         var uuid = UUID.randomUUID();
136         var id = CommonTestData.getParticipantId();
137         var stateChange = getStateChange(id, uuid, ControlLoopOrderedState.RUNNING);
138         var clh = commonTestData.setTestControlLoopHandler(id, uuid);
139         assertDoesNotThrow(() -> clh.handleControlLoopStateChange(mock(ControlLoopStateChange.class), List.of()));
140
141         clh.handleControlLoopStateChange(stateChange, List.of());
142         var newid = new ToscaConceptIdentifier("id", "1.2.3");
143         stateChange.setControlLoopId(newid);
144         stateChange.setParticipantId(newid);
145         assertDoesNotThrow(() -> clh.handleControlLoopStateChange(stateChange, List.of()));
146
147         var cld = new ControlLoopElementDefinition();
148         cld.setClElementDefinitionId(id);
149         var updateMsg = new ControlLoopUpdate();
150         updateMsg.setControlLoopId(id);
151         updateMsg.setMessageId(uuid);
152         updateMsg.setParticipantId(id);
153         updateMsg.setStartPhase(0);
154         var clElementDefinitions = List.of(cld);
155         assertDoesNotThrow(() -> clh.handleControlLoopUpdate(updateMsg, clElementDefinitions));
156         updateMsg.setStartPhase(1);
157         assertDoesNotThrow(() -> clh.handleControlLoopUpdate(updateMsg, clElementDefinitions));
158         assertThat(clh.getClElementInstanceProperties(uuid)).isEmpty();
159
160         clh.getControlLoopMap().clear();
161         updateMsg.setStartPhase(0);
162         assertDoesNotThrow(() -> clh.handleControlLoopUpdate(updateMsg, clElementDefinitions));
163
164         updateMsg.setControlLoopId(new ToscaConceptIdentifier("new", "0.0.1"));
165         updateMsg.setParticipantUpdatesList(List.of(mock(ParticipantUpdates.class)));
166         assertDoesNotThrow(() -> clh.handleControlLoopUpdate(updateMsg, clElementDefinitions));
167
168         updateMsg.setStartPhase(1);
169         var participantUpdate = new ParticipantUpdates();
170         participantUpdate.setParticipantId(id);
171         var element = new ControlLoopElement();
172         element.setParticipantType(id);
173         element.setDefinition(id);
174         participantUpdate.setControlLoopElementList(List.of(element));
175         updateMsg.setParticipantUpdatesList(List.of(participantUpdate));
176
177         var cld2 = new ControlLoopElementDefinition();
178         cld2.setClElementDefinitionId(id);
179         cld2.setControlLoopElementToscaNodeTemplate(mock(ToscaNodeTemplate.class));
180         assertDoesNotThrow(() -> clh.handleControlLoopUpdate(updateMsg, List.of(cld2)));
181
182     }
183
184     @Test
185     void controlLoopStateChangeUninitialisedTest() throws CoderException {
186         var uuid = UUID.randomUUID();
187         var id = CommonTestData.getParticipantId();
188
189         var stateChangeUninitialised = getStateChange(id, uuid, ControlLoopOrderedState.UNINITIALISED);
190
191         var clh = commonTestData.setTestControlLoopHandler(id, uuid);
192         clh.handleControlLoopStateChange(stateChangeUninitialised, List.of());
193         var newid = new ToscaConceptIdentifier("id", "1.2.3");
194         stateChangeUninitialised.setControlLoopId(newid);
195         stateChangeUninitialised.setParticipantId(newid);
196         assertDoesNotThrow(() -> clh.handleControlLoopStateChange(stateChangeUninitialised, List.of()));
197     }
198
199     @Test
200     void controlLoopStateChangePassiveTest() throws CoderException {
201         var uuid = UUID.randomUUID();
202         var id = CommonTestData.getParticipantId();
203
204         var stateChangePassive = getStateChange(id, uuid, ControlLoopOrderedState.PASSIVE);
205
206         var clh = commonTestData.setTestControlLoopHandler(id, uuid);
207         clh.handleControlLoopStateChange(stateChangePassive, List.of());
208         var newid = new ToscaConceptIdentifier("id", "1.2.3");
209         stateChangePassive.setControlLoopId(newid);
210         stateChangePassive.setParticipantId(newid);
211         assertDoesNotThrow(() -> clh.handleControlLoopStateChange(stateChangePassive, List.of()));
212     }
213
214
215     private ControlLoopStateChange getStateChange(ToscaConceptIdentifier id, UUID uuid, ControlLoopOrderedState state) {
216         var stateChange = new ControlLoopStateChange();
217         stateChange.setControlLoopId(id);
218         stateChange.setParticipantId(id);
219         stateChange.setMessageId(uuid);
220         stateChange.setOrderedState(state);
221         stateChange.setCurrentState(ControlLoopState.UNINITIALISED);
222         stateChange.setTimestamp(Instant.ofEpochMilli(3000));
223         return stateChange;
224     }
225
226 }