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