f537e2cd73dbd38ad000f413ebb7e8f791c2dead
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 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.Assert.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.ToscaTypedEntityFilter;
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
51     private List<Participant> inputParticipants = new ArrayList<>();
52     private List<JpaParticipant> jpaParticipantList;
53     private String originalJson = ResourceUtils.getResourceAsString(PARTICIPANT_JSON);
54
55     @BeforeEach
56     void beforeSetupDao() throws Exception {
57         inputParticipants.add(CODER.decode(originalJson, Participant.class));
58         jpaParticipantList = ProviderUtils.getJpaAndValidateList(inputParticipants, JpaParticipant::new, "participant");
59     }
60
61     @Test
62     void testParticipantSave() throws Exception {
63         var participantRepository = mock(ParticipantRepository.class);
64         for (var participant : jpaParticipantList) {
65             when(participantRepository.getById(new PfConceptKey(participant.getName(), participant.getVersion())))
66                     .thenReturn(participant);
67         }
68         var participantProvider = new ParticipantProvider(participantRepository);
69
70         assertThatThrownBy(() -> {
71             participantProvider.saveParticipant(null);
72         }).hasMessageMatching(LIST_IS_NULL);
73
74         when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0));
75
76         Participant savedParticipant = participantProvider.saveParticipant(inputParticipants.get(0));
77         assertEquals(savedParticipant, inputParticipants.get(0));
78
79         when(participantRepository.save(any())).thenThrow(IllegalArgumentException.class);
80
81         assertThatThrownBy(() -> {
82             participantProvider.saveParticipant(inputParticipants.get(0));
83         }).hasMessageMatching("Error in save Participant");
84     }
85
86     @Test
87     void testGetAutomationCompositions() throws Exception {
88         var participantRepository = mock(ParticipantRepository.class);
89         var participantProvider = new ParticipantProvider(participantRepository);
90
91         // Return empty list when no data present in db
92         List<Participant> getResponse = participantProvider.getParticipants(null, null);
93         assertThat(getResponse).isEmpty();
94
95         String name = inputParticipants.get(0).getName();
96         String version = inputParticipants.get(0).getVersion();
97         when(participantRepository.getFiltered(any(), eq(name), eq(version)))
98                 .thenReturn(List.of(jpaParticipantList.get(0)));
99         assertEquals(1, participantProvider.getParticipants(name, version).size());
100
101         assertThat(participantProvider.getParticipants("invalid_name", "1.0.1")).isEmpty();
102
103         assertThat(participantProvider.findParticipant("invalid_name", "1.0.1")).isEmpty();
104
105         when(participantRepository.findAll()).thenReturn(jpaParticipantList);
106         assertThat(participantProvider.getParticipants()).hasSize(inputParticipants.size());
107
108         when(participantRepository.findById(any())).thenThrow(IllegalArgumentException.class);
109
110         assertThatThrownBy(() -> {
111             participantProvider.findParticipant("notValid", "notValid");
112         }).hasMessageMatching("Error in find Participant");
113
114         assertThatThrownBy(() -> {
115             participantProvider.getFilteredParticipants(null);
116         }).hasMessageMatching("filter is marked .*ull but is null");
117
118         when(participantRepository.getFiltered(eq(JpaParticipant.class), eq(null), eq(null)))
119                 .thenReturn(jpaParticipantList);
120
121         final ToscaTypedEntityFilter<Participant> filter = ToscaTypedEntityFilter.<Participant>builder()
122                 .type("org.onap.domain.pmsh.PMSHAutomationCompositionDefinition").build();
123         assertEquals(1, participantProvider.getFilteredParticipants(filter).size());
124
125     }
126
127     @Test
128     void testDeleteParticipant() throws Exception {
129         var participantRepository = mock(ParticipantRepository.class);
130         var participantProvider = new ParticipantProvider(participantRepository);
131
132         assertThatThrownBy(() -> {
133             participantProvider.deleteParticipant("Invalid_name", "1.0.1");
134         }).hasMessageMatching(".*.failed, participant does not exist");
135
136         String name = inputParticipants.get(0).getName();
137         String version = inputParticipants.get(0).getVersion();
138
139         when(participantRepository.findById(any())).thenReturn(Optional.of(jpaParticipantList.get(0)));
140
141         Participant deletedParticipant = participantProvider.deleteParticipant(name, version);
142         assertEquals(inputParticipants.get(0), deletedParticipant);
143
144         when(participantRepository.findById(any())).thenThrow(IllegalArgumentException.class);
145         assertThatThrownBy(() -> {
146             participantProvider.deleteParticipant(name, version);
147         }).hasMessageMatching("Error in delete Participant");
148     }
149 }