2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actor.test;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.lenient;
27 import jakarta.ws.rs.client.AsyncInvoker;
28 import jakarta.ws.rs.client.Entity;
29 import jakarta.ws.rs.client.Invocation.Builder;
30 import jakarta.ws.rs.client.InvocationCallback;
31 import jakarta.ws.rs.client.WebTarget;
32 import jakarta.ws.rs.core.MediaType;
33 import jakarta.ws.rs.core.Response;
35 import java.util.concurrent.CompletableFuture;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.Captor;
38 import org.mockito.Mock;
39 import org.mockito.stubbing.Answer;
40 import org.onap.policy.common.endpoints.http.client.HttpClient;
41 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
42 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
45 * Superclass for various HttpOperation tests.
47 public class BasicHttpOperation extends BasicOperation {
48 protected static final String MY_CLIENT = "my-client";
49 protected static final String BASE_URI = "http://my-host:6969/base-uri/";
50 protected static final String PATH = "my-path/";
53 protected ArgumentCaptor<InvocationCallback<Response>> callbackCaptor;
55 protected ArgumentCaptor<Entity<String>> requestCaptor;
57 protected ArgumentCaptor<Map<String, Object>> headerCaptor;
60 protected HttpConfig config;
62 protected WebTarget webTarget;
64 protected Builder webBuilder;
66 protected AsyncInvoker webAsync;
68 protected HttpClient client;
70 protected HttpClientFactory factory;
72 protected Response rawResponse;
76 * Constructs the object using a default actor and operation name.
78 public BasicHttpOperation() {
83 * Constructs the object.
85 * @param actor actor name
86 * @param operation operation name
88 public BasicHttpOperation(String actor, String operation) {
89 super(actor, operation);
93 * Initializes mocks and sets up.
96 public void setUpBasic() {
99 lenient().when(factory.get(MY_CLIENT)).thenReturn(client);
101 lenient().when(rawResponse.getStatus()).thenReturn(200);
103 lenient().when(webBuilder.async()).thenReturn(webAsync);
104 lenient().when(webBuilder.accept(any(MediaType.class))).thenReturn(webBuilder);
105 lenient().when(webBuilder.accept(any(String.class))).thenReturn(webBuilder);
107 lenient().when(webTarget.request()).thenReturn(webBuilder);
108 lenient().when(webTarget.path(any())).thenReturn(webTarget);
109 lenient().when(webTarget.queryParam(any(), any())).thenReturn(webTarget);
111 lenient().when(client.getWebTarget()).thenReturn(webTarget);
113 lenient().when(client.getBaseUrl()).thenReturn(BASE_URI);
119 * Initializes a configuration.
121 protected void initConfig() {
122 lenient().when(config.getClient()).thenReturn(client);
123 lenient().when(config.getPath()).thenReturn(PATH);
127 * Provides a response to an asynchronous HttpClient call.
129 * @param response response to be provided to the call
130 * @return a function that provides the response to the call
132 protected Answer<CompletableFuture<Response>> provideResponse(Response response) {
133 return provideResponse(response, 0);
137 * Provides a response to an asynchronous HttpClient call.
139 * @param response response to be provided to the call
140 * @param index index of the callback within the arguments
141 * @return a function that provides the response to the call
143 protected Answer<CompletableFuture<Response>> provideResponse(Response response, int index) {
145 InvocationCallback<Response> cb = args.getArgument(index);
146 cb.completed(response);
147 return CompletableFuture.completedFuture(response);