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