Make targetEntity a property
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / impl / BidirectionalTopicOperationTest.java
index 587564a..f63e07e 100644 (file)
@@ -35,12 +35,13 @@ import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.CompletableFuture;
 import java.util.function.BiConsumer;
+import lombok.EqualsAndHashCode;
 import lombok.Getter;
 import lombok.Setter;
-import org.apache.commons.lang3.tuple.Pair;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
@@ -67,7 +68,6 @@ public class BidirectionalTopicOperationTest {
     private static final String OPERATION = "my-operation";
     private static final String REQ_ID = "my-request-id";
     private static final String TEXT = "some text";
-    private static final String SUB_REQID = "my-sub-request-id";
     private static final int TIMEOUT_SEC = 10;
     private static final long TIMEOUT_MS = 1000 * TIMEOUT_SEC;
     private static final int MAX_REQUESTS = 100;
@@ -87,6 +87,7 @@ public class BidirectionalTopicOperationTest {
     private ControlLoopOperationParams params;
     private OperationOutcome outcome;
     private StandardCoderObject stdResponse;
+    private MyResponse response;
     private String responseText;
     private PseudoExecutor executor;
     private int ntimes;
@@ -109,9 +110,11 @@ public class BidirectionalTopicOperationTest {
         executor = new PseudoExecutor();
 
         params = ControlLoopOperationParams.builder().actor(ACTOR).operation(OPERATION).executor(executor).build();
-        outcome = params.makeOutcome();
+        outcome = params.makeOutcome(null);
 
-        responseText = coder.encode(new MyResponse());
+        response = new MyResponse();
+        response.setRequestId(REQ_ID);
+        responseText = coder.encode(response);
         stdResponse = coder.decode(responseText, StandardCoderObject.class);
 
         ntimes = 1;
@@ -138,8 +141,6 @@ public class BidirectionalTopicOperationTest {
         CompletableFuture<OperationOutcome> future = oper.startOperationAsync(1, outcome);
         assertFalse(future.isDone());
 
-        assertEquals(SUB_REQID, outcome.getSubRequestId());
-
         verify(forwarder).register(eq(Arrays.asList(REQ_ID)), listenerCaptor.capture());
 
         verify(forwarder, never()).unregister(any(), any());
@@ -163,6 +164,35 @@ public class BidirectionalTopicOperationTest {
 
         assertSame(outcome, future.get());
         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        assertEquals(response, outcome.getResponse());
+
+        verify(forwarder).unregister(eq(Arrays.asList(REQ_ID)), eq(listenerCaptor.getValue()));
+    }
+
+    /**
+     * Tests startOperationAsync() when processResponse() throws an exception.
+     */
+    @Test
+    public void testStartOperationAsyncProcException() throws Exception {
+        oper = new MyOperation() {
+            @Override
+            protected OperationOutcome processResponse(OperationOutcome outcome, String rawResponse,
+                            StandardCoderObject scoResponse) {
+                throw EXPECTED_EXCEPTION;
+            }
+        };
+
+        CompletableFuture<OperationOutcome> future = oper.startOperationAsync(1, outcome);
+        assertFalse(future.isDone());
+
+        verify(forwarder).register(eq(Arrays.asList(REQ_ID)), listenerCaptor.capture());
+
+        verify(forwarder, never()).unregister(any(), any());
+
+        // provide a response
+        listenerCaptor.getValue().accept(responseText, stdResponse);
+        assertTrue(executor.runAll(MAX_REQUESTS));
+        assertTrue(future.isCompletedExceptionally());
 
         verify(forwarder).unregister(eq(Arrays.asList(REQ_ID)), eq(listenerCaptor.getValue()));
     }
@@ -171,7 +201,7 @@ public class BidirectionalTopicOperationTest {
      * Tests startOperationAsync() when the publisher throws an exception.
      */
     @Test
-    public void testStartOperationAsyncException() throws Exception {
+    public void testStartOperationAsyncPubException() throws Exception {
         // indicate that nothing was published
         when(handler.send(any())).thenReturn(false);
 
@@ -234,6 +264,7 @@ public class BidirectionalTopicOperationTest {
 
         assertSame(outcome, oper2.processResponse(outcome, TEXT, null));
         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        assertEquals(TEXT, outcome.getResponse());
     }
 
     /**
@@ -246,6 +277,7 @@ public class BidirectionalTopicOperationTest {
 
         assertSame(outcome, oper2.processResponse(outcome, responseText, stdResponse));
         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        assertEquals(stdResponse, outcome.getResponse());
     }
 
     /**
@@ -262,6 +294,7 @@ public class BidirectionalTopicOperationTest {
 
         assertSame(outcome, oper.processResponse(outcome, responseText, stdResponse));
         assertEquals(PolicyResult.FAILURE, outcome.getResult());
+        assertEquals(resp, outcome.getResponse());
     }
 
     /**
@@ -271,6 +304,7 @@ public class BidirectionalTopicOperationTest {
     public void testProcessResponseDecodeOk() throws CoderException {
         assertSame(outcome, oper.processResponse(outcome, responseText, stdResponse));
         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+        assertEquals(response, outcome.getResponse());
     }
 
     /**
@@ -290,8 +324,8 @@ public class BidirectionalTopicOperationTest {
     }
 
     @Test
-    public void testMakeCoder() {
-        assertNotNull(oper.makeCoder());
+    public void testGetCoder() {
+        assertNotNull(oper.getCoder());
     }
 
     /**
@@ -300,7 +334,7 @@ public class BidirectionalTopicOperationTest {
     private void setOperCoderException() {
         oper = new MyOperation() {
             @Override
-            protected Coder makeCoder() {
+            protected Coder getCoder() {
                 return new StandardCoder() {
                     @Override
                     public String encode(Object object, boolean pretty) throws CoderException {
@@ -320,8 +354,9 @@ public class BidirectionalTopicOperationTest {
 
     @Getter
     @Setter
+    @EqualsAndHashCode
     public static class MyResponse {
-        private String requestId = REQ_ID;
+        private String requestId;
         private String output;
     }
 
@@ -329,12 +364,12 @@ public class BidirectionalTopicOperationTest {
     private class MyStringOperation extends BidirectionalTopicOperation<String, String> {
 
         public MyStringOperation() {
-            super(BidirectionalTopicOperationTest.this.params, config, String.class);
+            super(BidirectionalTopicOperationTest.this.params, config, String.class, Collections.emptyList());
         }
 
         @Override
-        protected Pair<String, String> makeRequest(int attempt) {
-            return Pair.of(SUB_REQID, TEXT);
+        protected String makeRequest(int attempt) {
+            return TEXT;
         }
 
         @Override
@@ -351,12 +386,13 @@ public class BidirectionalTopicOperationTest {
 
     private class MyScoOperation extends BidirectionalTopicOperation<MyRequest, StandardCoderObject> {
         public MyScoOperation() {
-            super(BidirectionalTopicOperationTest.this.params, config, StandardCoderObject.class);
+            super(BidirectionalTopicOperationTest.this.params, config, StandardCoderObject.class,
+                            Collections.emptyList());
         }
 
         @Override
-        protected Pair<String, MyRequest> makeRequest(int attempt) {
-            return Pair.of(SUB_REQID, new MyRequest());
+        protected MyRequest makeRequest(int attempt) {
+            return new MyRequest();
         }
 
         @Override
@@ -373,12 +409,12 @@ public class BidirectionalTopicOperationTest {
 
     private class MyOperation extends BidirectionalTopicOperation<MyRequest, MyResponse> {
         public MyOperation() {
-            super(BidirectionalTopicOperationTest.this.params, config, MyResponse.class);
+            super(BidirectionalTopicOperationTest.this.params, config, MyResponse.class, Collections.emptyList());
         }
 
         @Override
-        protected Pair<String, MyRequest> makeRequest(int attempt) {
-            return Pair.of(SUB_REQID, new MyRequest());
+        protected MyRequest makeRequest(int attempt) {
+            return new MyRequest();
         }
 
         @Override