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