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.ArgumentMatchers.any;
24 import static org.mockito.Mockito.when;
27 import java.util.concurrent.CompletableFuture;
28 import javax.ws.rs.client.AsyncInvoker;
29 import javax.ws.rs.client.Entity;
30 import javax.ws.rs.client.Invocation.Builder;
31 import javax.ws.rs.client.InvocationCallback;
32 import javax.ws.rs.client.WebTarget;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.Captor;
37 import org.mockito.Mock;
38 import org.mockito.stubbing.Answer;
39 import org.onap.policy.common.endpoints.http.client.HttpClient;
40 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
41 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
44 * Superclass for various HttpOperation tests.
46 public class BasicHttpOperation extends BasicOperation {
47 protected static final String MY_CLIENT = "my-client";
48 protected static final String BASE_URI = "http://my-host:6969/base-uri/";
49 protected static final String PATH = "my-path/";
52 protected ArgumentCaptor<InvocationCallback<Response>> callbackCaptor;
54 protected ArgumentCaptor<Entity<String>> requestCaptor;
56 protected ArgumentCaptor<Map<String, Object>> headerCaptor;
59 protected HttpConfig config;
61 protected WebTarget webTarget;
63 protected Builder webBuilder;
65 protected AsyncInvoker webAsync;
67 protected HttpClient client;
69 protected HttpClientFactory factory;
71 protected Response rawResponse;
75 * Constructs the object using a default actor and operation name.
77 public BasicHttpOperation() {
82 * Constructs the object.
84 * @param actor actor name
85 * @param operation operation name
87 public BasicHttpOperation(String actor, String operation) {
88 super(actor, operation);
92 * Initializes mocks and sets up.
95 public void setUpBasic() {
98 when(factory.get(MY_CLIENT)).thenReturn(client);
100 when(rawResponse.getStatus()).thenReturn(200);
102 when(webBuilder.async()).thenReturn(webAsync);
103 when(webBuilder.accept(any(MediaType.class))).thenReturn(webBuilder);
104 when(webBuilder.accept(any(String.class))).thenReturn(webBuilder);
106 when(webTarget.request()).thenReturn(webBuilder);
107 when(webTarget.path(any())).thenReturn(webTarget);
108 when(webTarget.queryParam(any(), any())).thenReturn(webTarget);
110 when(client.getWebTarget()).thenReturn(webTarget);
112 when(client.getBaseUrl()).thenReturn(BASE_URI);
118 * Initializes a configuration.
120 protected void initConfig() {
121 when(config.getClient()).thenReturn(client);
122 when(config.getPath()).thenReturn(PATH);
126 * Provides a response to an asynchronous HttpClient call.
128 * @param response response to be provided to the call
129 * @return a function that provides the response to the call
131 protected Answer<CompletableFuture<Response>> provideResponse(Response response) {
132 return provideResponse(response, 0);
136 * Provides a response to an asynchronous HttpClient call.
138 * @param response response to be provided to the call
139 * @param index index of the callback within the arguments
140 * @return a function that provides the response to the call
142 protected Answer<CompletableFuture<Response>> provideResponse(Response response, int index) {
144 InvocationCallback<Response> cb = args.getArgument(index);
145 cb.completed(response);
146 return CompletableFuture.completedFuture(response);