690f5f2f44013b69ed61e4ee544db861244611a9
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 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.acm.participant.a1pms.handler;
22
23 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import java.util.Map;
30 import org.junit.jupiter.api.BeforeAll;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.extension.ExtendWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Spy;
36 import org.onap.policy.clamp.acm.participant.a1pms.exception.A1PolicyServiceException;
37 import org.onap.policy.clamp.acm.participant.a1pms.utils.CommonTestData;
38 import org.onap.policy.clamp.acm.participant.a1pms.utils.ToscaUtils;
39 import org.onap.policy.clamp.acm.participant.a1pms.webclient.AcA1PmsClient;
40 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
41 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
43 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
46 import org.springframework.test.context.junit.jupiter.SpringExtension;
47
48 @ExtendWith(SpringExtension.class)
49 class AcElementHandlerTest {
50
51     private final AcA1PmsClient acA1PmsClient = mock(AcA1PmsClient.class);
52
53     @InjectMocks
54     @Spy
55     private AutomationCompositionElementHandler automationCompositionElementHandler =
56         new AutomationCompositionElementHandler(acA1PmsClient);
57
58
59     private final CommonTestData commonTestData = new CommonTestData();
60
61     private static ToscaServiceTemplate serviceTemplate;
62     private static final String A1_AUTOMATION_COMPOSITION_ELEMENT =
63         "org.onap.domain.database.A1PMSAutomationCompositionElement";
64
65     @BeforeAll
66     static void init() {
67         serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
68     }
69
70     @BeforeEach
71     void startMocks() throws A1PolicyServiceException {
72         automationCompositionElementHandler.setIntermediaryApi(mock(ParticipantIntermediaryApi.class));
73         when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.TRUE);
74         doNothing().when(acA1PmsClient).createService(any());
75     }
76
77     @Test
78     void test_automationCompositionElementStateChange() throws A1PolicyServiceException {
79         var automationCompositionId = commonTestData.getAutomationCompositionId();
80         var element = commonTestData.getAutomationCompositionElement();
81         var automationCompositionElementId = element.getId();
82
83         Map<String, ToscaNodeTemplate> nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
84         automationCompositionElementHandler.automationCompositionElementUpdate(
85                 commonTestData.getAutomationCompositionId(), element,
86                 nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT));
87
88         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
89             automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
90             AutomationCompositionOrderedState.PASSIVE));
91
92         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
93             automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
94             AutomationCompositionOrderedState.UNINITIALISED));
95
96         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
97             automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
98             AutomationCompositionOrderedState.RUNNING));
99     }
100
101     @Test
102     void test_AutomationCompositionElementUpdate() {
103         AutomationCompositionElement element = commonTestData.getAutomationCompositionElement();
104
105         Map<String, ToscaNodeTemplate> nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
106         assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementUpdate(
107             commonTestData.getAutomationCompositionId(), element,
108             nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT)));
109     }
110 }