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 testGetAutomationCompositions() {
118 var participantRepository = mock(ParticipantRepository.class);
119 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
120 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
121 var participantProvider = new ParticipantProvider(participantRepository,
122 automationCompositionElementRepository, nodeTemplateStateRepository,
123 mock(ParticipantReplicaRepository.class));
125 assertThat(participantProvider.findParticipant(INVALID_ID)).isEmpty();
127 when(participantRepository.findAll()).thenReturn(jpaParticipantList);
128 assertThat(participantProvider.getParticipants()).hasSize(inputParticipants.size());
130 assertThatThrownBy(() -> participantProvider.getParticipantById(inputParticipants.get(0).getParticipantId()))
131 .hasMessageMatching("Participant Not Found with ID: " + inputParticipants.get(0).getParticipantId());
133 when(participantRepository.findById(any())).thenReturn(Optional.ofNullable(jpaParticipantList.get(0)));
135 var participant = participantProvider.getParticipantById(inputParticipants.get(0).getParticipantId());
137 assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(participant);
141 void testEmptyParticipant() {
142 var participantRepository = mock(ParticipantRepository.class);
143 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
144 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
145 var participantProvider = new ParticipantProvider(participantRepository,
146 automationCompositionElementRepository, nodeTemplateStateRepository,
147 mock(ParticipantReplicaRepository.class));
149 assertThatThrownBy(() -> participantProvider.getParticipantById(INVALID_ID)).isInstanceOf(
150 PfModelRuntimeException.class).hasMessageMatching("Participant Not Found with ID:.*.");
154 void testDeleteParticipant() {
155 var participantRepository = mock(ParticipantRepository.class);
156 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
157 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
158 var participantProvider = new ParticipantProvider(participantRepository,
159 automationCompositionElementRepository, nodeTemplateStateRepository,
160 mock(ParticipantReplicaRepository.class));
162 var participantId = inputParticipants.get(0).getParticipantId();
163 assertThatThrownBy(() -> participantProvider.deleteParticipant(participantId))
164 .hasMessageMatching(".*.failed, participant does not exist");
166 when(participantRepository.findById(participantId.toString()))
167 .thenReturn(Optional.of(jpaParticipantList.get(0)));
169 var deletedParticipant = participantProvider.deleteParticipant(participantId);
170 assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(deletedParticipant);
174 void testGetAutomationCompositionElements() {
175 var participantRepository = mock(ParticipantRepository.class);
176 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
177 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
178 var participantProvider = new ParticipantProvider(participantRepository,
179 automationCompositionElementRepository, nodeTemplateStateRepository,
180 mock(ParticipantReplicaRepository.class));
182 var acElementList = inputAutomationCompositionsJpa.get(0).getElements();
184 var participantId = UUID.randomUUID();
185 when(automationCompositionElementRepository.findByParticipantId(participantId.toString()))
186 .thenReturn(acElementList);
188 var listOfAcElements = participantProvider.getAutomationCompositionElements(participantId);
190 assertThat(listOfAcElements).hasSameSizeAs(acElementList);
191 assertEquals(UUID.fromString(acElementList.get(0).getElementId()), listOfAcElements.get(0).getId());
195 void testGetAcNodeTemplateState() {
196 var participantRepository = mock(ParticipantRepository.class);
197 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
198 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
199 var participantId = jpaParticipantList.get(0).getParticipantId();
200 when(nodeTemplateStateRepository.findByParticipantId(participantId)).thenReturn(jpaNodeTemplateStateList);
202 var participantProvider = new ParticipantProvider(participantRepository,
203 automationCompositionElementRepository, nodeTemplateStateRepository,
204 mock(ParticipantReplicaRepository.class));
206 var listOfNodeTemplateState = participantProvider.getAcNodeTemplateStates(UUID.fromString(participantId));
208 assertEquals(listOfNodeTemplateState, nodeTemplateStateList);
212 void testNotNullExceptions() {
213 var participantRepository = mock(ParticipantRepository.class);
214 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
215 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
217 var participantProvider = new ParticipantProvider(participantRepository,
218 automationCompositionElementRepository, nodeTemplateStateRepository,
219 mock(ParticipantReplicaRepository.class));
221 assertThrows(NullPointerException.class, () -> participantProvider.getParticipantById(null));
222 assertThrows(NullPointerException.class, () -> participantProvider.findParticipant(null));
223 assertThrows(NullPointerException.class, () -> participantProvider.saveParticipant(null));
224 assertThrows(NullPointerException.class, () -> participantProvider.deleteParticipant(null));
225 assertThrows(NullPointerException.class, () -> participantProvider.getAutomationCompositionElements(null));
226 assertThrows(NullPointerException.class, () -> participantProvider.getAcNodeTemplateStates(null));
227 assertThrows(NullPointerException.class, () -> participantProvider.findParticipantReplica(null));
228 assertThrows(NullPointerException.class, () -> participantProvider.saveParticipantReplica(null));
229 assertThrows(NullPointerException.class, () -> participantProvider.deleteParticipantReplica(null));
233 void testGetSupportedElementMap() {
234 var participantRepository = mock(ParticipantRepository.class);
235 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
236 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
237 when(participantRepository.findAll()).thenReturn(jpaParticipantList);
238 var participantProvider = new ParticipantProvider(participantRepository,
239 automationCompositionElementRepository, nodeTemplateStateRepository,
240 mock(ParticipantReplicaRepository.class));
242 var result = participantProvider.getSupportedElementMap();
243 assertThat(result).hasSize(2);
247 void testGetCompositionIds() {
248 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
249 var participantId = UUID.randomUUID();
250 when(nodeTemplateStateRepository.findByParticipantId(participantId.toString()))
251 .thenReturn(jpaNodeTemplateStateList);
252 var participantRepository = mock(ParticipantRepository.class);
253 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
255 var participantProvider = new ParticipantProvider(participantRepository,
256 automationCompositionElementRepository, nodeTemplateStateRepository,
257 mock(ParticipantReplicaRepository.class));
259 assertThatThrownBy(() -> participantProvider.getCompositionIds(null)).hasMessageMatching(LIST_IS_NULL);
261 var result = participantProvider.getCompositionIds(participantId);
262 assertThat(result).hasSize(1);
266 void testFindParticipantReplica() {
267 var replicaRepository = mock(ParticipantReplicaRepository.class);
268 var replica = inputParticipants.get(0).getReplicas().values().iterator().next();
269 var jpaReplica = jpaParticipantList.get(0).getReplicas().get(0);
270 when(replicaRepository.findById(replica.getReplicaId().toString())).thenReturn(Optional.of(jpaReplica));
271 var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
272 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
275 var result = participantProvider.findParticipantReplica(replica.getReplicaId());
276 assertThat(result).isNotEmpty();
277 assertEquals(replica.getReplicaId(), result.get().getReplicaId());
281 void testFindReplicasOnLine() {
282 var replicaRepository = mock(ParticipantReplicaRepository.class);
283 var replica = inputParticipants.get(0).getReplicas().values().iterator().next();
284 var jpaReplica = jpaParticipantList.get(0).getReplicas().get(0);
285 jpaReplica.fromAuthorative(replica);
286 when(replicaRepository.findByParticipantState(ParticipantState.ON_LINE)).thenReturn(List.of(jpaReplica));
287 var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
288 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
291 var result = participantProvider.findReplicasOnLine();
292 assertThat(result).hasSize(1);
293 assertEquals(replica.getReplicaId(), result.get(0).getReplicaId());
297 void testSaveParticipantReplica() {
298 var jpaReplica = jpaParticipantList.get(0).getReplicas().get(0);
299 var replicaRepository = mock(ParticipantReplicaRepository.class);
300 when(replicaRepository.getReferenceById(jpaReplica.getReplicaId())).thenReturn(jpaReplica);
301 var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
302 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
305 var replica = inputParticipants.get(0).getReplicas().values().iterator().next();
306 participantProvider.saveParticipantReplica(replica);
307 verify(replicaRepository).save(any());
311 void testDeleteParticipantReplica() {
312 var replicaRepository = mock(ParticipantReplicaRepository.class);
313 var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
314 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
316 participantProvider.deleteParticipantReplica(CommonTestData.getReplicaId());
317 verify(replicaRepository).deleteById(CommonTestData.getReplicaId().toString());
321 void testVerifyParticipantState() {
322 var jpaParticipant = new JpaParticipant(jpaParticipantList.get(0));
323 var participantId = jpaParticipant.getParticipantId();
324 var participantRepository = mock(ParticipantRepository.class);
325 when(participantRepository.getReferenceById(participantId)).thenReturn(jpaParticipant);
327 var replicaRepository = mock(ParticipantReplicaRepository.class);
328 var participantProvider = new ParticipantProvider(participantRepository,
329 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
332 jpaParticipant.setReplicas(List.of());
333 var set = Set.of(UUID.fromString(participantId));
334 assertThatThrownBy(() -> participantProvider.verifyParticipantState(set))
335 .hasMessageMatching("Participant: " + participantId + " is OFFLINE");
337 when(participantRepository.getReferenceById(participantId)).thenReturn(jpaParticipantList.get(0));
338 participantProvider.verifyParticipantState(set);
339 verify(participantRepository, times(2)).getReferenceById(participantId);