2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved.
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.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.timeout;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
30 import java.util.UUID;
31 import org.junit.jupiter.api.Test;
32 import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
33 import org.onap.policy.clamp.acm.participant.intermediary.handler.cache.AutomationCompositionMsg;
34 import org.onap.policy.clamp.acm.participant.intermediary.handler.cache.CacheProvider;
35 import org.onap.policy.clamp.acm.participant.intermediary.main.parameters.CommonTestData;
36 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionDeploy;
37 import org.onap.policy.clamp.models.acm.messages.kafka.participant.AutomationCompositionStateChange;
39 class MsgExecutorTest {
43 var parameters = CommonTestData.getParticipantParameters();
44 var cacheProvider = new CacheProvider(parameters);
45 var publisher = mock(ParticipantMessagePublisher.class);
46 var msgExecutor = new MsgExecutor(cacheProvider, publisher);
47 var automationCompositionHandler = mock(AutomationCompositionHandler.class);
48 var updateMsg = new AutomationCompositionDeploy();
49 var acMsg = new AutomationCompositionMsg<>(
50 automationCompositionHandler::handleAutomationCompositionDeploy, updateMsg);
51 msgExecutor.execute(acMsg);
52 verify(automationCompositionHandler).handleAutomationCompositionDeploy(updateMsg);
56 void testExecuteCompositionOutdated() {
57 var parameters = CommonTestData.getParticipantParameters();
58 var cacheProvider = new CacheProvider(parameters);
59 var publisher = mock(ParticipantMessagePublisher.class);
60 var msgExecutor = new MsgExecutor(cacheProvider, publisher);
61 var automationCompositionHandler = mock(AutomationCompositionHandler.class);
62 var updateMsg = new AutomationCompositionDeploy();
63 var acMsg = new AutomationCompositionMsg<>(
64 automationCompositionHandler::handleAutomationCompositionDeploy, updateMsg);
65 var compositionId = UUID.randomUUID();
66 acMsg.setCompositionId(compositionId);
67 var revisionIdComposition = UUID.randomUUID();
68 acMsg.setRevisionIdComposition(revisionIdComposition);
69 msgExecutor.execute(acMsg);
70 verify(automationCompositionHandler, times(0)).handleAutomationCompositionDeploy(updateMsg);
71 verify(publisher).sendParticipantReqSync(any());
72 assertThat(cacheProvider.getMessagesOnHold()).hasSize(1);
74 var automationComposition =
75 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
76 automationComposition.setInstanceId(UUID.randomUUID());
77 automationComposition.setCompositionId(compositionId);
79 CommonTestData.createAutomationCompositionElementDefinitionList(automationComposition);
80 cacheProvider.addElementDefinition(compositionId, definitions, revisionIdComposition);
82 verify(automationCompositionHandler, timeout(100)).handleAutomationCompositionDeploy(updateMsg);
83 assertThat(cacheProvider.getMessagesOnHold()).isEmpty();
87 void testCheckAndExecuteInstance() {
88 var automationCompositionHandler = mock(AutomationCompositionHandler.class);
89 var stateChangeMsg = new AutomationCompositionStateChange();
90 var acMsg = new AutomationCompositionMsg<>(
91 automationCompositionHandler::handleAutomationCompositionStateChange, stateChangeMsg);
92 var compositionId = UUID.randomUUID();
93 acMsg.setCompositionId(compositionId);
94 var revisionIdComposition = UUID.randomUUID();
95 acMsg.setRevisionIdComposition(revisionIdComposition);
96 var instanceId = UUID.randomUUID();
97 acMsg.setInstanceId(instanceId);
98 acMsg.setRevisionIdInstance(UUID.randomUUID());
100 var automationComposition =
101 CommonTestData.getTestAutomationCompositions().getAutomationCompositionList().get(0);
102 automationComposition.setInstanceId(instanceId);
103 automationComposition.setCompositionId(compositionId);
105 CommonTestData.createAutomationCompositionElementDefinitionList(automationComposition);
106 var parameters = CommonTestData.getParticipantParameters();
107 var cacheProvider = new CacheProvider(parameters);
108 cacheProvider.addElementDefinition(compositionId, definitions, revisionIdComposition);
110 var publisher = mock(ParticipantMessagePublisher.class);
111 var msgExecutor = new MsgExecutor(cacheProvider, publisher);
112 msgExecutor.execute(acMsg);
113 verify(automationCompositionHandler, times(0)).handleAutomationCompositionStateChange(stateChangeMsg);
114 verify(publisher).sendParticipantReqSync(any());
115 assertThat(cacheProvider.getMessagesOnHold()).hasSize(1);
117 var participantDeploy =
118 CommonTestData.createparticipantDeploy(cacheProvider.getParticipantId(), automationComposition);
119 cacheProvider.initializeAutomationComposition(compositionId, automationComposition.getInstanceId(),
120 participantDeploy, acMsg.getRevisionIdInstance());
122 verify(automationCompositionHandler, timeout(100)).handleAutomationCompositionStateChange(stateChangeMsg);
123 assertThat(cacheProvider.getMessagesOnHold()).isEmpty();