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
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.ArgumentMatchers.anyList;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
29 import java.util.List;
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.ParticipantRestart;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 public class AcDefinitionHandlerTest {
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));
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"))));
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));
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));
97 void restartedTest() {
98 var participantRestartMsg = new ParticipantRestart();
99 participantRestartMsg.setState(AcTypeState.PRIMED);
100 participantRestartMsg.setCompositionId(UUID.randomUUID());
101 participantRestartMsg.getParticipantDefinitionUpdates().add(createParticipantDefinition());
102 participantRestartMsg.setAutomationcompositionList(List.of(CommonTestData.createParticipantRestartAc()));
104 var cacheProvider = mock(CacheProvider.class);
105 var listener = mock(ThreadHandler.class);
106 var ach = new AcDefinitionHandler(cacheProvider, mock(ParticipantMessagePublisher.class), listener);
107 ach.handleParticipantRestart(participantRestartMsg);
108 verify(cacheProvider).initializeAutomationComposition(any(UUID.class), any());
109 verify(cacheProvider).addElementDefinition(any(), any());