c6259a28f61f133f35fc7c429d6f5ffccd9cd95e
[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.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.anyList;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28
29 import java.util.List;
30 import java.util.Map;
31 import java.util.UUID;
32 import org.junit.jupiter.api.Test;
33 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto;
34 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
35 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
36 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
37 import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
38 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrime;
39 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantPrimeAck;
40 import org.onap.policy.clamp.models.acm.messages.kafka.participant.ParticipantSync;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42
43 class AcDefinitionHandlerTest {
44
45     @Test
46     void handleComposiotPrimeTest() {
47         var listener = mock(ThreadHandler.class);
48         var cacheProvider = mock(CacheProvider.class);
49         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
50         var ach = new AcDefinitionHandler(cacheProvider, mock(ParticipantMessagePublisher.class), listener);
51         var participantPrimeMsg = new ParticipantPrime();
52         participantPrimeMsg.setCompositionId(UUID.randomUUID());
53         participantPrimeMsg.setParticipantDefinitionUpdates(List.of(createParticipantDefinition()));
54         ach.handlePrime(participantPrimeMsg);
55         verify(cacheProvider).addElementDefinition(any(UUID.class), anyList());
56         verify(listener).prime(any(UUID.class), any(CompositionDto.class));
57     }
58
59     private ParticipantDefinition createParticipantDefinition() {
60         var def = new ParticipantDefinition();
61         def.setParticipantId(CommonTestData.getParticipantId());
62         def.setAutomationCompositionElementDefinitionList(
63                 List.of(CommonTestData.createAutomationCompositionElementDefinition(
64                         new ToscaConceptIdentifier("key", "1.0.0"))));
65         return def;
66     }
67
68     @Test
69     void handleCompositionDeprimeTest() {
70         var acElementDefinition = CommonTestData.createAutomationCompositionElementDefinition(
71                 new ToscaConceptIdentifier("key", "1.0.0"));
72         var compositionId = UUID.randomUUID();
73         var listener = mock(ThreadHandler.class);
74         var cacheProvider = mock(CacheProvider.class);
75         var ach = new AcDefinitionHandler(cacheProvider, mock(ParticipantMessagePublisher.class), listener);
76         when(cacheProvider.getAcElementsDefinitions())
77                 .thenReturn(Map.of(compositionId, Map.of(new ToscaConceptIdentifier(), acElementDefinition)));
78         var participantPrimeMsg = new ParticipantPrime();
79         participantPrimeMsg.setCompositionId(compositionId);
80         ach.handlePrime(participantPrimeMsg);
81         verify(listener).deprime(any(UUID.class), any(CompositionDto.class));
82     }
83
84     @Test
85     void handleCompositionAlreadyDeprimedTest() {
86         var compositionId = UUID.randomUUID();
87         var participantMessagePublisher =  mock(ParticipantMessagePublisher.class);
88         var ach = new AcDefinitionHandler(mock(CacheProvider.class), participantMessagePublisher,
89                 mock(ThreadHandler.class));
90         var participantPrimeMsg = new ParticipantPrime();
91         participantPrimeMsg.setCompositionId(compositionId);
92         ach.handlePrime(participantPrimeMsg);
93         verify(participantMessagePublisher).sendParticipantPrimeAck(any(ParticipantPrimeAck.class));
94     }
95
96     @Test
97     void syncTest() {
98         var participantSyncMsg = new ParticipantSync();
99         participantSyncMsg.setState(AcTypeState.PRIMED);
100         participantSyncMsg.setCompositionId(UUID.randomUUID());
101         participantSyncMsg.getParticipantDefinitionUpdates().add(createParticipantDefinition());
102         participantSyncMsg.setAutomationcompositionList(List.of(CommonTestData.createParticipantRestartAc()));
103
104         var cacheProvider = mock(CacheProvider.class);
105         var listener = mock(ThreadHandler.class);
106         var ach = new AcDefinitionHandler(cacheProvider, mock(ParticipantMessagePublisher.class), listener);
107         ach.handleParticipantSync(participantSyncMsg);
108         verify(cacheProvider).initializeAutomationComposition(any(UUID.class), any());
109         verify(cacheProvider).addElementDefinition(any(), any());
110     }
111
112     @Test
113     void syncDeleteTest() {
114         var participantSyncMsg = new ParticipantSync();
115         participantSyncMsg.setState(AcTypeState.COMMISSIONED);
116         participantSyncMsg.setDelete(true);
117         participantSyncMsg.setCompositionId(UUID.randomUUID());
118         participantSyncMsg.getParticipantDefinitionUpdates().add(createParticipantDefinition());
119         var restartAc = CommonTestData.createParticipantRestartAc();
120         participantSyncMsg.setAutomationcompositionList(List.of(restartAc));
121
122         var cacheProvider = mock(CacheProvider.class);
123         var listener = mock(ThreadHandler.class);
124         var ach = new AcDefinitionHandler(cacheProvider, mock(ParticipantMessagePublisher.class), listener);
125         ach.handleParticipantSync(participantSyncMsg);
126         verify(cacheProvider).removeElementDefinition(participantSyncMsg.getCompositionId());
127         verify(cacheProvider).removeAutomationComposition(restartAc.getAutomationCompositionId());
128     }
129 }