9ceeef640604bdcd697168ec760eb04828e862e7
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2024 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.junit.jupiter.api.Assertions.assertThrows;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Optional;
36 import java.util.Set;
37 import java.util.UUID;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
41 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
42 import org.onap.policy.clamp.models.acm.concepts.NodeTemplateState;
43 import org.onap.policy.clamp.models.acm.concepts.Participant;
44 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
45 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationComposition;
46 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaNodeTemplateState;
47 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaParticipant;
48 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionElementRepository;
49 import org.onap.policy.clamp.models.acm.persistence.repository.NodeTemplateStateRepository;
50 import org.onap.policy.clamp.models.acm.persistence.repository.ParticipantReplicaRepository;
51 import org.onap.policy.clamp.models.acm.persistence.repository.ParticipantRepository;
52 import org.onap.policy.clamp.models.acm.utils.CommonTestData;
53 import org.onap.policy.common.utils.coder.Coder;
54 import org.onap.policy.common.utils.coder.StandardCoder;
55 import org.onap.policy.common.utils.resources.ResourceUtils;
56 import org.onap.policy.models.base.PfModelRuntimeException;
57
58 class ParticipantProviderTest {
59
60     private static final Coder CODER = new StandardCoder();
61     private static final String PARTICIPANT_JSON = "src/test/resources/providers/TestParticipant.json";
62
63     private static final String AUTOMATION_COMPOSITION_JSON =
64         "src/test/resources/providers/TestAutomationCompositions.json";
65
66     private static final String NODE_TEMPLATE_STATE_JSON = "src/test/resources/providers/NodeTemplateState.json";
67     private static final String LIST_IS_NULL = ".*. is marked .*ull but is null";
68     private static final UUID INVALID_ID = UUID.randomUUID();
69
70     private final List<Participant> inputParticipants = new ArrayList<>();
71     private List<JpaParticipant> jpaParticipantList;
72     private List<JpaAutomationComposition> inputAutomationCompositionsJpa;
73
74     private final List<NodeTemplateState> nodeTemplateStateList = new ArrayList<>();
75     private List<JpaNodeTemplateState> jpaNodeTemplateStateList;
76
77     @BeforeEach
78     void beforeSetup() throws Exception {
79         var originalJson = ResourceUtils.getResourceAsString(PARTICIPANT_JSON);
80         inputParticipants.add(CODER.decode(originalJson, Participant.class));
81         jpaParticipantList = ProviderUtils.getJpaAndValidateList(inputParticipants, JpaParticipant::new, "participant");
82
83         var originalAcJson = ResourceUtils.getResourceAsString(AUTOMATION_COMPOSITION_JSON);
84         var inputAutomationCompositions = CODER.decode(originalAcJson, AutomationCompositions.class);
85         inputAutomationCompositionsJpa =
86             ProviderUtils.getJpaAndValidateList(inputAutomationCompositions.getAutomationCompositionList(),
87                 JpaAutomationComposition::new, "automation compositions");
88
89         var nodeTemplateStatesJson = ResourceUtils.getResourceAsString(NODE_TEMPLATE_STATE_JSON);
90         nodeTemplateStateList.add(CODER.decode(nodeTemplateStatesJson, NodeTemplateState.class));
91         nodeTemplateStateList.get(0).setState(AcTypeState.COMMISSIONED);
92         jpaNodeTemplateStateList = ProviderUtils.getJpaAndValidateList(nodeTemplateStateList,
93             JpaNodeTemplateState::new, "node template state");
94     }
95
96     @Test
97     void testParticipantSave() {
98         var participantRepository = mock(ParticipantRepository.class);
99         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
100         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
101
102         var participantProvider = new ParticipantProvider(participantRepository,
103             automationCompositionElementRepository, nodeTemplateStateRepository,
104             mock(ParticipantReplicaRepository.class));
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
122         var participantProvider = new ParticipantProvider(participantRepository,
123             automationCompositionElementRepository, nodeTemplateStateRepository,
124             mock(ParticipantReplicaRepository.class));
125
126         assertThatThrownBy(() -> participantProvider.updateParticipant(null)).hasMessageMatching(LIST_IS_NULL);
127
128         when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0));
129
130         var updatedParticipant = participantProvider.updateParticipant(inputParticipants.get(0));
131         updatedParticipant.setParticipantId(inputParticipants.get(0).getParticipantId());
132         assertThat(updatedParticipant).usingRecursiveComparison().isEqualTo(inputParticipants.get(0));
133     }
134
135     @Test
136     void testGetAutomationCompositions() {
137         var participantRepository = mock(ParticipantRepository.class);
138         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
139         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
140         var participantProvider = new ParticipantProvider(participantRepository,
141             automationCompositionElementRepository, nodeTemplateStateRepository,
142             mock(ParticipantReplicaRepository.class));
143
144         assertThat(participantProvider.findParticipant(INVALID_ID)).isEmpty();
145
146         when(participantRepository.findAll()).thenReturn(jpaParticipantList);
147         assertThat(participantProvider.getParticipants()).hasSize(inputParticipants.size());
148
149         assertThatThrownBy(() -> participantProvider.getParticipantById(inputParticipants.get(0).getParticipantId()))
150                 .hasMessageMatching("Participant Not Found with ID: " + inputParticipants.get(0).getParticipantId());
151
152         when(participantRepository.findById(any())).thenReturn(Optional.ofNullable(jpaParticipantList.get(0)));
153
154         var participant = participantProvider.getParticipantById(inputParticipants.get(0).getParticipantId());
155
156         assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(participant);
157     }
158
159     @Test
160     void testEmptyParticipant() {
161         var participantRepository = mock(ParticipantRepository.class);
162         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
163         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
164         var participantProvider = new ParticipantProvider(participantRepository,
165             automationCompositionElementRepository, nodeTemplateStateRepository,
166             mock(ParticipantReplicaRepository.class));
167
168         assertThatThrownBy(() -> participantProvider.getParticipantById(INVALID_ID)).isInstanceOf(
169             PfModelRuntimeException.class).hasMessageMatching("Participant Not Found with ID:.*.");
170     }
171
172     @Test
173     void testDeleteParticipant() {
174         var participantRepository = mock(ParticipantRepository.class);
175         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
176         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
177         var participantProvider = new ParticipantProvider(participantRepository,
178             automationCompositionElementRepository, nodeTemplateStateRepository,
179             mock(ParticipantReplicaRepository.class));
180
181         var participantId = inputParticipants.get(0).getParticipantId();
182         assertThatThrownBy(() -> participantProvider.deleteParticipant(participantId))
183             .hasMessageMatching(".*.failed, participant does not exist");
184
185         when(participantRepository.findById(participantId.toString()))
186             .thenReturn(Optional.of(jpaParticipantList.get(0)));
187
188         var deletedParticipant = participantProvider.deleteParticipant(participantId);
189         assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(deletedParticipant);
190     }
191
192     @Test
193     void testGetAutomationCompositionElements() {
194         var participantRepository = mock(ParticipantRepository.class);
195         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
196         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
197         var participantProvider = new ParticipantProvider(participantRepository,
198             automationCompositionElementRepository, nodeTemplateStateRepository,
199             mock(ParticipantReplicaRepository.class));
200
201         var acElementList = inputAutomationCompositionsJpa.get(0).getElements();
202
203         var participantId = UUID.randomUUID();
204         when(automationCompositionElementRepository.findByParticipantId(participantId.toString()))
205             .thenReturn(acElementList);
206
207         var listOfAcElements = participantProvider.getAutomationCompositionElements(participantId);
208
209         assertThat(listOfAcElements).hasSameSizeAs(acElementList);
210         assertEquals(UUID.fromString(acElementList.get(0).getElementId()), listOfAcElements.get(0).getId());
211     }
212
213     @Test
214     void testGetAcNodeTemplateState() {
215         var participantRepository = mock(ParticipantRepository.class);
216         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
217         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
218         var participantId = jpaParticipantList.get(0).getParticipantId();
219         when(nodeTemplateStateRepository.findByParticipantId(participantId)).thenReturn(jpaNodeTemplateStateList);
220
221         var participantProvider = new ParticipantProvider(participantRepository,
222             automationCompositionElementRepository, nodeTemplateStateRepository,
223             mock(ParticipantReplicaRepository.class));
224
225         var listOfNodeTemplateState = participantProvider.getAcNodeTemplateStates(UUID.fromString(participantId));
226
227         assertEquals(listOfNodeTemplateState, nodeTemplateStateList);
228     }
229
230     @Test
231     void testNotNullExceptions() {
232         var participantRepository = mock(ParticipantRepository.class);
233         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
234         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
235
236         var participantProvider = new ParticipantProvider(participantRepository,
237             automationCompositionElementRepository, nodeTemplateStateRepository,
238             mock(ParticipantReplicaRepository.class));
239
240         assertThrows(NullPointerException.class, () -> participantProvider.getParticipantById(null));
241         assertThrows(NullPointerException.class, () -> participantProvider.findParticipant(null));
242         assertThrows(NullPointerException.class, () -> participantProvider.saveParticipant(null));
243         assertThrows(NullPointerException.class, () -> participantProvider.updateParticipant(null));
244         assertThrows(NullPointerException.class, () -> participantProvider.deleteParticipant(null));
245         assertThrows(NullPointerException.class, () -> participantProvider.getAutomationCompositionElements(null));
246         assertThrows(NullPointerException.class, () -> participantProvider.getAcNodeTemplateStates(null));
247         assertThrows(NullPointerException.class, () -> participantProvider.findParticipantReplica(null));
248         assertThrows(NullPointerException.class, () -> participantProvider.saveParticipantReplica(null));
249         assertThrows(NullPointerException.class, () -> participantProvider.deleteParticipantReplica(null));
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             mock(ParticipantReplicaRepository.class));
261
262         var result = participantProvider.getSupportedElementMap();
263         assertThat(result).hasSize(2);
264     }
265
266     @Test
267     void testGetCompositionIds() {
268         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
269         var participantId = UUID.randomUUID();
270         when(nodeTemplateStateRepository.findByParticipantId(participantId.toString()))
271                 .thenReturn(jpaNodeTemplateStateList);
272         var participantRepository = mock(ParticipantRepository.class);
273         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
274
275         var participantProvider = new ParticipantProvider(participantRepository,
276             automationCompositionElementRepository, nodeTemplateStateRepository,
277             mock(ParticipantReplicaRepository.class));
278
279         assertThatThrownBy(() -> participantProvider.getCompositionIds(null)).hasMessageMatching(LIST_IS_NULL);
280
281         var result = participantProvider.getCompositionIds(participantId);
282         assertThat(result).hasSize(1);
283     }
284
285     @Test
286     void testFindParticipantReplica() {
287         var replicaRepository = mock(ParticipantReplicaRepository.class);
288         var replica = inputParticipants.get(0).getReplicas().values().iterator().next();
289         var jpaReplica = jpaParticipantList.get(0).getReplicas().get(0);
290         when(replicaRepository.findById(replica.getReplicaId().toString())).thenReturn(Optional.of(jpaReplica));
291         var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
292                 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
293                 replicaRepository);
294
295         var result = participantProvider.findParticipantReplica(replica.getReplicaId());
296         assertThat(result).isNotEmpty();
297         assertEquals(replica.getReplicaId(), result.get().getReplicaId());
298     }
299
300     @Test
301     void testFindReplicasOnLine() {
302         var replicaRepository = mock(ParticipantReplicaRepository.class);
303         var replica = inputParticipants.get(0).getReplicas().values().iterator().next();
304         var jpaReplica = jpaParticipantList.get(0).getReplicas().get(0);
305         jpaReplica.fromAuthorative(replica);
306         when(replicaRepository.findByParticipantState(ParticipantState.ON_LINE)).thenReturn(List.of(jpaReplica));
307         var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
308                 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
309                 replicaRepository);
310
311         var result = participantProvider.findReplicasOnLine();
312         assertThat(result).hasSize(1);
313         assertEquals(replica.getReplicaId(), result.get(0).getReplicaId());
314     }
315
316     @Test
317     void testSaveParticipantReplica() {
318         var jpaReplica = jpaParticipantList.get(0).getReplicas().get(0);
319         var replicaRepository = mock(ParticipantReplicaRepository.class);
320         when(replicaRepository.getReferenceById(jpaReplica.getReplicaId())).thenReturn(jpaReplica);
321         var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
322                 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
323                 replicaRepository);
324
325         var replica = inputParticipants.get(0).getReplicas().values().iterator().next();
326         participantProvider.saveParticipantReplica(replica);
327         verify(replicaRepository).save(any());
328     }
329
330     @Test
331     void testDeleteParticipantReplica() {
332         var replicaRepository = mock(ParticipantReplicaRepository.class);
333         var participantProvider = new ParticipantProvider(mock(ParticipantRepository.class),
334                 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
335                 replicaRepository);
336         participantProvider.deleteParticipantReplica(CommonTestData.getReplicaId());
337         verify(replicaRepository).deleteById(CommonTestData.getReplicaId().toString());
338     }
339
340     @Test
341     void testVerifyParticipantState() {
342         var jpaParticipant = new JpaParticipant(jpaParticipantList.get(0));
343         var participantId = jpaParticipant.getParticipantId();
344         var participantRepository = mock(ParticipantRepository.class);
345         when(participantRepository.getReferenceById(participantId)).thenReturn(jpaParticipant);
346
347         var replicaRepository = mock(ParticipantReplicaRepository.class);
348         var participantProvider = new ParticipantProvider(participantRepository,
349                 mock(AutomationCompositionElementRepository.class), mock(NodeTemplateStateRepository.class),
350                 replicaRepository);
351
352         jpaParticipant.setReplicas(List.of());
353         var set = Set.of(UUID.fromString(participantId));
354         assertThatThrownBy(() -> participantProvider.verifyParticipantState(set))
355                 .hasMessageMatching("Participant: " + participantId + " is OFFLINE");
356
357         when(participantRepository.getReferenceById(participantId)).thenReturn(jpaParticipantList.get(0));
358         participantProvider.verifyParticipantState(set);
359         verify(participantRepository, times(2)).getReferenceById(participantId);
360     }
361 }