767a916b593eb5f8e7346b7d8fdfb855f0ba0311
[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.Mockito.clearInvocations;
24 import static org.mockito.Mockito.doThrow;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.timeout;
27 import static org.mockito.Mockito.verify;
28
29 import java.util.List;
30 import java.util.Map;
31 import java.util.UUID;
32 import javax.ws.rs.core.Response.Status;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
35 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
36 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
37 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
39 import org.onap.policy.clamp.models.acm.concepts.DeployState;
40 import org.onap.policy.clamp.models.acm.concepts.LockState;
41 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
42 import org.onap.policy.models.base.PfModelException;
43
44 class ThreadHandlerTest {
45
46     private static final int TIMEOUT = 400;
47
48     @Test
49     void test() throws PfModelException {
50         var listener = mock(AutomationCompositionElementListener.class);
51         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
52         var threadHandler = new ThreadHandler(listener, intermediaryApi, mock(CacheProvider.class));
53
54         var compositionId = UUID.randomUUID();
55         var list = List.of(new AutomationCompositionElementDefinition());
56         var messageId = UUID.randomUUID();
57         threadHandler.prime(messageId, compositionId, list);
58         verify(listener, timeout(TIMEOUT)).prime(compositionId, list);
59
60         clearInvocations(listener);
61         var element = new AcElementDeploy();
62         var elementId = UUID.randomUUID();
63         element.setId(elementId);
64         Map<String, Object> properties = Map.of("key", "value");
65         var instanceId = UUID.randomUUID();
66         threadHandler.deploy(messageId, instanceId, element, properties);
67         verify(listener, timeout(TIMEOUT)).deploy(instanceId, element, properties);
68
69         clearInvocations(listener);
70         threadHandler.update(messageId, instanceId, element, properties);
71         verify(listener, timeout(TIMEOUT)).update(instanceId, element, properties);
72
73         clearInvocations(listener);
74         threadHandler.lock(messageId, instanceId, elementId);
75         verify(listener, timeout(TIMEOUT)).lock(instanceId, elementId);
76
77         clearInvocations(listener);
78         threadHandler.unlock(messageId, instanceId, elementId);
79         verify(listener, timeout(TIMEOUT)).unlock(instanceId, elementId);
80
81         clearInvocations(listener);
82         threadHandler.undeploy(messageId, instanceId, elementId);
83         verify(listener, timeout(TIMEOUT)).undeploy(instanceId, elementId);
84
85         clearInvocations(listener);
86         threadHandler.delete(messageId, instanceId, elementId);
87         verify(listener, timeout(TIMEOUT)).delete(instanceId, elementId);
88
89         clearInvocations(listener);
90         threadHandler.deprime(messageId, compositionId);
91         verify(listener, timeout(TIMEOUT)).deprime(compositionId);
92     }
93
94     @Test
95     void testException() throws PfModelException {
96         var listener = mock(AutomationCompositionElementListener.class);
97         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
98         var threadHandler = new ThreadHandler(listener, intermediaryApi, mock(CacheProvider.class));
99
100         var compositionId = UUID.randomUUID();
101         var list = List.of(new AutomationCompositionElementDefinition());
102         doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).prime(compositionId, list);
103         var messageId = UUID.randomUUID();
104         threadHandler.prime(messageId, compositionId, list);
105         verify(intermediaryApi, timeout(TIMEOUT)).updateCompositionState(compositionId, AcTypeState.COMMISSIONED,
106                 StateChangeResult.FAILED, "Composition Defintion prime failed");
107
108         clearInvocations(intermediaryApi);
109         var element = new AcElementDeploy();
110         var elementId = UUID.randomUUID();
111         element.setId(elementId);
112         Map<String, Object> properties = Map.of("key", "value");
113         var instanceId = UUID.randomUUID();
114         doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).deploy(instanceId, element,
115                 properties);
116         threadHandler.deploy(messageId, instanceId, element, properties);
117         verify(intermediaryApi, timeout(TIMEOUT)).updateAutomationCompositionElementState(instanceId, elementId,
118                 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Automation composition element deploy failed");
119
120         clearInvocations(listener);
121         doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).update(instanceId, element,
122                 properties);
123         threadHandler.update(messageId, instanceId, element, properties);
124         verify(intermediaryApi, timeout(TIMEOUT)).updateAutomationCompositionElementState(instanceId, elementId,
125                 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Automation composition element update failed");
126
127         clearInvocations(listener);
128         doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).lock(instanceId, elementId);
129         threadHandler.lock(messageId, instanceId, elementId);
130         verify(intermediaryApi, timeout(TIMEOUT)).updateAutomationCompositionElementState(instanceId, elementId, null,
131                 LockState.UNLOCKED, StateChangeResult.FAILED, "Automation composition element lock failed");
132
133         clearInvocations(listener);
134         doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).unlock(instanceId,
135                 elementId);
136         threadHandler.unlock(messageId, instanceId, elementId);
137         verify(intermediaryApi, timeout(TIMEOUT)).updateAutomationCompositionElementState(instanceId, elementId, null,
138                 LockState.LOCKED, StateChangeResult.FAILED, "Automation composition element unlock failed");
139
140         clearInvocations(listener);
141         doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).undeploy(instanceId,
142                 elementId);
143         threadHandler.undeploy(messageId, instanceId, elementId);
144         verify(intermediaryApi, timeout(TIMEOUT)).updateAutomationCompositionElementState(instanceId, elementId,
145                 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Automation composition element undeploy failed");
146
147         clearInvocations(listener);
148         doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).delete(instanceId,
149                 elementId);
150         threadHandler.delete(messageId, instanceId, elementId);
151         verify(intermediaryApi, timeout(TIMEOUT)).updateAutomationCompositionElementState(instanceId, elementId,
152                 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Automation composition element delete failed");
153
154         clearInvocations(listener);
155         doThrow(new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error")).when(listener).deprime(compositionId);
156         threadHandler.deprime(messageId, compositionId);
157         verify(intermediaryApi, timeout(TIMEOUT)).updateCompositionState(compositionId,
158                 AcTypeState.PRIMED, StateChangeResult.FAILED, "Composition Defintion deprime failed");
159     }
160 }