2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2024 Nordix Foundation.
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;
58 class ParticipantProviderTest {
60 private static final Coder CODER = new StandardCoder();
61 private static final String PARTICIPANT_JSON = "src/test/resources/providers/TestParticipant.json";
63 private static final String AUTOMATION_COMPOSITION_JSON =
64 "src/test/resources/providers/TestAutomationCompositions.json";
66 private static final String NODE_TEMPLATE_STATE_JSON = "src/test/resources/providers/NodeTemplateState.json";
67 private static final String LIST_IS_NULL = ".*. is marked .*ull but is null";
68 private static final UUID INVALID_ID = UUID.randomUUID();
70 private final List<Participant> inputParticipants = new ArrayList<>();
71 private List<JpaParticipant> jpaParticipantList;
72 private List<JpaAutomationComposition> inputAutomationCompositionsJpa;
74 private final List<NodeTemplateState> nodeTemplateStateList = new ArrayList<>();
75 private List<JpaNodeTemplateState> jpaNodeTemplateStateList;
78 void beforeSetup() throws Exception {
79 var originalJson = ResourceUtils.getResourceAsString(PARTICIPANT_JSON);
80 inputParticipants.add(CODER.decode(originalJson, Participant.class));
81 jpaParticipantList = ProviderUtils.getJpaAndValidateList(inputParticipants, JpaParticipant::new, "participant");
83 var originalAcJson = ResourceUtils.getResourceAsString(AUTOMATION_COMPOSITION_JSON);
84 var inputAutomationCompositions = CODER.decode(originalAcJson, AutomationCompositions.class);
85 inputAutomationCompositionsJpa =
86 ProviderUtils.getJpaAndValidateList(inputAutomationCompositions.getAutomationCompositionList(),
87 JpaAutomationComposition::new, "automation compositions");
89 var nodeTemplateStatesJson = ResourceUtils.getResourceAsString(NODE_TEMPLATE_STATE_JSON);
90 nodeTemplateStateList.add(CODER.decode(nodeTemplateStatesJson, NodeTemplateState.class));
91 nodeTemplateStateList.get(0).setState(AcTypeState.COMMISSIONED);
92 jpaNodeTemplateStateList = ProviderUtils.getJpaAndValidateList(nodeTemplateStateList,
93 JpaNodeTemplateState::new, "node template state");
97 void testParticipantSave() {
98 var participantRepository = mock(ParticipantRepository.class);
99 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
100 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
102 var participantProvider = new ParticipantProvider(participantRepository,
103 automationCompositionElementRepository, nodeTemplateStateRepository,
104 mock(ParticipantReplicaRepository.class));
106 assertThatThrownBy(() -> participantProvider.saveParticipant(null)).hasMessageMatching(LIST_IS_NULL);
108 when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0));
110 var savedParticipant = participantProvider.saveParticipant(inputParticipants.get(0));
111 savedParticipant.setParticipantId(inputParticipants.get(0).getParticipantId());
113 assertThat(savedParticipant).usingRecursiveComparison().isEqualTo(inputParticipants.get(0));
117 void testParticipantUpdate() {
118 var participantRepository = mock(ParticipantRepository.class);
119 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
120 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
122 var participantProvider = new ParticipantProvider(participantRepository,
123 automationCompositionElementRepository, nodeTemplateStateRepository,
124 mock(ParticipantReplicaRepository.class));
126 assertThatThrownBy(() -> participantProvider.updateParticipant(null)).hasMessageMatching(LIST_IS_NULL);
128 when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0));
130 var updatedParticipant = participantProvider.updateParticipant(inputParticipants.get(0));
131 updatedParticipant.setParticipantId(inputParticipants.get(0).getParticipantId());
132 assertThat(updatedParticipant).usingRecursiveComparison().isEqualTo(inputParticipants.get(0));
136 void testGetAutomationCompositions() {
137 var participantRepository = mock(ParticipantRepository.class);
138 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
139 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
140 var participantProvider = new ParticipantProvider(participantRepository,
141 automationCompositionElementRepository, nodeTemplateStateRepository,
142 mock(ParticipantReplicaRepository.class));
144 assertThat(participantProvider.findParticipant(INVALID_ID)).isEmpty();
146 when(participantRepository.findAll()).thenReturn(jpaParticipantList);
147 assertThat(participantProvider.getParticipants()).hasSize(inputParticipants.size());
149 assertThatThrownBy(() -> participantProvider.getParticipantById(inputParticipants.get(0).getParticipantId()))
150 .hasMessageMatching("Participant Not Found with ID: " + inputParticipants.get(0).getParticipantId());
152 when(participantRepository.findById(any())).thenReturn(Optional.ofNullable(jpaParticipantList.get(0)));
154 var participant = participantProvider.getParticipantById(inputParticipants.get(0).getParticipantId());
156 assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(participant);
160 void testEmptyParticipant() {
161 var participantRepository = mock(ParticipantRepository.class);
162 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
163 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
164 var participantProvider = new ParticipantProvider(participantRepository,
165 automationCompositionElementRepository, nodeTemplateStateRepository,
166 mock(ParticipantReplicaRepository.class));
168 assertThatThrownBy(() -> participantProvider.getParticipantById(INVALID_ID)).isInstanceOf(
169 PfModelRuntimeException.class).hasMessageMatching("Participant Not Found with ID:.*.");
173 void testDeleteParticipant() {
174 var participantRepository = mock(ParticipantRepository.class);
175 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
176 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
177 var participantProvider = new ParticipantProvider(participantRepository,
178 automationCompositionElementRepository, nodeTemplateStateRepository,
179 mock(ParticipantReplicaRepository.class));
181 var participantId = inputParticipants.get(0).getParticipantId();
182 assertThatThrownBy(() -> participantProvider.deleteParticipant(participantId))
183 .hasMessageMatching(".*.failed, participant does not exist");
185 when(participantRepository.findById(participantId.toString()))
186 .thenReturn(Optional.of(jpaParticipantList.get(0)));
188 var deletedParticipant = participantProvider.deleteParticipant(participantId);
189 assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(deletedParticipant);
193 void testGetAutomationCompositionElements() {
194 var participantRepository = mock(ParticipantRepository.class);
195 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
196 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
197 var participantProvider = new ParticipantProvider(participantRepository,
198 automationCompositionElementRepository, nodeTemplateStateRepository,
199 mock(ParticipantReplicaRepository.class));
201 var acElementList = inputAutomationCompositionsJpa.get(0).getElements();
203 var participantId = UUID.randomUUID();
204 when(automationCompositionElementRepository.findByParticipantId(participantId.toString()))
205 .thenReturn(acElementList);
207 var listOfAcElements = participantProvider.getAutomationCompositionElements(participantId);
209 assertThat(listOfAcElements).hasSameSizeAs(acElementList);
210 assertEquals(UUID.fromString(acElementList.get(0).getElementId()), listOfAcElements.get(0).getId());
214 void testGetAcNodeTemplateState() {
215 var participantRepository = mock(ParticipantRepository.class);
216 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
217 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
218 var participantId = jpaParticipantList.get(0).getParticipantId();
219 when(nodeTemplateStateRepository.findByParticipantId(participantId)).thenReturn(jpaNodeTemplateStateList);
221 var participantProvider = new ParticipantProvider(participantRepository,
222 automationCompositionElementRepository, nodeTemplateStateRepository,
223 mock(ParticipantReplicaRepository.class));
225 var listOfNodeTemplateState = participantProvider.getAcNodeTemplateStates(UUID.fromString(participantId));
227 assertEquals(listOfNodeTemplateState, nodeTemplateStateList);
231 void testNotNullExceptions() {
232 var participantRepository = mock(ParticipantRepository.class);
233 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
234 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
236 var participantProvider = new ParticipantProvider(participantRepository,
237 automationCompositionElementRepository, nodeTemplateStateRepository,
238 mock(ParticipantReplicaRepository.class));
240 assertThrows(NullPointerException.class, () -> participantProvider.getParticipantById(null));
241 assertThrows(NullPointerException.class, () -> participantProvider.findParticipant(null));
242 assertThrows(NullPointerException.class, () -> participantProvider.saveParticipant(null));
243 assertThrows(NullPointerException.class, () -> participantProvider.updateParticipant(null));
244 assertThrows(NullPointerException.class, () -> participantProvider.deleteParticipant(null));
245 assertThrows(NullPointerException.class, () -> participantProvider.getAutomationCompositionElements(null));
246 assertThrows(NullPointerException.class, () -> participantProvider.getAcNodeTemplateStates(null));
247 assertThrows(NullPointerException.class, () -> participantProvider.findParticipantReplica(null));
248 assertThrows(NullPointerException.class, () -> participantProvider.saveParticipantReplica(null));
249 assertThrows(NullPointerException.class, () -> participantProvider.deleteParticipantReplica(null));
253 void testGetSupportedElementMap() {
254 var participantRepository = mock(ParticipantRepository.class);
255 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
256 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
257 when(participantRepository.findAll()).thenReturn(jpaParticipantList);
258 var participantProvider = new ParticipantProvider(participantRepository,
259 automationCompositionElementRepository, nodeTemplateStateRepository,
260 mock(ParticipantReplicaRepository.class));
262 var result = participantProvider.getSupportedElementMap();
263 assertThat(result).hasSize(2);
267 void testGetCompositionIds() {
268 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
269 var participantId = UUID.randomUUID();
270 when(nodeTemplateStateRepository.findByParticipantId(participantId.toString()))
271 .thenReturn(jpaNodeTemplateStateList);
272 var participantRepository = mock(ParticipantRepository.class);
273 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
275 var participantProvider = new ParticipantProvider(participantRepository,
276 automationCompositionElementRepository, nodeTemplateStateRepository,
277 mock(ParticipantReplicaRepository.class));
279 assertThatThrownBy(() -> participantProvider.getCompositionIds(null)).hasMessageMatching(LIST_IS_NULL);
281 var result = participantProvider.getCompositionIds(participantId);
282 assertThat(result).hasSize(1);
286 void testFindParticipantReplica() {
287 var replicaRepository = mock(ParticipantReplicaRepository.class);
288 var replica = inputParticipants.get(0).getReplicas().values().iterator().next();
289 var jpaReplica = jpaParticipantList.get(0).getReplicas().get(0);
290 when(replicaRepository.findById(replica.getReplicaId().toString())).thenReturn(Optional.of(jpaReplica));
291 var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
292 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
295 var result = participantProvider.findParticipantReplica(replica.getReplicaId());
296 assertThat(result).isNotEmpty();
297 assertEquals(replica.getReplicaId(), result.get().getReplicaId());
301 void testFindReplicasOnLine() {
302 var replicaRepository = mock(ParticipantReplicaRepository.class);
303 var replica = inputParticipants.get(0).getReplicas().values().iterator().next();
304 var jpaReplica = jpaParticipantList.get(0).getReplicas().get(0);
305 jpaReplica.fromAuthorative(replica);
306 when(replicaRepository.findByParticipantState(ParticipantState.ON_LINE)).thenReturn(List.of(jpaReplica));
307 var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
308 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
311 var result = participantProvider.findReplicasOnLine();
312 assertThat(result).hasSize(1);
313 assertEquals(replica.getReplicaId(), result.get(0).getReplicaId());
317 void testSaveParticipantReplica() {
318 var jpaReplica = jpaParticipantList.get(0).getReplicas().get(0);
319 var replicaRepository = mock(ParticipantReplicaRepository.class);
320 when(replicaRepository.getReferenceById(jpaReplica.getReplicaId())).thenReturn(jpaReplica);
321 var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
322 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
325 var replica = inputParticipants.get(0).getReplicas().values().iterator().next();
326 participantProvider.saveParticipantReplica(replica);
327 verify(replicaRepository).save(any());
331 void testDeleteParticipantReplica() {
332 var replicaRepository = mock(ParticipantReplicaRepository.class);
333 var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
334 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
336 participantProvider.deleteParticipantReplica(CommonTestData.getReplicaId());
337 verify(replicaRepository).deleteById(CommonTestData.getReplicaId().toString());
341 void testVerifyParticipantState() {
342 var jpaParticipant = new JpaParticipant(jpaParticipantList.get(0));
343 var participantId = jpaParticipant.getParticipantId();
344 var participantRepository = mock(ParticipantRepository.class);
345 when(participantRepository.getReferenceById(participantId)).thenReturn(jpaParticipant);
347 var replicaRepository = mock(ParticipantReplicaRepository.class);
348 var participantProvider = new ParticipantProvider(participantRepository,
349 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
352 jpaParticipant.setReplicas(List.of());
353 var set = Set.of(UUID.fromString(participantId));
354 assertThatThrownBy(() -> participantProvider.verifyParticipantState(set))
355 .hasMessageMatching("Participant: " + participantId + " is OFFLINE");
357 when(participantRepository.getReferenceById(participantId)).thenReturn(jpaParticipantList.get(0));
358 participantProvider.verifyParticipantState(set);
359 verify(participantRepository, times(2)).getReferenceById(participantId);