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.assertTrue;
26 import static org.junit.jupiter.api.Assertions.fail;
27 import static org.mockito.ArgumentMatchers.any;
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 java.util.UUID;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.Test;
36 import org.mockito.Mockito;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
38 import org.onap.policy.clamp.models.acm.document.concepts.DocToscaServiceTemplate;
39 import org.onap.policy.clamp.models.acm.persistence.concepts.JpaAutomationCompositionDefinition;
40 import org.onap.policy.clamp.models.acm.persistence.repository.AutomationCompositionDefinitionRepository;
41 import org.onap.policy.common.utils.coder.CoderException;
42 import org.onap.policy.common.utils.coder.StandardYamlCoder;
43 import org.onap.policy.common.utils.resources.ResourceUtils;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.springframework.data.domain.Example;
47 class AcDefinitionProviderTest {
49 private static final String TOSCA_SERVICE_TEMPLATE_YAML = "clamp/acm/pmsh/funtional-pmsh-usecase.yaml";
50 private static final String TOSCA_SERVICE_TEMPLATE_YAML_PROP =
51 "clamp/acm/test/tosca-template-additional-properties.yaml";
53 private static final StandardYamlCoder YAML_TRANSLATOR = new StandardYamlCoder();
55 private static ToscaServiceTemplate inputServiceTemplate;
58 static void loadServiceTemplate() {
59 inputServiceTemplate = getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
63 void testDocCopyCompare() {
65 var inputServiceTemplateProperties = getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML_PROP);
66 var docServiceTemplate = new DocToscaServiceTemplate(inputServiceTemplateProperties);
67 var docServiceTemplateCopy = new DocToscaServiceTemplate(docServiceTemplate);
69 assertTrue(docServiceTemplate.compareTo(docServiceTemplateCopy) < -1);
70 assertThat(docServiceTemplate.compareToWithoutEntities(docServiceTemplateCopy)).isZero();
72 var acmDefinition = getAcDefinition(docServiceTemplate);
73 var acmDefinitionCopy = getAcDefinition(docServiceTemplateCopy);
75 assertThat(acmDefinition.getServiceTemplate().getName()).isEqualTo(
76 acmDefinitionCopy.getServiceTemplate().getName());
81 void testCreateServiceTemplate() {
82 var docServiceTemplate = new DocToscaServiceTemplate(inputServiceTemplate);
83 var acmDefinition = getAcDefinition(docServiceTemplate);
85 var acmDefinitionRepository = mock(AutomationCompositionDefinitionRepository.class);
86 when(acmDefinitionRepository.save(any(JpaAutomationCompositionDefinition.class)))
87 .thenReturn(new JpaAutomationCompositionDefinition(acmDefinition));
89 var acDefinitionProvider = new AcDefinitionProvider(acmDefinitionRepository);
90 var result = acDefinitionProvider.createAutomationCompositionDefinition(inputServiceTemplate);
92 assertThat(result.getServiceTemplate()).isEqualTo(docServiceTemplate.toAuthorative());
96 void testDeleteAcDefintion() {
97 var docServiceTemplate = new DocToscaServiceTemplate(inputServiceTemplate);
98 var acmDefinition = getAcDefinition(docServiceTemplate);
100 var acmDefinitionRepository = mock(AutomationCompositionDefinitionRepository.class);
101 when(acmDefinitionRepository.findById(acmDefinition.getCompositionId().toString()))
102 .thenReturn(Optional.of(new JpaAutomationCompositionDefinition(acmDefinition)));
104 var acDefinitionProvider = new AcDefinitionProvider(acmDefinitionRepository);
105 var result = acDefinitionProvider.deleteAcDefintion(acmDefinition.getCompositionId());
107 assertThat(result).isEqualTo(docServiceTemplate.toAuthorative());
111 void testDeleteServiceTemplateEmpty() {
112 var compositionId = UUID.randomUUID();
113 var acmDefinitionRepository = mock(AutomationCompositionDefinitionRepository.class);
114 var acDefinitionProvider = new AcDefinitionProvider(acmDefinitionRepository);
115 assertThatThrownBy(() -> acDefinitionProvider.deleteAcDefintion(compositionId))
116 .hasMessage("delete of Automation Composition Definition \"" + compositionId
117 + "\" failed, Automation Composition Definition does not exist");
121 void testGetServiceTemplate() {
122 var docServiceTemplate = new DocToscaServiceTemplate(inputServiceTemplate);
123 var acmDefinition = getAcDefinition(docServiceTemplate);
124 var acmDefinitionRepository = mock(AutomationCompositionDefinitionRepository.class);
125 when(acmDefinitionRepository.findAll(Mockito.<Example<JpaAutomationCompositionDefinition>>any()))
126 .thenReturn(List.of(new JpaAutomationCompositionDefinition(acmDefinition)));
128 var acDefinitionProvider = new AcDefinitionProvider(acmDefinitionRepository);
129 var result = acDefinitionProvider.getServiceTemplateList(inputServiceTemplate.getName(),
130 inputServiceTemplate.getVersion());
132 assertThat(result).hasSize(1);
133 assertThat(result.get(0)).isEqualTo(acmDefinition.getServiceTemplate());
136 private AutomationCompositionDefinition getAcDefinition(DocToscaServiceTemplate docServiceTemplate) {
137 var acmDefinition = new AutomationCompositionDefinition();
138 acmDefinition.setCompositionId(UUID.randomUUID());
139 acmDefinition.setServiceTemplate(docServiceTemplate.toAuthorative());
140 return acmDefinition;
144 * Get ToscaServiceTemplate from resource.
146 * @param path path of the resource
148 public static ToscaServiceTemplate getToscaServiceTemplate(String path) {
151 return YAML_TRANSLATOR.decode(ResourceUtils.getResourceAsStream(path), ToscaServiceTemplate.class);
152 } catch (CoderException e) {
153 fail("Cannot read or decode " + path);