Remove Target and TargetType
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / impl / HttpOperationTest.java
index 50cb8fa..453592d 100644 (file)
@@ -29,7 +29,7 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.spy;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.when;
 
 import java.util.Collections;
@@ -39,6 +39,7 @@ import java.util.UUID;
 import java.util.concurrent.CancellationException;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executor;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
@@ -67,6 +68,7 @@ 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;
 import org.onap.policy.common.endpoints.http.client.HttpClient;
+import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
@@ -76,13 +78,12 @@ 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.Operation;
 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
-import org.onap.policy.controlloop.actorserviceprovider.Util;
+import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
 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;
 
 public class HttpOperationTest {
 
@@ -110,9 +111,12 @@ public class HttpOperationTest {
 
     @Mock
     private HttpClient client;
-
+    @Mock
+    private HttpClientFactory clientFactory;
     @Mock
     private Response response;
+    @Mock
+    private Executor executor;
 
     private VirtualControlLoopEvent event;
     private ControlLoopEventContext context;
@@ -120,7 +124,7 @@ public class HttpOperationTest {
     private OperationOutcome outcome;
     private AtomicReference<InvocationCallback<Response>> callback;
     private Future<Response> future;
-    private HttpOperator operator;
+    private HttpConfig config;
     private MyGetOperation<String> oper;
 
     /**
@@ -146,8 +150,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());
 
@@ -187,24 +190,14 @@ public class HttpOperationTest {
         context = new ControlLoopEventContext(event);
         params = ControlLoopOperationParams.builder().actor(ACTOR).operation(OPERATION).context(context).build();
 
-        outcome = params.makeOutcome();
+        outcome = params.makeOutcome(null);
 
         callback = new AtomicReference<>();
         future = new CompletableFuture<>();
 
-        operator = new HttpOperator(ACTOR, OPERATION) {
-            @Override
-            public Operation buildOperation(ControlLoopOperationParams params) {
-                return null;
-            }
-
-            @Override
-            public HttpClient getClient() {
-                return client;
-            }
-        };
+        when(clientFactory.get(any())).thenReturn(client);
 
-        initOper(operator, HTTP_CLIENT);
+        initConfig(HTTP_CLIENT);
 
         oper = new MyGetOperation<>(String.class);
     }
@@ -222,16 +215,18 @@ public class HttpOperationTest {
     }
 
     @Test
-    public void testMakePath() {
-        assertEquals(PATH, oper.makePath());
+    public void testGetPath() {
+        assertEquals(PATH, oper.getPath());
     }
 
     @Test
     public void testMakeUrl() {
         // use a real client
-        client = HttpClientFactoryInstance.getClientFactory().get(HTTP_CLIENT);
+        initRealConfig(HTTP_CLIENT);
+
+        oper = new MyGetOperation<>(String.class);
 
-        assertThat(oper.makeUrl()).endsWith("/" + BASE_URI + PATH);
+        assertThat(oper.getUrl()).endsWith("/" + BASE_URI + PATH);
     }
 
     @Test
@@ -243,19 +238,6 @@ public class HttpOperationTest {
 
         // should use given value
         assertEquals(20 * 1000L, oper.getTimeoutMs(20));
-
-        // indicate we have a timeout value
-        operator = spy(operator);
-        when(operator.getTimeoutMs()).thenReturn(30L);
-
-        oper = new MyGetOperation<String>(String.class);
-
-        // should use default
-        assertEquals(30L, oper.getTimeoutMs(null));
-        assertEquals(30L, oper.getTimeoutMs(0));
-
-        // should use given value
-        assertEquals(40 * 1000L, oper.getTimeoutMs(40));
     }
 
     /**
@@ -273,8 +255,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());
     }
 
     /**
@@ -302,32 +285,41 @@ public class HttpOperationTest {
      * Tests processResponse() when it's a success and the response type is a String.
      */
     @Test
-    public void testProcessResponseSuccessString() {
-        assertSame(outcome, oper.processResponse(outcome, PATH, response));
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+    public void testProcessResponseSuccessString() throws Exception {
+        CompletableFuture<OperationOutcome> result = oper.processResponse(outcome, PATH, response);
+        assertTrue(result.isDone());
+        assertSame(outcome, result.get());
+        assertEquals(OperationResult.SUCCESS, outcome.getResult());
+        assertSame(TEXT, outcome.getResponse());
     }
 
     /**
      * Tests processResponse() when it's a failure.
      */
     @Test
-    public void testProcessResponseFailure() {
+    public void testProcessResponseFailure() throws Exception {
         when(response.getStatus()).thenReturn(555);
-        assertSame(outcome, oper.processResponse(outcome, PATH, response));
-        assertEquals(PolicyResult.FAILURE, outcome.getResult());
+        CompletableFuture<OperationOutcome> result = oper.processResponse(outcome, PATH, response);
+        assertTrue(result.isDone());
+        assertSame(outcome, result.get());
+        assertEquals(OperationResult.FAILURE, outcome.getResult());
+        assertSame(TEXT, outcome.getResponse());
     }
 
     /**
      * Tests processResponse() when the decoder succeeds.
      */
     @Test
-    public void testProcessResponseDecodeOk() throws CoderException {
+    public void testProcessResponseDecodeOk() throws Exception {
         when(response.readEntity(String.class)).thenReturn("10");
 
         MyGetOperation<Integer> oper2 = new MyGetOperation<>(Integer.class);
 
-        assertSame(outcome, oper2.processResponse(outcome, PATH, response));
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        CompletableFuture<OperationOutcome> result = oper2.processResponse(outcome, PATH, response);
+        assertTrue(result.isDone());
+        assertSame(outcome, result.get());
+        assertEquals(OperationResult.SUCCESS, outcome.getResult());
+        assertEquals(Integer.valueOf(10), outcome.getResponse());
     }
 
     /**
@@ -360,14 +352,15 @@ public class HttpOperationTest {
     @Test
     public void testGet() throws Exception {
         // use a real client
-        client = HttpClientFactoryInstance.getClientFactory().get(HTTP_CLIENT);
+        initRealConfig(HTTP_CLIENT);
 
         MyGetOperation<MyResponse> oper2 = new MyGetOperation<>(MyResponse.class);
 
         OperationOutcome outcome = runOperation(oper2);
         assertNotNull(outcome);
         assertEquals(1, nget);
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        assertEquals(OperationResult.SUCCESS, outcome.getResult());
+        assertTrue(outcome.getResponse() instanceof MyResponse);
     }
 
     /**
@@ -376,14 +369,15 @@ public class HttpOperationTest {
     @Test
     public void testDelete() throws Exception {
         // use a real client
-        client = HttpClientFactoryInstance.getClientFactory().get(HTTP_CLIENT);
+        initRealConfig(HTTP_CLIENT);
 
         MyDeleteOperation oper2 = new MyDeleteOperation();
 
         OperationOutcome outcome = runOperation(oper2);
         assertNotNull(outcome);
         assertEquals(1, ndelete);
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        assertEquals(OperationResult.SUCCESS, outcome.getResult());
+        assertTrue(outcome.getResponse() instanceof String);
     }
 
     /**
@@ -392,14 +386,14 @@ public class HttpOperationTest {
     @Test
     public void testPost() throws Exception {
         // use a real client
-        client = HttpClientFactoryInstance.getClientFactory().get(HTTP_CLIENT);
-
+        initRealConfig(HTTP_CLIENT);
         MyPostOperation oper2 = new MyPostOperation();
 
         OperationOutcome outcome = runOperation(oper2);
         assertNotNull(outcome);
         assertEquals(1, npost);
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        assertEquals(OperationResult.SUCCESS, outcome.getResult());
+        assertTrue(outcome.getResponse() instanceof MyResponse);
     }
 
     /**
@@ -408,19 +402,20 @@ public class HttpOperationTest {
     @Test
     public void testPut() throws Exception {
         // use a real client
-        client = HttpClientFactoryInstance.getClientFactory().get(HTTP_CLIENT);
+        initRealConfig(HTTP_CLIENT);
 
         MyPutOperation oper2 = new MyPutOperation();
 
         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());
     }
 
     /**
@@ -448,18 +443,34 @@ public class HttpOperationTest {
     }
 
     /**
-     * Initializes the given operator.
+     * Initializes the configuration.
      *
      * @param operator operator to be initialized
      * @param clientName name of the client which it should use
      */
-    private void initOper(HttpOperator operator, String clientName) {
-        operator.stop();
+    private void initConfig(String clientName) {
+        initConfig(clientName, clientFactory);
+    }
 
+    /**
+     * 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) {
         HttpParams params = HttpParams.builder().clientName(clientName).path(PATH).timeoutSec(1).build();
-        Map<String, Object> mapParams = Util.translateToMap(OPERATION, params);
-        operator.configure(mapParams);
-        operator.start();
+        config = new HttpConfig(executor, params, factory);
+    }
+
+    /**
+     * 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) {
+        initConfig(clientName, HttpClientFactoryInstance.getClientFactory());
     }
 
     /**
@@ -491,7 +502,7 @@ public class HttpOperationTest {
 
     private class MyGetOperation<T> extends HttpOperation<T> {
         public MyGetOperation(Class<T> responseClass) {
-            super(HttpOperationTest.this.params, HttpOperationTest.this.operator, responseClass);
+            super(HttpOperationTest.this.params, HttpOperationTest.this.config, responseClass, Collections.emptyList());
         }
 
         @Override
@@ -499,20 +510,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 -> operator.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.operator, MyResponse.class);
+            super(HttpOperationTest.this.params, HttpOperationTest.this.config, MyResponse.class,
+                            Collections.emptyList());
         }
 
         @Override
@@ -520,25 +532,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 -> operator.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.operator, MyResponse.class);
+            super(HttpOperationTest.this.params, HttpOperationTest.this.config, MyResponse.class,
+                            Collections.emptyList());
         }
 
         @Override
@@ -546,25 +560,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 -> operator.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.operator, String.class);
+            super(HttpOperationTest.this.params, HttpOperationTest.this.config, String.class, Collections.emptyList());
         }
 
         @Override
@@ -572,13 +587,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 -> operator.getClient().delete(callback, makePath(), headers));
+                callback -> getClient().delete(callback, getPath(), headers));
             // @formatter:on
         }
     }