Merge "Add safe entity delete, fix multiple entity get"
[policy/models.git] / models-interactions / model-actors / actor.test / src / main / java / org / onap / policy / controlloop / actor / test / BasicHttpOperation.java
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.controlloop.actor.test;
22
23 import static org.mockito.Mockito.when;
24
25 import java.util.Map;
26 import java.util.TreeMap;
27 import java.util.UUID;
28 import java.util.concurrent.CompletableFuture;
29 import javax.ws.rs.client.Entity;
30 import javax.ws.rs.client.InvocationCallback;
31 import javax.ws.rs.core.Response;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.Captor;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.mockito.stubbing.Answer;
37 import org.onap.policy.common.endpoints.http.client.HttpClient;
38 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
39 import org.onap.policy.common.utils.time.PseudoExecutor;
40 import org.onap.policy.controlloop.VirtualControlLoopEvent;
41 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
42 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
43 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
44 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator;
45 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
46
47 /**
48  * Superclass for various HttpOperation tests.
49  *
50  * @param <Q> request type
51  */
52 public class BasicHttpOperation<Q> {
53     protected static final UUID REQ_ID = UUID.randomUUID();
54     protected static final String DEFAULT_ACTOR = "default-actor";
55     protected static final String DEFAULT_OPERATION = "default-operation";
56     protected static final String MY_CLIENT = "my-client";
57     protected static final String BASE_URI = "/base-uri";
58     protected static final String PATH = "/my-path";
59     protected static final String TARGET_ENTITY = "my-target";
60
61     protected final String actorName;
62     protected final String operationName;
63
64     @Captor
65     protected ArgumentCaptor<InvocationCallback<Response>> callbackCaptor;
66
67     @Captor
68     protected ArgumentCaptor<Entity<Q>> requestCaptor;
69
70     @Captor
71     protected ArgumentCaptor<Map<String, Object>> headerCaptor;
72
73     @Mock
74     protected ActorService service;
75
76     @Mock
77     protected HttpClient client;
78
79     @Mock
80     protected HttpClientFactory factory;
81
82     @Mock
83     protected Response rawResponse;
84
85     @Mock
86     protected HttpOperator operator;
87
88     protected CompletableFuture<Response> future;
89     protected ControlLoopOperationParams params;
90     protected Map<String, String> enrichment;
91     protected VirtualControlLoopEvent event;
92     protected ControlLoopEventContext context;
93     protected OperationOutcome outcome;
94     protected PseudoExecutor executor;
95
96     /**
97      * Constructs the object using a default actor and operation name.
98      */
99     public BasicHttpOperation() {
100         this.actorName = DEFAULT_ACTOR;
101         this.operationName = DEFAULT_OPERATION;
102     }
103
104     /**
105      * Constructs the object.
106      *
107      * @param actor actor name
108      * @param operation operation name
109      */
110     public BasicHttpOperation(String actor, String operation) {
111         this.actorName = actor;
112         this.operationName = operation;
113     }
114
115     /**
116      * Initializes mocks and sets up.
117      */
118     public void setUp() throws Exception {
119         MockitoAnnotations.initMocks(this);
120
121         when(factory.get(MY_CLIENT)).thenReturn(client);
122
123         when(rawResponse.getStatus()).thenReturn(200);
124
125         future = new CompletableFuture<>();
126         when(client.getBaseUrl()).thenReturn(BASE_URI);
127
128         executor = new PseudoExecutor();
129
130         makeContext();
131
132         outcome = params.makeOutcome();
133
134         initOperator();
135     }
136
137     /**
138      * Reinitializes {@link #enrichment}, {@link #event}, {@link #context}, and
139      * {@link #params}.
140      * <p/>
141      * Note: {@link #params} is configured to use {@link #executor}.
142      */
143     protected void makeContext() {
144         enrichment = new TreeMap<>(makeEnrichment());
145
146         event = new VirtualControlLoopEvent();
147         event.setRequestId(REQ_ID);
148         event.setAai(enrichment);
149
150         context = new ControlLoopEventContext(event);
151
152         params = ControlLoopOperationParams.builder().executor(executor).context(context).actorService(service)
153                         .actor(actorName).operation(operationName).targetEntity(TARGET_ENTITY).build();
154     }
155
156     /**
157      * Initializes an operator so that it is "alive" and has the given names.
158      */
159     protected void initOperator() {
160         when(operator.isAlive()).thenReturn(true);
161         when(operator.getFullName()).thenReturn(actorName + "." + operationName);
162         when(operator.getActorName()).thenReturn(actorName);
163         when(operator.getName()).thenReturn(operationName);
164         when(operator.getClient()).thenReturn(client);
165         when(operator.getPath()).thenReturn(PATH);
166     }
167
168     /**
169      * Makes enrichment data.
170      *
171      * @return enrichment data
172      */
173     protected Map<String, String> makeEnrichment() {
174         return new TreeMap<>();
175     }
176
177     /**
178      * Provides a response to an asynchronous HttpClient call.
179      *
180      * @param response response to be provided to the call
181      * @return a function that provides the response to the call
182      */
183     protected Answer<CompletableFuture<Response>> provideResponse(Response response) {
184         return args -> {
185             InvocationCallback<Response> cb = args.getArgument(0);
186             cb.completed(response);
187             return CompletableFuture.completedFuture(response);
188         };
189     }
190 }