5be603d0d554c1cdc00f3c5d59c8d7ec9ab67a6d
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2022 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.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.List;
32 import java.util.Optional;
33 import javax.persistence.EntityNotFoundException;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
38 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationComposition;
39 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionRepository;
40 import org.onap.policy.common.utils.coder.Coder;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.onap.policy.common.utils.resources.ResourceUtils;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
45
46 class AutomationCompositionProviderTest {
47
48     private static final String LIST_IS_NULL = "automationCompositions is marked .*ull but is null";
49     private static final String OBJECT_IS_NULL = "automationComposition is marked non-null but is null";
50
51     private static final String ID_NAME = "PMSHInstance1";
52     private static final String ID_VERSION = "1.0.1";
53     private static final String ID_NAME_NOT_EXTST = "not_exist";
54     private static final String ID_NAME_NOT_VALID = "not_valid";
55
56     private static final Coder CODER = new StandardCoder();
57     private static final String AUTOMATION_COMPOSITION_JSON =
58             "src/test/resources/providers/TestAutomationCompositions.json";
59
60     private AutomationCompositions inputAutomationCompositions;
61     private List<JpaAutomationComposition> inputAutomationCompositionsJpa;
62     private final String originalJson = ResourceUtils.getResourceAsString(AUTOMATION_COMPOSITION_JSON);
63
64     @BeforeEach
65     void beforeSetupDao() throws Exception {
66         inputAutomationCompositions = CODER.decode(originalJson, AutomationCompositions.class);
67         inputAutomationCompositionsJpa =
68                 ProviderUtils.getJpaAndValidateList(inputAutomationCompositions.getAutomationCompositionList(),
69                         JpaAutomationComposition::new, "automation compositions");
70     }
71
72     @Test
73     void testAutomationCompositionsSave() throws Exception {
74         var automationCompositionRepository = mock(AutomationCompositionRepository.class);
75         var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository);
76
77         assertThatThrownBy(() -> automationCompositionProvider.saveAutomationCompositions(null))
78                 .hasMessageMatching(LIST_IS_NULL);
79
80         when(automationCompositionRepository.saveAll(inputAutomationCompositionsJpa))
81                 .thenReturn(inputAutomationCompositionsJpa);
82
83         var createdAutomationCompositions = new AutomationCompositions();
84         createdAutomationCompositions.setAutomationCompositionList(automationCompositionProvider
85                 .saveAutomationCompositions(inputAutomationCompositions.getAutomationCompositionList()));
86
87         assertEquals(inputAutomationCompositions, createdAutomationCompositions);
88
89         when(automationCompositionRepository.saveAll(any())).thenThrow(IllegalArgumentException.class);
90
91         assertThatThrownBy(() -> automationCompositionProvider
92                 .saveAutomationCompositions(inputAutomationCompositions.getAutomationCompositionList()))
93                         .hasMessageMatching("Error in save AutomationCompositions");
94     }
95
96     @Test
97     void testAutomationCompositionSave() throws Exception {
98         var automationCompositionRepository = mock(AutomationCompositionRepository.class);
99         var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository);
100
101         assertThatThrownBy(() -> automationCompositionProvider.saveAutomationComposition(null))
102                 .hasMessageMatching(OBJECT_IS_NULL);
103
104         when(automationCompositionRepository.save(inputAutomationCompositionsJpa.get(0)))
105                 .thenReturn(inputAutomationCompositionsJpa.get(0));
106
107         var createdAutomationComposition = automationCompositionProvider
108                 .saveAutomationComposition(inputAutomationCompositions.getAutomationCompositionList().get(0));
109
110         assertEquals(inputAutomationCompositions.getAutomationCompositionList().get(0), createdAutomationComposition);
111
112         when(automationCompositionRepository.save(any())).thenThrow(IllegalArgumentException.class);
113
114         assertThatThrownBy(() -> automationCompositionProvider
115                 .saveAutomationComposition(inputAutomationCompositions.getAutomationCompositionList().get(0)))
116                         .hasMessageMatching("Error in save automationComposition");
117     }
118
119     @Test
120     void testGetAutomationCompositions() throws Exception {
121         var automationCompositionRepository = mock(AutomationCompositionRepository.class);
122         var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository);
123
124         automationCompositionProvider
125                 .saveAutomationCompositions(inputAutomationCompositions.getAutomationCompositionList());
126
127         var automationComposition0 = inputAutomationCompositions.getAutomationCompositionList().get(1);
128         var name = automationComposition0.getName();
129         var version = automationComposition0.getVersion();
130         var automationComposition1 = inputAutomationCompositions.getAutomationCompositionList().get(1);
131
132         when(automationCompositionRepository.getFiltered(eq(JpaAutomationComposition.class), any(), any()))
133                 .thenReturn(List.of(new JpaAutomationComposition(automationComposition0),
134                         new JpaAutomationComposition(automationComposition1)));
135         when(automationCompositionRepository.findById(automationComposition0.getKey().asIdentifier().asConceptKey()))
136                 .thenReturn(Optional.of(new JpaAutomationComposition(automationComposition0)));
137         when(automationCompositionRepository.getById(automationComposition0.getKey().asIdentifier().asConceptKey()))
138                 .thenReturn(new JpaAutomationComposition(automationComposition0));
139         when(automationCompositionRepository.getFiltered(JpaAutomationComposition.class, name, version))
140                 .thenReturn(List.of(new JpaAutomationComposition(automationComposition0)));
141         when(automationCompositionRepository.findById(automationComposition1.getKey().asIdentifier().asConceptKey()))
142                 .thenReturn(Optional.of(new JpaAutomationComposition(automationComposition1)));
143
144         assertEquals(1, automationCompositionProvider.getAutomationCompositions(name, version).size());
145
146         var ac = automationCompositionProvider
147                 .findAutomationComposition(new ToscaConceptIdentifier(ID_NAME, ID_VERSION))
148                 .orElse(new AutomationComposition());
149         assertEquals(inputAutomationCompositions.getAutomationCompositionList().get(1), ac);
150
151         ac = automationCompositionProvider.getAutomationComposition(new ToscaConceptIdentifier(ID_NAME, ID_VERSION));
152         assertEquals(inputAutomationCompositions.getAutomationCompositionList().get(1), ac);
153
154         when(automationCompositionRepository.getById(any())).thenThrow(EntityNotFoundException.class);
155
156         assertThatThrownBy(() -> automationCompositionProvider
157                 .getAutomationComposition(new ToscaConceptIdentifier(ID_NAME_NOT_EXTST, ID_VERSION)))
158                         .hasMessageMatching("AutomationComposition not found");
159
160         ac = automationCompositionProvider.findAutomationComposition(ID_NAME, ID_VERSION)
161                 .orElse(new AutomationComposition());
162         assertEquals(inputAutomationCompositions.getAutomationCompositionList().get(1), ac);
163
164         assertThat(automationCompositionProvider
165                 .findAutomationComposition(new ToscaConceptIdentifier(ID_NAME_NOT_EXTST, ID_VERSION))).isEmpty();
166
167         when(automationCompositionRepository.findById(any())).thenThrow(IllegalArgumentException.class);
168
169         assertThatThrownBy(() -> automationCompositionProvider.findAutomationComposition(ID_NAME_NOT_VALID, ID_VERSION))
170                 .hasMessageMatching("Not valid parameter");
171     }
172
173     @Test
174     void testDeleteAutomationComposition() throws Exception {
175         var automationCompositionRepository = mock(AutomationCompositionRepository.class);
176         var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository);
177
178         assertThatThrownBy(
179                 () -> automationCompositionProvider.deleteAutomationComposition(ID_NAME_NOT_EXTST, ID_VERSION))
180                         .hasMessageMatching(".*.failed, automation composition does not exist");
181
182         var automationComposition = inputAutomationCompositions.getAutomationCompositionList().get(0);
183         var name = automationComposition.getName();
184         var version = automationComposition.getVersion();
185
186         when(automationCompositionRepository.findById(new PfConceptKey(name, version)))
187                 .thenReturn(Optional.of(inputAutomationCompositionsJpa.get(0)));
188
189         AutomationComposition deletedAc = automationCompositionProvider.deleteAutomationComposition(name, version);
190         assertEquals(automationComposition, deletedAc);
191     }
192 }