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.ToscaPolicy;
 
  43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
 
  44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 
  45 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
 
  47 class AutomationCompositionElementHandlerTest {
 
  49     private static final String ID_NAME = "org.onap.PM_CDS_Blueprint";
 
  50     private static final String ID_VERSION = "1.0.1";
 
  51     private static final UUID automationCompositionElementId = UUID.randomUUID();
 
  52     public static final UUID AC_ID = UUID.randomUUID();
 
  53     private static final ToscaConceptIdentifier PARTICIPANT_ID = new ToscaConceptIdentifier(ID_NAME, ID_VERSION);
 
  55     private PolicyApiHttpClient api = Mockito.mock(PolicyApiHttpClient.class);
 
  56     private PolicyPapHttpClient pap = Mockito.mock(PolicyPapHttpClient.class);
 
  59     void testHandlerDoesNotThrowExceptions() {
 
  60         var handler = getTestingHandler();
 
  62         assertDoesNotThrow(() -> handler.automationCompositionElementStateChange(AC_ID, automationCompositionElementId,
 
  63                 AutomationCompositionState.UNINITIALISED, AutomationCompositionOrderedState.PASSIVE));
 
  65         assertDoesNotThrow(() -> handler.automationCompositionElementStateChange(AC_ID, automationCompositionElementId,
 
  66                 AutomationCompositionState.RUNNING, AutomationCompositionOrderedState.UNINITIALISED));
 
  68         assertDoesNotThrow(() -> handler.automationCompositionElementStateChange(AC_ID, automationCompositionElementId,
 
  69                 AutomationCompositionState.PASSIVE, AutomationCompositionOrderedState.RUNNING));
 
  72     private AutomationCompositionElementHandler getTestingHandler() {
 
  73         var handler = new AutomationCompositionElementHandler(api, pap);
 
  74         var intermediaryApi = Mockito.mock(ParticipantIntermediaryApi.class);
 
  75         var element = getTestingAcElement();
 
  76         when(intermediaryApi.getAutomationCompositionElement(automationCompositionElementId)).thenReturn(element);
 
  77         handler.setIntermediaryApi(intermediaryApi);
 
  81     private AutomationCompositionElement getTestingAcElement() {
 
  82         var element = new AutomationCompositionElement();
 
  83         element.setDefinition(PARTICIPANT_ID);
 
  84         element.setDescription("Description");
 
  85         element.setId(automationCompositionElementId);
 
  86         element.setOrderedState(AutomationCompositionOrderedState.UNINITIALISED);
 
  87         element.setParticipantId(PARTICIPANT_ID);
 
  88         element.setState(AutomationCompositionState.UNINITIALISED);
 
  89         var template = new ToscaServiceTemplate();
 
  90         template.setToscaTopologyTemplate(new ToscaTopologyTemplate());
 
  91         template.getToscaTopologyTemplate().setPolicies(List.of(Map.of("DummyPolicy", new ToscaPolicy())));
 
  92         template.setPolicyTypes(Map.of("dummy policy type", new ToscaPolicyType()));
 
  93         element.setToscaServiceTemplateFragment(template);
 
  98     void testAcElementUpdate() throws PfModelException {
 
  99         // Mock success scenario for policy creation and deployment
 
 100         doReturn(Response.ok().build()).when(api).createPolicyType(any());
 
 101         doReturn(Response.ok().build()).when(api).createPolicy(any());
 
 102         doReturn(Response.accepted().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
 
 104         var handler = getTestingHandler();
 
 105         var element = getTestingAcElement();
 
 107         assertDoesNotThrow(() -> handler.automationCompositionElementUpdate(AC_ID, element, Map.of()));
 
 109         assertDoesNotThrow(() -> handler.automationCompositionElementStateChange(AC_ID, automationCompositionElementId,
 
 110                 AutomationCompositionState.PASSIVE, AutomationCompositionOrderedState.UNINITIALISED));
 
 112         // Mock failure in policy deployment
 
 113         doReturn(Response.serverError().build()).when(pap).handlePolicyDeployOrUndeploy(any(), any(), any());
 
 114         assertDoesNotThrow(() -> handler.automationCompositionElementUpdate(AC_ID, element, Map.of()));
 
 116         // Mock failure in policy type creation
 
 117         doReturn(Response.serverError().build()).when(api).createPolicyType(any());
 
 118         assertDoesNotThrow(() -> handler.automationCompositionElementUpdate(AC_ID, element, Map.of()));