59e3767d1490ca5659f6344acdaddde9a28b77ea
[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.fail;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
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;
45
46 class AcDefinitionProviderTest {
47
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";
51
52     private static final StandardYamlCoder YAML_TRANSLATOR = new StandardYamlCoder();
53
54     private static ToscaServiceTemplate inputServiceTemplate;
55
56     @BeforeAll
57     static void loadServiceTemplate() {
58         inputServiceTemplate = getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
59     }
60
61     @Test
62     void testDocCopyCompare() {
63
64         var inputServiceTemplateProperties = getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML_PROP);
65         var docServiceTemplate = new DocToscaServiceTemplate(inputServiceTemplateProperties);
66         var docServiceTemplateCopy = new DocToscaServiceTemplate(docServiceTemplate);
67
68         assertThat(docServiceTemplate.compareTo(docServiceTemplateCopy)).isEqualByComparingTo(0);
69         assertThat(docServiceTemplate.compareToWithoutEntities(docServiceTemplateCopy)).isZero();
70
71         var acmDefinition = getAcDefinition(docServiceTemplate);
72         var acmDefinitionCopy = getAcDefinition(docServiceTemplateCopy);
73
74         assertThat(acmDefinition.getServiceTemplate().getName()).isEqualTo(
75                 acmDefinitionCopy.getServiceTemplate().getName());
76
77     }
78
79     @Test
80     void testCreateServiceTemplate() {
81         var docServiceTemplate = new DocToscaServiceTemplate(inputServiceTemplate);
82         var acmDefinition = getAcDefinition(docServiceTemplate);
83
84         var acmDefinitionRepository = mock(AutomationCompositionDefinitionRepository.class);
85         when(acmDefinitionRepository.save(any(JpaAutomationCompositionDefinition.class)))
86                 .thenReturn(new JpaAutomationCompositionDefinition(acmDefinition));
87
88         var acDefinitionProvider = new AcDefinitionProvider(acmDefinitionRepository);
89         var result = acDefinitionProvider.createAutomationCompositionDefinition(inputServiceTemplate);
90
91         assertThat(result.getServiceTemplate()).isEqualTo(docServiceTemplate.toAuthorative());
92     }
93
94     @Test
95     void testDeleteAcDefintion() {
96         var docServiceTemplate = new DocToscaServiceTemplate(inputServiceTemplate);
97         var acmDefinition = getAcDefinition(docServiceTemplate);
98
99         var acmDefinitionRepository = mock(AutomationCompositionDefinitionRepository.class);
100         when(acmDefinitionRepository.findById(acmDefinition.getCompositionId().toString()))
101                 .thenReturn(Optional.of(new JpaAutomationCompositionDefinition(acmDefinition)));
102
103         var acDefinitionProvider = new AcDefinitionProvider(acmDefinitionRepository);
104         var result = acDefinitionProvider.deleteAcDefintion(acmDefinition.getCompositionId());
105
106         assertThat(result).isEqualTo(docServiceTemplate.toAuthorative());
107     }
108
109     @Test
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");
117     }
118
119     @Test
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)));
126
127         var acDefinitionProvider = new AcDefinitionProvider(acmDefinitionRepository);
128         var result = acDefinitionProvider.getServiceTemplateList(inputServiceTemplate.getName(),
129                 inputServiceTemplate.getVersion());
130
131         assertThat(result).hasSize(1);
132         assertThat(result.get(0)).isEqualTo(acmDefinition.getServiceTemplate());
133     }
134
135     private AutomationCompositionDefinition getAcDefinition(DocToscaServiceTemplate docServiceTemplate) {
136         var acmDefinition = new AutomationCompositionDefinition();
137         acmDefinition.setCompositionId(UUID.randomUUID());
138         acmDefinition.setServiceTemplate(docServiceTemplate.toAuthorative());
139         return acmDefinition;
140     }
141
142     /**
143      * Get ToscaServiceTemplate from resource.
144      *
145      * @param path path of the resource
146      */
147     public static ToscaServiceTemplate getToscaServiceTemplate(String path) {
148
149         try {
150             return YAML_TRANSLATOR.decode(ResourceUtils.getResourceAsStream(path), ToscaServiceTemplate.class);
151         } catch (CoderException e) {
152             fail("Cannot read or decode " + path);
153             return null;
154         }
155     }
156 }