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.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Optional;
32 import java.util.UUID;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.clamp.models.acm.concepts.Participant;
36 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaParticipant;
37 import org.onap.policy.clamp.models.acm.persistence.repository.ParticipantRepository;
38 import org.onap.policy.common.utils.coder.Coder;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41 import org.onap.policy.models.base.PfConceptKey;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
44 class ParticipantProviderTest {
46 private static final Coder CODER = new StandardCoder();
47 private static final String PARTICIPANT_JSON = "src/test/resources/providers/TestParticipant.json";
48 private static final String LIST_IS_NULL = ".*. is marked .*ull but is null";
49 private static final ToscaConceptIdentifier INVALID_ID = new ToscaConceptIdentifier("invalid_name", "1.0.1");
51 private final List<Participant> inputParticipants = new ArrayList<>();
52 private List<JpaParticipant> jpaParticipantList;
53 private final String originalJson = ResourceUtils.getResourceAsString(PARTICIPANT_JSON);
56 void beforeSetupDao() throws Exception {
57 inputParticipants.add(CODER.decode(originalJson, Participant.class));
58 jpaParticipantList = ProviderUtils.getJpaAndValidateList(inputParticipants, JpaParticipant::new, "participant");
62 void testParticipantSave() {
63 var participantRepository = mock(ParticipantRepository.class);
64 for (var participant : jpaParticipantList) {
65 when(participantRepository.getById(new PfConceptKey(participant.getName(), participant.getVersion())))
66 .thenReturn(participant);
68 var participantProvider = new ParticipantProvider(participantRepository);
70 assertThatThrownBy(() -> participantProvider.saveParticipant(null)).hasMessageMatching(LIST_IS_NULL);
72 when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0));
74 Participant savedParticipant = participantProvider.saveParticipant(inputParticipants.get(0));
75 savedParticipant.setParticipantId(inputParticipants.get(0).getParticipantId());
77 assertThat(savedParticipant).usingRecursiveComparison().isEqualTo(inputParticipants.get(0));
81 void testParticipantUpdate() {
82 var participantRepository = mock(ParticipantRepository.class);
83 for (var participant : jpaParticipantList) {
84 when(participantRepository.getById(new PfConceptKey(participant.getName(), participant.getVersion())))
85 .thenReturn(participant);
87 var participantProvider = new ParticipantProvider(participantRepository);
89 assertThatThrownBy(() -> participantProvider.updateParticipant(null))
90 .hasMessageMatching(LIST_IS_NULL);
92 when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0));
94 Participant updatedParticipant = participantProvider.updateParticipant(inputParticipants.get(0));
95 updatedParticipant.setParticipantId(inputParticipants.get(0).getParticipantId());
96 assertThat(updatedParticipant).usingRecursiveComparison().isEqualTo(inputParticipants.get(0));
100 void testGetAutomationCompositions() {
101 var participantRepository = mock(ParticipantRepository.class);
102 var participantProvider = new ParticipantProvider(participantRepository);
104 assertThat(participantProvider.findParticipant(INVALID_ID)).isEmpty();
106 when(participantRepository.findAll()).thenReturn(jpaParticipantList);
107 assertThat(participantProvider.getParticipants()).hasSize(inputParticipants.size());
109 when(participantRepository.findByParticipantId(any())).thenReturn(
110 Optional.ofNullable(jpaParticipantList.get(0)));
112 var participant = participantProvider.getParticipantById(inputParticipants.get(0)
113 .getParticipantId());
115 assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(participant);
119 void testDeleteParticipant() {
120 var participantRepository = mock(ParticipantRepository.class);
121 var participantProvider = new ParticipantProvider(participantRepository);
123 var participantId = UUID.randomUUID();
124 assertThatThrownBy(() -> participantProvider.deleteParticipant(participantId))
125 .hasMessageMatching(".*.failed, participant does not exist");
127 when(participantRepository.findByParticipantId(participantId.toString()))
128 .thenReturn(Optional.of(jpaParticipantList.get(0)));
130 Participant deletedParticipant =
131 participantProvider.deleteParticipant(participantId);
132 assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(deletedParticipant);