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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.models.acm.persistence.provider;
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;
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;
46 class AutomationCompositionProviderTest {
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";
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";
56 private static final Coder CODER = new StandardCoder();
57 private static final String AUTOMATION_COMPOSITION_JSON =
58 "src/test/resources/providers/TestAutomationCompositions.json";
60 private AutomationCompositions inputAutomationCompositions;
61 private List<JpaAutomationComposition> inputAutomationCompositionsJpa;
62 private final String originalJson = ResourceUtils.getResourceAsString(AUTOMATION_COMPOSITION_JSON);
65 void beforeSetupDao() throws Exception {
66 inputAutomationCompositions = CODER.decode(originalJson, AutomationCompositions.class);
67 inputAutomationCompositionsJpa =
68 ProviderUtils.getJpaAndValidateList(inputAutomationCompositions.getAutomationCompositionList(),
69 JpaAutomationComposition::new, "automation compositions");
73 void testAutomationCompositionsSave() throws Exception {
74 var automationCompositionRepository = mock(AutomationCompositionRepository.class);
75 var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository);
77 assertThatThrownBy(() -> automationCompositionProvider.saveAutomationCompositions(null))
78 .hasMessageMatching(LIST_IS_NULL);
80 when(automationCompositionRepository.saveAll(inputAutomationCompositionsJpa))
81 .thenReturn(inputAutomationCompositionsJpa);
83 var createdAutomationCompositions = new AutomationCompositions();
84 createdAutomationCompositions.setAutomationCompositionList(automationCompositionProvider
85 .saveAutomationCompositions(inputAutomationCompositions.getAutomationCompositionList()));
87 assertEquals(inputAutomationCompositions, createdAutomationCompositions);
89 when(automationCompositionRepository.saveAll(any())).thenThrow(IllegalArgumentException.class);
91 assertThatThrownBy(() -> automationCompositionProvider
92 .saveAutomationCompositions(inputAutomationCompositions.getAutomationCompositionList()))
93 .hasMessageMatching("Error in save AutomationCompositions");
97 void testAutomationCompositionSave() throws Exception {
98 var automationCompositionRepository = mock(AutomationCompositionRepository.class);
99 var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository);
101 assertThatThrownBy(() -> automationCompositionProvider.saveAutomationComposition(null))
102 .hasMessageMatching(OBJECT_IS_NULL);
104 when(automationCompositionRepository.save(inputAutomationCompositionsJpa.get(0)))
105 .thenReturn(inputAutomationCompositionsJpa.get(0));
107 var createdAutomationComposition = automationCompositionProvider
108 .saveAutomationComposition(inputAutomationCompositions.getAutomationCompositionList().get(0));
110 assertEquals(inputAutomationCompositions.getAutomationCompositionList().get(0), createdAutomationComposition);
112 when(automationCompositionRepository.save(any())).thenThrow(IllegalArgumentException.class);
114 assertThatThrownBy(() -> automationCompositionProvider
115 .saveAutomationComposition(inputAutomationCompositions.getAutomationCompositionList().get(0)))
116 .hasMessageMatching("Error in save automationComposition");
120 void testGetAutomationCompositions() throws Exception {
121 var automationCompositionRepository = mock(AutomationCompositionRepository.class);
122 var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository);
124 automationCompositionProvider
125 .saveAutomationCompositions(inputAutomationCompositions.getAutomationCompositionList());
127 var automationComposition0 = inputAutomationCompositions.getAutomationCompositionList().get(1);
128 var name = automationComposition0.getName();
129 var version = automationComposition0.getVersion();
130 var automationComposition1 = inputAutomationCompositions.getAutomationCompositionList().get(1);
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)));
144 assertEquals(1, automationCompositionProvider.getAutomationCompositions(name, version).size());
146 var ac = automationCompositionProvider
147 .findAutomationComposition(new ToscaConceptIdentifier(ID_NAME, ID_VERSION))
148 .orElse(new AutomationComposition());
149 assertEquals(inputAutomationCompositions.getAutomationCompositionList().get(1), ac);
151 ac = automationCompositionProvider.getAutomationComposition(new ToscaConceptIdentifier(ID_NAME, ID_VERSION));
152 assertEquals(inputAutomationCompositions.getAutomationCompositionList().get(1), ac);
154 when(automationCompositionRepository.getById(any())).thenThrow(EntityNotFoundException.class);
156 assertThatThrownBy(() -> automationCompositionProvider
157 .getAutomationComposition(new ToscaConceptIdentifier(ID_NAME_NOT_EXTST, ID_VERSION)))
158 .hasMessageMatching("AutomationComposition not found");
160 ac = automationCompositionProvider.findAutomationComposition(ID_NAME, ID_VERSION)
161 .orElse(new AutomationComposition());
162 assertEquals(inputAutomationCompositions.getAutomationCompositionList().get(1), ac);
164 assertThat(automationCompositionProvider
165 .findAutomationComposition(new ToscaConceptIdentifier(ID_NAME_NOT_EXTST, ID_VERSION))).isEmpty();
167 when(automationCompositionRepository.findById(any())).thenThrow(IllegalArgumentException.class);
169 assertThatThrownBy(() -> automationCompositionProvider.findAutomationComposition(ID_NAME_NOT_VALID, ID_VERSION))
170 .hasMessageMatching("Not valid parameter");
174 void testDeleteAutomationComposition() throws Exception {
175 var automationCompositionRepository = mock(AutomationCompositionRepository.class);
176 var automationCompositionProvider = new AutomationCompositionProvider(automationCompositionRepository);
179 () -> automationCompositionProvider.deleteAutomationComposition(ID_NAME_NOT_EXTST, ID_VERSION))
180 .hasMessageMatching(".*.failed, automation composition does not exist");
182 var automationComposition = inputAutomationCompositions.getAutomationCompositionList().get(0);
183 var name = automationComposition.getName();
184 var version = automationComposition.getVersion();
186 when(automationCompositionRepository.findById(new PfConceptKey(name, version)))
187 .thenReturn(Optional.of(inputAutomationCompositionsJpa.get(0)));
189 AutomationComposition deletedAc = automationCompositionProvider.deleteAutomationComposition(name, version);
190 assertEquals(automationComposition, deletedAc);