676747d04ae611d20bca117c75eeec52a40001fa
[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.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;
49
50 @ExtendWith(SpringExtension.class)
51 class ControlLoopHandlerTest {
52
53     private CommonTestData commonTestData = new CommonTestData();
54
55     @Test
56     void controlLoopHandlerTest() {
57         var clh = commonTestData.getMockControlLoopHandler();
58         assertNotNull(clh.getControlLoops());
59
60         assertNotNull(clh.getControlLoopMap());
61         assertNotNull(clh.getElementsOnThisParticipant());
62
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"));
68
69         element.setOrderedState(ControlLoopOrderedState.PASSIVE);
70
71         ControlLoopElementListener listener = mock(ControlLoopElementListener.class);
72         clh.registerControlLoopElementListener(listener);
73         assertThat(clh.getListeners()).contains(listener);
74     }
75
76     @Test
77     void updateNullControlLoopHandlerTest() {
78         var id = UUID.randomUUID();
79
80         var clh = commonTestData.getMockControlLoopHandler();
81         assertNull(clh.updateControlLoopElementState(null, null, ControlLoopOrderedState.UNINITIALISED,
82                 ControlLoopState.PASSIVE));
83
84         assertNull(clh.updateControlLoopElementState(null, id, ControlLoopOrderedState.UNINITIALISED,
85                 ControlLoopState.PASSIVE));
86
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());
92
93         clh.updateControlLoopElementStatistics(id, clElementStatistics);
94         assertNull(clh.updateControlLoopElementState(controlLoopId, id, ControlLoopOrderedState.UNINITIALISED,
95                 ControlLoopState.PASSIVE));
96     }
97
98     @Test
99     void updateControlLoopHandlerTest() throws CoderException {
100         var uuid = UUID.randomUUID();
101         var id = CommonTestData.getParticipantId();
102
103         var clh = 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());
110
111         var clElementStatistics = new ClElementStatistics();
112         clElementStatistics.setParticipantId(id);
113         clElementStatistics.setControlLoopState(ControlLoopState.RUNNING);
114         clElementStatistics.setTimeStamp(Instant.now());
115
116         assertNotEquals(uuid, value.getClElementStatistics().getId());
117         clh.updateControlLoopElementStatistics(uuid, clElementStatistics);
118         assertEquals(uuid, value.getClElementStatistics().getId());
119     }
120
121     @Test
122     void handleControlLoopUpdateExceptionTest() throws CoderException {
123         var uuid = UUID.randomUUID();
124         var id = CommonTestData.getParticipantId();
125
126         var stateChange = new ControlLoopStateChange();
127         stateChange.setControlLoopId(id);
128         stateChange.setParticipantId(id);
129         stateChange.setMessageId(uuid);
130         stateChange.setOrderedState(ControlLoopOrderedState.RUNNING);
131         stateChange.setCurrentState(ControlLoopState.UNINITIALISED);
132         stateChange.setTimestamp(Instant.ofEpochMilli(3000));
133
134         var clh = setTestControlLoopHandler(id, uuid);
135         clh.handleControlLoopStateChange(stateChange, List.of());
136         var newid = new ToscaConceptIdentifier("id", "1.2.3");
137         stateChange.setControlLoopId(newid);
138         stateChange.setParticipantId(newid);
139         assertDoesNotThrow(() -> clh.handleControlLoopStateChange(stateChange, List.of()));
140
141         List<ControlLoopElementDefinition> clElementDefinitions = new ArrayList<>();
142         var cld = new ControlLoopElementDefinition();
143         cld.setClElementDefinitionId(id);
144         clElementDefinitions.add(cld);
145         var updateMsg = new ControlLoopUpdate();
146         updateMsg.setControlLoopId(id);
147         updateMsg.setMessageId(uuid);
148         updateMsg.setParticipantId(id);
149         updateMsg.setStartPhase(0);
150         assertDoesNotThrow(() -> clh.handleControlLoopUpdate(updateMsg, clElementDefinitions));
151         updateMsg.setStartPhase(1);
152         assertDoesNotThrow(() -> clh.handleControlLoopUpdate(updateMsg, clElementDefinitions));
153     }
154
155     private ControlLoopHandler setTestControlLoopHandler(ToscaConceptIdentifier id, UUID uuid) throws CoderException {
156         var clh = commonTestData.getMockControlLoopHandler();
157
158         var key = commonTestData.getTestControlLoopMap().keySet().iterator().next();
159         var value = commonTestData.getTestControlLoopMap().get(key);
160         clh.getControlLoopMap().put(key, value);
161
162         var keyElem = commonTestData.setControlLoopElementTest(uuid, id).keySet().iterator().next();
163         var valueElem = commonTestData.setControlLoopElementTest(uuid, id).get(keyElem);
164         clh.getElementsOnThisParticipant().put(keyElem, valueElem);
165
166         return clh;
167     }
168
169 }