54aa8be8dff6eb6003cfc69d2248499e1b3dd238
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-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
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.models.acm.persistence.provider;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertThrows;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Optional;
37 import java.util.Set;
38 import java.util.UUID;
39 import org.junit.jupiter.api.BeforeEach;
40 import org.junit.jupiter.api.Test;
41 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
43 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
44 import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState;
45 import org.onap.policy.clamp.models.acm.concepts.Participant;
46 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
47 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationComposition;
48 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaNodeTemplateState;
49 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaParticipant;
50 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionElementRepository;
51 import org.onap.policy.clamp.models.acm.persistence.repository.NodeTemplateStateRepository;
52 import org.onap.policy.clamp.models.acm.persistence.repository.ParticipantReplicaRepository;
53 import org.onap.policy.clamp.models.acm.persistence.repository.ParticipantRepository;
54 import org.onap.policy.clamp.models.acm.utils.CommonTestData;
55 import org.onap.policy.common.utils.coder.Coder;
56 import org.onap.policy.common.utils.coder.StandardCoder;
57 import org.onap.policy.common.utils.resources.ResourceUtils;
58 import org.onap.policy.models.base.PfModelRuntimeException;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
60 import org.springframework.data.domain.PageRequest;
61 import org.springframework.data.domain.Pageable;
62
63 class ParticipantProviderTest {
64
65     private static final Coder CODER = new StandardCoder();
66     private static final String PARTICIPANT_JSON = "src/test/resources/providers/TestParticipant.json";
67
68     private static final String AUTOMATION_COMPOSITION_JSON =
69         "src/test/resources/providers/TestAutomationCompositions.json";
70
71     private static final String NODE_TEMPLATE_STATE_JSON = "src/test/resources/providers/NodeTemplateState.json";
72     private static final String LIST_IS_NULL = ".*. is marked .*ull but is null";
73     private static final UUID INVALID_ID = UUID.randomUUID();
74
75     private final List<Participant> inputParticipants = new ArrayList<>();
76     private List<JpaParticipant> jpaParticipantList;
77     private List<JpaAutomationComposition> inputAutomationCompositionsJpa;
78
79     private final List<NodeTemplateState> nodeTemplateStateList = new ArrayList<>();
80     private List<JpaNodeTemplateState> jpaNodeTemplateStateList;
81
82     @BeforeEach
83     void beforeSetup() throws Exception {
84         var originalJson = ResourceUtils.getResourceAsString(PARTICIPANT_JSON);
85         inputParticipants.add(CODER.decode(originalJson, Participant.class));
86         jpaParticipantList = ProviderUtils.getJpaAndValidateList(inputParticipants, JpaParticipant::new, "participant");
87
88         var originalAcJson = ResourceUtils.getResourceAsString(AUTOMATION_COMPOSITION_JSON);
89         var inputAutomationCompositions = CODER.decode(originalAcJson, AutomationCompositions.class);
90         inputAutomationCompositionsJpa =
91             ProviderUtils.getJpaAndValidateList(inputAutomationCompositions.getAutomationCompositionList(),
92                 JpaAutomationComposition::new, "automation compositions");
93
94         var nodeTemplateStatesJson = ResourceUtils.getResourceAsString(NODE_TEMPLATE_STATE_JSON);
95         nodeTemplateStateList.add(CODER.decode(nodeTemplateStatesJson, NodeTemplateState.class));
96         nodeTemplateStateList.get(0).setState(AcTypeState.COMMISSIONED);
97         jpaNodeTemplateStateList = ProviderUtils.getJpaAndValidateList(nodeTemplateStateList,
98             JpaNodeTemplateState::new, "node template state");
99     }
100
101     @Test
102     void testParticipantSave() {
103         var participantRepository = mock(ParticipantRepository.class);
104         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
105         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
106
107         var participantProvider = new ParticipantProvider(participantRepository,
108             automationCompositionElementRepository, nodeTemplateStateRepository,
109             mock(ParticipantReplicaRepository.class));
110
111         assertThatThrownBy(() -> participantProvider.saveParticipant(null)).hasMessageMatching(LIST_IS_NULL);
112
113         when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0));
114
115         var savedParticipant = participantProvider.saveParticipant(inputParticipants.get(0));
116         savedParticipant.setParticipantId(inputParticipants.get(0).getParticipantId());
117
118         assertThat(savedParticipant).usingRecursiveComparison().isEqualTo(inputParticipants.get(0));
119     }
120
121     @Test
122     void testGetAutomationCompositions() {
123         var participantRepository = mock(ParticipantRepository.class);
124         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
125         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
126         var participantProvider = new ParticipantProvider(participantRepository,
127             automationCompositionElementRepository, nodeTemplateStateRepository,
128             mock(ParticipantReplicaRepository.class));
129
130         assertThat(participantProvider.findParticipant(INVALID_ID)).isEmpty();
131
132         when(participantRepository.findAll()).thenReturn(jpaParticipantList);
133         assertThat(participantProvider.getParticipants()).hasSize(inputParticipants.size());
134
135         assertThatThrownBy(() -> participantProvider.getParticipantById(inputParticipants.get(0).getParticipantId()))
136                 .hasMessageMatching("Participant Not Found with ID: " + inputParticipants.get(0).getParticipantId());
137
138         when(participantRepository.findById(any())).thenReturn(Optional.ofNullable(jpaParticipantList.get(0)));
139
140         var participant = participantProvider.getParticipantById(inputParticipants.get(0).getParticipantId());
141
142         assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(participant);
143     }
144
145     @Test
146     void testEmptyParticipant() {
147         var participantRepository = mock(ParticipantRepository.class);
148         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
149         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
150         var participantProvider = new ParticipantProvider(participantRepository,
151             automationCompositionElementRepository, nodeTemplateStateRepository,
152             mock(ParticipantReplicaRepository.class));
153
154         assertThatThrownBy(() -> participantProvider.getParticipantById(INVALID_ID)).isInstanceOf(
155             PfModelRuntimeException.class).hasMessageMatching("Participant Not Found with ID:.*.");
156     }
157
158     @Test
159     void testDeleteParticipant() {
160         var participantRepository = mock(ParticipantRepository.class);
161         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
162         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
163         var participantProvider = new ParticipantProvider(participantRepository,
164             automationCompositionElementRepository, nodeTemplateStateRepository,
165             mock(ParticipantReplicaRepository.class));
166
167         var participantId = inputParticipants.get(0).getParticipantId();
168         assertThatThrownBy(() -> participantProvider.deleteParticipant(participantId))
169             .hasMessageMatching(".*.failed, participant does not exist");
170
171         when(participantRepository.findById(participantId.toString()))
172             .thenReturn(Optional.of(jpaParticipantList.get(0)));
173
174         var deletedParticipant = participantProvider.deleteParticipant(participantId);
175         assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(deletedParticipant);
176     }
177
178     @Test
179     void testGetAutomationCompositionElements() {
180         var participantRepository = mock(ParticipantRepository.class);
181         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
182         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
183         var participantProvider = new ParticipantProvider(participantRepository,
184             automationCompositionElementRepository, nodeTemplateStateRepository,
185             mock(ParticipantReplicaRepository.class));
186
187         var acElementList = inputAutomationCompositionsJpa.get(0).getElements();
188
189         var participantId = UUID.randomUUID();
190         var pageable = PageRequest.of(0, 5);
191         when(automationCompositionElementRepository.findByParticipantId(participantId.toString(), pageable))
192                 .thenReturn(acElementList);
193
194         var listOfAcElements = participantProvider.getAutomationCompositionElements(participantId, pageable);
195
196         assertThat(listOfAcElements).hasSameSizeAs(acElementList);
197         assertEquals(UUID.fromString(acElementList.get(0).getElementId()), listOfAcElements.get(0).getId());
198     }
199
200     @Test
201     void testGetAcNodeTemplateState() {
202         var participantRepository = mock(ParticipantRepository.class);
203         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
204         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
205         var participantId = jpaParticipantList.get(0).getParticipantId();
206         var pageable = PageRequest.of(0, 5);
207         when(nodeTemplateStateRepository
208             .findByParticipantId(participantId, pageable)).thenReturn(jpaNodeTemplateStateList);
209
210         var participantProvider = new ParticipantProvider(participantRepository,
211             automationCompositionElementRepository, nodeTemplateStateRepository,
212             mock(ParticipantReplicaRepository.class));
213
214         var listOfNodeTemplateState =
215             participantProvider.getAcNodeTemplateStates(UUID.fromString(participantId), pageable);
216
217         assertEquals(listOfNodeTemplateState, nodeTemplateStateList);
218     }
219
220     @Test
221     void testNotNullExceptions() {
222         var participantRepository = mock(ParticipantRepository.class);
223         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
224         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
225
226         var participantProvider = new ParticipantProvider(participantRepository,
227             automationCompositionElementRepository, nodeTemplateStateRepository,
228             mock(ParticipantReplicaRepository.class));
229
230         assertThrows(NullPointerException.class, () -> participantProvider.getParticipantById(null));
231         assertThrows(NullPointerException.class, () -> participantProvider.findParticipant(null));
232         assertThrows(NullPointerException.class, () -> participantProvider.saveParticipant(null));
233         assertThrows(NullPointerException.class, () -> participantProvider.deleteParticipant(null));
234
235         var pageable = Pageable.unpaged();
236         assertThrows(NullPointerException.class, () ->
237                 participantProvider.getAutomationCompositionElements(null, pageable));
238         var participantId = UUID.randomUUID();
239         assertThrows(NullPointerException.class, () ->
240                 participantProvider.getAutomationCompositionElements(participantId, null));
241         assertThrows(NullPointerException.class, () ->
242                 participantProvider.getAcNodeTemplateStates(null, pageable));
243         assertThrows(NullPointerException.class, () ->
244                 participantProvider.getAcNodeTemplateStates(participantId, null));
245
246         assertThrows(NullPointerException.class, () -> participantProvider.findParticipantReplica(null));
247         assertThrows(NullPointerException.class, () -> participantProvider.saveParticipantReplica(null));
248         assertThrows(NullPointerException.class, () -> participantProvider.deleteParticipantReplica(null));
249         assertThrows(NullPointerException.class, () ->
250                 participantProvider.getAutomationCompositionElements(null, pageable));
251         assertThrows(NullPointerException.class, () ->
252                 participantProvider.getAutomationCompositionElements(participantId, null));
253         assertThrows(NullPointerException.class, () -> participantProvider.getAcNodeTemplateStates(null, null));
254     }
255
256     @Test
257     void testGetSupportedElementMap() {
258         var participantRepository = mock(ParticipantRepository.class);
259         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
260         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
261         when(participantRepository.findAll()).thenReturn(jpaParticipantList);
262         var participantProvider = new ParticipantProvider(participantRepository,
263             automationCompositionElementRepository, nodeTemplateStateRepository,
264             mock(ParticipantReplicaRepository.class));
265
266         var result = participantProvider.getSupportedElementMap();
267         assertThat(result).hasSize(2);
268     }
269
270     @Test
271     void testGetCompositionIds() {
272         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
273         var participantId = UUID.randomUUID();
274         when(nodeTemplateStateRepository.findByParticipantId(participantId.toString()))
275                 .thenReturn(jpaNodeTemplateStateList);
276         var participantRepository = mock(ParticipantRepository.class);
277         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
278
279         var participantProvider = new ParticipantProvider(participantRepository,
280             automationCompositionElementRepository, nodeTemplateStateRepository,
281             mock(ParticipantReplicaRepository.class));
282
283         assertThatThrownBy(() -> participantProvider.getCompositionIds(null)).hasMessageMatching(LIST_IS_NULL);
284
285         var result = participantProvider.getCompositionIds(participantId);
286         assertThat(result).hasSize(1);
287     }
288
289     @Test
290     void testFindParticipantReplica() {
291         var replicaRepository = mock(ParticipantReplicaRepository.class);
292         var replica = inputParticipants.get(0).getReplicas().values().iterator().next();
293         var jpaReplica = jpaParticipantList.get(0).getReplicas().get(0);
294         when(replicaRepository.findById(replica.getReplicaId().toString())).thenReturn(Optional.of(jpaReplica));
295         var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
296                 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
297                 replicaRepository);
298
299         var result = participantProvider.findParticipantReplica(replica.getReplicaId());
300         assertThat(result).isNotEmpty();
301         assertEquals(replica.getReplicaId(), result.get().getReplicaId());
302     }
303
304     @Test
305     void testFindReplicasOnLine() {
306         var replicaRepository = mock(ParticipantReplicaRepository.class);
307         var replica = inputParticipants.get(0).getReplicas().values().iterator().next();
308         var jpaReplica = jpaParticipantList.get(0).getReplicas().get(0);
309         jpaReplica.fromAuthorative(replica);
310         when(replicaRepository.findByParticipantState(ParticipantState.ON_LINE)).thenReturn(List.of(jpaReplica));
311         var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
312                 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
313                 replicaRepository);
314
315         var result = participantProvider.findReplicasOnLine();
316         assertThat(result).hasSize(1);
317         assertEquals(replica.getReplicaId(), result.get(0).getReplicaId());
318     }
319
320     @Test
321     void testSaveParticipantReplica() {
322         var jpaReplica = jpaParticipantList.get(0).getReplicas().get(0);
323         var replicaRepository = mock(ParticipantReplicaRepository.class);
324         when(replicaRepository.getReferenceById(jpaReplica.getReplicaId())).thenReturn(jpaReplica);
325         var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
326                 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
327                 replicaRepository);
328
329         var replica = inputParticipants.get(0).getReplicas().values().iterator().next();
330         participantProvider.saveParticipantReplica(replica);
331         verify(replicaRepository).save(any());
332     }
333
334     @Test
335     void testDeleteParticipantReplica() {
336         var replicaRepository = mock(ParticipantReplicaRepository.class);
337         var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
338                 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
339                 replicaRepository);
340         participantProvider.deleteParticipantReplica(CommonTestData.getReplicaId());
341         verify(replicaRepository).deleteById(CommonTestData.getReplicaId().toString());
342     }
343
344     @Test
345     void testVerifyParticipantState() {
346         var jpaParticipant = new JpaParticipant(jpaParticipantList.get(0));
347         var participantId = jpaParticipant.getParticipantId();
348         var participantRepository = mock(ParticipantRepository.class);
349         when(participantRepository.getReferenceById(participantId)).thenReturn(jpaParticipant);
350
351         var replicaRepository = mock(ParticipantReplicaRepository.class);
352         var participantProvider = new ParticipantProvider(participantRepository,
353                 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
354                 replicaRepository);
355
356         jpaParticipant.setReplicas(List.of());
357         var set = Set.of(UUID.fromString(participantId));
358         assertThatThrownBy(() -> participantProvider.verifyParticipantState(set))
359                 .hasMessageMatching("Participant: " + participantId + " is OFFLINE");
360
361         when(participantRepository.getReferenceById(participantId)).thenReturn(jpaParticipantList.get(0));
362         participantProvider.verifyParticipantState(set);
363         verify(participantRepository, times(2)).getReferenceById(participantId);
364     }
365
366     @Test
367     void testCheckRegisteredParticipant() {
368         var jpaParticipant = new JpaParticipant(jpaParticipantList.get(0));
369         var participantId = jpaParticipant.getParticipantId();
370         var participantRepository = mock(ParticipantRepository.class);
371         when(participantRepository.getReferenceById(participantId)).thenReturn(jpaParticipant);
372
373         var acDefinition = new AutomationCompositionDefinition();
374         var nodeTemplateState = new NodeTemplateState();
375         nodeTemplateState.setNodeTemplateId(new ToscaConceptIdentifier("name", "0.0.0"));
376         nodeTemplateState.setParticipantId(UUID.fromString(participantId));
377         acDefinition.setElementStateMap(Map.of(nodeTemplateState.getNodeTemplateId().getName(), nodeTemplateState));
378
379         var replicaRepository = mock(ParticipantReplicaRepository.class);
380         var participantProvider = new ParticipantProvider(participantRepository,
381                 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
382                 replicaRepository);
383         participantProvider.checkRegisteredParticipant(acDefinition);
384         verify(participantRepository).getReferenceById(participantId);
385     }
386 }