diff options
Diffstat (limited to 'vid-automation/src/main/java/vid/automation/test/utils')
4 files changed, 140 insertions, 11 deletions
diff --git a/vid-automation/src/main/java/vid/automation/test/utils/CookieAndJsonHttpHeadersInterceptor.java b/vid-automation/src/main/java/vid/automation/test/utils/CookieAndJsonHttpHeadersInterceptor.java new file mode 100644 index 000000000..07fd3754e --- /dev/null +++ b/vid-automation/src/main/java/vid/automation/test/utils/CookieAndJsonHttpHeadersInterceptor.java @@ -0,0 +1,50 @@ +package vid.automation.test.utils; + +import org.junit.Assert; +import org.openecomp.sdc.ci.tests.datatypes.UserCredentials; +import org.springframework.http.*; +import org.springframework.http.client.ClientHttpRequestExecution; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.http.client.support.HttpRequestWrapper; +import org.springframework.web.client.RestTemplate; + +import java.io.IOException; +import java.net.URI; +import java.util.Collections; +import java.util.List; + +public class CookieAndJsonHttpHeadersInterceptor implements ClientHttpRequestInterceptor { + private final HttpHeaders cookieAndJsonHttpHeaders; + + public CookieAndJsonHttpHeadersInterceptor(URI uri, UserCredentials userCredentials) { + this.cookieAndJsonHttpHeaders = getCookieAndJsonHttpHeaders(uri, userCredentials); + } + + @Override + public ClientHttpResponse intercept(HttpRequest request, byte[] body, + ClientHttpRequestExecution execution) throws IOException { + HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request); + requestWrapper.getHeaders().putAll(cookieAndJsonHttpHeaders); + + return execution.execute(requestWrapper, body); + } + + protected HttpHeaders getCookieAndJsonHttpHeaders(URI uri, UserCredentials userCredentials) { + HttpHeaders loginRequestHeaders = new HttpHeaders(); + loginRequestHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity<String> loginRes = restTemplate.postForEntity(uri.toASCIIString() + "/login_external.htm", new HttpEntity<>("loginId=" + userCredentials.getUserId() + "&password=" + userCredentials.getPassword(), loginRequestHeaders), String.class); + Assert.assertEquals("Login failed - wrong http status with user:" + userCredentials.getUserId() + " password:" + userCredentials.getPassword(), HttpStatus.FOUND, loginRes.getStatusCode()); + Assert.assertNull("Failed to login with user:" + userCredentials.getUserId() + " password:" + userCredentials.getPassword(), loginRes.getBody()); + HttpHeaders loginResponseHeaders = loginRes.getHeaders(); + List<String> cookie = loginResponseHeaders.get(HttpHeaders.SET_COOKIE); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); + headers.put(HttpHeaders.COOKIE, cookie); + return headers; + } + +} diff --git a/vid-automation/src/main/java/vid/automation/test/utils/DB_CONFIG.java b/vid-automation/src/main/java/vid/automation/test/utils/DB_CONFIG.java new file mode 100644 index 000000000..18cda6bda --- /dev/null +++ b/vid-automation/src/main/java/vid/automation/test/utils/DB_CONFIG.java @@ -0,0 +1,10 @@ +package vid.automation.test.utils; + +public class DB_CONFIG { + public static String url = String.format("jdbc:mariadb://%s:%d/vid_portal", + System.getProperty("DB_HOST", System.getProperty("VID_HOST", "127.0.0.1" )), + Integer.valueOf(System.getProperty("DB_PORT", "3306")) + ); + public static String username = "euser"; + public static String password = "euser"; +} diff --git a/vid-automation/src/main/java/vid/automation/test/utils/ReadFile.java b/vid-automation/src/main/java/vid/automation/test/utils/ReadFile.java index 7652e09b7..655cc20a6 100644 --- a/vid-automation/src/main/java/vid/automation/test/utils/ReadFile.java +++ b/vid-automation/src/main/java/vid/automation/test/utils/ReadFile.java @@ -1,33 +1,70 @@ package vid.automation.test.utils; import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.io.IOUtils; import org.openecomp.sdc.ci.tests.utilities.FileHandling; -import vid.automation.test.model.User; -import vid.automation.test.model.UsersObject; +import vid.automation.test.infra.Input; -import java.io.File; import java.io.IOException; -import java.util.HashMap; +import java.io.InputStream; +import java.net.URL; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; + +import static java.nio.file.Files.copy; +import static java.nio.file.Files.createTempDirectory; public class ReadFile { - public static <T> T getJsonFile(String fileName, Class<T> clazz) throws IOException { + + private static Path tempDirectory; + + public static <T> T getJsonFile(String fileName, Class<T> clazz) { ObjectMapper mapper = new ObjectMapper(); T list; try { - File testCaseFile = FileHandling.getConfigFile(fileName); + java.io.File testCaseFile = FileHandling.getConfigFile(fileName); if(!testCaseFile.exists()) { String basePath = System.getProperty("BASE_PATH"); - testCaseFile = new File( basePath + File.separator + "conf" + File.separator + fileName); + testCaseFile = new java.io.File( basePath + java.io.File.separator + "conf" + java.io.File.separator + fileName); } list = (T) mapper.readValue(testCaseFile, clazz); return list; - } catch (IOException e) { - e.printStackTrace(); - return null; } catch (Exception e) { e.printStackTrace(); - return null; + throw new RuntimeException("cannot read file '" + fileName + "' file as '" + clazz.getCanonicalName() + "'", e); } } + public static String copyOfFileFromResources(String pathInResources) { + + // takes a file from resources, copies it to a temp folder, then + // returns the newly created path file + + URL resource = Input.class.getClassLoader().getResource(pathInResources); + if (resource == null) { + throw new RuntimeException("file not found in resources: " + pathInResources); + } + + Path target; + try { + tempDirectory = (tempDirectory != null) ? tempDirectory : createTempDirectory("resources-"); + target = tempDirectory.resolve(FilenameUtils.getName(resource.getPath())); + copy(resource.openStream(), target, StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + throw new RuntimeException(e); + } + + return target.toString(); + } + + public static String loadResourceAsString(String relativePath){ + final InputStream resource = ReadFile.class.getClassLoader().getResourceAsStream(relativePath); + if (resource == null) throw new RuntimeException("template file not found: " + relativePath); + try { + return IOUtils.toString(resource, "UTF-8"); + } catch (IOException e) { + throw new RuntimeException(e); + } + } } diff --git a/vid-automation/src/main/java/vid/automation/test/utils/TestConfigurationHelper.java b/vid-automation/src/main/java/vid/automation/test/utils/TestConfigurationHelper.java new file mode 100644 index 000000000..ae731dbc0 --- /dev/null +++ b/vid-automation/src/main/java/vid/automation/test/utils/TestConfigurationHelper.java @@ -0,0 +1,32 @@ +package vid.automation.test.utils; + +import vid.automation.test.infra.FeaturesTogglingConfiguration; + +public class TestConfigurationHelper { + + private TestConfigurationHelper(){} + + public static org.openecomp.sdc.ci.tests.datatypes.Configuration getEnvConfiguration() { + + try { + String envUrl = System.getProperty("ENV_URL"); + boolean isCustomLogin = Boolean.parseBoolean(System.getProperty("CUSTOM_LOGIN")); + + org.openecomp.sdc.ci.tests.datatypes.Configuration configuration = new org.openecomp.sdc.ci.tests.datatypes.Configuration(envUrl, isCustomLogin); + + String geckoDriverPath = System.getProperty("GECKO_PATH"); + if(geckoDriverPath == null){ + throw new RuntimeException("Missing path to gecko driver." + + " Make sure to provide path to the gecko driver executable with -DGECKO_PATH=<path>"); + } + + configuration.setGeckoDriverPath(geckoDriverPath); + FeaturesTogglingConfiguration.initializeFeatureManager(); + return configuration; + + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } +} |