9869c21829b69e5279dbf5a63e966c545058d53d
[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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.never;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 import java.util.UUID;
33 import java.util.concurrent.CompletableFuture;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.onap.policy.aai.AaiCqResponse;
39 import org.onap.policy.controlloop.VirtualControlLoopEvent;
40 import org.onap.policy.controlloop.actor.aai.AaiActor;
41 import org.onap.policy.controlloop.actor.aai.AaiCustomQueryOperation;
42 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
43 import org.onap.policy.controlloop.actorserviceprovider.Operation;
44 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
45 import org.onap.policy.controlloop.actorserviceprovider.Operator;
46 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
47 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
48 import org.onap.policy.controlloop.eventmanager.StepContext;
49
50 public class AaiCqStep2Test {
51     private static final String MY_TARGET = "my-target";
52     private static final UUID REQ_ID = UUID.randomUUID();
53
54     @Mock
55     private Operator policyOperator;
56     @Mock
57     private Operation policyOperation;
58     @Mock
59     private Actor policyActor;
60     @Mock
61     private ActorService actors;
62     @Mock
63     private ControlLoopOperationParams params;
64     @Mock
65     private StepContext stepContext;
66     @Mock
67     private VirtualControlLoopEvent event;
68
69     private CompletableFuture<OperationOutcome> future;
70     private Step2 master;
71     private AaiCqStep2 step;
72
73     /**
74      * Sets up.
75      */
76     @Before
77     public void setUp() {
78         MockitoAnnotations.initMocks(this);
79
80         future = new CompletableFuture<>();
81
82         when(params.toBuilder()).thenReturn(ControlLoopOperationParams.builder().actorService(actors)
83                         .targetEntity(MY_TARGET).requestId(REQ_ID));
84
85         // configure policy operation
86         when(actors.getActor(AaiActor.NAME)).thenReturn(policyActor);
87         when(policyActor.getOperator(AaiCustomQueryOperation.NAME)).thenReturn(policyOperator);
88         when(policyOperator.buildOperation(any())).thenReturn(policyOperation);
89         when(policyOperation.start()).thenReturn(future);
90
91         master = new Step2(stepContext, params, event);
92         step = new AaiCqStep2(master);
93     }
94
95     @Test
96     public void testConstructor() {
97         assertEquals(AaiActor.NAME, step.getActorName());
98         assertEquals(AaiCustomQueryOperation.NAME, step.getOperationName());
99         assertSame(stepContext, step.stepContext);
100         assertSame(event, step.event);
101     }
102
103     @Test
104     public void testStart() {
105         step.init();
106         assertTrue(step.start(100));
107         verify(policyOperation).start();
108     }
109
110     /**
111      * Tests start() when the data has already been retrieved.
112      */
113     @Test
114     public void testStartAlreadyHaveData() {
115         when(stepContext.contains(AaiCqResponse.CONTEXT_KEY)).thenReturn(true);
116
117         step.init();
118         assertFalse(step.start(200));
119         verify(policyOperation, never()).start();
120     }
121
122     @Test
123     public void testSuccess() {
124         AaiCqResponse data = new AaiCqResponse("{}");
125         OperationOutcome outcome = new OperationOutcome();
126         outcome.setResponse(data);
127
128         step.success(outcome);
129         verify(stepContext).setProperty(AaiCqResponse.CONTEXT_KEY, data);
130     }
131 }