293a4415b4add064105d7a81b24e75d2d9518aed
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 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.intermediary.handler;
22
23 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.UUID;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
35 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
37 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
38 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionMigration;
39 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionPrepare;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41
42 class AcSubStateHandlerTest {
43
44     @Test
45     void handleAcStateChangeNullTest() {
46         var cacheProvider = mock(CacheProvider.class);
47         var ach = new AcSubStateHandler(cacheProvider, mock(ThreadHandler.class));
48
49         var acMigration = new AutomationCompositionMigration();
50         acMigration.setPrecheck(true);
51         assertDoesNotThrow(() -> ach.handleAcMigrationPrecheck(acMigration));
52
53         var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
54         acMigration.setAutomationCompositionId(automationComposition.getInstanceId());
55         acMigration.setCompositionTargetId(UUID.randomUUID());
56         assertDoesNotThrow(() -> ach.handleAcMigrationPrecheck(acMigration));
57
58         var acPrepare = new AutomationCompositionPrepare();
59         assertDoesNotThrow(() -> ach.handleAcPrepare(acPrepare));
60
61         acPrepare.setAutomationCompositionId(automationComposition.getInstanceId());
62         assertDoesNotThrow(() -> ach.handleAcPrepare(acPrepare));
63     }
64
65     @Test
66     void handleAcMigrationPrecheckTest() {
67         var listener = mock(ThreadHandler.class);
68         var cacheProvider = mock(CacheProvider.class);
69         var ach = new AcSubStateHandler(cacheProvider, listener);
70         var migrationMsg = new AutomationCompositionMigration();
71         migrationMsg.setPrecheck(true);
72         assertDoesNotThrow(() -> ach.handleAcMigrationPrecheck(migrationMsg));
73         var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
74         migrationMsg.setCompositionTargetId(UUID.randomUUID());
75         migrationMsg.setAutomationCompositionId(automationComposition.getInstanceId());
76         assertDoesNotThrow(() -> ach.handleAcMigrationPrecheck(migrationMsg));
77         when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId()))
78                 .thenReturn(automationComposition);
79         var participantDeploy = new ParticipantDeploy();
80         participantDeploy.setParticipantId(CommonTestData.getParticipantId());
81         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
82         migrationMsg.getParticipantUpdatesList().add(participantDeploy);
83         Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition> map = new HashMap<>();
84         for (var element : automationComposition.getElements().values()) {
85             var acElementDeploy = new AcElementDeploy();
86             acElementDeploy.setProperties(Map.of());
87             acElementDeploy.setId(element.getId());
88             acElementDeploy.setDefinition(element.getDefinition());
89             participantDeploy.getAcElementList().add(acElementDeploy);
90             map.put(element.getDefinition(), new AutomationCompositionElementDefinition());
91         }
92         when(cacheProvider.getAcElementsDefinitions())
93                 .thenReturn(Map.of(automationComposition.getCompositionId(), map,
94                         migrationMsg.getCompositionTargetId(), map));
95
96         ach.handleAcMigrationPrecheck(migrationMsg);
97         verify(listener, times(automationComposition.getElements().size()))
98                 .migratePrecheck(any(), any(), any(), any(), any());
99     }
100
101     @Test
102     void handlePrepareTest() {
103         var listener = mock(ThreadHandler.class);
104         var cacheProvider = mock(CacheProvider.class);
105         var ach = new AcSubStateHandler(cacheProvider, listener);
106
107         var acPrepareMsg = new AutomationCompositionPrepare();
108         acPrepareMsg.setPreDeploy(true);
109         assertDoesNotThrow(() -> ach.handleAcPrepare(acPrepareMsg));
110
111         acPrepareMsg.setParticipantId(CommonTestData.getParticipantId());
112         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
113         var participantDeploy = new ParticipantDeploy();
114         participantDeploy.setParticipantId(CommonTestData.getParticipantId());
115         acPrepareMsg.getParticipantList().add(participantDeploy);
116
117         var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
118         acPrepareMsg.setAutomationCompositionId(automationComposition.getInstanceId());
119         when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId()))
120                 .thenReturn(automationComposition);
121         Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition> map = new HashMap<>();
122         for (var element : automationComposition.getElements().values()) {
123             var acElementDeploy = new AcElementDeploy();
124             acElementDeploy.setProperties(Map.of());
125             acElementDeploy.setId(element.getId());
126             participantDeploy.getAcElementList().add(acElementDeploy);
127             map.put(element.getDefinition(), new AutomationCompositionElementDefinition());
128         }
129         when(cacheProvider.getAcElementsDefinitions())
130             .thenReturn(Map.of(automationComposition.getCompositionId(), map));
131
132         ach.handleAcPrepare(acPrepareMsg);
133         verify(listener, times(automationComposition.getElements().size())).prepare(any(), any(), any());
134     }
135
136     @Test
137     void handleReviewTest() {
138         var cacheProvider = mock(CacheProvider.class);
139         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
140
141         var acPrepareMsg = new AutomationCompositionPrepare();
142         acPrepareMsg.setPreDeploy(false);
143         acPrepareMsg.setParticipantId(CommonTestData.getParticipantId());
144
145         var automationComposition = CommonTestData.getTestAutomationCompositionMap().values().iterator().next();
146         acPrepareMsg.setAutomationCompositionId(automationComposition.getInstanceId());
147         when(cacheProvider.getAutomationComposition(automationComposition.getInstanceId()))
148             .thenReturn(automationComposition);
149         Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition> map = new HashMap<>();
150         for (var element : automationComposition.getElements().values()) {
151             var acElementDeploy = new AcElementDeploy();
152             acElementDeploy.setProperties(Map.of());
153             acElementDeploy.setId(element.getId());
154             map.put(element.getDefinition(), new AutomationCompositionElementDefinition());
155         }
156         when(cacheProvider.getAcElementsDefinitions())
157             .thenReturn(Map.of(automationComposition.getCompositionId(), map));
158
159         var listener = mock(ThreadHandler.class);
160         var ach = new AcSubStateHandler(cacheProvider, listener);
161         ach.handleAcPrepare(acPrepareMsg);
162         verify(listener, times(automationComposition.getElements().size())).review(any(), any(), any());
163     }
164 }