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