d78b851d4d81a698c6c0b522b83a0a91bd4b935c
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-2024 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.sim.main.handler;
22
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.verify;
25
26 import java.util.List;
27 import java.util.Map;
28 import java.util.UUID;
29 import org.junit.jupiter.api.Test;
30 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
31 import org.onap.policy.clamp.acm.participant.sim.comm.CommonTestData;
32 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
33 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
34 import org.onap.policy.clamp.models.acm.concepts.DeployState;
35 import org.onap.policy.clamp.models.acm.concepts.LockState;
36 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
37
38 class AutomationCompositionElementHandlerV1Test {
39
40     private static final UUID COMPOSITION_ID = UUID.randomUUID();
41     private static final UUID INSTANCE_ID = UUID.randomUUID();
42     private static final UUID ELEMENT_ID = UUID.randomUUID();
43
44     private AcElementDeploy createAcElementDeploy() {
45         var element = new AcElementDeploy();
46         element.setId(ELEMENT_ID);
47         return element;
48     }
49
50     @Test
51     void testDeploy() {
52         var config = CommonTestData.createSimConfig();
53         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
54         var simulatorService = new SimulatorService(intermediaryApi);
55         var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService);
56         simulatorService.setConfig(config);
57         var element = createAcElementDeploy();
58         acElementHandler.deploy(INSTANCE_ID, element, Map.of());
59         verify(intermediaryApi).updateAutomationCompositionElementState(INSTANCE_ID, element.getId(),
60                 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
61
62         config.setDeploySuccess(false);
63         acElementHandler.deploy(INSTANCE_ID, element, Map.of());
64         verify(intermediaryApi).updateAutomationCompositionElementState(INSTANCE_ID, element.getId(),
65                 DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Deploy failed!");
66     }
67
68     @Test
69     void testUndeploy() {
70         var config = CommonTestData.createSimConfig();
71         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
72         var simulatorService = new SimulatorService(intermediaryApi);
73         var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService);
74         simulatorService.setConfig(config);
75         acElementHandler.undeploy(INSTANCE_ID, ELEMENT_ID);
76         verify(intermediaryApi).updateAutomationCompositionElementState(INSTANCE_ID, ELEMENT_ID, DeployState.UNDEPLOYED,
77                 null, StateChangeResult.NO_ERROR, "Undeployed");
78
79         config.setUndeploySuccess(false);
80         acElementHandler.undeploy(INSTANCE_ID, ELEMENT_ID);
81         verify(intermediaryApi).updateAutomationCompositionElementState(INSTANCE_ID, ELEMENT_ID, DeployState.DEPLOYED,
82                 null, StateChangeResult.FAILED, "Undeploy failed!");
83     }
84
85     @Test
86     void testLock() {
87         var config = CommonTestData.createSimConfig();
88         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
89         var simulatorService = new SimulatorService(intermediaryApi);
90         var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService);
91         simulatorService.setConfig(config);
92         acElementHandler.lock(INSTANCE_ID, ELEMENT_ID);
93         verify(intermediaryApi).updateAutomationCompositionElementState(INSTANCE_ID, ELEMENT_ID,
94                 null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
95
96         config.setLockSuccess(false);
97         acElementHandler.lock(INSTANCE_ID, ELEMENT_ID);
98         verify(intermediaryApi).updateAutomationCompositionElementState(INSTANCE_ID, ELEMENT_ID,
99                 null, LockState.UNLOCKED, StateChangeResult.FAILED, "Lock failed!");
100     }
101
102     @Test
103     void testUnlock() {
104         var config = CommonTestData.createSimConfig();
105         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
106         var simulatorService = new SimulatorService(intermediaryApi);
107         var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService);
108         simulatorService.setConfig(config);
109         acElementHandler.unlock(INSTANCE_ID, ELEMENT_ID);
110         verify(intermediaryApi).updateAutomationCompositionElementState(INSTANCE_ID, ELEMENT_ID,
111                 null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
112
113         config.setUnlockSuccess(false);
114         acElementHandler.unlock(INSTANCE_ID, ELEMENT_ID);
115         verify(intermediaryApi).updateAutomationCompositionElementState(INSTANCE_ID, ELEMENT_ID,
116                 null, LockState.LOCKED, StateChangeResult.FAILED, "Unlock failed!");
117     }
118
119     @Test
120     void testUpdate() {
121         var config = CommonTestData.createSimConfig();
122         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
123         var simulatorService = new SimulatorService(intermediaryApi);
124         var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService);
125         simulatorService.setConfig(config);
126         var element = createAcElementDeploy();
127         acElementHandler.update(INSTANCE_ID, element, Map.of());
128         verify(intermediaryApi).updateAutomationCompositionElementState(INSTANCE_ID, element.getId(),
129                 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Updated");
130
131         config.setUpdateSuccess(false);
132         acElementHandler.update(INSTANCE_ID, element, Map.of());
133         verify(intermediaryApi).updateAutomationCompositionElementState(INSTANCE_ID, element.getId(),
134                 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Update failed!");
135     }
136
137     @Test
138     void testDelete() {
139         var config = CommonTestData.createSimConfig();
140         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
141         var simulatorService = new SimulatorService(intermediaryApi);
142         var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService);
143         simulatorService.setConfig(config);
144         acElementHandler.delete(INSTANCE_ID, ELEMENT_ID);
145         verify(intermediaryApi).updateAutomationCompositionElementState(INSTANCE_ID, ELEMENT_ID, DeployState.DELETED,
146                 null, StateChangeResult.NO_ERROR, "Deleted");
147
148         config.setDeleteSuccess(false);
149         acElementHandler.delete(INSTANCE_ID, ELEMENT_ID);
150         verify(intermediaryApi).updateAutomationCompositionElementState(INSTANCE_ID, ELEMENT_ID, DeployState.UNDEPLOYED,
151                 null, StateChangeResult.FAILED, "Delete failed!");
152     }
153
154     @Test
155     void testPrime() {
156         var config = CommonTestData.createSimConfig();
157         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
158         var simulatorService = new SimulatorService(intermediaryApi);
159         var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService);
160         simulatorService.setConfig(config);
161         acElementHandler.prime(COMPOSITION_ID, List.of());
162         verify(intermediaryApi).updateCompositionState(COMPOSITION_ID, AcTypeState.PRIMED, StateChangeResult.NO_ERROR,
163                 "Primed");
164
165         config.setPrimeSuccess(false);
166         acElementHandler.prime(COMPOSITION_ID, List.of());
167         verify(intermediaryApi).updateCompositionState(COMPOSITION_ID, AcTypeState.COMMISSIONED,
168                 StateChangeResult.FAILED, "Prime failed!");
169     }
170
171     @Test
172     void testDeprime() {
173         var config = CommonTestData.createSimConfig();
174         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
175         var simulatorService = new SimulatorService(intermediaryApi);
176         var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService);
177         simulatorService.setConfig(config);
178         acElementHandler.deprime(COMPOSITION_ID);
179         verify(intermediaryApi).updateCompositionState(COMPOSITION_ID, AcTypeState.COMMISSIONED,
180                 StateChangeResult.NO_ERROR, "Deprimed");
181
182         config.setDeprimeSuccess(false);
183         acElementHandler.deprime(COMPOSITION_ID);
184         verify(intermediaryApi).updateCompositionState(COMPOSITION_ID, AcTypeState.PRIMED, StateChangeResult.FAILED,
185                 "Deprime failed!");
186     }
187
188     @Test
189     void testMigrate() {
190         var config = CommonTestData.createSimConfig();
191         var intermediaryApi = mock(ParticipantIntermediaryApi.class);
192         var simulatorService = new SimulatorService(intermediaryApi);
193         var acElementHandler = new AutomationCompositionElementHandlerV1(intermediaryApi, simulatorService);
194         simulatorService.setConfig(config);
195         var element = createAcElementDeploy();
196         acElementHandler.migrate(INSTANCE_ID, element, COMPOSITION_ID, Map.of());
197         verify(intermediaryApi).updateAutomationCompositionElementState(INSTANCE_ID, element.getId(),
198                 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
199
200         config.setMigrateSuccess(false);
201         acElementHandler.migrate(INSTANCE_ID, element, COMPOSITION_ID, Map.of());
202         verify(intermediaryApi).updateAutomationCompositionElementState(INSTANCE_ID, element.getId(),
203                 DeployState.DEPLOYED, null, StateChangeResult.FAILED, "Migrate failed!");
204     }
205 }