454864fd7e0d735838b72823fdf30345d23c5dc0
[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         assertDoesNotThrow(() -> handler
83             .handleStatistics(automationCompositionElementId));
84     }
85
86     private AutomationCompositionElementHandler getTestingHandler() {
87         var handler = new AutomationCompositionElementHandler(api, pap);
88         var intermediaryApi = Mockito.mock(ParticipantIntermediaryApi.class);
89         var element = getTestingAcElement();
90         when(intermediaryApi.getAutomationCompositionElement(automationCompositionElementId)).thenReturn(element);
91         handler.setIntermediaryApi(intermediaryApi);
92         return handler;
93     }
94
95     private AutomationCompositionElement getTestingAcElement() {
96         var element = new AutomationCompositionElement();
97         element.setDefinition(automationCompositionId);
98         element.setDescription("Description");
99         element.setId(automationCompositionElementId);
100         element.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
101         element.setParticipantId(automationCompositionId);
102         element.setState(AutomationCompositionState.UNINITIALISED);
103         var template = new ToscaServiceTemplate();
104         template.setToscaTopologyTemplate(new ToscaTopologyTemplate());
105         template.getToscaTopologyTemplate().setPolicies(List.of(Map.of("DummyPolicy", new ToscaPolicy())));
106         template.setPolicyTypes(Map.of("dummy policy type", new ToscaPolicyType()));
107         element.setToscaServiceTemplateFragment(template);
108         return element;
109     }
110
111     @Test
112     void testAcElementUpdate() throws PfModelException {
113         // Mock success scenario for policy creation and deployment
114         doReturn(Response.ok().build()).when(api).createPolicyType(any());
115         doReturn(Response.ok().build()).when(api).createPolicy(any());
116         doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
117
118         AutomationCompositionElementHandler handler = getTestingHandler();
119         var element = getTestingAcElement();
120         var acElementDefinition = Mockito.mock(ToscaNodeTemplate.class);
121
122         assertDoesNotThrow(() -> handler
123                 .automationCompositionElementUpdate(automationCompositionId, element, acElementDefinition));
124
125         assertDoesNotThrow(() -> handler
126                 .automationCompositionElementStateChange(automationCompositionId,
127                         automationCompositionElementId,
128                         AutomationCompositionState.PASSIVE,
129                         AutomationCompositionOrderedState.UNINITIALISED));
130
131
132         //Mock failure in policy deployment
133         doReturn(Response.serverError().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
134         assertDoesNotThrow(() -> handler
135                 .automationCompositionElementUpdate(automationCompositionId, element, acElementDefinition));
136
137         // Mock failure in policy type creation
138         doReturn(Response.serverError().build()).when(api).createPolicyType(any());
139         assertDoesNotThrow(() -> handler
140                 .automationCompositionElementUpdate(automationCompositionId, element, acElementDefinition));
141     }
142 }