Moving common polling code into HttpOperation
[policy/models.git] / models-interactions / model-actors / actor.so / src / test / java / org / onap / policy / controlloop / actor / so / SoOperationTest.java
index 9920656..46d96fd 100644 (file)
@@ -28,15 +28,14 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
-import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.time.LocalDateTime;
+import java.time.Month;
 import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ForkJoinPool;
-import java.util.concurrent.TimeUnit;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
 import org.junit.Before;
@@ -46,6 +45,7 @@ import org.onap.aai.domain.yang.GenericVnf;
 import org.onap.aai.domain.yang.ServiceInstance;
 import org.onap.aai.domain.yang.Tenant;
 import org.onap.policy.aai.AaiCqResponse;
+import org.onap.policy.common.utils.coder.Coder;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.controlloop.ControlLoopOperation;
 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
@@ -53,7 +53,6 @@ import org.onap.policy.controlloop.policy.PolicyResult;
 import org.onap.policy.so.SoModelInfo;
 import org.onap.policy.so.SoRequest;
 import org.onap.policy.so.SoRequestInfo;
-import org.onap.policy.so.SoRequestReferences;
 import org.onap.policy.so.SoRequestStatus;
 import org.onap.policy.so.SoResponse;
 
@@ -76,11 +75,11 @@ public class SoOperationTest extends BasicSoOperation {
     }
 
     @Test
