9d95b0b6821e5b75e6eddd7715da599c2488fbd9
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.drools.apps.controller.usecases.step;
23
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;
35
36 import java.util.Map;
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;
53
54 class AaiGetTenantStep2Test {
55     private static final String MY_VSERVER = "my-vserver";
56     private static final UUID REQ_ID = UUID.randomUUID();
57
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);
65
66     private CompletableFuture<OperationOutcome> future;
67     private Step2 master;
68     private AaiGetTenantStep2 step;
69
70     /**
71      * Sets up.
72      */
73     @BeforeEach
74     public void setUp() {
75         future = new CompletableFuture<>();
76
77         when(params.toBuilder()).thenReturn(ControlLoopOperationParams.builder().actorService(actors)
78                         .requestId(REQ_ID));
79
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);
85
86         when(event.getAai()).thenReturn(Map.of(Step2.VSERVER_VSERVER_NAME, MY_VSERVER));
87
88         master = new Step2(stepContext, params, event);
89         step = new AaiGetTenantStep2(master);
90     }
91
92     @Test
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);
98
99         // empty vserver name
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");
103
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");
108     }
109
110     @Test
111     void testGetPropertyNames() {
112         assertThat(step.getPropertyNames()).isEmpty();
113     }
114
115     @Test
116     void testSetProperties() {
117         step.init();
118
119         step.setProperties();
120
121         verify(policyOperation).setProperty(OperationProperties.AAI_TARGET_ENTITY, MY_VSERVER);
122     }
123
124     @Test
125     void testStart() {
126         step.init();
127         assertTrue(step.start(100));
128         verify(policyOperation).start();
129     }
130
131     /**
132      * Tests start() when the data has already been retrieved.
133      */
134     @Test
135     void testStartAlreadyHaveData() {
136         when(stepContext.contains(AaiGetTenantOperation.getKey(MY_VSERVER))).thenReturn(true);
137
138         step.init();
139         assertFalse(step.start(200));
140         verify(policyOperation, never()).start();
141     }
142
143     @Test
144     void testSuccess() {
145         var data = new StandardCoderObject();
146         var outcome = new OperationOutcome();
147         outcome.setResponse(data);
148
149         step.success(outcome);
150         verify(stepContext).setProperty(AaiGetTenantOperation.getKey(MY_VSERVER), data);
151     }
152 }