1 package org.onap.vid.api;
3 import static org.apache.commons.text.CharacterPredicates.DIGITS;
4 import static org.apache.commons.text.CharacterPredicates.LETTERS;
5 import static org.hamcrest.CoreMatchers.is;
6 import static org.hamcrest.Matchers.hasEntry;
7 import static org.hamcrest.Matchers.hasKey;
8 import static org.hamcrest.Matchers.not;
9 import static vid.automation.test.utils.RegExMatcher.matchesRegEx;
11 import com.fasterxml.jackson.core.JsonParser;
12 import com.fasterxml.jackson.databind.ObjectMapper;
13 import com.fasterxml.jackson.databind.SerializationFeature;
14 import java.io.IOException;
15 import java.io.InputStream;
17 import java.util.Scanner;
18 import javax.ws.rs.client.WebTarget;
19 import javax.ws.rs.core.Response;
20 import org.apache.commons.text.RandomStringGenerator;
21 import org.hamcrest.Matcher;
22 import org.springframework.core.io.Resource;
23 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
24 import org.springframework.core.io.support.ResourcePatternResolver;
25 import org.springframework.http.HttpStatus;
26 import org.testng.annotations.DataProvider;
28 public class TestUtils {
30 protected static ObjectMapper objectMapper = new ObjectMapper();
32 public static void assertStatusOK(Object request, WebTarget webTarget, Response response) throws IOException {
33 assertHttpStatus(request, webTarget, response, HttpStatus.OK);
36 public static void assertHttpStatus(Object request, WebTarget webTarget, Response response, HttpStatus exceptedHttpStatus) throws IOException {
37 objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
39 org.testng.Assert.assertEquals(response.getStatus(), exceptedHttpStatus.value(),
40 String.format("Failed post URI: %s with request %s. Got Status:%d and body: %s",
42 objectMapper.writeValueAsString(request),
44 objectMapper.writeValueAsString(response.getEntity())));
47 public static String convertRequest(ObjectMapper objectMapper, String msoRequestDetailsFileName) {
49 ClassLoader cl = TestUtils.class.getClassLoader();
50 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
53 resources = resolver.getResources(msoRequestDetailsFileName);
54 //using InputStream and not file. see https://stackoverflow.com/questions/14876836/file-inside-jar-is-not-visible-for-spring/51131841#51131841
55 InputStream inputStream = resources[0].getInputStream();
56 String content = new Scanner(inputStream).useDelimiter("\\Z").next();
57 objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
58 return objectMapper.writeValueAsString(objectMapper.readValue(content, Object.class));
60 catch (IOException e) {
62 throw new RuntimeException(e);
66 public static String getNestedPropertyInMap(Object item, String path) {
67 return getNestedPropertyInMap(item, path, String.class, "/");
70 public static <T> T getNestedPropertyInMap(Object item, String path, Class<T> valueType) {
71 return getNestedPropertyInMap(item, path, valueType, "/");
75 Use this method to extract item from Map that represent Json hierarchy (Map<String,Map>)
77 public static <T> T getNestedPropertyInMap(Object item, String path, Class<T> valueType, String delimeter) {
78 String[] pathes = path.split(delimeter);
79 return valueType.cast(getNestedPropertyInMap(item,pathes,0));
82 private static Object getNestedPropertyInMap(Object item, String[] pathes, int index) {
83 if (index==pathes.length) {
86 return getNestedPropertyInMap(((Map<String,Object>)item).get(pathes[index]), pathes, ++index);
89 static Matcher hasOrLacksOfEntry(String pathRegex, Long expectedCounter) {
90 return expectedCounter.equals(0L) ? not(hasKey(matchesRegEx(pathRegex))) : hasEntry(matchesRegEx(pathRegex), is(expectedCounter));
93 private static RandomStringGenerator generator = new RandomStringGenerator.Builder()
94 .withinRange('0', 'z')
95 .filteredBy(LETTERS, DIGITS)
98 public static String generateRandomAlphaNumeric(int length) {
99 return generator.generate(length);
103 public static Object[][] trueAndFalse() {
104 return new Object[][]{{true}, {false}};