Fix intermittent test inProgressJobMoreThan24HoursIsFailedInVidAudit
[vid.git] / vid-automation / src / main / java / org / onap / vid / api / TestUtils.java
1 package org.onap.vid.api;
2
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;
10
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;
16 import java.util.Map;
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;
27
28 public class TestUtils {
29
30     protected static ObjectMapper objectMapper = new ObjectMapper();
31
32     public static void assertStatusOK(Object request, WebTarget webTarget, Response response) throws IOException {
33         assertHttpStatus(request, webTarget, response, HttpStatus.OK);
34     }
35
36     public static void assertHttpStatus(Object request, WebTarget webTarget, Response response, HttpStatus exceptedHttpStatus) throws IOException {
37         objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
38
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",
41                         webTarget.getUri(),
42                         objectMapper.writeValueAsString(request),
43                         response.getStatus(),
44                         objectMapper.writeValueAsString(response.getEntity())));
45     }
46
47     public static String convertRequest(ObjectMapper objectMapper, String msoRequestDetailsFileName) {
48
49         ClassLoader cl = TestUtils.class.getClassLoader();
50         ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
51         Resource[] resources;
52         try {
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));
59         }
60         catch (IOException e) {
61             e.printStackTrace();
62             throw new RuntimeException(e);
63         }
64     }
65
66     public static String getNestedPropertyInMap(Object item, String path) {
67         return getNestedPropertyInMap(item, path, String.class, "/");
68     }
69
70     public static <T> T getNestedPropertyInMap(Object item, String path, Class<T> valueType) {
71         return getNestedPropertyInMap(item, path, valueType, "/");
72     }
73
74     /*
75     Use this method to extract item from Map that represent Json hierarchy (Map<String,Map>)
76      */
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));
80     }
81
82     private static Object getNestedPropertyInMap(Object item, String[] pathes, int index) {
83         if (index==pathes.length) {
84             return item;
85         }
86         return getNestedPropertyInMap(((Map<String,Object>)item).get(pathes[index]), pathes, ++index);
87     }
88
89     static Matcher hasOrLacksOfEntry(String pathRegex, Long expectedCounter) {
90         return expectedCounter.equals(0L) ? not(hasKey(matchesRegEx(pathRegex))) : hasEntry(matchesRegEx(pathRegex), is(expectedCounter));
91     }
92
93     private static RandomStringGenerator generator = new RandomStringGenerator.Builder()
94             .withinRange('0', 'z')
95             .filteredBy(LETTERS, DIGITS)
96             .build();
97
98     public static String generateRandomAlphaNumeric(int length) {
99         return generator.generate(length);
100     }
101
102     @DataProvider
103     public static Object[][] trueAndFalse() {
104         return new Object[][]{{true}, {false}};
105     }
106
107 }