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