2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 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.actorserviceprovider.impl;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatCode;
26 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
27 import static org.assertj.core.api.Assertions.assertThatThrownBy;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertSame;
32 import static org.junit.Assert.assertTrue;
33 import static org.mockito.ArgumentMatchers.any;
34 import static org.mockito.Mockito.when;
36 import jakarta.ws.rs.Consumes;
37 import jakarta.ws.rs.DELETE;
38 import jakarta.ws.rs.GET;
39 import jakarta.ws.rs.POST;
40 import jakarta.ws.rs.PUT;
41 import jakarta.ws.rs.Path;
42 import jakarta.ws.rs.Produces;
43 import jakarta.ws.rs.client.Entity;
44 import jakarta.ws.rs.client.InvocationCallback;
45 import jakarta.ws.rs.core.MediaType;
46 import jakarta.ws.rs.core.Response;
47 import jakarta.ws.rs.core.Response.Status;
48 import java.util.Collections;
50 import java.util.Properties;
51 import java.util.UUID;
52 import java.util.concurrent.CancellationException;
53 import java.util.concurrent.CompletableFuture;
54 import java.util.concurrent.ExecutionException;
55 import java.util.concurrent.Executor;
56 import java.util.concurrent.Future;
57 import java.util.concurrent.TimeUnit;
58 import java.util.concurrent.TimeoutException;
59 import java.util.concurrent.atomic.AtomicReference;
62 import org.junit.AfterClass;
63 import org.junit.Before;
64 import org.junit.BeforeClass;
65 import org.junit.Test;
66 import org.junit.runner.RunWith;
67 import org.mockito.Mock;
68 import org.mockito.junit.MockitoJUnitRunner;
69 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
70 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
71 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams.TopicParamsBuilder;
72 import org.onap.policy.common.endpoints.http.client.HttpClient;
73 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
74 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
75 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
76 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
77 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
78 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
79 import org.onap.policy.common.gson.GsonMessageBodyHandler;
80 import org.onap.policy.common.utils.coder.CoderException;
81 import org.onap.policy.common.utils.network.NetworkUtil;
82 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
83 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
84 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
85 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
86 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
88 @RunWith(MockitoJUnitRunner.class)
89 public class HttpOperationTest {
91 private static final IllegalStateException EXPECTED_EXCEPTION = new IllegalStateException("expected exception");
92 private static final String ACTOR = "my-actor";
93 private static final String OPERATION = "my-name";
94 private static final String HTTP_CLIENT = "my-client";
95 private static final String HTTP_NO_SERVER = "my-http-no-server-client";
96 private static final String MEDIA_TYPE_APPLICATION_JSON = "application/json";
97 private static final String BASE_URI = "oper";
98 private static final String PATH = "/my-path";
99 private static final String TEXT = "my-text";
100 private static final UUID REQ_ID = UUID.randomUUID();
103 * {@code True} if the server should reject the request, {@code false} otherwise.
105 private static boolean rejectRequest;
107 // call counts of each method type in the server
108 private static int nget;
109 private static int npost;
110 private static int nput;
111 private static int ndelete;
114 private HttpClient client;
116 private HttpClientFactory clientFactory;
118 private Response response;
120 private Executor executor;
122 private ControlLoopOperationParams params;
123 private OperationOutcome outcome;
124 private AtomicReference<InvocationCallback<Response>> callback;
125 private Future<Response> future;
126 private HttpConfig config;
127 private MyGetOperation<String> oper;
130 * Starts the simulator.
133 public static void setUpBeforeClass() throws Exception {
135 int port = NetworkUtil.allocPort();
138 * Start the simulator. Must use "Properties" to configure it, otherwise the
139 * server will use the wrong serialization provider.
141 Properties svrprops = getServerProperties("my-server", port);
142 HttpServletServerFactoryInstance.getServerFactory().build(svrprops).forEach(HttpServletServer::start);
144 if (!NetworkUtil.isTcpPortOpen("localhost", port, 100, 100)) {
145 HttpServletServerFactoryInstance.getServerFactory().destroy();
146 throw new IllegalStateException("server is not running");
150 * Start the clients, one to the server, and one to a non-existent server.
152 TopicParamsBuilder builder = BusTopicParams.builder().managed(true).hostname("localhost").basePath(BASE_URI);
154 HttpClientFactoryInstance.getClientFactory().build(builder.clientName(HTTP_CLIENT).port(port).build());
156 HttpClientFactoryInstance.getClientFactory()
157 .build(builder.clientName(HTTP_NO_SERVER).port(NetworkUtil.allocPort()).build());
161 * Destroys the Http factories and stops the appender.
164 public static void tearDownAfterClass() {
165 HttpClientFactoryInstance.getClientFactory().destroy();
166 HttpServletServerFactoryInstance.getServerFactory().destroy();
170 * Initializes fields, including {@link #oper}, and resets the static fields used by
174 public void setUp() {
175 rejectRequest = false;
181 when(response.readEntity(String.class)).thenReturn(TEXT);
182 when(response.getStatus()).thenReturn(200);
184 params = ControlLoopOperationParams.builder().actor(ACTOR).operation(OPERATION).requestId(REQ_ID).build();
186 outcome = params.makeOutcome();
188 callback = new AtomicReference<>();
189 future = new CompletableFuture<>();
191 when(clientFactory.get(any())).thenReturn(client);
193 initConfig(HTTP_CLIENT);
195 oper = new MyGetOperation<>(String.class);
199 public void testHttpOperator() {
200 assertEquals(ACTOR, oper.getActorName());
201 assertEquals(OPERATION, oper.getName());
202 assertEquals(ACTOR + "." + OPERATION, oper.getFullName());
206 public void testMakeHeaders() {
207 assertEquals(Collections.emptyMap(), oper.makeHeaders());
211 public void testGetPath() {
212 assertEquals(PATH, oper.getPath());
216 public void testMakeUrl() {
218 initRealConfig(HTTP_CLIENT);
220 oper = new MyGetOperation<>(String.class);
222 assertThat(oper.getUrl()).endsWith("/" + BASE_URI + PATH);
226 public void testDoConfigureMapOfStringObject_testGetClient_testGetPath_testGetTimeoutMs() {
228 // use value from operator
229 assertEquals(1000L, oper.getTimeoutMs(null));
230 assertEquals(1000L, oper.getTimeoutMs(0));
232 // should use given value
233 assertEquals(20 * 1000L, oper.getTimeoutMs(20));
237 * Tests handleResponse() when it completes.
240 public void testHandleResponseComplete() throws Exception {
241 CompletableFuture<OperationOutcome> future2 = oper.handleResponse(outcome, PATH, cb -> {
246 assertFalse(future2.isDone());
247 assertNotNull(callback.get());
248 callback.get().completed(response);
250 assertSame(outcome, future2.get(5, TimeUnit.SECONDS));
251 assertSame(TEXT, outcome.getResponse());
253 assertEquals(OperationResult.SUCCESS, outcome.getResult());
257 * Tests handleResponse() when it fails.
260 public void testHandleResponseFailed() throws Exception {
261 CompletableFuture<OperationOutcome> future2 = oper.handleResponse(outcome, PATH, cb -> {
266 assertFalse(future2.isDone());
267 assertNotNull(callback.get());
268 callback.get().failed(EXPECTED_EXCEPTION);
270 assertThatThrownBy(() -> future2.get(5, TimeUnit.SECONDS)).hasCause(EXPECTED_EXCEPTION);
272 // future and future2 may be completed in parallel so we must wait again
273 assertThatThrownBy(() -> future.get(5, TimeUnit.SECONDS)).isInstanceOf(CancellationException.class);
274 assertTrue(future.isCancelled());
278 * Tests processResponse() when it's a success and the response type is a String.
281 public void testProcessResponseSuccessString() throws Exception {
282 CompletableFuture<OperationOutcome> result = oper.processResponse(outcome, PATH, response);
283 assertTrue(result.isDone());
284 assertSame(outcome, result.get());
285 assertEquals(OperationResult.SUCCESS, outcome.getResult());
286 assertSame(TEXT, outcome.getResponse());
290 * Tests processResponse() when it's a failure.
293 public void testProcessResponseFailure() throws Exception {
294 when(response.getStatus()).thenReturn(555);
295 CompletableFuture<OperationOutcome> result = oper.processResponse(outcome, PATH, response);
296 assertTrue(result.isDone());
297 assertSame(outcome, result.get());
298 assertEquals(OperationResult.FAILURE, outcome.getResult());
299 assertSame(TEXT, outcome.getResponse());
303 * Tests processResponse() when the decoder succeeds.
306 public void testProcessResponseDecodeOk() throws Exception {
307 when(response.readEntity(String.class)).thenReturn("10");
309 MyGetOperation<Integer> oper2 = new MyGetOperation<>(Integer.class);
311 CompletableFuture<OperationOutcome> result = oper2.processResponse(outcome, PATH, response);
312 assertTrue(result.isDone());
313 assertSame(outcome, result.get());
314 assertEquals(OperationResult.SUCCESS, outcome.getResult());
315 assertEquals(Integer.valueOf(10), outcome.getResponse());
319 * Tests processResponse() when the decoder throws an exception.
322 public void testProcessResponseDecodeExcept() throws CoderException {
323 MyGetOperation<Integer> oper2 = new MyGetOperation<>(Integer.class);
325 assertThatIllegalArgumentException().isThrownBy(() -> oper2.processResponse(outcome, PATH, response));
329 public void testPostProcessResponse() {
330 assertThatCode(() -> oper.postProcessResponse(outcome, PATH, null, null)).doesNotThrowAnyException();
334 public void testIsSuccess() {
335 when(response.getStatus()).thenReturn(200);
336 assertTrue(oper.isSuccess(response, null));
338 when(response.getStatus()).thenReturn(555);
339 assertFalse(oper.isSuccess(response, null));
346 public void testGet() throws Exception {
348 initRealConfig(HTTP_CLIENT);
350 MyGetOperation<MyResponse> oper2 = new MyGetOperation<>(MyResponse.class);
352 OperationOutcome outcome = runOperation(oper2);
353 assertNotNull(outcome);
354 assertEquals(1, nget);
355 assertEquals(OperationResult.SUCCESS, outcome.getResult());
356 assertTrue(outcome.getResponse() instanceof MyResponse);
363 public void testDelete() throws Exception {
365 initRealConfig(HTTP_CLIENT);
367 MyDeleteOperation oper2 = new MyDeleteOperation();
369 OperationOutcome outcome = runOperation(oper2);
370 assertNotNull(outcome);
371 assertEquals(1, ndelete);
372 assertEquals(OperationResult.SUCCESS, outcome.getResult());
373 assertTrue(outcome.getResponse() instanceof String);
380 public void testPost() throws Exception {
382 initRealConfig(HTTP_CLIENT);
383 MyPostOperation oper2 = new MyPostOperation();
385 OperationOutcome outcome = runOperation(oper2);
386 assertNotNull(outcome);
387 assertEquals(1, npost);
388 assertEquals(OperationResult.SUCCESS, outcome.getResult());
389 assertTrue(outcome.getResponse() instanceof MyResponse);
396 public void testPut() throws Exception {
398 initRealConfig(HTTP_CLIENT);
400 MyPutOperation oper2 = new MyPutOperation();
402 OperationOutcome outcome = runOperation(oper2);
403 assertNotNull(outcome);
404 assertEquals(1, nput);
405 assertEquals(OperationResult.SUCCESS, outcome.getResult());
406 assertTrue(outcome.getResponse() instanceof MyResponse);
410 public void testMakeDecoder() {
411 assertNotNull(oper.getCoder());
415 * Gets server properties.
417 * @param name server name
418 * @param port server port
419 * @return server properties
421 private static Properties getServerProperties(String name, int port) {
422 final Properties props = new Properties();
423 props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, name);
425 final String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + name;
427 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, Server.class.getName());
428 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, "localhost");
429 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, String.valueOf(port));
430 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
431 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "false");
433 props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
434 GsonMessageBodyHandler.class.getName());
439 * Initializes the configuration.
441 * @param clientName name of the client which it should use
443 private void initConfig(String clientName) {
444 initConfig(clientName, clientFactory);
448 * Initializes the configuration with a real client.
450 * @param clientName name of the client which it should use
452 private void initConfig(String clientName, HttpClientFactory factory) {
453 HttpParams params = HttpParams.builder().clientName(clientName).path(PATH).timeoutSec(1).build();
454 config = new HttpConfig(executor, params, factory);
458 * Initializes the configuration with a real client.
460 * @param clientName name of the client which it should use
462 private void initRealConfig(String clientName) {
463 initConfig(clientName, HttpClientFactoryInstance.getClientFactory());
467 * Runs the operation.
469 * @param operator operator on which to start the operation
470 * @return the outcome of the operation, or {@code null} if it does not complete in
473 private <T> OperationOutcome runOperation(HttpOperation<T> operator)
474 throws InterruptedException, ExecutionException, TimeoutException {
476 CompletableFuture<OperationOutcome> future = operator.start();
478 return future.get(5, TimeUnit.SECONDS);
483 public static class MyRequest {
484 private String input = "some input";
489 public static class MyResponse {
490 private String output = "some output";
493 private class MyGetOperation<T> extends HttpOperation<T> {
494 public MyGetOperation(Class<T> responseClass) {
495 super(HttpOperationTest.this.params, HttpOperationTest.this.config, responseClass, Collections.emptyList());
499 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
500 Map<String, Object> headers = makeHeaders();
502 headers.put("Accept", MediaType.APPLICATION_JSON);
503 String url = getUrl();
505 logMessage(EventType.OUT, CommInfrastructure.REST, url, null);
508 return handleResponse(outcome, url,
509 callback -> getClient().get(callback, getPath(), headers));
514 private class MyPostOperation extends HttpOperation<MyResponse> {
515 public MyPostOperation() {
516 super(HttpOperationTest.this.params, HttpOperationTest.this.config, MyResponse.class,
517 Collections.emptyList());
521 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
523 MyRequest request = new MyRequest();
525 Map<String, Object> headers = makeHeaders();
527 headers.put("Accept", MediaType.APPLICATION_JSON);
528 String url = getUrl();
530 String strRequest = prettyPrint(request);
531 logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
533 Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
536 return handleResponse(outcome, url,
537 callback -> getClient().post(callback, getPath(), entity, headers));
542 private class MyPutOperation extends HttpOperation<MyResponse> {
543 public MyPutOperation() {
544 super(HttpOperationTest.this.params, HttpOperationTest.this.config, MyResponse.class,
545 Collections.emptyList());
549 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
551 MyRequest request = new MyRequest();
553 Map<String, Object> headers = makeHeaders();
555 headers.put("Accept", MediaType.APPLICATION_JSON);
556 String url = getUrl();
558 String strRequest = prettyPrint(request);
559 logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
561 Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
564 return handleResponse(outcome, url,
565 callback -> getClient().put(callback, getPath(), entity, headers));
570 private class MyDeleteOperation extends HttpOperation<String> {
571 public MyDeleteOperation() {
572 super(HttpOperationTest.this.params, HttpOperationTest.this.config, String.class, Collections.emptyList());
576 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
577 Map<String, Object> headers = makeHeaders();
579 headers.put("Accept", MediaType.APPLICATION_JSON);
580 String url = getUrl();
582 logMessage(EventType.OUT, CommInfrastructure.REST, url, null);
585 return handleResponse(outcome, url,
586 callback -> getClient().delete(callback, getPath(), headers));
594 @Path("/" + BASE_URI)
595 @Produces(MEDIA_TYPE_APPLICATION_JSON)
596 @Consumes(value = {MEDIA_TYPE_APPLICATION_JSON})
597 public static class Server {
600 * Generates a response to a GET.
602 * @return resulting response
606 public Response getRequest() {
610 return Response.status(Status.BAD_REQUEST).build();
613 return Response.status(Status.OK).entity(new MyResponse()).build();
618 * Generates a response to a POST.
620 * @param request incoming request
621 * @return resulting response
625 public Response postRequest(MyRequest request) {
629 return Response.status(Status.BAD_REQUEST).build();
632 return Response.status(Status.OK).entity(new MyResponse()).build();
637 * Generates a response to a PUT.
639 * @param request incoming request
640 * @return resulting response
644 public Response putRequest(MyRequest request) {
648 return Response.status(Status.BAD_REQUEST).build();
651 return Response.status(Status.OK).entity(new MyResponse()).build();
656 * Generates a response to a DELETE.
658 * @return resulting response
662 public Response deleteRequest() {
666 return Response.status(Status.BAD_REQUEST).build();
669 return Response.status(Status.OK).entity(new MyResponse()).build();