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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.a1pms.handler;
23 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.doNothing;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.mockito.InjectMocks;
36 import org.mockito.Spy;
37 import org.onap.policy.clamp.acm.participant.a1pms.exception.A1PolicyServiceException;
38 import org.onap.policy.clamp.acm.participant.a1pms.utils.CommonTestData;
39 import org.onap.policy.clamp.acm.participant.a1pms.utils.ToscaUtils;
40 import org.onap.policy.clamp.acm.participant.a1pms.webclient.AcA1PmsClient;
41 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
42 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
43 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
44 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
47 import org.springframework.test.context.junit.jupiter.SpringExtension;
49 @ExtendWith(SpringExtension.class)
50 class AcElementHandlerTest {
52 private final AcA1PmsClient acA1PmsClient = mock(AcA1PmsClient.class);
56 private AutomationCompositionElementHandler automationCompositionElementHandler =
57 new AutomationCompositionElementHandler(acA1PmsClient);
60 private final CommonTestData commonTestData = new CommonTestData();
62 private static ToscaServiceTemplate serviceTemplate;
63 private static final String A1_AUTOMATION_COMPOSITION_ELEMENT =
64 "org.onap.domain.database.A1PMSAutomationCompositionElement";
68 serviceTemplate = ToscaUtils.readAutomationCompositionFromTosca();
72 void startMocks() throws A1PolicyServiceException {
73 automationCompositionElementHandler.setIntermediaryApi(mock(ParticipantIntermediaryApi.class));
74 when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.TRUE);
75 doNothing().when(acA1PmsClient).createService(any());
79 void test_automationCompositionElementStateChange() throws A1PolicyServiceException {
80 var automationCompositionId = commonTestData.getAutomationCompositionId();
81 var element = commonTestData.getAutomationCompositionElement();
82 var automationCompositionElementId = element.getId();
84 Map<String, ToscaNodeTemplate> nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
85 automationCompositionElementHandler.automationCompositionElementUpdate(
86 commonTestData.getAutomationCompositionId(), element,
87 nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT));
89 assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
90 automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
91 AutomationCompositionOrderedState.PASSIVE));
93 assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
94 automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
95 AutomationCompositionOrderedState.UNINITIALISED));
97 assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementStateChange(
98 automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
99 AutomationCompositionOrderedState.RUNNING));
101 when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.FALSE);
102 assertThrows(A1PolicyServiceException.class,
103 () -> automationCompositionElementHandler.automationCompositionElementStateChange(
104 automationCompositionId, automationCompositionElementId, AutomationCompositionState.PASSIVE,
105 AutomationCompositionOrderedState.UNINITIALISED));
109 void test_AutomationCompositionElementUpdate() {
110 AutomationCompositionElement element = commonTestData.getAutomationCompositionElement();
112 Map<String, ToscaNodeTemplate> nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
113 assertDoesNotThrow(() -> automationCompositionElementHandler.automationCompositionElementUpdate(
114 commonTestData.getAutomationCompositionId(), element,
115 nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT)));
119 void test_AutomationCompositionElementUpdateWithUnhealthyA1pms() {
120 AutomationCompositionElement element = commonTestData.getAutomationCompositionElement();
121 when(acA1PmsClient.isPmsHealthy()).thenReturn(Boolean.FALSE);
123 Map<String, ToscaNodeTemplate> nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
124 assertThrows(A1PolicyServiceException.class,
125 () -> automationCompositionElementHandler.automationCompositionElementUpdate(
126 commonTestData.getAutomationCompositionId(), element,
127 nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT)));
131 void test_AutomationCompositionElementUpdateWithInvalidConfiguration() {
132 AutomationCompositionElement element = commonTestData.getAutomationCompositionElement();
134 Map<String, ToscaNodeTemplate> nodeTemplatesMap = serviceTemplate.getToscaTopologyTemplate().getNodeTemplates();
135 ToscaNodeTemplate toscaNodeTemplate = nodeTemplatesMap.get(A1_AUTOMATION_COMPOSITION_ELEMENT);
136 toscaNodeTemplate.setProperties(Map.of());
137 assertThrows(A1PolicyServiceException.class,
138 () -> automationCompositionElementHandler.automationCompositionElementUpdate(
139 commonTestData.getAutomationCompositionId(), element, toscaNodeTemplate));