7b1bae7d86060f11d5574111d13bf3564183c6f8
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.drools.apps.controller.usecases.step;
22
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;
33
34 import java.util.Map;
35 import java.util.UUID;
36 import java.util.concurrent.CompletableFuture;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
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 public class AaiGetTenantStep2Test {
55     private static final String MY_VSERVER = "my-vserver";
56     private static final UUID REQ_ID = UUID.randomUUID();
57
58     @Mock
59     private Operator policyOperator;
60     @Mock
61     private Operation policyOperation;
62     @Mock
63     private Actor policyActor;
64     @Mock
65     private ActorService actors;
66     @Mock
67     private ControlLoopOperationParams params;
68     @Mock
69     private StepContext stepContext;
70     @Mock
71     private VirtualControlLoopEvent event;
72
73     private CompletableFuture<OperationOutcome> future;
74     private Step2 master;
75     private AaiGetTenantStep2 step;
76
77     /**
78      * Sets up.
79      */
80     @Before
81     public void setUp() {
82         MockitoAnnotations.initMocks(this);
83
84         future = new CompletableFuture<>();
85
86         when(params.toBuilder()).thenReturn(ControlLoopOperationParams.builder().actorService(actors)
87                         .requestId(REQ_ID));
88
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);
94
95         when(event.getAai()).thenReturn(Map.of(Step2.VSERVER_VSERVER_NAME, MY_VSERVER));
96
97         master = new Step2(stepContext, params, event);
98         step = new AaiGetTenantStep2(master);
99     }
100
101     @Test
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);
107
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");
112
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");
117     }
118
119     @Test
120     public void testGetPropertyNames() {
121         assertThat(step.getPropertyNames()).isEmpty();
122     }
123
124     @Test
125     public void testSetProperties() {
126         step.init();
127
128         step.setProperties();
129
130         verify(policyOperation).setProperty(OperationProperties.AAI_TARGET_ENTITY, MY_VSERVER);
131     }
132
133     @Test
134     public void testStart() {
135         step.init();
136         assertTrue(step.start(100));
137         verify(policyOperation).start();
138     }
139
140     /**
141      * Tests start() when the data has already been retrieved.
142      */
143     @Test
144     public void testStartAlreadyHaveData() {
145         when(stepContext.contains(AaiGetTenantOperation.getKey(MY_VSERVER))).thenReturn(true);
146
147         step.init();
148         assertFalse(step.start(200));
149         verify(policyOperation, never()).start();
150     }
151
152     @Test
153     public void testSuccess() {
154         StandardCoderObject data = new StandardCoderObject();
155         OperationOutcome outcome = new OperationOutcome();
156         outcome.setResponse(data);
157
158         step.success(outcome);
159         verify(stepContext).setProperty(AaiGetTenantOperation.getKey(MY_VSERVER), data);
160     }
161 }