Merge automation from ECOMP's repository
[vid.git] / vid-automation / src / main / java / org / onap / vid / api / TestUtils.java
@@ -1,8 +1,10 @@
 package org.onap.vid.api;
 
-import org.codehaus.jackson.JsonParser;
-import org.codehaus.jackson.map.ObjectMapper;
-import org.codehaus.jackson.map.SerializationConfig;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+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;
@@ -10,14 +12,17 @@ import org.springframework.http.HttpStatus;
 
 import javax.ws.rs.client.WebTarget;
 import javax.ws.rs.core.Response;
-import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.Map;
 import java.util.Scanner;
 
-/**
- * Created by Oren on 6/7/17.
- */
+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;
+
 public class TestUtils {
 
     protected static ObjectMapper objectMapper = new ObjectMapper();
@@ -27,7 +32,7 @@ public class TestUtils {
     }
 
     public static void assertHttpStatus(Object request, WebTarget webTarget, Response response, HttpStatus exceptedHttpStatus) throws IOException {
-        objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
+        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
 
         org.testng.Assert.assertEquals(response.getStatus(), exceptedHttpStatus.value(),
                 String.format("Failed post URI: %s with request %s. Got Status:%d and body: %s",
@@ -39,14 +44,14 @@ public class TestUtils {
 
     public static String convertRequest(ObjectMapper objectMapper, String msoRequestDetailsFileName) {
 
-        ClassLoader cl = pProbeMsoApiTest.class.getClassLoader();
+        ClassLoader cl = TestUtils.class.getClassLoader();
         ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
         Resource[] resources;
         try {
             resources = resolver.getResources(msoRequestDetailsFileName);
-            String content;
-            File file = resources[0].getFile();
-            content = new Scanner(file).useDelimiter("\\Z").next();
+            //using InputStream and not file. see https://stackoverflow.com/questions/14876836/file-inside-jar-is-not-visible-for-spring/51131841#51131841
+            InputStream inputStream = resources[0].getInputStream();
+            String content = new Scanner(inputStream).useDelimiter("\\Z").next();
             objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
             return objectMapper.writeValueAsString(objectMapper.readValue(content, Object.class));
         }
@@ -78,4 +83,17 @@ public class TestUtils {
         }
         return getNestedPropertyInMap(((Map<String,Object>)item).get(pathes[index]), pathes, ++index);
     }
+
+    static Matcher hasOrLacksOfEntry(String pathRegex, Long expectedCounter) {
+        return expectedCounter.equals(0L) ? not(hasKey(matchesRegEx(pathRegex))) : hasEntry(matchesRegEx(pathRegex), is(expectedCounter));
+    }
+
+    private static RandomStringGenerator generator = new RandomStringGenerator.Builder()
+            .withinRange('0', 'z')
+            .filteredBy(LETTERS, DIGITS)
+            .build();
+
+    public static String generateRandomAlphaNumeric(int length) {
+        return generator.generate(length);
+    }
 }