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
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.models.acm.persistence.provider;
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;
33 import java.util.ArrayList;
34 import java.util.List;
36 import java.util.Optional;
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;
63 class ParticipantProviderTest {
65 private static final Coder CODER = new StandardCoder();
66 private static final String PARTICIPANT_JSON = "src/test/resources/providers/TestParticipant.json";
68 private static final String AUTOMATION_COMPOSITION_JSON =
69 "src/test/resources/providers/TestAutomationCompositions.json";
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();
75 private final List<Participant> inputParticipants = new ArrayList<>();
76 private List<JpaParticipant> jpaParticipantList;
77 private List<JpaAutomationComposition> inputAutomationCompositionsJpa;
79 private final List<NodeTemplateState> nodeTemplateStateList = new ArrayList<>();
80 private List<JpaNodeTemplateState> jpaNodeTemplateStateList;
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");
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");
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");
102 void testParticipantSave() {
103 var participantRepository = mock(ParticipantRepository.class);
104 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
105 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
107 var participantProvider = new ParticipantProvider(participantRepository,
108 automationCompositionElementRepository, nodeTemplateStateRepository,
109 mock(ParticipantReplicaRepository.class));
111 assertThatThrownBy(() -> participantProvider.saveParticipant(null)).hasMessageMatching(LIST_IS_NULL);
113 when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0));
115 var savedParticipant = participantProvider.saveParticipant(inputParticipants.get(0));
116 savedParticipant.setParticipantId(inputParticipants.get(0).getParticipantId());
118 assertThat(savedParticipant).usingRecursiveComparison().isEqualTo(inputParticipants.get(0));
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));
130 assertThat(participantProvider.findParticipant(INVALID_ID)).isEmpty();
132 when(participantRepository.findAll()).thenReturn(jpaParticipantList);
133 assertThat(participantProvider.getParticipants()).hasSize(inputParticipants.size());
135 assertThatThrownBy(() -> participantProvider.getParticipantById(inputParticipants.get(0).getParticipantId()))
136 .hasMessageMatching("Participant Not Found with ID: " + inputParticipants.get(0).getParticipantId());
138 when(participantRepository.findById(any())).thenReturn(Optional.ofNullable(jpaParticipantList.get(0)));
140 var participant = participantProvider.getParticipantById(inputParticipants.get(0).getParticipantId());
142 assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(participant);
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));
154 assertThatThrownBy(() -> participantProvider.getParticipantById(INVALID_ID)).isInstanceOf(
155 PfModelRuntimeException.class).hasMessageMatching("Participant Not Found with ID:.*.");
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));
167 var participantId = inputParticipants.get(0).getParticipantId();
168 assertThatThrownBy(() -> participantProvider.deleteParticipant(participantId))
169 .hasMessageMatching(".*.failed, participant does not exist");
171 when(participantRepository.findById(participantId.toString()))
172 .thenReturn(Optional.of(jpaParticipantList.get(0)));
174 var deletedParticipant = participantProvider.deleteParticipant(participantId);
175 assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(deletedParticipant);
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));
187 var acElementList = inputAutomationCompositionsJpa.get(0).getElements();
189 var participantId = UUID.randomUUID();
190 var pageable = PageRequest.of(0, 5);
191 when(automationCompositionElementRepository.findByParticipantId(participantId.toString(), pageable))
192 .thenReturn(acElementList);
194 var listOfAcElements = participantProvider.getAutomationCompositionElements(participantId, pageable);
196 assertThat(listOfAcElements).hasSameSizeAs(acElementList);
197 assertEquals(UUID.fromString(acElementList.get(0).getElementId()), listOfAcElements.get(0).getId());
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);
210 var participantProvider = new ParticipantProvider(participantRepository,
211 automationCompositionElementRepository, nodeTemplateStateRepository,
212 mock(ParticipantReplicaRepository.class));
214 var listOfNodeTemplateState =
215 participantProvider.getAcNodeTemplateStates(UUID.fromString(participantId), pageable);
217 assertEquals(listOfNodeTemplateState, nodeTemplateStateList);
221 void testNotNullExceptions() {
222 var participantRepository = mock(ParticipantRepository.class);
223 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
224 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
226 var participantProvider = new ParticipantProvider(participantRepository,
227 automationCompositionElementRepository, nodeTemplateStateRepository,
228 mock(ParticipantReplicaRepository.class));
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));
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));
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));
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));
266 var result = participantProvider.getSupportedElementMap();
267 assertThat(result).hasSize(2);
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);
279 var participantProvider = new ParticipantProvider(participantRepository,
280 automationCompositionElementRepository, nodeTemplateStateRepository,
281 mock(ParticipantReplicaRepository.class));
283 assertThatThrownBy(() -> participantProvider.getCompositionIds(null)).hasMessageMatching(LIST_IS_NULL);
285 var result = participantProvider.getCompositionIds(participantId);
286 assertThat(result).hasSize(1);
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),
299 var result = participantProvider.findParticipantReplica(replica.getReplicaId());
300 assertThat(result).isNotEmpty();
301 assertEquals(replica.getReplicaId(), result.get().getReplicaId());
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),
315 var result = participantProvider.findReplicasOnLine();
316 assertThat(result).hasSize(1);
317 assertEquals(replica.getReplicaId(), result.get(0).getReplicaId());
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),
329 var replica = inputParticipants.get(0).getReplicas().values().iterator().next();
330 participantProvider.saveParticipantReplica(replica);
331 verify(replicaRepository).save(any());
335 void testDeleteParticipantReplica() {
336 var replicaRepository = mock(ParticipantReplicaRepository.class);
337 var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
338 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
340 participantProvider.deleteParticipantReplica(CommonTestData.getReplicaId());
341 verify(replicaRepository).deleteById(CommonTestData.getReplicaId().toString());
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);
351 var replicaRepository = mock(ParticipantReplicaRepository.class);
352 var participantProvider = new ParticipantProvider(participantRepository,
353 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
356 jpaParticipant.setReplicas(List.of());
357 var set = Set.of(UUID.fromString(participantId));
358 assertThatThrownBy(() -> participantProvider.verifyParticipantState(set))
359 .hasMessageMatching("Participant: " + participantId + " is OFFLINE");
361 when(participantRepository.getReferenceById(participantId)).thenReturn(jpaParticipantList.get(0));
362 participantProvider.verifyParticipantState(set);
363 verify(participantRepository, times(2)).getReferenceById(participantId);
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);
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));
379 var replicaRepository = mock(ParticipantReplicaRepository.class);
380 var participantProvider = new ParticipantProvider(participantRepository,
381 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
383 participantProvider.checkRegisteredParticipant(acDefinition);
384 verify(participantRepository).getReferenceById(participantId);