2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.drools.apps.controller.usecases.step;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.junit.jupiter.api.Assertions.assertFalse;
28 import static org.junit.jupiter.api.Assertions.assertSame;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.never;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
37 import java.util.UUID;
38 import java.util.concurrent.CompletableFuture;
39 import org.junit.jupiter.api.BeforeEach;
40 import org.junit.jupiter.api.Test;
41 import org.onap.policy.common.utils.coder.StandardCoderObject;
42 import org.onap.policy.controlloop.VirtualControlLoopEvent;
43 import org.onap.policy.controlloop.actor.aai.AaiActor;
44 import org.onap.policy.controlloop.actor.aai.AaiGetTenantOperation;
45 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
46 import org.onap.policy.controlloop.actorserviceprovider.Operation;
47 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
48 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
49 import org.onap.policy.controlloop.actorserviceprovider.Operator;
50 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
51 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
52 import org.onap.policy.controlloop.eventmanager.StepContext;
54 class AaiGetTenantStep2Test {
55 private static final String MY_VSERVER = "my-vserver";
56 private static final UUID REQ_ID = UUID.randomUUID();
58 private final Operator policyOperator = mock(Operator.class);
59 private final Operation policyOperation = mock(Operation.class);
60 private final Actor policyActor = mock(Actor.class);
61 private final ActorService actors = mock(ActorService.class);
62 private final ControlLoopOperationParams params = mock(ControlLoopOperationParams.class);
63 private final StepContext stepContext = mock(StepContext.class);
64 private final VirtualControlLoopEvent event = mock(VirtualControlLoopEvent.class);
66 private CompletableFuture<OperationOutcome> future;
68 private AaiGetTenantStep2 step;
75 future = new CompletableFuture<>();
77 when(params.toBuilder()).thenReturn(ControlLoopOperationParams.builder().actorService(actors)
80 // configure policy operation
81 when(actors.getActor(AaiActor.NAME)).thenReturn(policyActor);
82 when(policyActor.getOperator(AaiGetTenantOperation.NAME)).thenReturn(policyOperator);
83 when(policyOperator.buildOperation(any())).thenReturn(policyOperation);
84 when(policyOperation.start()).thenReturn(future);
86 when(event.getAai()).thenReturn(Map.of(Step2.VSERVER_VSERVER_NAME, MY_VSERVER));
88 master = new Step2(stepContext, params, event);
89 step = new AaiGetTenantStep2(master);
93 void testConstructor() {
94 assertEquals(AaiActor.NAME, step.getActorName());
95 assertEquals(AaiGetTenantOperation.NAME, step.getOperationName());
96 assertSame(stepContext, step.stepContext);
97 assertSame(event, step.event);
100 when(event.getAai()).thenReturn(Map.of(Step2.VSERVER_VSERVER_NAME, ""));
101 assertThatIllegalArgumentException().isThrownBy(() -> new AaiGetTenantStep2(master))
102 .withMessage("missing " + Step2.VSERVER_VSERVER_NAME + " in enrichment data");
104 // missing vserver name
105 when(event.getAai()).thenReturn(Map.of());
106 assertThatIllegalArgumentException().isThrownBy(() -> new AaiGetTenantStep2(master))
107 .withMessage("missing " + Step2.VSERVER_VSERVER_NAME + " in enrichment data");
111 void testGetPropertyNames() {
112 assertThat(step.getPropertyNames()).isEmpty();
116 void testSetProperties() {
119 step.setProperties();
121 verify(policyOperation).setProperty(OperationProperties.AAI_TARGET_ENTITY, MY_VSERVER);
127 assertTrue(step.start(100));
128 verify(policyOperation).start();
132 * Tests start() when the data has already been retrieved.
135 void testStartAlreadyHaveData() {
136 when(stepContext.contains(AaiGetTenantOperation.getKey(MY_VSERVER))).thenReturn(true);
139 assertFalse(step.start(200));
140 verify(policyOperation, never()).start();
145 var data = new StandardCoderObject();
146 var outcome = new OperationOutcome();
147 outcome.setResponse(data);
149 step.success(outcome);
150 verify(stepContext).setProperty(AaiGetTenantOperation.getKey(MY_VSERVER), data);