2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 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.assertThatIllegalArgumentException;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertSame;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.Mockito.never;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
35 import java.util.UUID;
36 import java.util.concurrent.CompletableFuture;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.onap.policy.common.utils.coder.StandardCoderObject;
43 import org.onap.policy.controlloop.VirtualControlLoopEvent;
44 import org.onap.policy.controlloop.actor.aai.AaiActor;
45 import org.onap.policy.controlloop.actor.aai.AaiGetTenantOperation;
46 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
47 import org.onap.policy.controlloop.actorserviceprovider.Operation;
48 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
49 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
50 import org.onap.policy.controlloop.actorserviceprovider.Operator;
51 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
52 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
53 import org.onap.policy.controlloop.eventmanager.StepContext;
55 @RunWith(MockitoJUnitRunner.class)
56 public class AaiGetTenantStep2Test {
57 private static final String MY_VSERVER = "my-vserver";
58 private static final UUID REQ_ID = UUID.randomUUID();
61 private Operator policyOperator;
63 private Operation policyOperation;
65 private Actor policyActor;
67 private ActorService actors;
69 private ControlLoopOperationParams params;
71 private StepContext stepContext;
73 private VirtualControlLoopEvent event;
75 private CompletableFuture<OperationOutcome> future;
77 private AaiGetTenantStep2 step;
84 future = new CompletableFuture<>();
86 when(params.toBuilder()).thenReturn(ControlLoopOperationParams.builder().actorService(actors)
89 // configure policy operation
90 when(actors.getActor(AaiActor.NAME)).thenReturn(policyActor);
91 when(policyActor.getOperator(AaiGetTenantOperation.NAME)).thenReturn(policyOperator);
92 when(policyOperator.buildOperation(any())).thenReturn(policyOperation);
93 when(policyOperation.start()).thenReturn(future);
95 when(event.getAai()).thenReturn(Map.of(Step2.VSERVER_VSERVER_NAME, MY_VSERVER));
97 master = new Step2(stepContext, params, event);
98 step = new AaiGetTenantStep2(master);
102 public void testConstructor() {
103 assertEquals(AaiActor.NAME, step.getActorName());
104 assertEquals(AaiGetTenantOperation.NAME, step.getOperationName());
105 assertSame(stepContext, step.stepContext);
106 assertSame(event, step.event);
108 // empty vserver name
109 when(event.getAai()).thenReturn(Map.of(Step2.VSERVER_VSERVER_NAME, ""));
110 assertThatIllegalArgumentException().isThrownBy(() -> new AaiGetTenantStep2(master))
111 .withMessage("missing " + Step2.VSERVER_VSERVER_NAME + " in enrichment data");
113 // missing vserver name
114 when(event.getAai()).thenReturn(Map.of());
115 assertThatIllegalArgumentException().isThrownBy(() -> new AaiGetTenantStep2(master))
116 .withMessage("missing " + Step2.VSERVER_VSERVER_NAME + " in enrichment data");
120 public void testGetPropertyNames() {
121 assertThat(step.getPropertyNames()).isEmpty();
125 public void testSetProperties() {
128 step.setProperties();
130 verify(policyOperation).setProperty(OperationProperties.AAI_TARGET_ENTITY, MY_VSERVER);
134 public void testStart() {
136 assertTrue(step.start(100));
137 verify(policyOperation).start();
141 * Tests start() when the data has already been retrieved.
144 public void testStartAlreadyHaveData() {
145 when(stepContext.contains(AaiGetTenantOperation.getKey(MY_VSERVER))).thenReturn(true);
148 assertFalse(step.start(200));
149 verify(policyOperation, never()).start();
153 public void testSuccess() {
154 StandardCoderObject data = new StandardCoderObject();
155 OperationOutcome outcome = new OperationOutcome();
156 outcome.setResponse(data);
158 step.success(outcome);
159 verify(stepContext).setProperty(AaiGetTenantOperation.getKey(MY_VSERVER), data);