Fix some sonars in policy-models
[policy/models.git] / models-interactions / model-actors / actor.appc / src / test / java / org / onap / policy / controlloop / actor / appc / BasicAppcOperation.java
index cbdcad6..1a8c603 100644 (file)
@@ -32,31 +32,35 @@ import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeoutException;
 import java.util.function.BiConsumer;
-import java.util.function.BiFunction;
+import org.onap.policy.appc.Request;
 import org.onap.policy.appc.Response;
 import org.onap.policy.appc.ResponseCode;
 import org.onap.policy.appc.ResponseStatus;
-import org.onap.policy.common.utils.coder.CoderException;
-import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.endpoints.event.comm.TopicSink;
+import org.onap.policy.common.endpoints.event.comm.TopicSource;
+import org.onap.policy.common.utils.coder.StandardCoderInstantAsMillis;
 import org.onap.policy.common.utils.coder.StandardCoderObject;
-import org.onap.policy.common.utils.resources.ResourceUtils;
 import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation;
 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
 import org.onap.policy.controlloop.actorserviceprovider.Util;
-import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperator;
-import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
+import org.onap.policy.controlloop.actorserviceprovider.impl.OperationMaker;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
 import org.onap.policy.controlloop.policy.PolicyResult;
 import org.onap.policy.controlloop.policy.Target;
+import org.onap.policy.simulators.AppcLegacyTopicServer;
+import org.onap.policy.simulators.TopicServer;
 import org.powermock.reflect.Whitebox;
 
 /**
  * Superclass for various operator tests.
  */
-public abstract class BasicAppcOperation extends BasicBidirectionalTopicOperation {
+public abstract class BasicAppcOperation extends BasicBidirectionalTopicOperation<Request> {
+    protected static final String[] IGNORE_FIELDS = {"RequestID", "subRequestID", "TimeStamp"};
     protected static final String MY_DESCRIPTION = "my-description";
     protected static final String MY_VNF = "my-vnf";
     protected static final String KEY1 = "my-key-A";
     protected static final String KEY2 = "my-key-B";
+    protected static final String KEY3 = "my-key-C";
     protected static final String VALUE1 = "{\"input\":\"hello\"}";
     protected static final String VALUE2 = "{\"output\":\"world\"}";
     protected static final String RESOURCE_ID = "my-resource";
@@ -67,7 +71,7 @@ public abstract class BasicAppcOperation extends BasicBidirectionalTopicOperatio
      * Constructs the object using a default actor and operation name.
      */
     public BasicAppcOperation() {
-        super();
+        this.coder = new StandardCoderInstantAsMillis();
     }
 
     /**
@@ -78,13 +82,14 @@ public abstract class BasicAppcOperation extends BasicBidirectionalTopicOperatio
      */
     public BasicAppcOperation(String actor, String operation) {
         super(actor, operation);
+        this.coder = new StandardCoderInstantAsMillis();
     }
 
     /**
      * Initializes mocks and sets up.
      */
     public void setUp() throws Exception {
-        super.setUp();
+        super.setUpBasic();
 
         response = new Response();
 
@@ -94,6 +99,14 @@ public abstract class BasicAppcOperation extends BasicBidirectionalTopicOperatio
         status.setDescription(MY_DESCRIPTION);
     }
 
+    public void tearDown() {
+        super.tearDownBasic();
+    }
+
+    protected TopicServer<Request> makeServer(TopicSink sink, TopicSource source) {
+        return new AppcLegacyTopicServer(sink, source);
+    }
+
     /**
      * Runs the operation and verifies that the response is successful.
      *
@@ -117,26 +130,6 @@ public abstract class BasicAppcOperation extends BasicBidirectionalTopicOperatio
         assertEquals(MY_DESCRIPTION, outcome.getMessage());
     }
 
-    /**
-     * Pretty-prints a request and verifies that the result matches the expected JSON.
-     *
-     * @param <T> request type
-     * @param expectedJsonFile name of the file containing the expected JSON
-     * @param request request to verify
-     * @throws CoderException if the request cannot be pretty-printed
-     */
-    protected <T> void verifyRequest(String expectedJsonFile, T request) throws CoderException {
-        String json = new StandardCoder().encode(request, true);
-        String expected = ResourceUtils.getResourceAsString(expectedJsonFile);
-
-        // strip request id, because it changes each time
-        final String stripper = "svc-request-id[^,]*";
-        json = json.replaceFirst(stripper, "").trim();
-        expected = expected.replaceFirst(stripper, "").trim();
-
-        assertEquals(expected, json);
-    }
-
     /**
      * Verifies that an exception is thrown if a field is missing from the enrichment
      * data.
@@ -145,12 +138,12 @@ public abstract class BasicAppcOperation extends BasicBidirectionalTopicOperatio
      * @param expectedText text expected in the exception message
      */
     protected void verifyMissing(String fieldName, String expectedText,
-                    BiFunction<ControlLoopOperationParams, BidirectionalTopicOperator, AppcOperation> maker) {
+                    OperationMaker<BidirectionalTopicConfig, AppcOperation> maker) {
 
         makeContext();
         enrichment.remove(fieldName);
 
-        AppcOperation oper = maker.apply(params, operator);
+        AppcOperation oper = maker.apply(params, config);
 
         assertThatIllegalArgumentException().isThrownBy(() -> Whitebox.invokeMethod(oper, "makeRequest", 1))
                         .withMessageContaining("missing").withMessageContaining(expectedText);
@@ -185,7 +178,7 @@ public abstract class BasicAppcOperation extends BasicBidirectionalTopicOperatio
     }
 
     @Override
-    protected Map<String, String> makePayload() {
+    protected Map<String, Object> makePayload() {
         return Map.of(KEY1, VALUE1, KEY2, VALUE2);
     }
 }