34162a29bc095bdfbf047aec5f1657adfd73e1f0
[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.acm.participant.policy.main.handler;
22
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;
27
28 import java.util.List;
29 import java.util.Map;
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;
47
48
49 class AutomationCompositionElementHandlerTest {
50
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);
56
57     private PolicyApiHttpClient api = Mockito.mock(PolicyApiHttpClient.class);
58     private PolicyPapHttpClient  pap = Mockito.mock(PolicyPapHttpClient.class);
59
60     @Test
61     void testHandlerDoesNotThrowExceptions() {
62         AutomationCompositionElementHandler handler = getTestingHandler();
63
64         assertDoesNotThrow(() -> handler
65             .automationCompositionElementStateChange(automationCompositionId,
66                 automationCompositionElementId,
67                 AutomationCompositionState.UNINITIALISED,
68                 AutomationCompositionOrderedState.PASSIVE));
69
70         assertDoesNotThrow(() -> handler
71             .automationCompositionElementStateChange(automationCompositionId,
72                 automationCompositionElementId,
73                 AutomationCompositionState.RUNNING,
74                 AutomationCompositionOrderedState.UNINITIALISED));
75
76         assertDoesNotThrow(() -> handler
77             .automationCompositionElementStateChange(automationCompositionId,
78                 automationCompositionElementId,
79                 AutomationCompositionState.PASSIVE,
80                 AutomationCompositionOrderedState.RUNNING));
81     }
82
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);
89         return handler;
90     }
91
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);
105         return element;
106     }
107
108     @Test
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());
114
115         AutomationCompositionElementHandler handler = getTestingHandler();
116         var element = getTestingAcElement();
117         var acElementDefinition = Mockito.mock(ToscaNodeTemplate.class);
118
119         assertDoesNotThrow(() -> handler
120                 .automationCompositionElementUpdate(automationCompositionId, element, acElementDefinition));
121
122         assertDoesNotThrow(() -> handler
123                 .automationCompositionElementStateChange(automationCompositionId,
124                         automationCompositionElementId,
125                         AutomationCompositionState.PASSIVE,
126                         AutomationCompositionOrderedState.UNINITIALISED));
127
128
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));
133
134         // Mock failure in policy type creation
135         doReturn(Response.serverError().build()).when(api).createPolicyType(any());
136         assertDoesNotThrow(() -> handler
137                 .automationCompositionElementUpdate(automationCompositionId, element, acElementDefinition));
138     }
139 }