6cad99fa10e1e637841a2ac232c45808b04e0a22
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 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.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import java.util.List;
32 import java.util.UUID;
33 import org.junit.jupiter.api.Test;
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.ParticipantDefinition;
37 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
38 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeploy;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionStateChange;
40 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantAckMessage;
41 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregister;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantDeregisterAck;
43 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessage;
44 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
45 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrime;
46 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrimeAck;
47 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegister;
48 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantRegisterAck;
49 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
50 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatusReq;
51 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.PropertiesUpdate;
52
53 class ParticipantHandlerTest {
54
55     @Test
56     void handleParticipantStatusReqTest() {
57         var publisher = mock(ParticipantMessagePublisher.class);
58         var cacheProvider = mock(CacheProvider.class);
59         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
60                 mock(AutomationCompositionOutHandler.class), publisher, cacheProvider);
61         participantHandler.handleParticipantStatusReq(new ParticipantStatusReq());
62         verify(publisher).sendParticipantStatus(any(ParticipantStatus.class));
63     }
64
65     @Test
66     void handleAutomationCompositionDeployTest() {
67         var acHandler = mock(AutomationCompositionHandler.class);
68         var participantHandler = new ParticipantHandler(acHandler, mock(AutomationCompositionOutHandler.class),
69                 mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
70         var automationCompositionDeploy = new AutomationCompositionDeploy();
71         participantHandler.handleAutomationCompositionDeploy(automationCompositionDeploy);
72         verify(acHandler).handleAutomationCompositionDeploy(automationCompositionDeploy);
73     }
74
75     @Test
76     void handleAutomationCompositionStateChangeTest() {
77         var acHandler = mock(AutomationCompositionHandler.class);
78         var participantHandler = new ParticipantHandler(acHandler, mock(AutomationCompositionOutHandler.class),
79                 mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
80         var acStateChange = new AutomationCompositionStateChange();
81         participantHandler.handleAutomationCompositionStateChange(acStateChange);
82         verify(acHandler).handleAutomationCompositionStateChange(acStateChange);
83     }
84
85     @Test
86     void handleAcPropertyUpdateTest() {
87         var acHandler = mock(AutomationCompositionHandler.class);
88         var participantHandler = new ParticipantHandler(acHandler, mock(AutomationCompositionOutHandler.class),
89                 mock(ParticipantMessagePublisher.class), mock(CacheProvider.class));
90         var propertyUpdateMsg = new PropertiesUpdate();
91         participantHandler.handleAcPropertyUpdate(propertyUpdateMsg);
92         verify(acHandler).handleAcPropertyUpdate(propertyUpdateMsg);
93     }
94
95     @Test
96     void appliesToTest() {
97         var cacheProvider = mock(CacheProvider.class);
98         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
99         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
100                 mock(AutomationCompositionOutHandler.class), mock(ParticipantMessagePublisher.class), cacheProvider);
101
102         var participantAckMsg = new ParticipantAckMessage(ParticipantMessageType.AUTOMATION_COMPOSITION_DEPLOY);
103         assertTrue(participantHandler.appliesTo(participantAckMsg));
104
105         var participantMsg = new ParticipantMessage(ParticipantMessageType.PARTICIPANT_STATUS);
106         assertTrue(participantHandler.appliesTo(participantMsg));
107
108         participantMsg.setParticipantId(UUID.randomUUID());
109         assertFalse(participantHandler.appliesTo(participantMsg));
110     }
111
112     @Test
113     void sendParticipantRegister() {
114         var publisher = mock(ParticipantMessagePublisher.class);
115         var cacheProvider = mock(CacheProvider.class);
116         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
117         when(cacheProvider.getSupportedAcElementTypes()).thenReturn(List.of(new ParticipantSupportedElementType()));
118         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
119                 mock(AutomationCompositionOutHandler.class), publisher, cacheProvider);
120
121         participantHandler.sendParticipantRegister();
122         verify(publisher).sendParticipantRegister(any(ParticipantRegister.class));
123     }
124
125     @Test
126     void handleParticipantRegisterAckTest() {
127         var publisher = mock(ParticipantMessagePublisher.class);
128         var cacheProvider = mock(CacheProvider.class);
129         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
130         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
131                 mock(AutomationCompositionOutHandler.class), publisher, cacheProvider);
132
133         participantHandler.handleParticipantRegisterAck(new ParticipantRegisterAck());
134         verify(publisher).sendParticipantStatus(any(ParticipantStatus.class));
135     }
136
137     @Test
138     void sendParticipantDeregisterTest() {
139         var publisher = mock(ParticipantMessagePublisher.class);
140         var cacheProvider = mock(CacheProvider.class);
141         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
142         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
143                 mock(AutomationCompositionOutHandler.class), publisher, cacheProvider);
144
145         participantHandler.sendParticipantDeregister();
146         verify(publisher).sendParticipantDeregister(any(ParticipantDeregister.class));
147     }
148
149     @Test
150     void handleParticipantDeregisterAckTest() {
151         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
152                 mock(AutomationCompositionOutHandler.class), mock(ParticipantMessagePublisher.class),
153                 mock(CacheProvider.class));
154         var participantDeregisterAck = new ParticipantDeregisterAck();
155         assertDoesNotThrow(() -> participantHandler.handleParticipantDeregisterAck(participantDeregisterAck));
156     }
157
158     @Test
159     void handleParticipantPrimeTest() {
160         var cacheProvider = mock(CacheProvider.class);
161         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
162         var publisher = mock(ParticipantMessagePublisher.class);
163         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
164                 mock(AutomationCompositionOutHandler.class), publisher, cacheProvider);
165
166         var participantPrime = new ParticipantPrime();
167         participantPrime.setCompositionId(UUID.randomUUID());
168         participantPrime.setParticipantDefinitionUpdates(List.of(createParticipantDefinition()));
169         participantHandler.handleParticipantPrime(participantPrime);
170         verify(cacheProvider).addElementDefinition(any(), any());
171         verify(publisher).sendParticipantPrimeAck(any(ParticipantPrimeAck.class));
172     }
173
174     @Test
175     void handleParticipantDeprimeTest() {
176         var cacheProvider = mock(CacheProvider.class);
177         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
178         var publisher = mock(ParticipantMessagePublisher.class);
179         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
180                 mock(AutomationCompositionOutHandler.class), publisher, cacheProvider);
181         var participantPrime = new ParticipantPrime();
182         var compositionId = UUID.randomUUID();
183         participantPrime.setCompositionId(compositionId);
184         participantHandler.handleParticipantPrime(participantPrime);
185         verify(cacheProvider).removeElementDefinition(compositionId);
186         verify(publisher).sendParticipantPrimeAck(any(ParticipantPrimeAck.class));
187     }
188
189     @Test
190     void sendHeartbeatTest() {
191         var cacheProvider = mock(CacheProvider.class);
192         when(cacheProvider.getParticipantId()).thenReturn(CommonTestData.getParticipantId());
193         when(cacheProvider.getAutomationCompositions()).thenReturn(CommonTestData.getTestAutomationCompositionMap());
194         var publisher = mock(ParticipantMessagePublisher.class);
195         var participantHandler = new ParticipantHandler(mock(AutomationCompositionHandler.class),
196                 mock(AutomationCompositionOutHandler.class), publisher, cacheProvider);
197         participantHandler.sendHeartbeat();
198         verify(publisher).sendHeartbeat(any(ParticipantStatus.class));
199     }
200
201     private ParticipantDefinition createParticipantDefinition() {
202         var def = new ParticipantDefinition();
203         def.setParticipantId(CommonTestData.getParticipantId());
204         return def;
205     }
206 }