Add some timed grace for assertion on log-lines
[vid.git] / vid-automation / src / main / java / org / onap / vid / api / TestUtils.java
index a2fe82e..af3cc57 100644 (file)
@@ -1,27 +1,32 @@
 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 com.google.common.util.concurrent.Uninterruptibles;
+import java.io.IOException;
+import java.io.InputStream;
+import java.time.Instant;
+import java.util.Map;
+import java.util.Scanner;
+import java.util.concurrent.TimeUnit;
+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 +101,27 @@ public class TestUtils {
     public static String generateRandomAlphaNumeric(int length) {
         return generator.generate(length);
     }
+
+    public static void assertAndRetryIfNeeded(long timeoutInSeconds, Runnable asserter) {
+        final Instant expiry = Instant.now().plusSeconds(timeoutInSeconds);
+        while (true) {
+            try {
+                asserter.run();
+                break; // we're cool, assertion passed
+            } catch (AssertionError fail) {
+                Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
+                if (Instant.now().isAfter(expiry)) {
+                    throw fail;
+                } else {
+                    System.out.println("retrying after: " + fail);
+                }
+            }
+        }
+    }
+
+    @DataProvider
+    public static Object[][] trueAndFalse() {
+        return new Object[][]{{true}, {false}};
+    }
+
 }