2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actor.test;
23 import static org.mockito.Mockito.when;
26 import java.util.concurrent.CompletableFuture;
27 import javax.ws.rs.client.Entity;
28 import javax.ws.rs.client.InvocationCallback;
29 import javax.ws.rs.core.Response;
30 import org.mockito.ArgumentCaptor;
31 import org.mockito.Captor;
32 import org.mockito.Mock;
33 import org.mockito.stubbing.Answer;
34 import org.onap.policy.common.endpoints.http.client.HttpClient;
35 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
36 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator;
39 * Superclass for various HttpOperation tests.
41 * @param <Q> request type
43 public class BasicHttpOperation<Q> extends BasicOperation {
44 protected static final String MY_CLIENT = "my-client";
45 protected static final String BASE_URI = "/base-uri";
46 protected static final String PATH = "/my-path";
49 protected ArgumentCaptor<InvocationCallback<Response>> callbackCaptor;
52 protected ArgumentCaptor<Entity<Q>> requestCaptor;
55 protected ArgumentCaptor<Map<String, Object>> headerCaptor;
58 protected HttpClient client;
61 protected HttpClientFactory factory;
64 protected Response rawResponse;
67 protected HttpOperator operator;
71 * Constructs the object using a default actor and operation name.
73 public BasicHttpOperation() {
78 * Constructs the object.
80 * @param actor actor name
81 * @param operation operation name
83 public BasicHttpOperation(String actor, String operation) {
84 super(actor, operation);
88 * Initializes mocks and sets up.
90 public void setUp() throws Exception {
93 when(factory.get(MY_CLIENT)).thenReturn(client);
95 when(rawResponse.getStatus()).thenReturn(200);
97 when(client.getBaseUrl()).thenReturn(BASE_URI);
103 * Initializes an operator so that it is "alive" and has the given names.
105 protected void initOperator() {
106 when(operator.isAlive()).thenReturn(true);
107 when(operator.getFullName()).thenReturn(actorName + "." + operationName);
108 when(operator.getActorName()).thenReturn(actorName);
109 when(operator.getName()).thenReturn(operationName);
110 when(operator.getClient()).thenReturn(client);
111 when(operator.getPath()).thenReturn(PATH);
115 * Provides a response to an asynchronous HttpClient call.
117 * @param response response to be provided to the call
118 * @return a function that provides the response to the call
120 protected Answer<CompletableFuture<Response>> provideResponse(Response response) {
122 InvocationCallback<Response> cb = args.getArgument(0);
123 cb.completed(response);
124 return CompletableFuture.completedFuture(response);