5585e5190b03e231e826086567029ae20315e254
[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.acm.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.acm.participant.intermediary.api.AutomationCompositionElementListener;
37 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
38 import org.onap.policy.clamp.models.acm.concepts.AcElementStatistics;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
40 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
41 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
43 import org.onap.policy.clamp.models.acm.concepts.ParticipantUpdates;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionUpdate;
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 AutomationCompositionHandlerTest {
53
54     private CommonTestData commonTestData = new CommonTestData();
55
56     @Test
57     void automationCompositionHandlerTest() {
58         var ach = commonTestData.getMockAutomationCompositionHandler();
59         assertNotNull(ach.getAutomationCompositions());
60
61         assertNotNull(ach.getAutomationCompositionMap());
62         assertNotNull(ach.getElementsOnThisParticipant());
63
64         var elementId1 = UUID.randomUUID();
65         var element = new AutomationCompositionElement();
66         element.setId(elementId1);
67         element.setDefinition(
68             new ToscaConceptIdentifier("org.onap.policy.acm.PolicyAutomationCompositionParticipant", "1.0.1"));
69
70         element.setOrderedState(AutomationCompositionOrderedState.PASSIVE);
71
72         AutomationCompositionElementListener listener = mock(AutomationCompositionElementListener.class);
73         ach.registerAutomationCompositionElementListener(listener);
74         assertThat(ach.getListeners()).contains(listener);
75     }
76
77     @Test
78     void updateNullAutomationCompositionHandlerTest() {
79         var id = UUID.randomUUID();
80
81         var ach = commonTestData.getMockAutomationCompositionHandler();
82         assertNull(ach.updateAutomationCompositionElementState(null, null,
83             AutomationCompositionOrderedState.UNINITIALISED, AutomationCompositionState.PASSIVE));
84
85         assertNull(ach.updateAutomationCompositionElementState(null, id,
86             AutomationCompositionOrderedState.UNINITIALISED, AutomationCompositionState.PASSIVE));
87
88         var acElementStatistics = new AcElementStatistics();
89         var automationCompositionId = new ToscaConceptIdentifier("defName", "0.0.1");
90         acElementStatistics.setParticipantId(automationCompositionId);
91         acElementStatistics.setState(AutomationCompositionState.RUNNING);
92         acElementStatistics.setTimeStamp(Instant.now());
93
94         ach.updateAutomationCompositionElementStatistics(id, acElementStatistics);
95         assertNull(ach.updateAutomationCompositionElementState(automationCompositionId, id,
96             AutomationCompositionOrderedState.UNINITIALISED, AutomationCompositionState.PASSIVE));
97     }
98
99     @Test
100     void updateAutomationCompositionHandlerTest() throws CoderException {
101         var uuid = UUID.randomUUID();
102         var id = CommonTestData.getParticipantId();
103
104         var ach = commonTestData.setTestAutomationCompositionHandler(id, uuid);
105         var key = ach.getElementsOnThisParticipant().keySet().iterator().next();
106         var value = ach.getElementsOnThisParticipant().get(key);
107         assertEquals(AutomationCompositionState.UNINITIALISED, value.getState());
108         ach.updateAutomationCompositionElementState(id, uuid, AutomationCompositionOrderedState.UNINITIALISED,
109             AutomationCompositionState.PASSIVE);
110         assertEquals(AutomationCompositionState.PASSIVE, value.getState());
111
112         ach.getAutomationCompositionMap().values().iterator().next().getElements().putIfAbsent(key, value);
113         ach.updateAutomationCompositionElementState(id, key, AutomationCompositionOrderedState.PASSIVE,
114             AutomationCompositionState.RUNNING);
115         assertEquals(AutomationCompositionState.RUNNING, value.getState());
116
117         var acElementStatistics = new AcElementStatistics();
118         acElementStatistics.setParticipantId(id);
119         acElementStatistics.setState(AutomationCompositionState.RUNNING);
120         acElementStatistics.setTimeStamp(Instant.now());
121
122         assertNotEquals(uuid, value.getAcElementStatistics().getId());
123         ach.updateAutomationCompositionElementStatistics(uuid, acElementStatistics);
124         assertEquals(uuid, value.getAcElementStatistics().getId());
125
126         ach.getElementsOnThisParticipant().remove(key, value);
127         ach.getAutomationCompositionMap().values().iterator().next().getElements().clear();
128         assertNull(ach.updateAutomationCompositionElementState(id, key, AutomationCompositionOrderedState.PASSIVE,
129             AutomationCompositionState.RUNNING));
130
131     }
132
133     @Test
134     void handleAutomationCompositionUpdateExceptionTest() throws CoderException {
135         var uuid = UUID.randomUUID();
136         var id = CommonTestData.getParticipantId();
137         var stateChange = getStateChange(id, uuid, AutomationCompositionOrderedState.RUNNING);
138         var ach = commonTestData.setTestAutomationCompositionHandler(id, uuid);
139         assertDoesNotThrow(
140             () -> ach.handleAutomationCompositionStateChange(mock(AutomationCompositionStateChange.class), List.of()));
141
142         ach.handleAutomationCompositionStateChange(stateChange, List.of());
143         var newid = new ToscaConceptIdentifier("id", "1.2.3");
144         stateChange.setAutomationCompositionId(newid);
145         stateChange.setParticipantId(newid);
146         assertDoesNotThrow(() -> ach.handleAutomationCompositionStateChange(stateChange, List.of()));
147
148         var acd = new AutomationCompositionElementDefinition();
149         acd.setAcElementDefinitionId(id);
150         var updateMsg = new AutomationCompositionUpdate();
151         updateMsg.setAutomationCompositionId(id);
152         updateMsg.setMessageId(uuid);
153         updateMsg.setParticipantId(id);
154         updateMsg.setStartPhase(0);
155         var acElementDefinitions = List.of(acd);
156         assertDoesNotThrow(() -> ach.handleAutomationCompositionUpdate(updateMsg, acElementDefinitions));
157         updateMsg.setStartPhase(1);
158         assertDoesNotThrow(() -> ach.handleAutomationCompositionUpdate(updateMsg, acElementDefinitions));
159         assertThat(ach.getAcElementInstanceProperties(uuid)).isEmpty();
160
161         ach.getAutomationCompositionMap().clear();
162         updateMsg.setStartPhase(0);
163         assertDoesNotThrow(() -> ach.handleAutomationCompositionUpdate(updateMsg, acElementDefinitions));
164
165         updateMsg.setAutomationCompositionId(new ToscaConceptIdentifier("new", "0.0.1"));
166         updateMsg.setParticipantUpdatesList(List.of(mock(ParticipantUpdates.class)));
167         assertDoesNotThrow(() -> ach.handleAutomationCompositionUpdate(updateMsg, acElementDefinitions));
168
169         updateMsg.setStartPhase(1);
170         var participantUpdate = new ParticipantUpdates();
171         participantUpdate.setParticipantId(id);
172         var element = new AutomationCompositionElement();
173         element.setParticipantType(id);
174         element.setDefinition(id);
175         participantUpdate.setAutomationCompositionElementList(List.of(element));
176         updateMsg.setParticipantUpdatesList(List.of(participantUpdate));
177
178         var acd2 = new AutomationCompositionElementDefinition();
179         acd2.setAcElementDefinitionId(id);
180         acd2.setAutomationCompositionElementToscaNodeTemplate(mock(ToscaNodeTemplate.class));
181         assertDoesNotThrow(() -> ach.handleAutomationCompositionUpdate(updateMsg, List.of(acd2)));
182
183     }
184
185     @Test
186     void automationCompositionStateChangeUninitialisedTest() throws CoderException {
187         var uuid = UUID.randomUUID();
188         var id = CommonTestData.getParticipantId();
189
190         var stateChangeUninitialised = getStateChange(id, uuid, AutomationCompositionOrderedState.UNINITIALISED);
191
192         var ach = commonTestData.setTestAutomationCompositionHandler(id, uuid);
193         ach.handleAutomationCompositionStateChange(stateChangeUninitialised, List.of());
194         var newid = new ToscaConceptIdentifier("id", "1.2.3");
195         stateChangeUninitialised.setAutomationCompositionId(newid);
196         stateChangeUninitialised.setParticipantId(newid);
197         assertDoesNotThrow(() -> ach.handleAutomationCompositionStateChange(stateChangeUninitialised, List.of()));
198     }
199
200     @Test
201     void automationCompositionStateChangePassiveTest() throws CoderException {
202         var uuid = UUID.randomUUID();
203         var id = CommonTestData.getParticipantId();
204
205         var stateChangePassive = getStateChange(id, uuid, AutomationCompositionOrderedState.PASSIVE);
206
207         var ach = commonTestData.setTestAutomationCompositionHandler(id, uuid);
208         ach.handleAutomationCompositionStateChange(stateChangePassive, List.of());
209         var newid = new ToscaConceptIdentifier("id", "1.2.3");
210         stateChangePassive.setAutomationCompositionId(newid);
211         stateChangePassive.setParticipantId(newid);
212         assertDoesNotThrow(() -> ach.handleAutomationCompositionStateChange(stateChangePassive, List.of()));
213     }
214
215     private AutomationCompositionStateChange getStateChange(ToscaConceptIdentifier id, UUID uuid,
216         AutomationCompositionOrderedState state) {
217         var stateChange = new AutomationCompositionStateChange();
218         stateChange.setAutomationCompositionId(id);
219         stateChange.setParticipantId(id);
220         stateChange.setMessageId(uuid);
221         stateChange.setOrderedState(state);
222         stateChange.setCurrentState(AutomationCompositionState.UNINITIALISED);
223         stateChange.setTimestamp(Instant.ofEpochMilli(3000));
224         return stateChange;
225     }
226
227 }