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 testGetAutomationCompositionElements() {
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 acElementList = inputAutomationCompositionsJpa.get(0).getElements();
169 var participantId = UUID.randomUUID();
170 var pageable = PageRequest.of(0, 5);
171 when(automationCompositionElementRepository.findByParticipantId(participantId.toString(), pageable))
172 .thenReturn(acElementList);
174 var listOfAcElements = participantProvider.getAutomationCompositionElements(participantId, pageable);
176 assertThat(listOfAcElements).hasSameSizeAs(acElementList);
177 assertEquals(UUID.fromString(acElementList.get(0).getElementId()), listOfAcElements.get(0).getId());
181 void testGetAcNodeTemplateState() {
182 var participantRepository = mock(ParticipantRepository.class);
183 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
184 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
185 var participantId = jpaParticipantList.get(0).getParticipantId();
186 var pageable = PageRequest.of(0, 5);
187 when(nodeTemplateStateRepository
188 .findByParticipantId(participantId, pageable)).thenReturn(jpaNodeTemplateStateList);
190 var participantProvider = new ParticipantProvider(participantRepository,
191 automationCompositionElementRepository, nodeTemplateStateRepository,
192 mock(ParticipantReplicaRepository.class));
194 var listOfNodeTemplateState =
195 participantProvider.getAcNodeTemplateStates(UUID.fromString(participantId), pageable);
197 assertEquals(listOfNodeTemplateState, nodeTemplateStateList);
201 void testNotNullExceptions() {
202 var participantRepository = mock(ParticipantRepository.class);
203 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
204 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
206 var participantProvider = new ParticipantProvider(participantRepository,
207 automationCompositionElementRepository, nodeTemplateStateRepository,
208 mock(ParticipantReplicaRepository.class));
210 assertThrows(NullPointerException.class, () -> participantProvider.getParticipantById(null));
211 assertThrows(NullPointerException.class, () -> participantProvider.findParticipant(null));
212 assertThrows(NullPointerException.class, () -> participantProvider.saveParticipant(null));
214 var pageable = Pageable.unpaged();
215 assertThrows(NullPointerException.class, () ->
216 participantProvider.getAutomationCompositionElements(null, pageable));
217 var participantId = UUID.randomUUID();
218 assertThrows(NullPointerException.class, () ->
219 participantProvider.getAutomationCompositionElements(participantId, null));
220 assertThrows(NullPointerException.class, () ->
221 participantProvider.getAcNodeTemplateStates(null, pageable));
222 assertThrows(NullPointerException.class, () ->
223 participantProvider.getAcNodeTemplateStates(participantId, null));
225 assertThrows(NullPointerException.class, () -> participantProvider.findParticipantReplica(null));
226 assertThrows(NullPointerException.class, () -> participantProvider.saveParticipantReplica(null));
227 assertThrows(NullPointerException.class, () -> participantProvider.deleteParticipantReplica(null));
228 assertThrows(NullPointerException.class, () ->
229 participantProvider.getAutomationCompositionElements(null, pageable));
230 assertThrows(NullPointerException.class, () ->
231 participantProvider.getAutomationCompositionElements(participantId, null));
232 assertThrows(NullPointerException.class, () -> participantProvider.getAcNodeTemplateStates(null, null));
236 void testGetSupportedElementMap() {
237 var participantRepository = mock(ParticipantRepository.class);
238 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
239 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
240 when(participantRepository.findAll()).thenReturn(jpaParticipantList);
241 var participantProvider = new ParticipantProvider(participantRepository,
242 automationCompositionElementRepository, nodeTemplateStateRepository,
243 mock(ParticipantReplicaRepository.class));
245 var result = participantProvider.getSupportedElementMap();
246 assertThat(result).hasSize(2);
250 void testGetCompositionIds() {
251 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
252 var participantId = UUID.randomUUID();
253 when(nodeTemplateStateRepository.findByParticipantId(participantId.toString()))
254 .thenReturn(jpaNodeTemplateStateList);
255 var participantRepository = mock(ParticipantRepository.class);
256 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
258 var participantProvider = new ParticipantProvider(participantRepository,
259 automationCompositionElementRepository, nodeTemplateStateRepository,
260 mock(ParticipantReplicaRepository.class));
262 assertThatThrownBy(() -> participantProvider.getCompositionIds(null)).hasMessageMatching(LIST_IS_NULL);
264 var result = participantProvider.getCompositionIds(participantId);
265 assertThat(result).hasSize(1);
269 void testFindParticipantReplica() {
270 var replicaRepository = mock(ParticipantReplicaRepository.class);
271 var replica = inputParticipants.get(0).getReplicas().values().iterator().next();
272 var jpaReplica = jpaParticipantList.get(0).getReplicas().get(0);
273 when(replicaRepository.findById(replica.getReplicaId().toString())).thenReturn(Optional.of(jpaReplica));
274 var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
275 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
278 var result = participantProvider.findParticipantReplica(replica.getReplicaId());
279 assertThat(result).isNotEmpty();
280 assertEquals(replica.getReplicaId(), result.get().getReplicaId());
284 void testFindReplicasOnLine() {
285 var replicaRepository = mock(ParticipantReplicaRepository.class);
286 var replica = inputParticipants.get(0).getReplicas().values().iterator().next();
287 var jpaReplica = jpaParticipantList.get(0).getReplicas().get(0);
288 jpaReplica.fromAuthorative(replica);
289 when(replicaRepository.findByParticipantState(ParticipantState.ON_LINE)).thenReturn(List.of(jpaReplica));
290 var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
291 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
294 var result = participantProvider.findReplicasOnLine();
295 assertThat(result).hasSize(1);
296 assertEquals(replica.getReplicaId(), result.get(0).getReplicaId());
300 void testSaveParticipantReplica() {
301 var jpaReplica = jpaParticipantList.get(0).getReplicas().get(0);
302 var replicaRepository = mock(ParticipantReplicaRepository.class);
303 when(replicaRepository.getReferenceById(jpaReplica.getReplicaId())).thenReturn(jpaReplica);
304 var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
305 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
308 var replica = inputParticipants.get(0).getReplicas().values().iterator().next();
309 participantProvider.saveParticipantReplica(replica);
310 verify(replicaRepository).save(any());
314 void testDeleteParticipantReplica() {
315 var replicaRepository = mock(ParticipantReplicaRepository.class);
316 var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
317 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
319 participantProvider.deleteParticipantReplica(CommonTestData.getReplicaId());
320 verify(replicaRepository).deleteById(CommonTestData.getReplicaId().toString());
324 void testVerifyParticipantState() {
325 var jpaParticipant = new JpaParticipant(jpaParticipantList.get(0));
326 var participantId = jpaParticipant.getParticipantId();
327 var participantRepository = mock(ParticipantRepository.class);
328 when(participantRepository.getReferenceById(participantId)).thenReturn(jpaParticipant);
330 var replicaRepository = mock(ParticipantReplicaRepository.class);
331 var participantProvider = new ParticipantProvider(participantRepository,
332 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
335 jpaParticipant.setReplicas(List.of());
336 var set = Set.of(UUID.fromString(participantId));
337 assertThatThrownBy(() -> participantProvider.verifyParticipantState(set))
338 .hasMessageMatching("Participant: " + participantId + " is OFFLINE");
340 when(participantRepository.getReferenceById(participantId)).thenReturn(jpaParticipantList.get(0));
341 participantProvider.verifyParticipantState(set);
342 verify(participantRepository, times(2)).getReferenceById(participantId);
346 void testCheckRegisteredParticipant() {
347 var jpaParticipant = new JpaParticipant(jpaParticipantList.get(0));
348 var participantId = jpaParticipant.getParticipantId();
349 var participantRepository = mock(ParticipantRepository.class);
350 when(participantRepository.getReferenceById(participantId)).thenReturn(jpaParticipant);
352 var acDefinition = new AutomationCompositionDefinition();
353 var nodeTemplateState = new NodeTemplateState();
354 nodeTemplateState.setNodeTemplateId(new ToscaConceptIdentifier("name", "0.0.0"));
355 nodeTemplateState.setParticipantId(UUID.fromString(participantId));
356 acDefinition.setElementStateMap(Map.of(nodeTemplateState.getNodeTemplateId().getName(), nodeTemplateState));
358 var replicaRepository = mock(ParticipantReplicaRepository.class);
359 var participantProvider = new ParticipantProvider(participantRepository,
360 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
362 participantProvider.checkRegisteredParticipant(acDefinition);
363 verify(participantRepository).getReferenceById(participantId);