Fix intermittent test inProgressJobMoreThan24HoursIsFailedInVidAudit 10/101810/2
authorIttay Stern <ittay.stern@att.com>
Mon, 17 Feb 2020 07:48:20 +0000 (09:48 +0200)
committerIttay Stern <ittay.stern@att.com>
Mon, 17 Feb 2020 08:55:03 +0000 (08:55 +0000)
Bit more time was needed.

Issue-ID: VID-647

Change-Id: Ie7e101a2eb85e71a38d1cb2efd9f7327e4705fd3
Signed-off-by: Ittay Stern <ittay.stern@att.com>
vid-automation/src/main/java/org/onap/vid/api/AsyncInstantiationBase.java
vid-automation/src/main/java/org/onap/vid/api/TestUtils.java
vid-automation/src/main/java/vid/automation/test/infra/Wait.java
vid-automation/src/test/java/org/onap/vid/api/AsyncInstantiationMacroApiTest.java

index 4475c36..ee3982c 100644 (file)
@@ -544,7 +544,7 @@ public class AsyncInstantiationBase extends BaseMsoApiTest {
                 .toString();
     }
 
-    protected void addBulkPendingWithCustomList(List<BasePreset> customPresets){
+    protected Map<Keys, String> addBulkPendingWithCustomList(List<BasePreset> customPresets){
         Map<Keys, String> names = generateNames();
         final int bulkSize = 2 + customPresets.size();
 
@@ -559,6 +559,8 @@ public class AsyncInstantiationBase extends BaseMsoApiTest {
 
         List<String> jobIds = createBulkOfMacroInstances(presets, false, bulkSize, names);
         Assert.assertEquals(jobIds.size(),bulkSize);
+
+        return names;
     }
 
     protected void verifyAuditStatuses(String jobId, List<String> statuses, JobAuditStatus.SourceStatus source) {
index a2fe82e..cc292ca 100644 (file)
@@ -1,27 +1,29 @@
 package org.onap.vid.api;
 
+import static org.apache.commons.text.CharacterPredicates.DIGITS;
+import static org.apache.commons.text.CharacterPredicates.LETTERS;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.Matchers.hasEntry;
+import static org.hamcrest.Matchers.hasKey;
+import static org.hamcrest.Matchers.not;
+import static vid.automation.test.utils.RegExMatcher.matchesRegEx;
+
 import com.fasterxml.jackson.core.JsonParser;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Map;
+import java.util.Scanner;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.Response;
 import org.apache.commons.text.RandomStringGenerator;
 import org.hamcrest.Matcher;
 import org.springframework.core.io.Resource;
 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 import org.springframework.core.io.support.ResourcePatternResolver;
 import org.springframework.http.HttpStatus;
-
-import javax.ws.rs.client.WebTarget;
-import javax.ws.rs.core.Response;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Map;
-import java.util.Scanner;
-
-import static org.apache.commons.text.CharacterPredicates.DIGITS;
-import static org.apache.commons.text.CharacterPredicates.LETTERS;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.Matchers.*;
-import static vid.automation.test.utils.RegExMatcher.matchesRegEx;
+import org.testng.annotations.DataProvider;
 
 public class TestUtils {
 
@@ -96,4 +98,10 @@ public class TestUtils {
     public static String generateRandomAlphaNumeric(int length) {
         return generator.generate(length);
     }
+
+    @DataProvider
+    public static Object[][] trueAndFalse() {
+        return new Object[][]{{true}, {false}};
+    }
+
 }
index cab3a2d..c392c28 100644 (file)
@@ -1,20 +1,21 @@
 package vid.automation.test.infra;
 
+import java.util.concurrent.TimeUnit;
+import java.util.function.Predicate;
 import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.onap.sdc.ci.tests.utilities.GeneralUIUtils;
 import org.openqa.selenium.JavascriptExecutor;
 import org.openqa.selenium.NoSuchElementException;
 import vid.automation.test.Constants;
 
-import java.util.concurrent.TimeUnit;
-import java.util.function.Predicate;
-
 public class Wait {
     public static boolean byText(String text) {
         return GeneralUIUtils.findAndWaitByText(text, Constants.generalTimeout);
     }
 
     public static <T> boolean waitFor(Predicate<T> predicate, T input, int numOfRetries, int interval, TimeUnit intervalUnit) {
+        Throwable lastError = null;
         for (int i=0; i<numOfRetries; i++) {
             try {
                 if (predicate.test(input)) {
@@ -22,6 +23,7 @@ public class Wait {
                 }
             }
             catch (Throwable t) {
+                lastError = t;
                 System.out.println(String.format("a retry failed due to: %s %s", t, t.getMessage()));
             }
             try {
@@ -30,7 +32,12 @@ public class Wait {
                 e.printStackTrace();
             }
         }
-        return false;
+
+        if (lastError != null) {
+            throw ExceptionUtils.<RuntimeException>rethrow(lastError);
+        } else {
+            return false;
+        }
     }
 
     public static <T> boolean waitFor(Predicate<T> predicate, T input, int numOfRetries, int interval) {
index 5abc9a8..00aa691 100644 (file)
@@ -1,5 +1,6 @@
 package org.onap.vid.api;
 
+import static java.util.Collections.singletonList;
 import static java.util.stream.Collectors.counting;
 import static java.util.stream.Collectors.groupingBy;
 import static java.util.stream.Collectors.toList;
@@ -300,48 +301,42 @@ public class AsyncInstantiationMacroApiTest extends AsyncInstantiationBase {
 
     }
 
-    @Test
-    public void inProgressJobMoreThan24HoursIsFailedInVidAudit() throws JsonProcessingException {
-        addBulkPendingWithCustomList(Collections.singletonList(new PresetMSOOrchestrationRequestGet("IN_PROGRESS",24)));
+    @Test(dataProvider = "trueAndFalse", dataProviderClass = TestUtils.class)
+    public void inProgressJobMoreThan24HoursIsFailedInVidAudit(Boolean over24Hours) throws JsonProcessingException {
 
-        AtomicReference<ServiceInfo> inProgressJob = new AtomicReference<>();
-        AtomicReference<List<ServiceInfo>> serviceInfoRef = new AtomicReference<>();
-        boolean isJobFound = Wait.waitFor(x->{
-            List<ServiceInfo> serviceInfoList = serviceListCall().getBody();
-            serviceInfoRef.set(serviceInfoList);
-            inProgressJob.set(serviceInfoList.stream().
-                    filter(serviceInfo -> serviceInfo.serviceInstanceId.equals(PresetMSOOrchestrationRequestGet.DEFAULT_SERVICE_INSTANCE_ID) && serviceInfo.jobStatus.equals(JobStatus.FAILED))
-                    .findFirst()
-                    .orElse(null));
-            return inProgressJob.get() != null;
-        }, null, 15, 1);
+        // in case 24 did not pass -- IN_PROGRESS should persist; if 24 hour did pass -- fail
 
-        org.junit.Assert.assertTrue(
-            "Job with DEFAULT_SERVICE_INSTANCE_ID=" + PresetMSOOrchestrationRequestGet.DEFAULT_SERVICE_INSTANCE_ID
-                + " and status FAILED should present: " + objectMapper.writeValueAsString(serviceInfoRef.get()), isJobFound);
+        int startedHoursAgo = over24Hours ? 24 : 23;
+        JobStatus jobStatus = over24Hours ? JobStatus.FAILED : JobStatus.IN_PROGRESS;
 
-        verifyAuditStatuses(inProgressJob.get().jobId, Arrays.asList(JobStatus.PENDING.name(), JobStatus.IN_PROGRESS.name(),JobStatus.FAILED.name()), JobAuditStatus.SourceStatus.VID);
-        verifyAuditStatuses(inProgressJob.get().jobId, Arrays.asList("REQUESTED", "IN_PROGRESS"), JobAuditStatus.SourceStatus.MSO);
-    }
+        List<String> expectedStatuses = over24Hours
+            ? Arrays.asList(JobStatus.PENDING.name(), JobStatus.IN_PROGRESS.name(), JobStatus.FAILED.name())
+            : Arrays.asList(JobStatus.PENDING.name(), JobStatus.IN_PROGRESS.name());
 
-    @Test
-    public void inProgressJobLessThan24HoursIsStillInProgressInVidAudit(){
-        addBulkPendingWithCustomList(Collections.singletonList(new PresetMSOOrchestrationRequestGet("IN_PROGRESS",23)));
+        AtomicReference<List<ServiceInfo>> serviceInfoRef = new AtomicReference<>();
+
+        Map<Keys, String> names = addBulkPendingWithCustomList(
+            singletonList(new PresetMSOOrchestrationRequestGet("IN_PROGRESS", startedHoursAgo)));
 
-        AtomicReference<ServiceInfo> inProgressJob = new AtomicReference<>();
-        boolean isJobFound = Wait.waitFor(x->{
+        boolean isJobFound = Wait.waitFor(x -> {
             List<ServiceInfo> serviceInfoList = serviceListCall().getBody();
-            inProgressJob.set(serviceInfoList.stream().filter(serviceInfo -> serviceInfo.serviceInstanceId.equals(PresetMSOOrchestrationRequestGet.DEFAULT_SERVICE_INSTANCE_ID))
-                    .findFirst()
-                    .orElse(null));
-            return inProgressJob.get() != null;
-        }, null, 15, 1);
+            serviceInfoRef.set(serviceInfoList);
+            ServiceInfo inProgressJob = serviceInfoList.stream()
+                .filter(serviceInfo -> serviceInfo.serviceInstanceName.equals(names.get(Keys.SERVICE_NAME)))
+                .findFirst()
+                .orElseThrow(() -> new AssertionError("Job with serviceInstanceName=" + Keys.SERVICE_NAME + " not found"));
 
-        org.junit.Assert.assertTrue("Job with DEFAULT_SERVICE_INSTANCE_ID should present", isJobFound);
-        org.junit.Assert.assertEquals("Tested job status is not as expected", JobStatus.IN_PROGRESS, inProgressJob.get().getJobStatus());
+            org.junit.Assert.assertEquals("Tested job status is not as expected",
+                jobStatus, inProgressJob.getJobStatus());
 
-        verifyAuditStatuses(inProgressJob.get().jobId, Arrays.asList(JobStatus.PENDING.name(), JobStatus.IN_PROGRESS.name()), JobAuditStatus.SourceStatus.VID);
-        verifyAuditStatuses(inProgressJob.get().jobId, Arrays.asList("REQUESTED", "IN_PROGRESS"), JobAuditStatus.SourceStatus.MSO);
+            verifyAuditStatuses(inProgressJob.jobId, expectedStatuses, JobAuditStatus.SourceStatus.VID);
+            verifyAuditStatuses(inProgressJob.jobId, Arrays.asList("REQUESTED", "IN_PROGRESS"), JobAuditStatus.SourceStatus.MSO);
+
+            return true;
+        }, null, 10, 2);
+
+        org.junit.Assert.assertTrue(
+            "Job with serviceInstanceName=" + Keys.SERVICE_NAME + " should present: " + objectMapper.writeValueAsString(serviceInfoRef.get()), isJobFound);
     }
 
     @Test