Merge "Use "coder" to serialize Actor requests"
[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.ArgumentMatchers.any;
24 import static org.mockito.Mockito.when;
25
26 import java.util.Map;
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;
42
43 /**
44  * Superclass for various HttpOperation tests.
45  *
46  * @param <Q> request type
47  */
48 public class BasicHttpOperation<Q> extends BasicOperation {
49     protected static final String MY_CLIENT = "my-client";
50     protected static final String BASE_URI = "http://my-host:6969/base-uri/";
51     protected static final String PATH = "my-path/";
52
53     @Captor
54     protected ArgumentCaptor<InvocationCallback<Response>> callbackCaptor;
55     @Captor
56     protected ArgumentCaptor<Entity<String>> requestCaptor;
57     @Captor
58     protected ArgumentCaptor<Map<String, Object>> headerCaptor;
59
60     @Mock
61     protected HttpConfig config;
62     @Mock
63     protected WebTarget webTarget;
64     @Mock
65     protected Builder webBuilder;
66     @Mock
67     protected AsyncInvoker webAsync;
68     @Mock
69     protected HttpClient client;
70     @Mock
71     protected HttpClientFactory factory;
72     @Mock
73     protected Response rawResponse;
74
75
76     /**
77      * Constructs the object using a default actor and operation name.
78      */
79     public BasicHttpOperation() {
80         super();
81     }
82
83     /**
84      * Constructs the object.
85      *
86      * @param actor actor name
87      * @param operation operation name
88      */
89     public BasicHttpOperation(String actor, String operation) {
90         super(actor, operation);
91     }
92
93     /**
94      * Initializes mocks and sets up.
95      */
96     @Override
97     public void setUpBasic() {
98         super.setUpBasic();
99
100         when(factory.get(MY_CLIENT)).thenReturn(client);
101
102         when(rawResponse.getStatus()).thenReturn(200);
103
104         when(webBuilder.async()).thenReturn(webAsync);
105         when(webBuilder.accept(any(MediaType.class))).thenReturn(webBuilder);
106         when(webBuilder.accept(any(String.class))).thenReturn(webBuilder);
107
108         when(webTarget.request()).thenReturn(webBuilder);
109         when(webTarget.path(any())).thenReturn(webTarget);
110         when(webTarget.queryParam(any(), any())).thenReturn(webTarget);
111
112         when(client.getWebTarget()).thenReturn(webTarget);
113
114         when(client.getBaseUrl()).thenReturn(BASE_URI);
115
116         initConfig();
117     }
118
119     /**
120      * Initializes a configuration.
121      */
122     protected void initConfig() {
123         when(config.getClient()).thenReturn(client);
124         when(config.getPath()).thenReturn(PATH);
125     }
126
127     /**
128      * Provides a response to an asynchronous HttpClient call.
129      *
130      * @param response response to be provided to the call
131      * @return a function that provides the response to the call
132      */
133     protected Answer<CompletableFuture<Response>> provideResponse(Response response) {
134         return provideResponse(response, 0);
135     }
136
137     /**
138      * Provides a response to an asynchronous HttpClient call.
139      *
140      * @param response response to be provided to the call
141      * @param index index of the callback within the arguments
142      * @return a function that provides the response to the call
143      */
144     protected Answer<CompletableFuture<Response>> provideResponse(Response response, int index) {
145         return args -> {
146             InvocationCallback<Response> cb = args.getArgument(index);
147             cb.completed(response);
148             return CompletableFuture.completedFuture(response);
149         };
150     }
151 }