2ac3c8df2ad9a80e67d26031d16220d26eff51cc
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.clearInvocations;
25 import static org.mockito.Mockito.doThrow;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.timeout;
28 import static org.mockito.Mockito.verify;
29
30 import jakarta.ws.rs.core.Response.Status;
31 import java.io.IOException;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.UUID;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
37 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
38 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
39 import org.onap.policy.clamp.models.acm.concepts.AcElementRestart;
40 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
41 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
42 import org.onap.policy.clamp.models.acm.concepts.DeployState;
43 import org.onap.policy.clamp.models.acm.concepts.LockState;
44 import org.onap.policy.clamp.models.acm.concepts.ParticipantRestartAc;
45 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
46 import org.onap.policy.models.base.PfModelException;
47
48 class ThreadHandlerTest {
49
50     private static final int TIMEOUT = 400;
51
52     @Test
53     void test() throws PfModelException, IOException {
54         var listener = mock(AutomationCompositionElementListener.class);
55         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
56         try (var threadHandler = new ThreadHandler(listener, intermediaryApi, mock(CacheProvider.class))) {
57
58             var compositionId = UUID.randomUUID();
59             var list = List.of(new AutomationCompositionElementDefinition());
60             var messageId = UUID.randomUUID();
61             threadHandler.prime(messageId, compositionId, list);
62             verify(listener, timeout(TIMEOUT)).prime(compositionId, list);
63
64             clearInvocations(listener);
65             var element = new AcElementDeploy();
66             var elementId = UUID.randomUUID();
67             element.setId(elementId);
68             Map<String, Object> properties = Map.of("key", "value");
69             var instanceId = UUID.randomUUID();
70             threadHandler.deploy(messageId, instanceId, element, properties);
71             verify(listener, timeout(TIMEOUT)).deploy(instanceId, element, properties);
72
73             clearInvocations(listener);
74             threadHandler.update(messageId, instanceId, element, properties);
75             verify(listener, timeout(TIMEOUT)).update(instanceId, element, properties);
76
77             clearInvocations(listener);
78             var compositionTargetId = UUID.randomUUID();
79             threadHandler.migrate(messageId, instanceId, element, compositionTargetId, properties);
80             verify(listener, timeout(TIMEOUT)).migrate(instanceId, element, compositionTargetId, properties);
81
82             clearInvocations(listener);
83             threadHandler.lock(messageId, instanceId, elementId);
84             verify(listener, timeout(TIMEOUT)).lock(instanceId, elementId);
85
86             clearInvocations(listener);
87             threadHandler.unlock(messageId, instanceId, elementId);
88             verify(listener, timeout(TIMEOUT)).unlock(instanceId, elementId);
89
90             clearInvocations(listener);
91             threadHandler.undeploy(messageId, instanceId, elementId);
92             verify(listener, timeout(TIMEOUT)).undeploy(instanceId, elementId);
93
94             clearInvocations(listener);
95             threadHandler.delete(messageId, instanceId, elementId);
96             verify(listener, timeout(TIMEOUT)).delete(instanceId, elementId);
97
98             clearInvocations(listener);
99             threadHandler.deprime(messageId, compositionId);
100             verify(listener, timeout(TIMEOUT)).deprime(compositionId);
101         }
102     }
103
104     @Test
105     void testException() throws PfModelException, IOException {
106         var listener = mock(AutomationCompositionElementListener.class);
107         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
108         try (var threadHandler = new ThreadHandler(listener, intermediaryApi, mock(CacheProvider.class))) {
109
110             var compositionId = UUID.randomUUID();
111             var list = List.of(new AutomationCompositionElementDefinition());
112             doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).prime(compositionId,
113                     list);
114             var messageId = UUID.randomUUID();
115             threadHandler.prime(messageId, compositionId, list);
116             verify(intermediaryApi, timeout(TIMEOUT)).updateCompositionState(compositionId, AcTypeState.COMMISSIONED,
117                     StateChangeResult.FAILED, "Composition Defintion prime failed");
118
119             clearInvocations(intermediaryApi);
120             var element = new AcElementDeploy();
121             var elementId = UUID.randomUUID();
122             element.setId(elementId);
123             Map<String, Object> properties = Map.of("key", "value");
124             var instanceId = UUID.randomUUID();
125             doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).deploy(instanceId,
126                     element, properties);
127             threadHandler.deploy(messageId, instanceId, element, properties);
128             verify(intermediaryApi, timeout(TIMEOUT)).updateAutomationCompositionElementState(instanceId, elementId,
129                     DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
130                     "Automation composition element deploy failed");
131
132             clearInvocations(listener);
133             doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).update(instanceId,
134                     element, properties);
135             threadHandler.update(messageId, instanceId, element, properties);
136             verify(intermediaryApi, timeout(TIMEOUT)).updateAutomationCompositionElementState(instanceId, elementId,
137                     DeployState.DEPLOYED, null, StateChangeResult.FAILED,
138                     "Automation composition element update failed");
139
140             clearInvocations(listener);
141             doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).lock(instanceId,
142                     elementId);
143             threadHandler.lock(messageId, instanceId, elementId);
144             verify(intermediaryApi, timeout(TIMEOUT)).updateAutomationCompositionElementState(instanceId, elementId,
145                     null, LockState.UNLOCKED, StateChangeResult.FAILED, "Automation composition element lock failed");
146
147             clearInvocations(listener);
148             doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).unlock(instanceId,
149                     elementId);
150             threadHandler.unlock(messageId, instanceId, elementId);
151             verify(intermediaryApi, timeout(TIMEOUT)).updateAutomationCompositionElementState(instanceId, elementId,
152                     null, LockState.LOCKED, StateChangeResult.FAILED, "Automation composition element unlock failed");
153
154             clearInvocations(listener);
155             doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).undeploy(instanceId,
156                     elementId);
157             threadHandler.undeploy(messageId, instanceId, elementId);
158             verify(intermediaryApi, timeout(TIMEOUT)).updateAutomationCompositionElementState(instanceId, elementId,
159                     DeployState.DEPLOYED, null, StateChangeResult.FAILED,
160                     "Automation composition element undeploy failed");
161
162             clearInvocations(listener);
163             doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).delete(instanceId,
164                     elementId);
165             threadHandler.delete(messageId, instanceId, elementId);
166             verify(intermediaryApi, timeout(TIMEOUT)).updateAutomationCompositionElementState(instanceId, elementId,
167                     DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
168                     "Automation composition element delete failed");
169
170             clearInvocations(listener);
171             doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).deprime(compositionId);
172             threadHandler.deprime(messageId, compositionId);
173             verify(intermediaryApi, timeout(TIMEOUT)).updateCompositionState(compositionId, AcTypeState.PRIMED,
174                     StateChangeResult.FAILED, "Composition Defintion deprime failed");
175
176             clearInvocations(listener);
177             doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener)
178                     .handleRestartComposition(compositionId, List.of(), AcTypeState.PRIMING);
179             threadHandler.restarted(messageId, compositionId, List.of(), AcTypeState.PRIMING, List.of());
180             verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED,
181                     "Composition Defintion deprime failed");
182         }
183     }
184
185     @Test
186     void testRestarted() throws IOException, PfModelException {
187         var listener = mock(AutomationCompositionElementListener.class);
188         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
189         var cacheProvider = mock(CacheProvider.class);
190         try (var threadHandler = new ThreadHandler(listener, intermediaryApi, cacheProvider)) {
191             var messageId = UUID.randomUUID();
192             var compositionId = UUID.randomUUID();
193             var participantRestartAc = new ParticipantRestartAc();
194             participantRestartAc.setAutomationCompositionId(UUID.randomUUID());
195             participantRestartAc.getAcElementList().add(new AcElementRestart());
196             threadHandler.restarted(messageId, compositionId, List.of(new AutomationCompositionElementDefinition()),
197                     AcTypeState.PRIMED, List.of(participantRestartAc));
198             verify(listener, timeout(TIMEOUT)).handleRestartInstance(any(), any(), any(), any(), any());
199         }
200     }
201 }