Java 17 Upgrade
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / impl / HttpOperationTest.java
index 2e9f58c..7a99a54 100644 (file)
@@ -2,7 +2,8 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2023 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -32,6 +33,18 @@ import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.when;
 
+import jakarta.ws.rs.Consumes;
+import jakarta.ws.rs.DELETE;
+import jakarta.ws.rs.GET;
+import jakarta.ws.rs.POST;
+import jakarta.ws.rs.PUT;
+import jakarta.ws.rs.Path;
+import jakarta.ws.rs.Produces;
+import jakarta.ws.rs.client.Entity;
+import jakarta.ws.rs.client.InvocationCallback;
+import jakarta.ws.rs.core.MediaType;
+import jakarta.ws.rs.core.Response;
+import jakarta.ws.rs.core.Response.Status;
 import java.util.Collections;
 import java.util.Map;
 import java.util.Properties;
@@ -44,26 +57,15 @@ import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicReference;
-import javax.ws.rs.Consumes;
-import javax.ws.rs.DELETE;
-import javax.ws.rs.GET;
-import javax.ws.rs.POST;
-import javax.ws.rs.PUT;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.InvocationCallback;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-import javax.ws.rs.core.Response.Status;
 import lombok.Getter;
 import lombok.Setter;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams.TopicParamsBuilder;
@@ -77,14 +79,13 @@ import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
 import org.onap.policy.common.gson.GsonMessageBodyHandler;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.network.NetworkUtil;
-import org.onap.policy.controlloop.VirtualControlLoopEvent;
 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
-import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
+import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams;
-import org.onap.policy.controlloop.policy.PolicyResult;
 
