dabb05347bf12f99b318726bc94f451fb870bf52
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.acm.runtime.commissioning;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_SERVICE_TEMPLATE_YAML;
29 import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_ST_TEMPLATE_YAML;
30
31 import java.util.List;
32 import java.util.UUID;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.clamp.acm.runtime.instantiation.InstantiationUtils;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
36 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
37 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
38 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
41
42 class CommissioningProviderTest {
43
44     /**
45      * Test the fetching of automation composition definitions (ToscaServiceTemplates).
46      *
47      * @throws Exception .
48      */
49     @Test
50     void testGetAutomationCompositionDefinitions() throws Exception {
51         var acProvider = mock(AutomationCompositionProvider.class);
52         var participantProvider = mock(ParticipantProvider.class);
53         var acDefinitionProvider = mock(AcDefinitionProvider.class);
54
55         var provider = new CommissioningProvider(acDefinitionProvider, acProvider, null, participantProvider);
56
57         var serviceTemplates = provider.getAutomationCompositionDefinitions(null, null);
58         assertThat(serviceTemplates.getServiceTemplates()).isEmpty();
59
60         when(acDefinitionProvider.getServiceTemplateList(null, null))
61                 .thenReturn(List.of(new ToscaServiceTemplate()));
62         serviceTemplates = provider.getAutomationCompositionDefinitions(null, null);
63         assertThat(serviceTemplates.getServiceTemplates()).hasSize(1);
64     }
65
66     /**
67      * Test the creation of automation composition definitions (ToscaServiceTemplates).
68      *
69      * @throws Exception .
70      */
71     @Test
72     void testCreateAutomationCompositionDefinitions() throws Exception {
73         var serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML);
74         var acmDefinition = new AutomationCompositionDefinition();
75         acmDefinition.setCompositionId(UUID.randomUUID());
76         acmDefinition.setServiceTemplate(serviceTemplate);
77         var acDefinitionProvider = mock(AcDefinitionProvider.class);
78         when(acDefinitionProvider.createAutomationCompositionDefinition(serviceTemplate)).thenReturn(acmDefinition);
79
80         // Response should return the number of node templates present in the service template
81         var acProvider = mock(AutomationCompositionProvider.class);
82         var participantProvider = mock(ParticipantProvider.class);
83         var provider = new CommissioningProvider(acDefinitionProvider, acProvider, null, participantProvider);
84         List<ToscaConceptIdentifier> affectedDefinitions = provider
85                 .createAutomationCompositionDefinitions(serviceTemplate).getAffectedAutomationCompositionDefinitions();
86         verify(acDefinitionProvider).createAutomationCompositionDefinition(serviceTemplate);
87         assertThat(affectedDefinitions).hasSize(13);
88     }
89
90     /**
91      * Test the fetching of a full ToscaServiceTemplate object - as opposed to the reduced template that is being
92      * tested in the testGetToscaServiceTemplateReduced() test.
93      *
94      */
95     @Test
96     void testGetToscaServiceTemplateList() throws Exception {
97         var acDefinitionProvider = mock(AcDefinitionProvider.class);
98         var acProvider = mock(AutomationCompositionProvider.class);
99         var participantProvider = mock(ParticipantProvider.class);
100
101         var provider =
102                 new CommissioningProvider(acDefinitionProvider, acProvider, null, participantProvider);
103         ToscaServiceTemplate serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_ST_TEMPLATE_YAML);
104         when(acDefinitionProvider.getServiceTemplateList(null, null)).thenReturn(List.of(serviceTemplate));
105
106         var returnedServiceTemplate = provider.getAutomationCompositionDefinitions(null, null);
107         assertThat(returnedServiceTemplate).isNotNull();
108         assertThat(returnedServiceTemplate.getServiceTemplates()).isNotEmpty();
109     }
110 }