3e100df181513f1b9113b58abec6ae1d1bcdd02b
[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.assertThrows;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
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.NodeTemplateState;
40 import org.onap.policy.clamp.models.acm.concepts.Participant;
41 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationComposition;
42 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaNodeTemplateState;
43 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaParticipant;
44 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionElementRepository;
45 import org.onap.policy.clamp.models.acm.persistence.repository.NodeTemplateStateRepository;
46 import org.onap.policy.clamp.models.acm.persistence.repository.ParticipantRepository;
47 import org.onap.policy.common.utils.coder.Coder;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.onap.policy.common.utils.resources.ResourceUtils;
50 import org.onap.policy.models.base.PfModelRuntimeException;
51
52 class ParticipantProviderTest {
53
54     private static final Coder CODER = new StandardCoder();
55     private static final String PARTICIPANT_JSON = "src/test/resources/providers/TestParticipant.json";
56
57     private static final String AUTOMATION_COMPOSITION_JSON =
58         "src/test/resources/providers/TestAutomationCompositions.json";
59
60     private static final String NODE_TEMPLATE_STATE_JSON = "src/test/resources/providers/NodeTemplateState.json";
61     private static final String LIST_IS_NULL = ".*. is marked .*ull but is null";
62     private static final UUID INVALID_ID = UUID.randomUUID();
63
64     private final List<Participant> inputParticipants = new ArrayList<>();
65     private List<JpaParticipant> jpaParticipantList;
66     private final String originalJson = ResourceUtils.getResourceAsString(PARTICIPANT_JSON);
67
68     private AutomationCompositions inputAutomationCompositions;
69     private List<JpaAutomationComposition> inputAutomationCompositionsJpa;
70     private final String originalAcJson = ResourceUtils.getResourceAsString(AUTOMATION_COMPOSITION_JSON);
71
72     private final String nodeTemplateStatesJson = ResourceUtils.getResourceAsString(NODE_TEMPLATE_STATE_JSON);
73
74     private List<NodeTemplateState> nodeTemplateStateList = new ArrayList<>();
75     private List<JpaNodeTemplateState> jpaNodeTemplateStateList;
76
77     @BeforeEach
78     void beforeSetup() throws Exception {
79         inputParticipants.add(CODER.decode(originalJson, Participant.class));
80         jpaParticipantList = ProviderUtils.getJpaAndValidateList(inputParticipants, JpaParticipant::new, "participant");
81
82         inputAutomationCompositions = CODER.decode(originalAcJson, AutomationCompositions.class);
83         inputAutomationCompositionsJpa =
84             ProviderUtils.getJpaAndValidateList(inputAutomationCompositions.getAutomationCompositionList(),
85                 JpaAutomationComposition::new, "automation compositions");
86
87         nodeTemplateStateList.add(CODER.decode(nodeTemplateStatesJson, NodeTemplateState.class));
88         nodeTemplateStateList.get(0).setState(AcTypeState.COMMISSIONED);
89         jpaNodeTemplateStateList = ProviderUtils.getJpaAndValidateList(nodeTemplateStateList,
90             JpaNodeTemplateState::new, "node template state");
91     }
92
93     @Test
94     void testParticipantSave() {
95         var participantRepository = mock(ParticipantRepository.class);
96         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
97         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
98
99         for (var participant : jpaParticipantList) {
100             when(participantRepository.getById(participant.getParticipantId()))
101                 .thenReturn(participant);
102         }
103         var participantProvider = new ParticipantProvider(participantRepository,
104             automationCompositionElementRepository, nodeTemplateStateRepository);
105
106         assertThatThrownBy(() -> participantProvider.saveParticipant(null)).hasMessageMatching(LIST_IS_NULL);
107
108         when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0));
109
110         var savedParticipant = participantProvider.saveParticipant(inputParticipants.get(0));
111         savedParticipant.setParticipantId(inputParticipants.get(0).getParticipantId());
112
113         assertThat(savedParticipant).usingRecursiveComparison().isEqualTo(inputParticipants.get(0));
114     }
115
116     @Test
117     void testParticipantUpdate() {
118         var participantRepository = mock(ParticipantRepository.class);
119         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
120         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
121         for (var participant : jpaParticipantList) {
122             when(participantRepository.getById(participant.getParticipantId()))
123                 .thenReturn(participant);
124         }
125         var participantProvider = new ParticipantProvider(participantRepository,
126             automationCompositionElementRepository, nodeTemplateStateRepository);
127
128         assertThatThrownBy(() -> participantProvider.updateParticipant(null))
129             .hasMessageMatching(LIST_IS_NULL);
130
131         when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0));
132
133         var updatedParticipant = participantProvider.updateParticipant(inputParticipants.get(0));
134         updatedParticipant.setParticipantId(inputParticipants.get(0).getParticipantId());
135         assertThat(updatedParticipant).usingRecursiveComparison().isEqualTo(inputParticipants.get(0));
136     }
137
138     @Test
139     void testGetAutomationCompositions() {
140         var participantRepository = mock(ParticipantRepository.class);
141         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
142         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
143         var participantProvider = new ParticipantProvider(participantRepository,
144             automationCompositionElementRepository, nodeTemplateStateRepository);
145
146
147         assertThat(participantProvider.findParticipant(INVALID_ID)).isEmpty();
148
149         when(participantRepository.findAll()).thenReturn(jpaParticipantList);
150         assertThat(participantProvider.getParticipants()).hasSize(inputParticipants.size());
151
152         assertThatThrownBy(() -> participantProvider.getParticipantById(inputParticipants.get(0).getParticipantId()))
153                 .hasMessageMatching("Participant Not Found with ID: " + inputParticipants.get(0).getParticipantId());
154
155         when(participantRepository.findById(any())).thenReturn(
156             Optional.ofNullable(jpaParticipantList.get(0)));
157
158         var participant = participantProvider.getParticipantById(inputParticipants.get(0)
159             .getParticipantId());
160
161         assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(participant);
162     }
163
164     @Test
165     void testEmptyParticipant() {
166         var participantRepository = mock(ParticipantRepository.class);
167         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
168         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
169         var participantProvider = new ParticipantProvider(participantRepository,
170             automationCompositionElementRepository, nodeTemplateStateRepository);
171
172         assertThatThrownBy(() -> participantProvider.getParticipantById(INVALID_ID)).isInstanceOf(
173             PfModelRuntimeException.class).hasMessageMatching("Participant Not Found with ID:.*.");
174     }
175
176     @Test
177     void testDeleteParticipant() {
178         var participantRepository = mock(ParticipantRepository.class);
179         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
180         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
181         var participantProvider = new ParticipantProvider(participantRepository,
182             automationCompositionElementRepository, nodeTemplateStateRepository);
183
184         var participantId = inputParticipants.get(0).getParticipantId();
185         assertThatThrownBy(() -> participantProvider.deleteParticipant(participantId))
186             .hasMessageMatching(".*.failed, participant does not exist");
187
188         when(participantRepository.findById(participantId.toString()))
189             .thenReturn(Optional.of(jpaParticipantList.get(0)));
190
191         var deletedParticipant = participantProvider.deleteParticipant(participantId);
192         assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(deletedParticipant);
193     }
194
195     @Test
196     void testGetAutomationCompositionElements() {
197         var participantRepository = mock(ParticipantRepository.class);
198         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
199         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
200         var participantProvider = new ParticipantProvider(participantRepository,
201             automationCompositionElementRepository, nodeTemplateStateRepository);
202
203         var acElementList = inputAutomationCompositionsJpa
204             .stream().map(c -> c.getElements()).collect(Collectors.toList());
205
206         when(automationCompositionElementRepository.findByParticipantId(any())).thenReturn(acElementList.get(0));
207
208         var listOfAcElements = participantProvider.getAutomationCompositionElements(UUID.randomUUID());
209
210         assertThat(acElementList.get(0).equals(listOfAcElements));
211
212
213     }
214
215     @Test
216     void testGetAcNodeTemplateState() {
217         var participantRepository = mock(ParticipantRepository.class);
218         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
219         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
220         when(nodeTemplateStateRepository.findByParticipantId(any())).thenReturn(jpaNodeTemplateStateList);
221
222         var participantProvider = new ParticipantProvider(participantRepository,
223             automationCompositionElementRepository, nodeTemplateStateRepository);
224
225         var listOfNodeTemplateState = participantProvider.getAcNodeTemplateStates(
226             UUID.fromString(jpaParticipantList.get(0).getParticipantId()));
227
228         assertThat(listOfNodeTemplateState.equals(nodeTemplateStateList));
229
230     }
231
232     @Test
233     void testNotNullExceptions() {
234         var participantRepository = mock(ParticipantRepository.class);
235         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
236         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
237
238         var participantProvider = new ParticipantProvider(participantRepository,
239             automationCompositionElementRepository, nodeTemplateStateRepository);
240
241         assertThrows(NullPointerException.class, () -> participantProvider.getParticipantById(null));
242         assertThrows(NullPointerException.class, () -> participantProvider.findParticipant(null));
243         assertThrows(NullPointerException.class, () -> participantProvider.saveParticipant(null));
244         assertThrows(NullPointerException.class, () -> participantProvider.updateParticipant(null));
245         assertThrows(NullPointerException.class, () -> participantProvider.deleteParticipant(null));
246         assertThrows(NullPointerException.class, () -> participantProvider.getAutomationCompositionElements(null));
247         assertThrows(NullPointerException.class, () -> participantProvider.getAcNodeTemplateStates(null));
248
249
250     }
251
252     @Test
253     void testGetSupportedElementMap() {
254         var participantRepository = mock(ParticipantRepository.class);
255         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
256         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
257         when(participantRepository.findAll()).thenReturn(jpaParticipantList);
258         var participantProvider = new ParticipantProvider(participantRepository,
259             automationCompositionElementRepository, nodeTemplateStateRepository);
260
261         var result = participantProvider.getSupportedElementMap();
262         assertThat(result).hasSize(2);
263     }
264 }