+@RunWith(MockitoJUnitRunner.class)
 public class HttpOperationTest {
 
     private static final IllegalStateException EXPECTED_EXCEPTION = new IllegalStateException("expected exception");
@@ -118,8 +119,6 @@ public class HttpOperationTest {
     @Mock
     private Executor executor;
 
-    private VirtualControlLoopEvent event;
-    private ControlLoopEventContext context;
     private ControlLoopOperationParams params;
     private OperationOutcome outcome;
     private AtomicReference<InvocationCallback<Response>> callback;
@@ -150,8 +149,7 @@ public class HttpOperationTest {
         /*
          * Start the clients, one to the server, and one to a non-existent server.
          */
-        TopicParamsBuilder builder = BusTopicParams.builder().managed(true).hostname("localhost").basePath(BASE_URI)
-                        .serializationProvider(GsonMessageBodyHandler.class.getName());
+        TopicParamsBuilder builder = BusTopicParams.builder().managed(true).hostname("localhost").basePath(BASE_URI);
 
         HttpClientFactoryInstance.getClientFactory().build(builder.clientName(HTTP_CLIENT).port(port).build());
 
@@ -174,8 +172,6 @@ public class HttpOperationTest {
      */
     @Before
     public void setUp() {
-        MockitoAnnotations.initMocks(this);
-
         rejectRequest = false;
         nget = 0;
         npost = 0;
@@ -185,11 +181,7 @@ public class HttpOperationTest {
         when(response.readEntity(String.class)).thenReturn(TEXT);
         when(response.getStatus()).thenReturn(200);
 
-        event = new VirtualControlLoopEvent();
-        event.setRequestId(REQ_ID);
-
-        context = new ControlLoopEventContext(event);
-        params = ControlLoopOperationParams.builder().actor(ACTOR).operation(OPERATION).context(context).build();
+        params = ControlLoopOperationParams.builder().actor(ACTOR).operation(OPERATION).requestId(REQ_ID).build();
 
         outcome = params.makeOutcome();
 
@@ -216,8 +208,8 @@ public class HttpOperationTest {
     }
 
     @Test
-    public void testMakePath() {
-        assertEquals(PATH, oper.makePath());
+    public void testGetPath() {
+        assertEquals(PATH, oper.getPath());
     }
 
     @Test
@@ -227,7 +219,7 @@ public class HttpOperationTest {
 
         oper = new MyGetOperation<>(String.class);
 
-        assertThat(oper.makeUrl()).endsWith("/" + BASE_URI + PATH);
+        assertThat(oper.getUrl()).endsWith("/" + BASE_URI + PATH);
     }
 
     @Test
@@ -256,8 +248,9 @@ public class HttpOperationTest {
         callback.get().completed(response);
 
         assertSame(outcome, future2.get(5, TimeUnit.SECONDS));
+        assertSame(TEXT, outcome.getResponse());
 
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        assertEquals(OperationResult.SUCCESS, outcome.getResult());
     }
 
     /**
@@ -289,7 +282,8 @@ public class HttpOperationTest {
         CompletableFuture<OperationOutcome> result = oper.processResponse(outcome, PATH, response);
         assertTrue(result.isDone());
         assertSame(outcome, result.get());
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        assertEquals(OperationResult.SUCCESS, outcome.getResult());
+        assertSame(TEXT, outcome.getResponse());
     }
 
     /**
@@ -301,7 +295,8 @@ public class HttpOperationTest {
         CompletableFuture<OperationOutcome> result = oper.processResponse(outcome, PATH, response);
         assertTrue(result.isDone());
         assertSame(outcome, result.get());
-        assertEquals(PolicyResult.FAILURE, outcome.getResult());
+        assertEquals(OperationResult.FAILURE, outcome.getResult());
+        assertSame(TEXT, outcome.getResponse());
     }
 
     /**
@@ -316,7 +311,8 @@ public class HttpOperationTest {
         CompletableFuture<OperationOutcome> result = oper2.processResponse(outcome, PATH, response);
         assertTrue(result.isDone());
         assertSame(outcome, result.get());
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        assertEquals(OperationResult.SUCCESS, outcome.getResult());
+        assertEquals(Integer.valueOf(10), outcome.getResponse());
     }
 
     /**
@@ -356,7 +352,8 @@ public class HttpOperationTest {
         OperationOutcome outcome = runOperation(oper2);
         assertNotNull(outcome);
         assertEquals(1, nget);
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        assertEquals(OperationResult.SUCCESS, outcome.getResult());
+        assertTrue(outcome.getResponse() instanceof MyResponse);
     }
 
     /**
@@ -372,7 +369,8 @@ public class HttpOperationTest {
         OperationOutcome outcome = runOperation(oper2);
         assertNotNull(outcome);
         assertEquals(1, ndelete);
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        assertEquals(OperationResult.SUCCESS, outcome.getResult());
+        assertTrue(outcome.getResponse() instanceof String);
     }
 
     /**
@@ -387,7 +385,8 @@ public class HttpOperationTest {
         OperationOutcome outcome = runOperation(oper2);
         assertNotNull(outcome);
         assertEquals(1, npost);
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        assertEquals(OperationResult.SUCCESS, outcome.getResult());
+        assertTrue(outcome.getResponse() instanceof MyResponse);
     }
 
     /**
@@ -403,12 +402,13 @@ public class HttpOperationTest {
         OperationOutcome outcome = runOperation(oper2);
         assertNotNull(outcome);
         assertEquals(1, nput);
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        assertEquals(OperationResult.SUCCESS, outcome.getResult());
+        assertTrue(outcome.getResponse() instanceof MyResponse);
     }
 
     @Test
     public void testMakeDecoder() {
-        assertNotNull(oper.makeCoder());
+        assertNotNull(oper.getCoder());
     }
 
     /**
@@ -438,7 +438,6 @@ public class HttpOperationTest {
     /**
      * Initializes the configuration.
      *
-     * @param operator operator to be initialized
      * @param clientName name of the client which it should use
      */
     private void initConfig(String clientName) {
@@ -448,7 +447,6 @@ public class HttpOperationTest {
     /**
      * Initializes the configuration with a real client.
      *
-     * @param operator operator to be initialized
      * @param clientName name of the client which it should use
      */
     private void initConfig(String clientName, HttpClientFactory factory) {
@@ -459,7 +457,6 @@ public class HttpOperationTest {
     /**
      * Initializes the configuration with a real client.
      *
-     * @param operator operator to be initialized
      * @param clientName name of the client which it should use
      */
     private void initRealConfig(String clientName) {
@@ -495,7 +492,7 @@ public class HttpOperationTest {
 
     private class MyGetOperation<T> extends HttpOperation<T> {
         public MyGetOperation(Class<T> responseClass) {
-            super(HttpOperationTest.this.params, HttpOperationTest.this.config, responseClass);
+            super(HttpOperationTest.this.params, HttpOperationTest.this.config, responseClass, Collections.emptyList());
         }
 
         @Override
@@ -503,20 +500,21 @@ public class HttpOperationTest {
             Map<String, Object> headers = makeHeaders();
 
             headers.put("Accept", MediaType.APPLICATION_JSON);
-            String url = makeUrl();
+            String url = getUrl();
 
             logMessage(EventType.OUT, CommInfrastructure.REST, url, null);
 
             // @formatter:off
             return handleResponse(outcome, url,
-                callback -> getClient().get(callback, makePath(), headers));
+                callback -> getClient().get(callback, getPath(), headers));
             // @formatter:on
         }
     }
 
     private class MyPostOperation extends HttpOperation<MyResponse> {
         public MyPostOperation() {
-            super(HttpOperationTest.this.params, HttpOperationTest.this.config, MyResponse.class);
+            super(HttpOperationTest.this.params, HttpOperationTest.this.config, MyResponse.class,
+                            Collections.emptyList());
         }
 
         @Override
@@ -524,25 +522,27 @@ public class HttpOperationTest {
 
             MyRequest request = new MyRequest();
 
-            Entity<MyRequest> entity = Entity.entity(request, MediaType.APPLICATION_JSON);
-
             Map<String, Object> headers = makeHeaders();
 
             headers.put("Accept", MediaType.APPLICATION_JSON);
-            String url = makeUrl();
+            String url = getUrl();
 
-            logMessage(EventType.OUT, CommInfrastructure.REST, url, request);
+            String strRequest = prettyPrint(request);
+            logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
+
+            Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
 
             // @formatter:off
             return handleResponse(outcome, url,
-                callback -> getClient().post(callback, makePath(), entity, headers));
+                callback -> getClient().post(callback, getPath(), entity, headers));
             // @formatter:on
         }
     }
 
     private class MyPutOperation extends HttpOperation<MyResponse> {
         public MyPutOperation() {
-            super(HttpOperationTest.this.params, HttpOperationTest.this.config, MyResponse.class);
+            super(HttpOperationTest.this.params, HttpOperationTest.this.config, MyResponse.class,
+                            Collections.emptyList());
         }
 
         @Override
@@ -550,25 +550,26 @@ public class HttpOperationTest {
 
             MyRequest request = new MyRequest();
 
-            Entity<MyRequest> entity = Entity.entity(request, MediaType.APPLICATION_JSON);
-
             Map<String, Object> headers = makeHeaders();
 
             headers.put("Accept", MediaType.APPLICATION_JSON);
-            String url = makeUrl();
+            String url = getUrl();
+
+            String strRequest = prettyPrint(request);
+            logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
 
-            logMessage(EventType.OUT, CommInfrastructure.REST, url, request);
+            Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
 
             // @formatter:off
             return handleResponse(outcome, url,
-                callback -> getClient().put(callback, makePath(), entity, headers));
+                callback -> getClient().put(callback, getPath(), entity, headers));
             // @formatter:on
         }
     }
 
     private class MyDeleteOperation extends HttpOperation<String> {
         public MyDeleteOperation() {
-            super(HttpOperationTest.this.params, HttpOperationTest.this.config, String.class);
+            super(HttpOperationTest.this.params, HttpOperationTest.this.config, String.class, Collections.emptyList());
         }
 
         @Override
@@ -576,13 +577,13 @@ public class HttpOperationTest {
             Map<String, Object> headers = makeHeaders();
 
             headers.put("Accept", MediaType.APPLICATION_JSON);
-            String url = makeUrl();
+            String url = getUrl();
 
             logMessage(EventType.OUT, CommInfrastructure.REST, url, null);
 
             // @formatter:off
             return handleResponse(outcome, url,
-                callback -> getClient().delete(callback, makePath(), headers));
+                callback -> getClient().delete(callback, getPath(), headers));
             // @formatter:on
         }
     }