More actor clean-up
[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.Mockito.when;
24
25 import java.util.Map;
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.parameters.HttpConfig;
37
38 /**
39  * Superclass for various HttpOperation tests.
40  *
41  * @param <Q> request type
42  */
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";
47
48     @Captor
49     protected ArgumentCaptor<InvocationCallback<Response>> callbackCaptor;
50     @Captor
51     protected ArgumentCaptor<Entity<Q>> requestCaptor;
52     @Captor
53     protected ArgumentCaptor<Map<String, Object>> headerCaptor;
54
55     @Mock
56     protected HttpConfig config;
57     @Mock
58     protected HttpClient client;
59     @Mock
60     protected HttpClientFactory factory;
61     @Mock
62     protected Response rawResponse;
63
64
65     /**
66      * Constructs the object using a default actor and operation name.
67      */
68     public BasicHttpOperation() {
69         super();
70     }
71
72     /**
73      * Constructs the object.
74      *
75      * @param actor actor name
76      * @param operation operation name
77      */
78     public BasicHttpOperation(String actor, String operation) {
79         super(actor, operation);
80     }
81
82     /**
83      * Initializes mocks and sets up.
84      */
85     @Override
86     public void setUpBasic() {
87         super.setUpBasic();
88
89         when(factory.get(MY_CLIENT)).thenReturn(client);
90
91         when(rawResponse.getStatus()).thenReturn(200);
92
93         when(client.getBaseUrl()).thenReturn(BASE_URI);
94
95         initConfig();
96     }
97
98     /**
99      * Initializes a configuration.
100      */
101     protected void initConfig() {
102         when(config.getClient()).thenReturn(client);
103         when(config.getPath()).thenReturn(PATH);
104     }
105
106     /**
107      * Provides a response to an asynchronous HttpClient call.
108      *
109      * @param response response to be provided to the call
110      * @return a function that provides the response to the call
111      */
112     protected Answer<CompletableFuture<Response>> provideResponse(Response response) {
113         return args -> {
114             InvocationCallback<Response> cb = args.getArgument(0);
115             cb.completed(response);
116             return CompletableFuture.completedFuture(response);
117         };
118     }
119 }