2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.drools.apps.controller.usecases.step;
 
  23 import static org.assertj.core.api.Assertions.assertThat;
 
  24 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
 
  25 import static org.junit.Assert.assertEquals;
 
  26 import static org.junit.Assert.assertSame;
 
  27 import static org.junit.Assert.assertTrue;
 
  28 import static org.mockito.Mockito.when;
 
  30 import java.util.List;
 
  32 import java.util.UUID;
 
  33 import org.junit.Before;
 
  34 import org.junit.Test;
 
  35 import org.mockito.Mock;
 
  36 import org.mockito.MockitoAnnotations;
 
  37 import org.onap.policy.controlloop.VirtualControlLoopEvent;
 
  38 import org.onap.policy.controlloop.actor.guard.DecisionOperation;
 
  39 import org.onap.policy.controlloop.actor.guard.GuardActor;
 
  40 import org.onap.policy.controlloop.actor.so.VfModuleCreate;
 
  41 import org.onap.policy.controlloop.actorserviceprovider.Operation;
 
  42 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
 
  43 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
 
  44 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
 
  45 import org.onap.policy.controlloop.eventmanager.StepContext;
 
  47 public class GuardStep2Test {
 
  48     private static final String CL_NAME = "my-closed-loop";
 
  49     private static final String MASTER_ACTOR = "master-actor";
 
  50     private static final String MASTER_OPERATION = "master-operation";
 
  51     private static final String MY_TARGET = "my-target";
 
  52     private static final UUID REQ_ID = UUID.randomUUID();
 
  53     private static final int VF_COUNT = 10;
 
  56     private ControlLoopEventContext context;
 
  58     private StepContext stepContext;
 
  60     private VirtualControlLoopEvent event;
 
  62     private Operation policyOper;
 
  64     private ControlLoopOperationParams params;
 
  66     private GuardStep2 step;
 
  73         MockitoAnnotations.initMocks(this);
 
  75         when(event.getRequestId()).thenReturn(REQ_ID);
 
  77         when(context.getEvent()).thenReturn(event);
 
  79         when(stepContext.getProperty(OperationProperties.AAI_TARGET_ENTITY)).thenReturn(MY_TARGET);
 
  80         when(stepContext.contains(OperationProperties.DATA_VF_COUNT)).thenReturn(true);
 
  81         when(stepContext.getProperty(OperationProperties.DATA_VF_COUNT)).thenReturn(VF_COUNT);
 
  84         params = ControlLoopOperationParams.builder().actor(MASTER_ACTOR).operation(MASTER_OPERATION)
 
  85                         .targetEntity(MY_TARGET).context(context).build();
 
  87         master = new Step2(stepContext, params, event) {
 
  89             protected Operation buildOperation() {
 
  94         // force it to build the operation
 
  97         step = new GuardStep2(master, CL_NAME);
 
 101     public void testConstructor() {
 
 102         assertEquals(GuardActor.NAME, step.getActorName());
 
 103         assertEquals(DecisionOperation.NAME, step.getOperationName());
 
 104         assertSame(stepContext, step.stepContext);
 
 105         assertSame(event, step.event);
 
 107         // test when master is uninitialized
 
 108         master = new Step2(stepContext, params, event);
 
 109         assertThatIllegalStateException().isThrownBy(() -> new GuardStep2(master, CL_NAME));
 
 111         ControlLoopOperationParams params2 = step.getParams();
 
 114         assertThat(params2.getPayload()).isEqualTo(Map.of(
 
 115                         "actor", MASTER_ACTOR,
 
 116                         "operation", MASTER_OPERATION,
 
 123     public void testAcceptsEvent() {
 
 124         // it should always accept events
 
 125         assertTrue(step.acceptsEvent());
 
 129     public void testGetPropertyNames() {
 
 130         // unmatching property names
 
 131         when(policyOper.getPropertyNames()).thenReturn(List.of("propA", "propB"));
 
 132         assertThat(step.getPropertyNames()).isEmpty();
 
 134         // matching property names
 
 135         when(policyOper.getPropertyNames()).thenReturn(List.of("propA", OperationProperties.DATA_VF_COUNT, "propB"));
 
 136         assertThat(step.getPropertyNames()).isEqualTo(List.of(OperationProperties.DATA_VF_COUNT));
 
 140     public void testLoadTargetEntity() {
 
 141         step.loadTargetEntity(OperationProperties.AAI_TARGET_ENTITY);
 
 142         assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_TARGET_ENTITY, MY_TARGET);
 
 146      * Tests loadVfCount() when the policy operation is NOT "VF Module Create".
 
 149     public void testLoadVfCountNotVfModuleCreate() {
 
 150         // should decrement the count
 
 151         step.loadVfCount("");
 
 152         assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_VF_COUNT, VF_COUNT - 1);
 
 156      * Tests loadVfCount() when the policy operation is "VF Module Create".
 
 159     public void testLoadVfCountVfModuleCreate() {
 
 160         when(policyOper.getName()).thenReturn(VfModuleCreate.NAME);
 
 162         // should increment the count
 
 163         step.loadVfCount("");
 
 164         assertThat(step.getParams().getPayload()).containsEntry(GuardStep2.PAYLOAD_KEY_VF_COUNT, VF_COUNT + 1);