2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 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.assertThrows;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Optional;
33 import java.util.UUID;
34 import java.util.stream.Collectors;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
39 import org.onap.policy.clamp.models.acm.concepts.DeployState;
40 import org.onap.policy.clamp.models.acm.concepts.LockState;
41 import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState;
42 import org.onap.policy.clamp.models.acm.concepts.Participant;
43 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationComposition;
44 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaNodeTemplateState;
45 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaParticipant;
46 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionElementRepository;
47 import org.onap.policy.clamp.models.acm.persistence.repository.NodeTemplateStateRepository;
48 import org.onap.policy.clamp.models.acm.persistence.repository.ParticipantRepository;
49 import org.onap.policy.common.utils.coder.Coder;
50 import org.onap.policy.common.utils.coder.StandardCoder;
51 import org.onap.policy.common.utils.resources.ResourceUtils;
52 import org.onap.policy.models.base.PfModelRuntimeException;
54 class ParticipantProviderTest {
56 private static final Coder CODER = new StandardCoder();
57 private static final String PARTICIPANT_JSON = "src/test/resources/providers/TestParticipant.json";
59 private static final String AUTOMATION_COMPOSITION_JSON =
60 "src/test/resources/providers/TestAutomationCompositions.json";
62 private static final String AUTOMATION_COMPOSITION_JSON_DEREGISTER =
63 "src/test/resources/providers/TestAutomationCompositionsDeregister.json";
65 private static final String NODE_TEMPLATE_STATE_JSON = "src/test/resources/providers/NodeTemplateState.json";
66 private static final String LIST_IS_NULL = ".*. is marked .*ull but is null";
67 private static final UUID INVALID_ID = UUID.randomUUID();
69 private final List<Participant> inputParticipants = new ArrayList<>();
70 private List<JpaParticipant> jpaParticipantList;
71 private final String originalJson = ResourceUtils.getResourceAsString(PARTICIPANT_JSON);
73 private AutomationCompositions inputAutomationCompositions;
74 private List<JpaAutomationComposition> inputAutomationCompositionsJpa;
75 private final String originalAcJson = ResourceUtils.getResourceAsString(AUTOMATION_COMPOSITION_JSON);
77 private AutomationCompositions inputAutomationCompositionsDeregister;
78 private List<JpaAutomationComposition> inputAutomationCompositionsJpaDeregister;
79 private final String deregisterAcJson = ResourceUtils.getResourceAsString(AUTOMATION_COMPOSITION_JSON_DEREGISTER);
81 private final String nodeTemplateStatesJson = ResourceUtils.getResourceAsString(NODE_TEMPLATE_STATE_JSON);
83 private List<NodeTemplateState> nodeTemplateStateList = new ArrayList<>();
84 private List<JpaNodeTemplateState> jpaNodeTemplateStateList;
87 void beforeSetup() throws Exception {
88 inputParticipants.add(CODER.decode(originalJson, Participant.class));
89 jpaParticipantList = ProviderUtils.getJpaAndValidateList(inputParticipants, JpaParticipant::new, "participant");
91 inputAutomationCompositions = CODER.decode(originalAcJson, AutomationCompositions.class);
92 inputAutomationCompositionsJpa =
93 ProviderUtils.getJpaAndValidateList(inputAutomationCompositions.getAutomationCompositionList(),
94 JpaAutomationComposition::new, "automation compositions");
96 inputAutomationCompositionsDeregister = CODER.decode(deregisterAcJson, AutomationCompositions.class);
97 inputAutomationCompositionsJpaDeregister =
98 ProviderUtils.getJpaAndValidateList(inputAutomationCompositionsDeregister.getAutomationCompositionList(),
99 JpaAutomationComposition::new, "automation compositions");
101 nodeTemplateStateList.add(CODER.decode(nodeTemplateStatesJson, NodeTemplateState.class));
102 nodeTemplateStateList.get(0).setState(AcTypeState.COMMISSIONED);
103 jpaNodeTemplateStateList = ProviderUtils.getJpaAndValidateList(nodeTemplateStateList,
104 JpaNodeTemplateState::new, "node template state");
108 void testParticipantSave() {
109 var participantRepository = mock(ParticipantRepository.class);
110 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
111 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
113 var participantProvider = new ParticipantProvider(participantRepository,
114 automationCompositionElementRepository, nodeTemplateStateRepository);
116 assertThatThrownBy(() -> participantProvider.saveParticipant(null)).hasMessageMatching(LIST_IS_NULL);
118 when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0));
120 var savedParticipant = participantProvider.saveParticipant(inputParticipants.get(0));
121 savedParticipant.setParticipantId(inputParticipants.get(0).getParticipantId());
123 assertThat(savedParticipant).usingRecursiveComparison().isEqualTo(inputParticipants.get(0));
127 void testParticipantUpdate() {
128 var participantRepository = mock(ParticipantRepository.class);
129 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
130 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
132 var participantProvider = new ParticipantProvider(participantRepository,
133 automationCompositionElementRepository, nodeTemplateStateRepository);
135 assertThatThrownBy(() -> participantProvider.updateParticipant(null))
136 .hasMessageMatching(LIST_IS_NULL);
138 when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0));
140 var updatedParticipant = participantProvider.updateParticipant(inputParticipants.get(0));
141 updatedParticipant.setParticipantId(inputParticipants.get(0).getParticipantId());
142 assertThat(updatedParticipant).usingRecursiveComparison().isEqualTo(inputParticipants.get(0));
146 void testGetAutomationCompositions() {
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);
154 assertThat(participantProvider.findParticipant(INVALID_ID)).isEmpty();
156 when(participantRepository.findAll()).thenReturn(jpaParticipantList);
157 assertThat(participantProvider.getParticipants()).hasSize(inputParticipants.size());
159 assertThatThrownBy(() -> participantProvider.getParticipantById(inputParticipants.get(0).getParticipantId()))
160 .hasMessageMatching("Participant Not Found with ID: " + inputParticipants.get(0).getParticipantId());
162 when(participantRepository.findById(any())).thenReturn(
163 Optional.ofNullable(jpaParticipantList.get(0)));
165 var participant = participantProvider.getParticipantById(inputParticipants.get(0)
166 .getParticipantId());
168 assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(participant);
172 void testEmptyParticipant() {
173 var participantRepository = mock(ParticipantRepository.class);
174 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
175 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
176 var participantProvider = new ParticipantProvider(participantRepository,
177 automationCompositionElementRepository, nodeTemplateStateRepository);
179 assertThatThrownBy(() -> participantProvider.getParticipantById(INVALID_ID)).isInstanceOf(
180 PfModelRuntimeException.class).hasMessageMatching("Participant Not Found with ID:.*.");
184 void testDeleteParticipant() {
185 var participantRepository = mock(ParticipantRepository.class);
186 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
187 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
188 var participantProvider = new ParticipantProvider(participantRepository,
189 automationCompositionElementRepository, nodeTemplateStateRepository);
191 var participantId = inputParticipants.get(0).getParticipantId();
192 assertThatThrownBy(() -> participantProvider.deleteParticipant(participantId))
193 .hasMessageMatching(".*.failed, participant does not exist");
195 when(participantRepository.findById(participantId.toString()))
196 .thenReturn(Optional.of(jpaParticipantList.get(0)));
198 var deletedParticipant = participantProvider.deleteParticipant(participantId);
199 assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(deletedParticipant);
203 void testGetAutomationCompositionElements() {
204 var participantRepository = mock(ParticipantRepository.class);
205 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
206 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
207 var participantProvider = new ParticipantProvider(participantRepository,
208 automationCompositionElementRepository, nodeTemplateStateRepository);
210 var acElementList = inputAutomationCompositionsJpa
211 .stream().map(c -> c.getElements()).collect(Collectors.toList());
213 when(automationCompositionElementRepository.findByParticipantId(any())).thenReturn(acElementList.get(0));
215 var listOfAcElements = participantProvider.getAutomationCompositionElements(UUID.randomUUID());
217 assertThat(acElementList.get(0).equals(listOfAcElements));
223 void testGetAcNodeTemplateState() {
224 var participantRepository = mock(ParticipantRepository.class);
225 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
226 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
227 when(nodeTemplateStateRepository.findByParticipantId(any())).thenReturn(jpaNodeTemplateStateList);
229 var participantProvider = new ParticipantProvider(participantRepository,
230 automationCompositionElementRepository, nodeTemplateStateRepository);
232 var listOfNodeTemplateState = participantProvider.getAcNodeTemplateStates(
233 UUID.fromString(jpaParticipantList.get(0).getParticipantId()));
235 assertThat(listOfNodeTemplateState.equals(nodeTemplateStateList));
240 void testNotNullExceptions() {
241 var participantRepository = mock(ParticipantRepository.class);
242 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
243 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
245 var participantProvider = new ParticipantProvider(participantRepository,
246 automationCompositionElementRepository, nodeTemplateStateRepository);
248 assertThrows(NullPointerException.class, () -> participantProvider.getParticipantById(null));
249 assertThrows(NullPointerException.class, () -> participantProvider.findParticipant(null));
250 assertThrows(NullPointerException.class, () -> participantProvider.saveParticipant(null));
251 assertThrows(NullPointerException.class, () -> participantProvider.updateParticipant(null));
252 assertThrows(NullPointerException.class, () -> participantProvider.deleteParticipant(null));
253 assertThrows(NullPointerException.class, () -> participantProvider.getAutomationCompositionElements(null));
254 assertThrows(NullPointerException.class, () -> participantProvider.getAcNodeTemplateStates(null));
255 assertThrows(NullPointerException.class, () -> participantProvider.resetParticipantAcElementState(null));
259 void testGetSupportedElementMap() {
260 var participantRepository = mock(ParticipantRepository.class);
261 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
262 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
263 when(participantRepository.findAll()).thenReturn(jpaParticipantList);
264 var participantProvider = new ParticipantProvider(participantRepository,
265 automationCompositionElementRepository, nodeTemplateStateRepository);
267 var result = participantProvider.getSupportedElementMap();
268 assertThat(result).hasSize(2);
272 void testResetParticipantAcElementState() {
273 var participantRepository = mock(ParticipantRepository.class);
274 var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
276 var acElementList = inputAutomationCompositionsJpaDeregister
277 .stream().map(c -> c.getElements()).collect(Collectors.toList());
279 when(automationCompositionElementRepository.findByParticipantId(any())).thenReturn(acElementList.get(0));
281 var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
282 var participantProvider = new ParticipantProvider(participantRepository,
283 automationCompositionElementRepository, nodeTemplateStateRepository);
285 acElementList.get(0).stream().forEach(e -> {
286 assertThat(e.getDeployState().equals(DeployState.DEPLOYED));
287 assertThat(e.getLockState().equals(LockState.LOCKED));
290 participantProvider.resetParticipantAcElementState(UUID.randomUUID());
292 acElementList.get(0).stream().forEach(e -> {
293 assertThat(e.getDeployState().equals(DeployState.UNDEPLOYED));
294 assertThat(e.getLockState().equals(LockState.NONE));