2a5c15a099da19ae14c04fa26a591b697b337bf7
[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.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.when;
30
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Optional;
34 import java.util.UUID;
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
71     private final String originalAcJson = ResourceUtils.getResourceAsString(AUTOMATION_COMPOSITION_JSON);
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         var participantProvider = new ParticipantProvider(participantRepository,
100             automationCompositionElementRepository, nodeTemplateStateRepository);
101
102         assertThatThrownBy(() -> participantProvider.saveParticipant(null)).hasMessageMatching(LIST_IS_NULL);
103
104         when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0));
105
106         var savedParticipant = participantProvider.saveParticipant(inputParticipants.get(0));
107         savedParticipant.setParticipantId(inputParticipants.get(0).getParticipantId());
108
109         assertThat(savedParticipant).usingRecursiveComparison().isEqualTo(inputParticipants.get(0));
110     }
111
112     @Test
113     void testParticipantUpdate() {
114         var participantRepository = mock(ParticipantRepository.class);
115         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
116         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
117
118         var participantProvider = new ParticipantProvider(participantRepository,
119             automationCompositionElementRepository, nodeTemplateStateRepository);
120
121         assertThatThrownBy(() -> participantProvider.updateParticipant(null)).hasMessageMatching(LIST_IS_NULL);
122
123         when(participantRepository.save(any())).thenReturn(jpaParticipantList.get(0));
124
125         var updatedParticipant = participantProvider.updateParticipant(inputParticipants.get(0));
126         updatedParticipant.setParticipantId(inputParticipants.get(0).getParticipantId());
127         assertThat(updatedParticipant).usingRecursiveComparison().isEqualTo(inputParticipants.get(0));
128     }
129
130     @Test
131     void testGetAutomationCompositions() {
132         var participantRepository = mock(ParticipantRepository.class);
133         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
134         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
135         var participantProvider = new ParticipantProvider(participantRepository,
136             automationCompositionElementRepository, nodeTemplateStateRepository);
137
138         assertThat(participantProvider.findParticipant(INVALID_ID)).isEmpty();
139
140         when(participantRepository.findAll()).thenReturn(jpaParticipantList);
141         assertThat(participantProvider.getParticipants()).hasSize(inputParticipants.size());
142
143         assertThatThrownBy(() -> participantProvider.getParticipantById(inputParticipants.get(0).getParticipantId()))
144                 .hasMessageMatching("Participant Not Found with ID: " + inputParticipants.get(0).getParticipantId());
145
146         when(participantRepository.findById(any())).thenReturn(Optional.ofNullable(jpaParticipantList.get(0)));
147
148         var participant = participantProvider.getParticipantById(inputParticipants.get(0).getParticipantId());
149
150         assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(participant);
151     }
152
153     @Test
154     void testEmptyParticipant() {
155         var participantRepository = mock(ParticipantRepository.class);
156         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
157         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
158         var participantProvider = new ParticipantProvider(participantRepository,
159             automationCompositionElementRepository, nodeTemplateStateRepository);
160
161         assertThatThrownBy(() -> participantProvider.getParticipantById(INVALID_ID)).isInstanceOf(
162             PfModelRuntimeException.class).hasMessageMatching("Participant Not Found with ID:.*.");
163     }
164
165     @Test
166     void testDeleteParticipant() {
167         var participantRepository = mock(ParticipantRepository.class);
168         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
169         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
170         var participantProvider = new ParticipantProvider(participantRepository,
171             automationCompositionElementRepository, nodeTemplateStateRepository);
172
173         var participantId = inputParticipants.get(0).getParticipantId();
174         assertThatThrownBy(() -> participantProvider.deleteParticipant(participantId))
175             .hasMessageMatching(".*.failed, participant does not exist");
176
177         when(participantRepository.findById(participantId.toString()))
178             .thenReturn(Optional.of(jpaParticipantList.get(0)));
179
180         var deletedParticipant = participantProvider.deleteParticipant(participantId);
181         assertThat(inputParticipants.get(0)).usingRecursiveComparison().isEqualTo(deletedParticipant);
182     }
183
184     @Test
185     void testGetAutomationCompositionElements() {
186         var participantRepository = mock(ParticipantRepository.class);
187         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
188         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
189         var participantProvider = new ParticipantProvider(participantRepository,
190             automationCompositionElementRepository, nodeTemplateStateRepository);
191
192         var acElementList = inputAutomationCompositionsJpa.get(0).getElements();
193
194         var participantId = UUID.randomUUID();
195         when(automationCompositionElementRepository.findByParticipantId(participantId.toString()))
196                 .thenReturn(acElementList);
197
198         var listOfAcElements = participantProvider.getAutomationCompositionElements(participantId);
199
200         assertThat(listOfAcElements).hasSameSizeAs(acElementList);
201         assertEquals(UUID.fromString(acElementList.get(0).getElementId()), listOfAcElements.get(0).getId());
202     }
203
204     @Test
205     void testGetAcNodeTemplateState() {
206         var participantRepository = mock(ParticipantRepository.class);
207         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
208         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
209         var participantId = jpaParticipantList.get(0).getParticipantId();
210         when(nodeTemplateStateRepository.findByParticipantId(participantId)).thenReturn(jpaNodeTemplateStateList);
211
212         var participantProvider = new ParticipantProvider(participantRepository,
213             automationCompositionElementRepository, nodeTemplateStateRepository);
214
215         var listOfNodeTemplateState = participantProvider.getAcNodeTemplateStates(UUID.fromString(participantId));
216
217         assertEquals(listOfNodeTemplateState, nodeTemplateStateList);
218     }
219
220     @Test
221     void testNotNullExceptions() {
222         var participantRepository = mock(ParticipantRepository.class);
223         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
224         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
225
226         var participantProvider = new ParticipantProvider(participantRepository,
227             automationCompositionElementRepository, nodeTemplateStateRepository);
228
229         assertThrows(NullPointerException.class, () -> participantProvider.getParticipantById(null));
230         assertThrows(NullPointerException.class, () -> participantProvider.findParticipant(null));
231         assertThrows(NullPointerException.class, () -> participantProvider.saveParticipant(null));
232         assertThrows(NullPointerException.class, () -> participantProvider.updateParticipant(null));
233         assertThrows(NullPointerException.class, () -> participantProvider.deleteParticipant(null));
234         assertThrows(NullPointerException.class, () -> participantProvider.getAutomationCompositionElements(null));
235         assertThrows(NullPointerException.class, () -> participantProvider.getAcNodeTemplateStates(null));
236     }
237
238     @Test
239     void testGetSupportedElementMap() {
240         var participantRepository = mock(ParticipantRepository.class);
241         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
242         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
243         when(participantRepository.findAll()).thenReturn(jpaParticipantList);
244         var participantProvider = new ParticipantProvider(participantRepository,
245             automationCompositionElementRepository, nodeTemplateStateRepository);
246
247         var result = participantProvider.getSupportedElementMap();
248         assertThat(result).hasSize(2);
249     }
250
251     @Test
252     void testGetCompositionIds() {
253         var nodeTemplateStateRepository = mock(NodeTemplateStateRepository.class);
254         var participantId = UUID.randomUUID();
255         when(nodeTemplateStateRepository.findByParticipantId(participantId.toString()))
256                 .thenReturn(jpaNodeTemplateStateList);
257         var participantRepository = mock(ParticipantRepository.class);
258         var automationCompositionElementRepository = mock(AutomationCompositionElementRepository.class);
259
260         var participantProvider = new ParticipantProvider(participantRepository,
261             automationCompositionElementRepository, nodeTemplateStateRepository);
262
263         assertThatThrownBy(() -> participantProvider.getCompositionIds(null)).hasMessageMatching(LIST_IS_NULL);
264
265         var result = participantProvider.getCompositionIds(participantId);
266         assertThat(result).hasSize(1);
267     }
268 }