Java 17 Upgrade
[policy/models.git] / models-interactions / model-actors / actor.sdnc / src / test / java / org / onap / policy / controlloop / actor / sdnc / BasicSdncOperation.java
index db8751d..a88bb02 100644 (file)
 
 package org.onap.policy.controlloop.actor.sdnc;
 
-import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
-import java.util.Map;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeoutException;
-import java.util.function.BiFunction;
+import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
+import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
+import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
-import org.onap.policy.common.utils.resources.ResourceUtils;
 import org.onap.policy.controlloop.actor.test.BasicHttpOperation;
 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
-import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator;
-import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
-import org.onap.policy.controlloop.policy.PolicyResult;
+import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
 import org.onap.policy.sdnc.SdncRequest;
 import org.onap.policy.sdnc.SdncResponse;
 import org.onap.policy.sdnc.SdncResponseOutput;
-import org.powermock.reflect.Whitebox;
+import org.onap.policy.simulators.Util;
 
 /**
  * Superclass for various operator tests.
  */
-public abstract class BasicSdncOperation extends BasicHttpOperation<SdncRequest> {
+public abstract class BasicSdncOperation extends BasicHttpOperation {
+    /**
+     * Fields to be ignored when comparing requests with JSON.
+     */
+    protected static final String[] IGNORE_FIELDS = {"svc-request-id"};
 
     protected SdncResponse response;
 
@@ -70,11 +72,28 @@ public abstract class BasicSdncOperation extends BasicHttpOperation<SdncRequest>
         super(actor, operation);
     }
 
+    /**
+     * Starts the simulator.
+     */
+    protected static void initBeforeClass() throws Exception {
+        var testServer = Util.buildSdncSim();
+        assertNotNull(testServer);
+
+        BusTopicParams clientParams = BusTopicParams.builder().clientName(MY_CLIENT).basePath("restconf/operations/")
+                        .hostname("localhost").managed(true).port(Util.SDNCSIM_SERVER_PORT).build();
+        HttpClientFactoryInstance.getClientFactory().build(clientParams);
+    }
+
+    protected static void destroyAfterClass() {
+        HttpClientFactoryInstance.getClientFactory().destroy();
+        HttpServletServerFactoryInstance.getServerFactory().destroy();
+    }
+
     /**
      * Initializes mocks and sets up.
      */
     public void setUp() throws Exception {
-        super.setUp();
+        super.setUpBasic();
 
         response = new SdncResponse();
 
@@ -92,7 +111,7 @@ public abstract class BasicSdncOperation extends BasicHttpOperation<SdncRequest>
      * @return the request that was posted
      */
     protected SdncRequest verifyOperation(SdncOperation operation)
-                    throws InterruptedException, ExecutionException, TimeoutException {
+                    throws InterruptedException, ExecutionException, TimeoutException, CoderException {
 
         CompletableFuture<OperationOutcome> future2 = operation.start();
         executor.runAll(100);
@@ -104,49 +123,14 @@ public abstract class BasicSdncOperation extends BasicHttpOperation<SdncRequest>
         executor.runAll(100);
         assertTrue(future2.isDone());
 
-        assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
+        outcome = future2.get();
+        assertEquals(OperationResult.SUCCESS, outcome.getResult());
+        assertTrue(outcome.getResponse() instanceof SdncResponse);
 
-        return requestCaptor.getValue().getEntity();
-    }
+        assertNotNull(outcome.getSubRequestId());
 
-    /**
-     * 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();
+        String reqText = requestCaptor.getValue().getEntity();
 
-        assertEquals(expected, json);
+        return coder.decode(reqText, SdncRequest.class);
     }
-
-    /**
-     * Verifies that an exception is thrown if a field is missing from the enrichment
-     * data.
-     *
-     * @param fieldName name of the field to be removed from the enrichment data
-     * @param expectedText text expected in the exception message
-     */
-    protected void verifyMissing(String fieldName, String expectedText,
-                    BiFunction<ControlLoopOperationParams,HttpOperator,SdncOperation> maker) {
-
-        makeContext();
-        enrichment.remove(fieldName);
-
-        SdncOperation oper = maker.apply(params, operator);
-
-        assertThatIllegalArgumentException().isThrownBy(() -> Whitebox.invokeMethod(oper, "makeRequest", 1))
-                        .withMessageContaining("missing").withMessageContaining(expectedText);
-    }
-
-    protected abstract Map<String, String> makeEnrichment();
 }