1 package org.onap.vid.api;
3 import static java.util.Collections.singletonList;
4 import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
5 import static net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER;
6 import static org.apache.commons.text.StringEscapeUtils.unescapeJson;
7 import static org.hamcrest.MatcherAssert.assertThat;
8 import static org.hamcrest.Matchers.isEmptyOrNullString;
9 import static org.hamcrest.Matchers.not;
11 import com.fasterxml.jackson.core.JsonProcessingException;
12 import com.fasterxml.jackson.databind.ObjectMapper;
13 import com.fasterxml.jackson.databind.SerializationFeature;
14 import java.io.IOException;
15 import java.io.InputStream;
18 import java.util.List;
19 import java.util.Properties;
20 import java.util.Random;
21 import java.util.TimeZone;
22 import javax.ws.rs.client.Client;
23 import javax.ws.rs.client.ClientBuilder;
24 import org.apache.commons.io.IOUtils;
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
27 import org.glassfish.jersey.client.ClientProperties;
28 import org.glassfish.jersey.uri.internal.JerseyUriBuilder;
29 import org.onap.sdc.ci.tests.datatypes.UserCredentials;
30 import org.springframework.http.client.ClientHttpRequestInterceptor;
31 import org.springframework.http.client.ClientHttpResponse;
32 import org.springframework.web.client.DefaultResponseErrorHandler;
33 import org.springframework.web.client.HttpStatusCodeException;
34 import org.springframework.web.client.RestTemplate;
35 import org.testng.annotations.BeforeClass;
36 import vid.automation.test.infra.FeaturesTogglingConfiguration;
37 import vid.automation.test.services.UsersService;
38 import vid.automation.test.utils.CookieAndJsonHttpHeadersInterceptor;
40 public class BaseApiTest {
41 protected static final Logger LOGGER = LogManager.getLogger(BaseApiTest.class);
43 @SuppressWarnings("WeakerAccess")
45 @SuppressWarnings("WeakerAccess")
46 protected ObjectMapper objectMapper = new ObjectMapper();
47 @SuppressWarnings("WeakerAccess")
48 protected Client client;
49 protected Random random;
50 protected final RestTemplate restTemplate = new RestTemplate();
52 protected final UsersService usersService = new UsersService();
53 protected final RestTemplate restTemplateErrorAgnostic = new RestTemplate();
58 objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
59 client = ClientBuilder.newClient();
60 client.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
61 random = new Random(System.currentTimeMillis());
62 FeaturesTogglingConfiguration.initializeFeatureManager();
65 private URI getUri() {
66 String host = System.getProperty("VID_HOST", "10.0.0.10");
67 int port = Integer.valueOf(System.getProperty("VID_PORT", "8080"));
68 return new JerseyUriBuilder().host(host).port(port).scheme("http").path("vid").build();
72 login(getUserCredentials());
75 public void login(UserCredentials userCredentials) {
76 final List<ClientHttpRequestInterceptor> interceptors = singletonList(new CookieAndJsonHttpHeadersInterceptor(getUri(), userCredentials));
77 restTemplate.setInterceptors(interceptors);
78 restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
80 public void handleError(ClientHttpResponse response) throws IOException {
82 super.handleError(response);
83 } catch (HttpStatusCodeException e) {
84 LOGGER.error("HTTP {}: {}", e.getStatusCode(), e.getResponseBodyAsString(), e);
90 restTemplateErrorAgnostic.setInterceptors(interceptors);
91 restTemplateErrorAgnostic.setErrorHandler(new DefaultResponseErrorHandler() {
93 public boolean hasError(ClientHttpResponse response) {
100 //set time zone to UTC so clock will go closely with VID app
102 public void setDefaultTimeZoneToUTC() {
103 System.setProperty("user.timezone", "UTC");
104 TimeZone.setDefault(TimeZone.getTimeZone("UTC")); //since TimeZone cache previous user.timezone
107 public UserCredentials getUserCredentials() {
108 final Properties configProp = new Properties();
110 InputStream input = ClassLoader.getSystemResourceAsStream("test_config.properties");
111 configProp.load(input);
112 } catch (IOException e) {
113 throw new RuntimeException(e);
116 String loginId = configProp.getProperty("test.loginId", "i'm illegal");
117 String loginPassword = configProp.getProperty("test.loginPassword", "i'm illegal");
118 return new UserCredentials(loginId, loginPassword, null, null, null);
124 protected String getCleanJsonString(String jsonString) {
125 // remove leading/trailing double-quotes and unescape
126 String res = unescapeJson(jsonString.replaceAll("^\"|\"$", ""));
127 LOGGER.debug("getCleanJsonString: " + jsonString + " ==> " + res);
131 protected String getCleanJsonString(Object object) throws JsonProcessingException {
132 if (object instanceof String) {
133 return getCleanJsonString((String) object);
135 return new com.fasterxml.jackson.databind.ObjectMapper().writeValueAsString(object);
139 protected String buildUri(String path) {
140 return uri + "/" + path;
143 public static String getResourceAsString(String resourcePath) {
144 // load expected result
145 final URL resource = BaseApiTest.class.getClassLoader().getResource(resourcePath);
146 if (resource == null) throw new RuntimeException("resource file not found: " + resourcePath);
148 return IOUtils.toString(resource, "UTF-8");
149 } catch (IOException e) {
150 throw new RuntimeException(e);
154 protected void assertJsonEquals(String actual, String expected) {
156 assertThat(actual, not(isEmptyOrNullString()));
158 assertThat(actual, jsonEquals(expected)
159 .when(IGNORING_ARRAY_ORDER)