2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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.controlloop.models.controlloop.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.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
33 import java.util.List;
34 import java.util.Optional;
35 import javax.persistence.EntityNotFoundException;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops;
40 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoop;
41 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ControlLoopRepository;
42 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ToscaNodeTemplateRepository;
43 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ToscaNodeTemplatesRepository;
44 import org.onap.policy.common.utils.coder.Coder;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
47 import org.onap.policy.common.utils.resources.ResourceUtils;
48 import org.onap.policy.models.base.PfConceptKey;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
53 import org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate;
55 class ControlLoopProviderTest {
57 private static final String LIST_IS_NULL = "controlLoops is marked .*ull but is null";
58 private static final String OBJECT_IS_NULL = "controlLoop is marked non-null but is null";
60 private static final String ID_NAME = "PMSHInstance1";
61 private static final String ID_VERSION = "1.0.1";
62 private static final String ID_NAME_NOT_EXTST = "not_exist";
63 private static final String ID_NAME_NOT_VALID = "not_valid";
65 private static final Coder CODER = new StandardCoder();
66 private static final String CONTROL_LOOP_JSON = "src/test/resources/providers/TestControlLoops.json";
67 private static final String TOSCA_TEMPLATE_YAML = "examples/controlloop/PMSubscriptionHandling.yaml";
69 private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
71 private ControlLoops inputControlLoops;
72 private List<JpaControlLoop> inputControlLoopsJpa;
73 private String originalJson = ResourceUtils.getResourceAsString(CONTROL_LOOP_JSON);
76 void beforeSetupDao() throws Exception {
77 inputControlLoops = CODER.decode(originalJson, ControlLoops.class);
78 inputControlLoopsJpa = ProviderUtils.getJpaAndValidateList(inputControlLoops.getControlLoopList(),
79 JpaControlLoop::new, "control loops");
83 void testControlLoopsSave() throws Exception {
84 var controlLoopRepository = mock(ControlLoopRepository.class);
85 var controlLoopProvider = new ControlLoopProvider(controlLoopRepository,
86 mock(ToscaNodeTemplateRepository.class), mock(ToscaNodeTemplatesRepository.class));
88 assertThatThrownBy(() -> {
89 controlLoopProvider.saveControlLoops(null);
90 }).hasMessageMatching(LIST_IS_NULL);
92 when(controlLoopRepository.saveAll(inputControlLoopsJpa)).thenReturn(inputControlLoopsJpa);
94 var createdControlLoops = new ControlLoops();
96 .setControlLoopList(controlLoopProvider.saveControlLoops(inputControlLoops.getControlLoopList()));
98 assertEquals(inputControlLoops, createdControlLoops);
100 when(controlLoopRepository.saveAll(any())).thenThrow(IllegalArgumentException.class);
102 assertThatThrownBy(() -> {
103 controlLoopProvider.saveControlLoops(inputControlLoops.getControlLoopList());
104 }).hasMessageMatching("Error in save ControlLoops");
108 void testControlLoopSave() throws Exception {
109 var controlLoopRepository = mock(ControlLoopRepository.class);
110 var controlLoopProvider = new ControlLoopProvider(controlLoopRepository,
111 mock(ToscaNodeTemplateRepository.class), mock(ToscaNodeTemplatesRepository.class));
113 assertThatThrownBy(() -> {
114 controlLoopProvider.saveControlLoop(null);
115 }).hasMessageMatching(OBJECT_IS_NULL);
117 when(controlLoopRepository.save(inputControlLoopsJpa.get(0))).thenReturn(inputControlLoopsJpa.get(0));
119 var createdControlLoop = controlLoopProvider.saveControlLoop(inputControlLoops.getControlLoopList().get(0));
121 assertEquals(inputControlLoops.getControlLoopList().get(0), createdControlLoop);
123 when(controlLoopRepository.save(any())).thenThrow(IllegalArgumentException.class);
125 assertThatThrownBy(() -> {
126 controlLoopProvider.saveControlLoop(inputControlLoops.getControlLoopList().get(0));
127 }).hasMessageMatching("Error in save controlLoop");
131 void testGetControlLoops() throws Exception {
132 var controlLoopRepository = mock(ControlLoopRepository.class);
133 var controlLoopProvider = new ControlLoopProvider(controlLoopRepository,
134 mock(ToscaNodeTemplateRepository.class), mock(ToscaNodeTemplatesRepository.class));
136 // Return empty list when no data present in db
137 List<ControlLoop> getResponse = controlLoopProvider.getControlLoops();
138 assertThat(getResponse).isEmpty();
140 controlLoopProvider.saveControlLoops(inputControlLoops.getControlLoopList());
142 var controlLoop0 = inputControlLoops.getControlLoopList().get(1);
143 var name = controlLoop0.getName();
144 var version = controlLoop0.getVersion();
145 var controlLoop1 = inputControlLoops.getControlLoopList().get(1);
147 when(controlLoopRepository.getFiltered(eq(JpaControlLoop.class), any(), any()))
148 .thenReturn(List.of(new JpaControlLoop(controlLoop0), new JpaControlLoop(controlLoop1)));
149 when(controlLoopRepository.findById(controlLoop0.getKey().asIdentifier().asConceptKey()))
150 .thenReturn(Optional.of(new JpaControlLoop(controlLoop0)));
151 when(controlLoopRepository.getById(controlLoop0.getKey().asIdentifier().asConceptKey()))
152 .thenReturn(new JpaControlLoop(controlLoop0));
153 when(controlLoopRepository.getFiltered(JpaControlLoop.class, name, version))
154 .thenReturn(List.of(new JpaControlLoop(controlLoop0)));
155 when(controlLoopRepository.findById(controlLoop1.getKey().asIdentifier().asConceptKey()))
156 .thenReturn(Optional.of(new JpaControlLoop(controlLoop1)));
158 assertEquals(1, controlLoopProvider.getControlLoops(name, version).size());
160 var cl = controlLoopProvider.findControlLoop(new ToscaConceptIdentifier(ID_NAME, ID_VERSION)).get();
161 assertEquals(inputControlLoops.getControlLoopList().get(1), cl);
163 cl = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(ID_NAME, ID_VERSION));
164 assertEquals(inputControlLoops.getControlLoopList().get(1), cl);
166 when(controlLoopRepository.getById(any())).thenThrow(EntityNotFoundException.class);
168 assertThatThrownBy(() -> {
169 controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(ID_NAME_NOT_EXTST, ID_VERSION));
170 }).hasMessageMatching("ControlLoop not found");
172 cl = controlLoopProvider.findControlLoop(ID_NAME, ID_VERSION).get();
173 assertEquals(inputControlLoops.getControlLoopList().get(1), cl);
175 assertThat(controlLoopProvider.findControlLoop(new ToscaConceptIdentifier(ID_NAME_NOT_EXTST, ID_VERSION)))
178 when(controlLoopRepository.findById(any())).thenThrow(IllegalArgumentException.class);
180 assertThatThrownBy(() -> {
181 controlLoopProvider.findControlLoop(ID_NAME_NOT_VALID, ID_VERSION);
182 }).hasMessageMatching("Not valid parameter");
186 void testDeleteControlLoop() throws Exception {
187 var controlLoopRepository = mock(ControlLoopRepository.class);
188 var controlLoopProvider = new ControlLoopProvider(controlLoopRepository,
189 mock(ToscaNodeTemplateRepository.class), mock(ToscaNodeTemplatesRepository.class));
191 assertThatThrownBy(() -> {
192 controlLoopProvider.deleteControlLoop(ID_NAME_NOT_EXTST, ID_VERSION);
193 }).hasMessageMatching(".*.failed, control loop does not exist");
195 var controlLoop = inputControlLoops.getControlLoopList().get(0);
196 var name = controlLoop.getName();
197 var version = controlLoop.getVersion();
199 when(controlLoopRepository.findById(new PfConceptKey(name, version)))
200 .thenReturn(Optional.of(inputControlLoopsJpa.get(0)));
202 ControlLoop deletedCl = controlLoopProvider.deleteControlLoop(name, version);
203 assertEquals(controlLoop, deletedCl);
207 void testDeleteAllInstanceProperties() throws Exception {
208 var controlLoopProvider = new ControlLoopProvider(mock(ControlLoopRepository.class),
209 mock(ToscaNodeTemplateRepository.class), mock(ToscaNodeTemplatesRepository.class));
210 var toscaServiceTemplate = testControlLoopRead();
211 controlLoopProvider.deleteInstanceProperties(controlLoopProvider.saveInstanceProperties(toscaServiceTemplate),
212 controlLoopProvider.getNodeTemplates(null, null));
213 assertThat(controlLoopProvider.getControlLoops()).isEmpty();
217 void testSaveAndDeleteInstanceProperties() throws Exception {
218 var toscaNodeTemplatesRepository = mock(ToscaNodeTemplatesRepository.class);
219 var toscaNodeTemplateRepository = mock(ToscaNodeTemplateRepository.class);
220 var controlLoopProvider = new ControlLoopProvider(mock(ControlLoopRepository.class),
221 toscaNodeTemplateRepository, toscaNodeTemplatesRepository);
222 var toscaServiceTest = testControlLoopRead();
224 controlLoopProvider.saveInstanceProperties(toscaServiceTest);
225 verify(toscaNodeTemplatesRepository).save(any());
227 var name = "org.onap.policy.controlloop.PolicyControlLoopParticipant";
228 var version = "2.3.1";
229 var elem = toscaServiceTest.getToscaTopologyTemplate().getNodeTemplates().get(name);
230 when(toscaNodeTemplateRepository.getFiltered(JpaToscaNodeTemplate.class, name, version))
231 .thenReturn(List.of(new JpaToscaNodeTemplate(elem)));
233 var filtered = controlLoopProvider.getNodeTemplates(name, version);
234 verify(toscaNodeTemplateRepository).getFiltered(JpaToscaNodeTemplate.class, name, version);
236 controlLoopProvider.deleteInstanceProperties(controlLoopProvider.saveInstanceProperties(toscaServiceTest),
239 verify(toscaNodeTemplateRepository).delete(any());
243 void testGetNodeTemplates() throws Exception {
244 var toscaNodeTemplateRepository = mock(ToscaNodeTemplateRepository.class);
245 var controlLoopProvider = new ControlLoopProvider(mock(ControlLoopRepository.class),
246 toscaNodeTemplateRepository, mock(ToscaNodeTemplatesRepository.class));
248 var toscaNodeTemplate0 = new JpaToscaNodeTemplate(new PfConceptKey(ID_NAME, ID_VERSION));
249 var toscaNodeTemplate1 = new JpaToscaNodeTemplate(new PfConceptKey("PMSHInstance2", ID_VERSION));
251 when(toscaNodeTemplateRepository.getFiltered(JpaToscaNodeTemplate.class, null, null))
252 .thenReturn(List.of(toscaNodeTemplate0, toscaNodeTemplate1));
253 when(toscaNodeTemplateRepository.findAll()).thenReturn(List.of(toscaNodeTemplate0, toscaNodeTemplate1));
254 when(toscaNodeTemplateRepository.getFiltered(JpaToscaNodeTemplate.class, ID_NAME, ID_VERSION))
255 .thenReturn(List.of(toscaNodeTemplate0));
258 var listNodes = controlLoopProvider.getNodeTemplates(null, null);
259 assertNotNull(listNodes);
260 assertThat(listNodes).hasSize(2);
262 listNodes = controlLoopProvider.getNodeTemplates(ID_NAME, ID_VERSION);
263 assertNotNull(listNodes);
264 assertThat(listNodes).hasSize(1);
266 listNodes = controlLoopProvider.getAllNodeTemplates();
267 assertNotNull(listNodes);
268 assertThat(listNodes).hasSize(2);
270 var nodeTemplateFilter =
271 ToscaTypedEntityFilter.<ToscaNodeTemplate>builder().name(ID_NAME).version(ID_VERSION).build();
273 listNodes = controlLoopProvider.getFilteredNodeTemplates(nodeTemplateFilter);
274 assertNotNull(listNodes);
275 assertThat(listNodes).hasSize(1);
277 assertThatThrownBy(() -> {
278 controlLoopProvider.getFilteredNodeTemplates(null);
279 }).hasMessageMatching("filter is marked non-null but is null");
282 private static ToscaServiceTemplate testControlLoopRead() {
283 return testControlLoopYamlSerialization(TOSCA_TEMPLATE_YAML);
286 private static ToscaServiceTemplate testControlLoopYamlSerialization(String controlLoopFilePath) {
287 var controlLoopString = ResourceUtils.getResourceAsString(controlLoopFilePath);
288 return yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);