1 package org.onap.vid.api;
3 import com.fasterxml.jackson.core.JsonParser;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import com.fasterxml.jackson.databind.SerializationFeature;
6 import org.apache.commons.text.RandomStringGenerator;
7 import org.hamcrest.Matcher;
8 import org.springframework.core.io.Resource;
9 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
10 import org.springframework.core.io.support.ResourcePatternResolver;
11 import org.springframework.http.HttpStatus;
13 import javax.ws.rs.client.WebTarget;
14 import javax.ws.rs.core.Response;
15 import java.io.IOException;
16 import java.io.InputStream;
18 import java.util.Scanner;
20 import static org.apache.commons.text.CharacterPredicates.DIGITS;
21 import static org.apache.commons.text.CharacterPredicates.LETTERS;
22 import static org.hamcrest.CoreMatchers.is;
23 import static org.hamcrest.Matchers.*;
24 import static vid.automation.test.utils.RegExMatcher.matchesRegEx;
26 public class TestUtils {
28 protected static ObjectMapper objectMapper = new ObjectMapper();
30 public static void assertStatusOK(Object request, WebTarget webTarget, Response response) throws IOException {
31 assertHttpStatus(request, webTarget, response, HttpStatus.OK);
34 public static void assertHttpStatus(Object request, WebTarget webTarget, Response response, HttpStatus exceptedHttpStatus) throws IOException {
35 objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
37 org.testng.Assert.assertEquals(response.getStatus(), exceptedHttpStatus.value(),
38 String.format("Failed post URI: %s with request %s. Got Status:%d and body: %s",
40 objectMapper.writeValueAsString(request),
42 objectMapper.writeValueAsString(response.getEntity())));
45 public static String convertRequest(ObjectMapper objectMapper, String msoRequestDetailsFileName) {
47 ClassLoader cl = TestUtils.class.getClassLoader();
48 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
51 resources = resolver.getResources(msoRequestDetailsFileName);
52 //using InputStream and not file. see https://stackoverflow.com/questions/14876836/file-inside-jar-is-not-visible-for-spring/51131841#51131841
53 InputStream inputStream = resources[0].getInputStream();
54 String content = new Scanner(inputStream).useDelimiter("\\Z").next();
55 objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
56 return objectMapper.writeValueAsString(objectMapper.readValue(content, Object.class));
58 catch (IOException e) {
60 throw new RuntimeException(e);
64 public static String getNestedPropertyInMap(Object item, String path) {
65 return getNestedPropertyInMap(item, path, String.class, "/");
68 public static <T> T getNestedPropertyInMap(Object item, String path, Class<T> valueType) {
69 return getNestedPropertyInMap(item, path, valueType, "/");
73 Use this method to extract item from Map that represent Json hierarchy (Map<String,Map>)
75 public static <T> T getNestedPropertyInMap(Object item, String path, Class<T> valueType, String delimeter) {
76 String[] pathes = path.split(delimeter);
77 return valueType.cast(getNestedPropertyInMap(item,pathes,0));
80 private static Object getNestedPropertyInMap(Object item, String[] pathes, int index) {
81 if (index==pathes.length) {
84 return getNestedPropertyInMap(((Map<String,Object>)item).get(pathes[index]), pathes, ++index);
87 static Matcher hasOrLacksOfEntry(String pathRegex, Long expectedCounter) {
88 return expectedCounter.equals(0L) ? not(hasKey(matchesRegEx(pathRegex))) : hasEntry(matchesRegEx(pathRegex), is(expectedCounter));
91 private static RandomStringGenerator generator = new RandomStringGenerator.Builder()
92 .withinRange('0', 'z')
93 .filteredBy(LETTERS, DIGITS)
96 public static String generateRandomAlphaNumeric(int length) {
97 return generator.generate(length);