-    public void testConstructor_testGetWaitMsGet() {
+    public void testConstructor() {
         assertEquals(DEFAULT_ACTOR, oper.getActorName());
         assertEquals(DEFAULT_OPERATION, oper.getName());
         assertSame(config, oper.getConfig());
-        assertEquals(1000 * WAIT_SEC_GETS, oper.getWaitMsGet());
+        assertTrue(oper.isUsePolling());
 
         // check when Target is null
         params = params.toBuilder().target(null).build();
@@ -91,9 +90,9 @@ public class SoOperationTest extends BasicSoOperation {
     @Test
     public void testValidateTarget() {
         // check when various fields are null
-        verifyNotNull("model-customization-id", target::getModelCustomizationId, target::setModelCustomizationId);
-        verifyNotNull("model-invariant-id", target::getModelInvariantId, target::setModelInvariantId);
-        verifyNotNull("model-version-id", target::getModelVersionId, target::setModelVersionId);
+        verifyNotNull("modelCustomizationId", target::getModelCustomizationId, target::setModelCustomizationId);
+        verifyNotNull("modelInvariantId", target::getModelInvariantId, target::setModelInvariantId);
+        verifyNotNull("modelVersionId", target::getModelVersionId, target::setModelVersionId);
 
         // verify it's still valid
         assertThatCode(() -> new VfModuleCreate(params, config)).doesNotThrowAnyException();
@@ -163,91 +162,6 @@ public class SoOperationTest extends BasicSoOperation {
         assertNull(oper.obtainVfCount());
     }
 
-    @Test
-    public void testPostProcess() throws Exception {
-        // completed
-        outcome.setSubRequestId(null);
-        CompletableFuture<OperationOutcome> future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
-        assertTrue(future2.isDone());
-        assertSame(outcome, future2.get());
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
-        assertNotNull(outcome.getSubRequestId());
-
-        // failed
-        outcome.setSubRequestId(null);
-        response.getRequest().getRequestStatus().setRequestState(SoOperation.FAILED);
-        future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
-        assertTrue(future2.isDone());
-        assertSame(outcome, future2.get());
-        assertEquals(PolicyResult.FAILURE, outcome.getResult());
-        assertNotNull(outcome.getSubRequestId());
-
-        // no request id in the response
-        response.getRequestReferences().setRequestId(null);
-        response.getRequest().getRequestStatus().setRequestState("unknown");
-        assertThatIllegalArgumentException()
-                        .isThrownBy(() -> oper.postProcessResponse(outcome, PATH, rawResponse, response))
-                        .withMessage("missing request ID in response");
-        response.getRequestReferences().setRequestId(REQ_ID.toString());
-
-        // status = 500
-        when(rawResponse.getStatus()).thenReturn(500);
-
-        // null request reference
-        SoRequestReferences ref = response.getRequestReferences();
-        response.setRequestReferences(null);
-        assertThatIllegalArgumentException()
-                        .isThrownBy(() -> oper.postProcessResponse(outcome, PATH, rawResponse, response))
-                        .withMessage("missing request ID in response");
-        response.setRequestReferences(ref);
-    }
-
-    /**
-     * Tests postProcess() when the "get" is repeated a couple of times.
-     */
-    @Test
-    public void testPostProcessRepeated_testResetGetCount() throws Exception {
-        /*
-         * Two failures and then a success - should result in two "get" calls.
-         *
-         * Note: getStatus() is invoked twice during each call, so have to double up the
-         * return values.
-         */
-        when(rawResponse.getStatus()).thenReturn(500, 500, 500, 500, 200, 200);
-
-        when(client.get(any(), any(), any())).thenAnswer(provideResponse(rawResponse));
-
-        // use a real executor
-        params = params.toBuilder().executor(ForkJoinPool.commonPool()).build();
-
-        oper = new SoOperation(params, config) {
-            @Override
-            public long getWaitMsGet() {
-                return 1;
-            }
-        };
-
-        CompletableFuture<OperationOutcome> future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
-
-        assertSame(outcome, future2.get(500, TimeUnit.SECONDS));
-        assertEquals(PolicyResult.SUCCESS, outcome.getResult());
-        assertEquals(2, oper.getGetCount());
-
-        /*
-         * repeat - this time, the "get" operations will be exhausted, so it should fail
-         */
-        when(rawResponse.getStatus()).thenReturn(500);
-
-        future2 = oper.postProcessResponse(outcome, PATH, rawResponse, response);
-
-        assertSame(outcome, future2.get(5, TimeUnit.SECONDS));
-        assertEquals(PolicyResult.FAILURE_TIMEOUT, outcome.getResult());
-        assertEquals(MAX_GETS + 1, oper.getGetCount());
-
-        oper.resetGetCount();
-        assertEquals(0, oper.getGetCount());
-    }
-
     @Test
     public void testGetRequestState() {
         SoResponse resp = new SoResponse();
@@ -283,6 +197,7 @@ public class SoOperationTest extends BasicSoOperation {
 
         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
         assertEquals("200 " + ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
+        assertSame(response, outcome.getResponse());
 
         // failure case
         when(rawResponse.getStatus()).thenReturn(500);
@@ -290,6 +205,7 @@ public class SoOperationTest extends BasicSoOperation {
 
         assertEquals(PolicyResult.FAILURE, outcome.getResult());
         assertEquals("500 " + ControlLoopOperation.FAILED_MSG, outcome.getMessage());
+        assertSame(response, outcome.getResponse());
     }
 
     @Test
@@ -417,4 +333,18 @@ public class SoOperationTest extends BasicSoOperation {
         when(cq.getDefaultCloudRegion()).thenReturn(region);
         assertSame(region, oper.getDefaultCloudRegion(cq));
     }
+
+    @Test
+    public void testGetCoder() throws CoderException {
+        Coder opcoder = oper.getCoder();
+
+        // ensure we can decode an SO timestamp
+        String json = "{'request':{'finishTime':'Fri, 15 May 2020 12:14:21 GMT'}}";
+        SoResponse resp = opcoder.decode(json.replace('\'', '"'), SoResponse.class);
+
+        LocalDateTime tfinish = resp.getRequest().getFinishTime();
+        assertNotNull(tfinish);
+        assertEquals(2020, tfinish.getYear());
+        assertEquals(Month.MAY, tfinish.getMonth());
+    }
 }