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;
35 import java.util.Optional;
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;
60 class ParticipantProviderTest {
62 private static final Coder CODER = new StandardCoder();
63 private static final String PARTICIPANT_JSON = "src/test/resources/providers/TestParticipant.json";
65 private static final String AUTOMATION_COMPOSITION_JSON =
66 "src/test/resources/providers/TestAutomationCompositions.json";
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();
72 private final List<Participant> inputParticipants = new ArrayList<>();
73 private List<JpaParticipant> jpaParticipantList;
74 private List<JpaAutomationComposition> inputAutomationCompositionsJpa;
76 private final List<NodeTemplateState> nodeTemplateStateList = new ArrayList<>();
77 private List<JpaNodeTemplateState> jpaNodeTemplateStateList;
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");
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");
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");
99 void testParticipantSave() {
100 var participantRepository = mock(ParticipantRepository.class);
101 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
102 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
104 var participantProvider = new ParticipantProvider(participantRepository,
105 automationCompositionElementRepository, nodeTemplateStateRepository,
106 mock(ParticipantReplicaRepository.class));
108 assertThatThrownBy(() -> participantProvider.saveParticipant(null)).hasMessageMatching(LIST_IS_NULL);
110 when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0));
112 var savedParticipant = participantProvider.saveParticipant(inputParticipants.get(0));
113 savedParticipant.setParticipantId(inputParticipants.get(0).getParticipantId());
115 assertThat(savedParticipant).usingRecursiveComparison().isEqualTo(inputParticipants.get(0));
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));
127 assertThat(participantProvider.findParticipant(INVALID_ID)).isEmpty();
129 when(participantRepository.findAll()).thenReturn(jpaParticipantList);
130 assertThat(participantProvider.getParticipants()).hasSize(inputParticipants.size());
132 assertThatThrownBy(() -> participantProvider.getParticipantById(inputParticipants.get(0).getParticipantId()))
133 .hasMessageMatching("Participant Not Found with ID: " + inputParticipants.get(0).getParticipantId());
135 when(participantRepository.findById(any())).thenReturn(Optional.ofNullable(jpaParticipantList.get(0)));
137 var participant = participantProvider.getParticipantById(inputParticipants.get(0).getParticipantId());
139 assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(participant);
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));
151 assertThatThrownBy(() -> participantProvider.getParticipantById(INVALID_ID)).isInstanceOf(
152 PfModelRuntimeException.class).hasMessageMatching("Participant Not Found with ID:.*.");
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));
164 var participantId = inputParticipants.get(0).getParticipantId();
165 assertThatThrownBy(() -> participantProvider.deleteParticipant(participantId))
166 .hasMessageMatching(".*.failed, participant does not exist");
168 when(participantRepository.findById(participantId.toString()))
169 .thenReturn(Optional.of(jpaParticipantList.get(0)));
171 var deletedParticipant = participantProvider.deleteParticipant(participantId);
172 assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(deletedParticipant);
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));
184 var acElementList = inputAutomationCompositionsJpa.get(0).getElements();
186 var participantId = UUID.randomUUID();
187 var pageable = PageRequest.of(0, 5);
188 when(automationCompositionElementRepository.findByParticipantId(participantId.toString(), pageable))
189 .thenReturn(acElementList);
191 var listOfAcElements = participantProvider.getAutomationCompositionElements(participantId, pageable);
193 assertThat(listOfAcElements).hasSameSizeAs(acElementList);
194 assertEquals(UUID.fromString(acElementList.get(0).getElementId()), listOfAcElements.get(0).getId());
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);
207 var participantProvider = new ParticipantProvider(participantRepository,
208 automationCompositionElementRepository, nodeTemplateStateRepository,
209 mock(ParticipantReplicaRepository.class));
211 var listOfNodeTemplateState =
212 participantProvider.getAcNodeTemplateStates(UUID.fromString(participantId), pageable);
214 assertEquals(listOfNodeTemplateState, nodeTemplateStateList);
218 void testNotNullExceptions() {
219 var participantRepository = mock(ParticipantRepository.class);
220 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
221 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
223 var participantProvider = new ParticipantProvider(participantRepository,
224 automationCompositionElementRepository, nodeTemplateStateRepository,
225 mock(ParticipantReplicaRepository.class));
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));
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));
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));
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));
263 var result = participantProvider.getSupportedElementMap();
264 assertThat(result).hasSize(2);
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);
276 var participantProvider = new ParticipantProvider(participantRepository,
277 automationCompositionElementRepository, nodeTemplateStateRepository,
278 mock(ParticipantReplicaRepository.class));
280 assertThatThrownBy(() -> participantProvider.getCompositionIds(null)).hasMessageMatching(LIST_IS_NULL);
282 var result = participantProvider.getCompositionIds(participantId);
283 assertThat(result).hasSize(1);
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),
296 var result = participantProvider.findParticipantReplica(replica.getReplicaId());
297 assertThat(result).isNotEmpty();
298 assertEquals(replica.getReplicaId(), result.get().getReplicaId());
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),
312 var result = participantProvider.findReplicasOnLine();
313 assertThat(result).hasSize(1);
314 assertEquals(replica.getReplicaId(), result.get(0).getReplicaId());
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),
326 var replica = inputParticipants.get(0).getReplicas().values().iterator().next();
327 participantProvider.saveParticipantReplica(replica);
328 verify(replicaRepository).save(any());
332 void testDeleteParticipantReplica() {
333 var replicaRepository = mock(ParticipantReplicaRepository.class);
334 var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
335 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
337 participantProvider.deleteParticipantReplica(CommonTestData.getReplicaId());
338 verify(replicaRepository).deleteById(CommonTestData.getReplicaId().toString());
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);
348 var replicaRepository = mock(ParticipantReplicaRepository.class);
349 var participantProvider = new ParticipantProvider(participantRepository,
350 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
353 jpaParticipant.setReplicas(List.of());
354 var set = Set.of(UUID.fromString(participantId));
355 assertThatThrownBy(() -> participantProvider.verifyParticipantState(set))
356 .hasMessageMatching("Participant: " + participantId + " is OFFLINE");
358 when(participantRepository.getReferenceById(participantId)).thenReturn(jpaParticipantList.get(0));
359 participantProvider.verifyParticipantState(set);
360 verify(participantRepository, times(2)).getReferenceById(participantId);