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