a2d6d69c2d9a5bddef375c51f62e4ab219532668
[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
50     private static final StandardYamlCoder YAML_TRANSLATOR = new StandardYamlCoder();
51
52     private static ToscaServiceTemplate inputServiceTemplate;
53
54     @BeforeAll
55     static void loadServiceTemplate() {
56         inputServiceTemplate = getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
57     }
58
59     @Test
60     void testCreateServiceTemplate() {
61         var docServiceTemplate = new DocToscaServiceTemplate(inputServiceTemplate);
62         var acmDefinition = getAcDefinition(docServiceTemplate);
63
64         var acmDefinitionRepository = mock(AutomationCompositionDefinitionRepository.class);
65         when(acmDefinitionRepository.save(any(JpaAutomationCompositionDefinition.class)))
66                 .thenReturn(new JpaAutomationCompositionDefinition(acmDefinition));
67
68         var acDefinitionProvider = new AcDefinitionProvider(acmDefinitionRepository);
69         var result = acDefinitionProvider.createAutomationCompositionDefinition(inputServiceTemplate);
70
71         assertThat(result.getServiceTemplate()).isEqualTo(docServiceTemplate.toAuthorative());
72     }
73
74     @Test
75     void testDeleteAcDefintion() {
76         var docServiceTemplate = new DocToscaServiceTemplate(inputServiceTemplate);
77         var acmDefinition = getAcDefinition(docServiceTemplate);
78
79         var acmDefinitionRepository = mock(AutomationCompositionDefinitionRepository.class);
80         when(acmDefinitionRepository.findById(acmDefinition.getCompositionId().toString()))
81                 .thenReturn(Optional.of(new JpaAutomationCompositionDefinition(acmDefinition)));
82
83         var acDefinitionProvider = new AcDefinitionProvider(acmDefinitionRepository);
84         var result = acDefinitionProvider.deleteAcDefintion(acmDefinition.getCompositionId());
85
86         assertThat(result).isEqualTo(docServiceTemplate.toAuthorative());
87     }
88
89     @Test
90     void testDeleteServiceTemplateEmpty() {
91         var compositionId = UUID.randomUUID();
92         var acmDefinitionRepository = mock(AutomationCompositionDefinitionRepository.class);
93         var acDefinitionProvider = new AcDefinitionProvider(acmDefinitionRepository);
94         assertThatThrownBy(() -> acDefinitionProvider.deleteAcDefintion(compositionId))
95                 .hasMessage("delete of Automation Composition Definition \"" + compositionId
96                         + "\" failed, Automation Composition Definition does not exist");
97     }
98
99     @Test
100     void testGetServiceTemplate() {
101         var docServiceTemplate = new DocToscaServiceTemplate(inputServiceTemplate);
102         var acmDefinition = getAcDefinition(docServiceTemplate);
103         var acmDefinitionRepository = mock(AutomationCompositionDefinitionRepository.class);
104         when(acmDefinitionRepository.findAll(Mockito.<Example<JpaAutomationCompositionDefinition>>any()))
105                 .thenReturn(List.of(new JpaAutomationCompositionDefinition(acmDefinition)));
106
107         var acDefinitionProvider = new AcDefinitionProvider(acmDefinitionRepository);
108         var result = acDefinitionProvider.getServiceTemplateList(inputServiceTemplate.getName(),
109                 inputServiceTemplate.getVersion());
110
111         assertThat(result).hasSize(1);
112         assertThat(result.get(0)).isEqualTo(acmDefinition.getServiceTemplate());
113     }
114
115     private AutomationCompositionDefinition getAcDefinition(DocToscaServiceTemplate docServiceTemplate) {
116         var acmDefinition = new AutomationCompositionDefinition();
117         acmDefinition.setCompositionId(UUID.randomUUID());
118         acmDefinition.setServiceTemplate(docServiceTemplate.toAuthorative());
119         return acmDefinition;
120     }
121
122     /**
123      * Get ToscaServiceTemplate from resource.
124      *
125      * @param path path of the resource
126      */
127     public static ToscaServiceTemplate getToscaServiceTemplate(String path) {
128
129         try {
130             return YAML_TRANSLATOR.decode(ResourceUtils.getResourceAsStream(path), ToscaServiceTemplate.class);
131         } catch (CoderException e) {
132             fail("Cannot read or decode " + path);
133             return null;
134         }
135     }
136 }