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
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.policy.main.handler;
23 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.when;
28 import java.util.List;
30 import java.util.UUID;
31 import javax.ws.rs.core.Response;
32 import org.junit.jupiter.api.Test;
33 import org.mockito.Mockito;
34 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
35 import org.onap.policy.clamp.acm.participant.policy.client.PolicyApiHttpClient;
36 import org.onap.policy.clamp.acm.participant.policy.client.PolicyPapHttpClient;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
49 class AutomationCompositionElementHandlerTest {
51 private static final String ID_NAME = "org.onap.PM_CDS_Blueprint";
52 private static final String ID_VERSION = "1.0.1";
53 private static final UUID automationCompositionElementId = UUID.randomUUID();
54 private static final ToscaConceptIdentifier automationCompositionId =
55 new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
57 private PolicyApiHttpClient api = Mockito.mock(PolicyApiHttpClient.class);
58 private PolicyPapHttpClient pap = Mockito.mock(PolicyPapHttpClient.class);
61 void testHandlerDoesNotThrowExceptions() {
62 AutomationCompositionElementHandler handler = getTestingHandler();
64 assertDoesNotThrow(() -> handler
65 .automationCompositionElementStateChange(automationCompositionId,
66 automationCompositionElementId,
67 AutomationCompositionState.UNINITIALISED,
68 AutomationCompositionOrderedState.PASSIVE));
70 assertDoesNotThrow(() -> handler
71 .automationCompositionElementStateChange(automationCompositionId,
72 automationCompositionElementId,
73 AutomationCompositionState.RUNNING,
74 AutomationCompositionOrderedState.UNINITIALISED));
76 assertDoesNotThrow(() -> handler
77 .automationCompositionElementStateChange(automationCompositionId,
78 automationCompositionElementId,
79 AutomationCompositionState.PASSIVE,
80 AutomationCompositionOrderedState.RUNNING));
83 private AutomationCompositionElementHandler getTestingHandler() {
84 var handler = new AutomationCompositionElementHandler(api, pap);
85 var intermediaryApi = Mockito.mock(ParticipantIntermediaryApi.class);
86 var element = getTestingAcElement();
87 when(intermediaryApi.getAutomationCompositionElement(automationCompositionElementId)).thenReturn(element);
88 handler.setIntermediaryApi(intermediaryApi);
92 private AutomationCompositionElement getTestingAcElement() {
93 var element = new AutomationCompositionElement();
94 element.setDefinition(automationCompositionId);
95 element.setDescription("Description");
96 element.setId(automationCompositionElementId);
97 element.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
98 element.setParticipantId(automationCompositionId);
99 element.setState(AutomationCompositionState.UNINITIALISED);
100 var template = new ToscaServiceTemplate();
101 template.setToscaTopologyTemplate(new ToscaTopologyTemplate());
102 template.getToscaTopologyTemplate().setPolicies(List.of(Map.of("DummyPolicy", new ToscaPolicy())));
103 template.setPolicyTypes(Map.of("dummy policy type", new ToscaPolicyType()));
104 element.setToscaServiceTemplateFragment(template);
109 void testAcElementUpdate() throws PfModelException {
110 // Mock success scenario for policy creation and deployment
111 doReturn(Response.ok().build()).when(api).createPolicyType(any());
112 doReturn(Response.ok().build()).when(api).createPolicy(any());
113 doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
115 AutomationCompositionElementHandler handler = getTestingHandler();
116 var element = getTestingAcElement();
117 var acElementDefinition = Mockito.mock(ToscaNodeTemplate.class);
119 assertDoesNotThrow(() -> handler
120 .automationCompositionElementUpdate(automationCompositionId, element, acElementDefinition));
122 assertDoesNotThrow(() -> handler
123 .automationCompositionElementStateChange(automationCompositionId,
124 automationCompositionElementId,
125 AutomationCompositionState.PASSIVE,
126 AutomationCompositionOrderedState.UNINITIALISED));
129 //Mock failure in policy deployment
130 doReturn(Response.serverError().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
131 assertDoesNotThrow(() -> handler
132 .automationCompositionElementUpdate(automationCompositionId, element, acElementDefinition));
134 // Mock failure in policy type creation
135 doReturn(Response.serverError().build()).when(api).createPolicyType(any());
136 assertDoesNotThrow(() -> handler
137 .automationCompositionElementUpdate(automationCompositionId, element, acElementDefinition));