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.fail;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
30 import java.util.List;
31 import java.util.Optional;
32 import java.util.UUID;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.Test;
35 import org.mockito.Mockito;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
37 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaServiceTemplate;
38 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationCompositionDefinition;
39 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionDefinitionRepository;
40 import org.onap.policy.common.utils.coder.CoderException;
41 import org.onap.policy.common.utils.coder.StandardYamlCoder;
42 import org.onap.policy.common.utils.resources.ResourceUtils;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 import org.springframework.data.domain.Example;
46 class AcDefinitionProviderTest {
48 private static final String TOSCA_SERVICE_TEMPLATE_YAML = "clamp/acm/pmsh/funtional-pmsh-usecase.yaml";
49 private static final String TOSCA_SERVICE_TEMPLATE_YAML_PROP =
50 "clamp/acm/test/tosca-template-additional-properties.yaml";
52 private static final StandardYamlCoder YAML_TRANSLATOR = new StandardYamlCoder();
54 private static ToscaServiceTemplate inputServiceTemplate;
57 static void loadServiceTemplate() {
58 inputServiceTemplate = getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
62 void testDocCopyCompare() {
64 var inputServiceTemplateProperties = getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML_PROP);
65 var docServiceTemplate = new DocToscaServiceTemplate(inputServiceTemplateProperties);
66 var docServiceTemplateCopy = new DocToscaServiceTemplate(docServiceTemplate);
68 assertThat(docServiceTemplate.compareTo(docServiceTemplateCopy)).isEqualByComparingTo(0);
69 assertThat(docServiceTemplate.compareToWithoutEntities(docServiceTemplateCopy)).isZero();
71 var acmDefinition = getAcDefinition(docServiceTemplate);
72 var acmDefinitionCopy = getAcDefinition(docServiceTemplateCopy);
74 assertThat(acmDefinition.getServiceTemplate().getName()).isEqualTo(
75 acmDefinitionCopy.getServiceTemplate().getName());
80 void testCreateServiceTemplate() {
81 var docServiceTemplate = new DocToscaServiceTemplate(inputServiceTemplate);
82 var acmDefinition = getAcDefinition(docServiceTemplate);
84 var acmDefinitionRepository = mock(AutomationCompositionDefinitionRepository.class);
85 when(acmDefinitionRepository.save(any(JpaAutomationCompositionDefinition.class)))
86 .thenReturn(new JpaAutomationCompositionDefinition(acmDefinition));
88 var acDefinitionProvider = new AcDefinitionProvider(acmDefinitionRepository);
89 var result = acDefinitionProvider.createAutomationCompositionDefinition(inputServiceTemplate);
91 assertThat(result.getServiceTemplate()).isEqualTo(docServiceTemplate.toAuthorative());
95 void testDeleteAcDefintion() {
96 var docServiceTemplate = new DocToscaServiceTemplate(inputServiceTemplate);
97 var acmDefinition = getAcDefinition(docServiceTemplate);
99 var acmDefinitionRepository = mock(AutomationCompositionDefinitionRepository.class);
100 when(acmDefinitionRepository.findById(acmDefinition.getCompositionId().toString()))
101 .thenReturn(Optional.of(new JpaAutomationCompositionDefinition(acmDefinition)));
103 var acDefinitionProvider = new AcDefinitionProvider(acmDefinitionRepository);
104 var result = acDefinitionProvider.deleteAcDefintion(acmDefinition.getCompositionId());
106 assertThat(result).isEqualTo(docServiceTemplate.toAuthorative());
110 void testDeleteServiceTemplateEmpty() {
111 var compositionId = UUID.randomUUID();
112 var acmDefinitionRepository = mock(AutomationCompositionDefinitionRepository.class);
113 var acDefinitionProvider = new AcDefinitionProvider(acmDefinitionRepository);
114 assertThatThrownBy(() -> acDefinitionProvider.deleteAcDefintion(compositionId))
115 .hasMessage("delete of Automation Composition Definition \"" + compositionId
116 + "\" failed, Automation Composition Definition does not exist");
120 void testGetServiceTemplate() {
121 var docServiceTemplate = new DocToscaServiceTemplate(inputServiceTemplate);
122 var acmDefinition = getAcDefinition(docServiceTemplate);
123 var acmDefinitionRepository = mock(AutomationCompositionDefinitionRepository.class);
124 when(acmDefinitionRepository.findAll(Mockito.<Example<JpaAutomationCompositionDefinition>>any()))
125 .thenReturn(List.of(new JpaAutomationCompositionDefinition(acmDefinition)));
127 var acDefinitionProvider = new AcDefinitionProvider(acmDefinitionRepository);
128 var result = acDefinitionProvider.getServiceTemplateList(inputServiceTemplate.getName(),
129 inputServiceTemplate.getVersion());
131 assertThat(result).hasSize(1);
132 assertThat(result.get(0)).isEqualTo(acmDefinition.getServiceTemplate());
135 private AutomationCompositionDefinition getAcDefinition(DocToscaServiceTemplate docServiceTemplate) {
136 var acmDefinition = new AutomationCompositionDefinition();
137 acmDefinition.setCompositionId(UUID.randomUUID());
138 acmDefinition.setServiceTemplate(docServiceTemplate.toAuthorative());
139 return acmDefinition;
143 * Get ToscaServiceTemplate from resource.
145 * @param path path of the resource
147 public static ToscaServiceTemplate getToscaServiceTemplate(String path) {
150 return YAML_TRANSLATOR.decode(ResourceUtils.getResourceAsStream(path), ToscaServiceTemplate.class);
151 } catch (CoderException e) {
152 fail("Cannot read or decode " + path);