Add A&AI actor and some operators
[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.TreeMap;
27 import java.util.UUID;
28 import java.util.concurrent.CompletableFuture;
29 import javax.ws.rs.client.Entity;
30 import javax.ws.rs.client.InvocationCallback;
31 import javax.ws.rs.core.Response;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.Captor;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.mockito.stubbing.Answer;
37 import org.onap.policy.common.endpoints.http.client.HttpClient;
38 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
39 import org.onap.policy.controlloop.VirtualControlLoopEvent;
40 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
41 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
42 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
43 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator;
44 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
45
46 /**
47  * Superclass for various operator tests.
48  *
49  * @param <Q> request type
50  */
51 public class BasicHttpOperation<Q> {
52     protected static final UUID REQ_ID = UUID.randomUUID();
53     protected static final String DEFAULT_ACTOR = "default-actor";
54     protected static final String DEFAULT_OPERATION = "default-operation";
55     protected static final String MY_CLIENT = "my-client";
56     protected static final String BASE_URI = "/base-uri";
57     protected static final String PATH = "/my-path";
58     protected static final String TARGET_ENTITY = "my-target";
59
60     protected final String actorName;
61     protected final String operationName;
62
63     @Captor
64     protected ArgumentCaptor<InvocationCallback<Response>> callbackCaptor;
65
66     @Captor
67     protected ArgumentCaptor<Entity<Q>> requestCaptor;
68
69     @Captor
70     protected ArgumentCaptor<Map<String, Object>> headerCaptor;
71
72     @Mock
73     protected ActorService service;
74
75     @Mock
76     protected HttpClient client;
77
78     @Mock
79     protected HttpClientFactory factory;
80
81     @Mock
82     protected Response rawResponse;
83
84     @Mock
85     protected HttpOperator operator;
86
87     protected CompletableFuture<Response> future;
88     protected ControlLoopOperationParams params;
89     protected Map<String, String> enrichment;
90     protected VirtualControlLoopEvent event;
91     protected ControlLoopEventContext context;
92     protected OperationOutcome outcome;
93
94     /**
95      * Constructs the object using a default actor and operation name.
96      */
97     public BasicHttpOperation() {
98         this.actorName = DEFAULT_ACTOR;
99         this.operationName = DEFAULT_OPERATION;
100     }
101
102     /**
103      * Constructs the object.
104      *
105      * @param actor actor name
106      * @param operation operation name
107      */
108     public BasicHttpOperation(String actor, String operation) {
109         this.actorName = actor;
110         this.operationName = operation;
111     }
112
113     /**
114      * Initializes mocks and sets up.
115      */
116     public void setUp() throws Exception {
117         MockitoAnnotations.initMocks(this);
118
119         when(factory.get(MY_CLIENT)).thenReturn(client);
120
121         when(rawResponse.getStatus()).thenReturn(200);
122
123         future = new CompletableFuture<>();
124         when(client.getBaseUrl()).thenReturn(BASE_URI);
125
126         makeContext();
127
128         outcome = params.makeOutcome();
129
130         initOperator();
131     }
132
133     /**
134      * Reinitializes {@link #enrichment}, {@link #event}, {@link #context}, and
135      * {@link #params}.
136      */
137     protected void makeContext() {
138         enrichment = new TreeMap<>(makeEnrichment());
139
140         event = new VirtualControlLoopEvent();
141         event.setRequestId(REQ_ID);
142         event.setAai(enrichment);
143
144         context = new ControlLoopEventContext(event);
145
146         params = ControlLoopOperationParams.builder().context(context).actorService(service).actor(actorName)
147                         .operation(operationName).targetEntity(TARGET_ENTITY).build();
148     }
149
150     /**
151      * Initializes an operator so that it is "alive" and has the given names.
152      */
153     protected void initOperator() {
154         when(operator.isAlive()).thenReturn(true);
155         when(operator.getFullName()).thenReturn(actorName + "." + operationName);
156         when(operator.getActorName()).thenReturn(actorName);
157         when(operator.getName()).thenReturn(operationName);
158         when(operator.getClient()).thenReturn(client);
159         when(operator.getPath()).thenReturn(PATH);
160     }
161
162     /**
163      * Makes enrichment data.
164      *
165      * @return enrichment data
166      */
167     protected Map<String, String> makeEnrichment() {
168         return new TreeMap<>();
169     }
170
171     /**
172      * Provides a response to an asynchronous HttpClient call.
173      *
174      * @param response response to be provided to the call
175      * @return a function that provides the response to the call
176      */
177     protected Answer<CompletableFuture<Response>> provideResponse(Response response) {
178         return args -> {
179             InvocationCallback<Response> cb = args.getArgument(0);
180             cb.completed(response);
181             return CompletableFuture.completedFuture(response);
182         };
183     }
184 }