c28355d7f3568a54f0d65eecae650ecc9109ade4
[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
27 import java.util.List;
28 import java.util.Map;
29 import java.util.UUID;
30 import javax.ws.rs.core.Response;
31 import org.junit.jupiter.api.Test;
32 import org.mockito.Mockito;
33 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
34 import org.onap.policy.clamp.acm.participant.policy.client.PolicyApiHttpClient;
35 import org.onap.policy.clamp.acm.participant.policy.client.PolicyPapHttpClient;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
39 import org.onap.policy.models.base.PfModelException;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
45
46 class AutomationCompositionElementHandlerTest {
47
48     private static final String ID_NAME = "org.onap.PM_CDS_Blueprint";
49     private static final String ID_VERSION = "1.0.1";
50     private static final UUID automationCompositionElementId = UUID.randomUUID();
51     public static final UUID AC_ID = UUID.randomUUID();
52     private static final ToscaConceptIdentifier PARTICIPANT_ID = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
53
54     private PolicyApiHttpClient api = Mockito.mock(PolicyApiHttpClient.class);
55     private PolicyPapHttpClient pap = Mockito.mock(PolicyPapHttpClient.class);
56
57     @Test
58     void testHandlerDoesNotThrowExceptions() {
59         var handler = getTestingHandler();
60
61         assertDoesNotThrow(() -> handler.automationCompositionElementStateChange(AC_ID, automationCompositionElementId,
62                 AutomationCompositionState.UNINITIALISED, AutomationCompositionOrderedState.PASSIVE));
63
64         assertDoesNotThrow(() -> handler.automationCompositionElementStateChange(AC_ID, automationCompositionElementId,
65                 AutomationCompositionState.RUNNING, AutomationCompositionOrderedState.UNINITIALISED));
66
67         assertDoesNotThrow(() -> handler.automationCompositionElementStateChange(AC_ID, automationCompositionElementId,
68                 AutomationCompositionState.PASSIVE, AutomationCompositionOrderedState.RUNNING));
69     }
70
71     private AutomationCompositionElementHandler getTestingHandler() {
72         var handler = new AutomationCompositionElementHandler(api, pap);
73         var intermediaryApi = Mockito.mock(ParticipantIntermediaryApi.class);
74         handler.setIntermediaryApi(intermediaryApi);
75         return handler;
76     }
77
78     private AutomationCompositionElement getTestingAcElement() {
79         var element = new AutomationCompositionElement();
80         element.setDefinition(PARTICIPANT_ID);
81         element.setDescription("Description");
82         element.setId(automationCompositionElementId);
83         element.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
84         element.setParticipantId(PARTICIPANT_ID);
85         element.setState(AutomationCompositionState.UNINITIALISED);
86         var template = new ToscaServiceTemplate();
87         template.setToscaTopologyTemplate(new ToscaTopologyTemplate());
88         template.getToscaTopologyTemplate().setPolicies(List.of(Map.of("DummyPolicy", new ToscaPolicy())));
89         template.setPolicyTypes(Map.of("dummy policy type", new ToscaPolicyType()));
90         element.setToscaServiceTemplateFragment(template);
91         return element;
92     }
93
94     @Test
95     void testAcElementUpdate() throws PfModelException {
96         // Mock success scenario for policy creation and deployment
97         doReturn(Response.ok().build()).when(api).createPolicyType(any());
98         doReturn(Response.ok().build()).when(api).createPolicy(any());
99         doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
100
101         var handler = getTestingHandler();
102         var element = getTestingAcElement();
103
104         assertDoesNotThrow(() -> handler.automationCompositionElementUpdate(AC_ID, element, Map.of()));
105
106         assertDoesNotThrow(() -> handler.automationCompositionElementStateChange(AC_ID, automationCompositionElementId,
107                 AutomationCompositionState.PASSIVE, AutomationCompositionOrderedState.UNINITIALISED));
108
109         // Mock failure in policy deployment
110         doReturn(Response.serverError().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
111         assertDoesNotThrow(() -> handler.automationCompositionElementUpdate(AC_ID, element, Map.of()));
112
113         // Mock failure in policy type creation
114         doReturn(Response.serverError().build()).when(api).createPolicyType(any());
115         assertDoesNotThrow(() -> handler.automationCompositionElementUpdate(AC_ID, element, Map.of()));
116     }
117 }