logging tests - retry for failed calls to log checker 82/100582/3
authorEylon Malin <eylon.malin@intl.att.com>
Wed, 22 Jan 2020 08:20:03 +0000 (10:20 +0200)
committerEylon Malin <eylon.malin@intl.att.com>
Wed, 22 Jan 2020 10:29:24 +0000 (12:29 +0200)
Sometimes log checker server is not available so retry again after some sleep

Issue-ID: VID-647
Change-Id: I73ef5af9de4f5531732d46eabcb28bab285960f5
Signed-off-by: Eylon Malin <eylon.malin@intl.att.com>
vid-automation/src/test/java/org/onap/vid/more/LoggerFormatTest.java

index 9651002..962a786 100644 (file)
@@ -18,7 +18,6 @@ import static org.hamcrest.Matchers.matchesPattern;
 import static vid.automation.test.services.SimulatorApi.retrieveRecordedRequests;
 
 import com.fasterxml.jackson.databind.JsonNode;
-
 import java.lang.reflect.Method;
 import java.net.URI;
 import java.util.ArrayList;
@@ -28,12 +27,15 @@ import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.Random;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.jetbrains.annotations.NotNull;
 import org.onap.simulator.presetGenerator.presets.aai.PresetAAIGetSubscribersGet;
 import org.onap.vid.api.BaseApiTest;
+import org.springframework.web.client.RestClientException;
 import org.springframework.web.client.RestTemplate;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.DataProvider;
@@ -234,12 +236,29 @@ public class LoggerFormatTest extends BaseApiTest {
     }
 
     private JsonNode getCheckerResults (String logtype, String logLines){
+
+        final int MAX_RETRIES = 3;
+
         Map<String, String> params = new HashMap<>();
         params.put("format", "raw");
         params.put("type", logtype);
         params.put("component", "vid");
         params.put("data", logLines);
 
-        return restTemplate.postForObject(logChecker, params, JsonNode.class);
+        for (int i=0; i< MAX_RETRIES; i++) {
+            try {
+                return restTemplate.postForObject(logChecker, params, JsonNode.class);
+            } catch (RestClientException exception) { //retry for cases that logchecker is not available immediately
+                logger.error("Failed to call to logChecker try: " + i, exception);
+                if (i<(MAX_RETRIES-1)) { //no need to sleep on last retry
+                    try {
+                        Thread.sleep((new Random().nextInt(2000) + 1000)); //random sleep between 1-3 seconds
+                    } catch (InterruptedException e) {
+                        ExceptionUtils.rethrow(e);
+                    }
+                }
+            }
+        }
+        throw new AssertionError("failed to call to logChecker after max retries: "+MAX_RETRIES);
     }
 }