6e6637c7ee226dff8592943987739e8a6420f605
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.models.acm.persistence.provider;
22
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.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
30
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Optional;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.clamp.models.acm.concepts.Participant;
37 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaParticipant;
38 import org.onap.policy.clamp.models.acm.persistence.repository.ParticipantRepository;
39 import org.onap.policy.common.utils.coder.Coder;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.common.utils.resources.ResourceUtils;
42 import org.onap.policy.models.base.PfConceptKey;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
44
45 class ParticipantProviderTest {
46
47     private static final Coder CODER = new StandardCoder();
48     private static final String PARTICIPANT_JSON = "src/test/resources/providers/TestParticipant.json";
49     private static final String LIST_IS_NULL = ".*. is marked .*ull but is null";
50     private static final ToscaConceptIdentifier INVALID_ID = new ToscaConceptIdentifier("invalid_name", "1.0.1");
51
52     private final List<Participant> inputParticipants = new ArrayList<>();
53     private List<JpaParticipant> jpaParticipantList;
54     private final String originalJson = ResourceUtils.getResourceAsString(PARTICIPANT_JSON);
55
56     @BeforeEach
57     void beforeSetupDao() throws Exception {
58         inputParticipants.add(CODER.decode(originalJson, Participant.class));
59         jpaParticipantList = ProviderUtils.getJpaAndValidateList(inputParticipants, JpaParticipant::new, "participant");
60     }
61
62     @Test
63     void testParticipantSave() throws Exception {
64         var participantRepository = mock(ParticipantRepository.class);
65         for (var participant : jpaParticipantList) {
66             when(participantRepository.getById(new PfConceptKey(participant.getName(), participant.getVersion())))
67                 .thenReturn(participant);
68         }
69         var participantProvider = new ParticipantProvider(participantRepository);
70
71         assertThatThrownBy(() -> participantProvider.saveParticipant(null)).hasMessageMatching(LIST_IS_NULL);
72
73         when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0));
74
75         Participant savedParticipant = participantProvider.saveParticipant(inputParticipants.get(0));
76         savedParticipant.setParticipantId(inputParticipants.get(0).getParticipantId());
77         assertEquals(savedParticipant, inputParticipants.get(0));
78     }
79
80     @Test
81     void testParticipantUpdate() throws Exception {
82         var participantRepository = mock(ParticipantRepository.class);
83         for (var participant : jpaParticipantList) {
84             when(participantRepository.getById(new PfConceptKey(participant.getName(), participant.getVersion())))
85                 .thenReturn(participant);
86         }
87         var participantProvider = new ParticipantProvider(participantRepository);
88
89         assertThatThrownBy(() -> participantProvider.updateParticipant(null))
90             .hasMessageMatching(LIST_IS_NULL);
91
92         when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0));
93
94         Participant updatedParticipant = participantProvider.updateParticipant(inputParticipants.get(0));
95         updatedParticipant.setParticipantId(inputParticipants.get(0).getParticipantId());
96         assertEquals(updatedParticipant, inputParticipants.get(0));
97     }
98
99     @Test
100     void testGetAutomationCompositions() throws Exception {
101         var participantRepository = mock(ParticipantRepository.class);
102         var participantProvider = new ParticipantProvider(participantRepository);
103
104         // Return empty list when no data present in db
105         List<Participant> getResponse = participantProvider.getParticipants(null, null);
106         assertThat(getResponse).isEmpty();
107
108         String name = inputParticipants.get(0).getName();
109         String version = inputParticipants.get(0).getVersion();
110         when(participantRepository.getFiltered(any(), eq(name), eq(version)))
111             .thenReturn(List.of(jpaParticipantList.get(0)));
112         assertEquals(1, participantProvider.getParticipants(name, version).size());
113
114         assertThat(participantProvider.getParticipants("invalid_name", "1.0.1")).isEmpty();
115
116         assertThat(participantProvider.findParticipant(INVALID_ID)).isEmpty();
117
118         when(participantRepository.findAll()).thenReturn(jpaParticipantList);
119         assertThat(participantProvider.getParticipants()).hasSize(inputParticipants.size());
120
121         when(participantRepository.findByParticipantId(any())).thenReturn(
122             Optional.ofNullable(jpaParticipantList.get(0)));
123
124         var participant = participantProvider.getParticipantById(inputParticipants.get(0)
125             .getParticipantId().toString());
126
127         assertThat(participant).isEqualTo(inputParticipants.get(0));
128     }
129
130     @Test
131     void testDeleteParticipant() throws Exception {
132         var participantRepository = mock(ParticipantRepository.class);
133         var participantProvider = new ParticipantProvider(participantRepository);
134
135         assertThatThrownBy(() -> participantProvider.deleteParticipant(INVALID_ID))
136             .hasMessageMatching(".*.failed, participant does not exist");
137
138         when(participantRepository.findById(any())).thenReturn(Optional.of(jpaParticipantList.get(0)));
139
140         Participant deletedParticipant =
141             participantProvider.deleteParticipant(inputParticipants.get(0).getDefinition());
142         assertEquals(inputParticipants.get(0), deletedParticipant);
143     }
144 }