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
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.acm.participant.intermediary.handler;
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;
30 import jakarta.ws.rs.core.Response.Status;
31 import java.io.IOException;
32 import java.util.List;
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;
48 class ThreadHandlerTest {
50 private static final int TIMEOUT = 400;
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))) {
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);
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);
73 clearInvocations(listener);
74 threadHandler.update(messageId, instanceId, element, properties);
75 verify(listener, timeout(TIMEOUT)).update(instanceId, element, properties);
77 clearInvocations(listener);
78 threadHandler.lock(messageId, instanceId, elementId);
79 verify(listener, timeout(TIMEOUT)).lock(instanceId, elementId);
81 clearInvocations(listener);
82 threadHandler.unlock(messageId, instanceId, elementId);
83 verify(listener, timeout(TIMEOUT)).unlock(instanceId, elementId);
85 clearInvocations(listener);
86 threadHandler.undeploy(messageId, instanceId, elementId);
87 verify(listener, timeout(TIMEOUT)).undeploy(instanceId, elementId);
89 clearInvocations(listener);
90 threadHandler.delete(messageId, instanceId, elementId);
91 verify(listener, timeout(TIMEOUT)).delete(instanceId, elementId);
93 clearInvocations(listener);
94 threadHandler.deprime(messageId, compositionId);
95 verify(listener, timeout(TIMEOUT)).deprime(compositionId);
100 void testException() throws PfModelException, IOException {
101 var listener = mock(AutomationCompositionElementListener.class);
102 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
103 try (var threadHandler = new ThreadHandler(listener, intermediaryApi, mock(CacheProvider.class))) {
105 var compositionId = UUID.randomUUID();
106 var list = List.of(new AutomationCompositionElementDefinition());
107 doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).prime(compositionId,
109 var messageId = UUID.randomUUID();
110 threadHandler.prime(messageId, compositionId, list);
111 verify(intermediaryApi, timeout(TIMEOUT)).updateCompositionState(compositionId, AcTypeState.COMMISSIONED,
112 StateChangeResult.FAILED, "Composition Defintion prime failed");
114 clearInvocations(intermediaryApi);
115 var element = new AcElementDeploy();
116 var elementId = UUID.randomUUID();
117 element.setId(elementId);
118 Map<String, Object> properties = Map.of("key", "value");
119 var instanceId = UUID.randomUUID();
120 doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).deploy(instanceId,
121 element, properties);
122 threadHandler.deploy(messageId, instanceId, element, properties);
123 verify(intermediaryApi, timeout(TIMEOUT)).updateAutomationCompositionElementState(instanceId, elementId,
124 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
125 "Automation composition element deploy failed");
127 clearInvocations(listener);
128 doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).update(instanceId,
129 element, properties);
130 threadHandler.update(messageId, instanceId, element, properties);
131 verify(intermediaryApi, timeout(TIMEOUT)).updateAutomationCompositionElementState(instanceId, elementId,
132 DeployState.DEPLOYED, null, StateChangeResult.FAILED,
133 "Automation composition element update failed");
135 clearInvocations(listener);
136 doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).lock(instanceId,
138 threadHandler.lock(messageId, instanceId, elementId);
139 verify(intermediaryApi, timeout(TIMEOUT)).updateAutomationCompositionElementState(instanceId, elementId,
140 null, LockState.UNLOCKED, StateChangeResult.FAILED, "Automation composition element lock failed");
142 clearInvocations(listener);
143 doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).unlock(instanceId,
145 threadHandler.unlock(messageId, instanceId, elementId);
146 verify(intermediaryApi, timeout(TIMEOUT)).updateAutomationCompositionElementState(instanceId, elementId,
147 null, LockState.LOCKED, StateChangeResult.FAILED, "Automation composition element unlock failed");
149 clearInvocations(listener);
150 doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).undeploy(instanceId,
152 threadHandler.undeploy(messageId, instanceId, elementId);
153 verify(intermediaryApi, timeout(TIMEOUT)).updateAutomationCompositionElementState(instanceId, elementId,
154 DeployState.DEPLOYED, null, StateChangeResult.FAILED,
155 "Automation composition element undeploy failed");
157 clearInvocations(listener);
158 doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).delete(instanceId,
160 threadHandler.delete(messageId, instanceId, elementId);
161 verify(intermediaryApi, timeout(TIMEOUT)).updateAutomationCompositionElementState(instanceId, elementId,
162 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
163 "Automation composition element delete failed");
165 clearInvocations(listener);
166 doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).deprime(compositionId);
167 threadHandler.deprime(messageId, compositionId);
168 verify(intermediaryApi, timeout(TIMEOUT)).updateCompositionState(compositionId, AcTypeState.PRIMED,
169 StateChangeResult.FAILED, "Composition Defintion deprime failed");
171 clearInvocations(listener);
172 doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener)
173 .handleRestartComposition(compositionId, List.of(), AcTypeState.PRIMING);
174 threadHandler.restarted(messageId, compositionId, List.of(), AcTypeState.PRIMING, List.of());
175 verify(intermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.FAILED,
176 "Composition Defintion deprime failed");
181 void testRestarted() throws IOException, PfModelException {
182 var listener = mock(AutomationCompositionElementListener.class);
183 var intermediaryApi = mock(ParticipantIntermediaryApi.class);
184 var cacheProvider = mock(CacheProvider.class);
185 try (var threadHandler = new ThreadHandler(listener, intermediaryApi, cacheProvider)) {
186 var messageId = UUID.randomUUID();
187 var compositionId = UUID.randomUUID();
188 var participantRestartAc = new ParticipantRestartAc();
189 participantRestartAc.setAutomationCompositionId(UUID.randomUUID());
190 participantRestartAc.getAcElementList().add(new AcElementRestart());
191 threadHandler.restarted(messageId, compositionId, List.of(new AutomationCompositionElementDefinition()),
192 AcTypeState.PRIMED, List.of(participantRestartAc));
193 verify(listener, timeout(TIMEOUT)).handleRestartInstance(any(), any(), any(), any(), any());