From 638176a6d498bcd19e6d032b4a4c8193f88dd98a Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Fri, 4 Oct 2019 15:31:24 +0200 Subject: [PATCH] Move REST client and Webhook from openvidu-test-e2e to openvidu-test-browsers repo --- openvidu-test-browsers/pom.xml | 20 ++++ .../browsers}/utils/CustomHttpClient.java | 96 ++++++++++--------- .../test/browsers}/utils/CustomWebhook.java | 2 +- .../test/e2e/OpenViduTestAppE2eTest.java | 17 ++-- 4 files changed, 85 insertions(+), 50 deletions(-) rename {openvidu-test-e2e/src/test/java/io/openvidu/test/e2e => openvidu-test-browsers/src/main/java/io/openvidu/test/browsers}/utils/CustomHttpClient.java (67%) rename {openvidu-test-e2e/src/test/java/io/openvidu/test/e2e => openvidu-test-browsers/src/main/java/io/openvidu/test/browsers}/utils/CustomWebhook.java (98%) diff --git a/openvidu-test-browsers/pom.xml b/openvidu-test-browsers/pom.xml index efb97bc0..5da33c85 100644 --- a/openvidu-test-browsers/pom.xml +++ b/openvidu-test-browsers/pom.xml @@ -57,6 +57,16 @@ + + org.springframework.boot + spring-boot-starter + ${version.spring-boot} + + + org.springframework.boot + spring-boot-starter-web + ${version.spring-boot} + org.slf4j slf4j-api @@ -77,6 +87,16 @@ selenium-firefox-driver ${version.selenium} + + com.google.code.gson + gson + 2.8.5 + + + com.mashape.unirest + unirest-java + 1.4.9 + diff --git a/openvidu-test-e2e/src/test/java/io/openvidu/test/e2e/utils/CustomHttpClient.java b/openvidu-test-browsers/src/main/java/io/openvidu/test/browsers/utils/CustomHttpClient.java similarity index 67% rename from openvidu-test-e2e/src/test/java/io/openvidu/test/e2e/utils/CustomHttpClient.java rename to openvidu-test-browsers/src/main/java/io/openvidu/test/browsers/utils/CustomHttpClient.java index e36fc916..e32c8995 100644 --- a/openvidu-test-e2e/src/test/java/io/openvidu/test/e2e/utils/CustomHttpClient.java +++ b/openvidu-test-browsers/src/main/java/io/openvidu/test/browsers/utils/CustomHttpClient.java @@ -15,7 +15,7 @@ * */ -package io.openvidu.test.e2e.utils; +package io.openvidu.test.browsers.utils; import java.security.KeyManagementException; import java.security.KeyStoreException; @@ -24,11 +24,9 @@ import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.Base64; import java.util.Map; -import java.util.Map.Entry; import javax.net.ssl.SSLContext; -import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.TrustSelfSignedStrategy; @@ -37,7 +35,6 @@ import org.apache.http.ssl.SSLContextBuilder; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import org.junit.Assert; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,9 +53,9 @@ public class CustomHttpClient { private String openviduUrl; private String headerAuth; - public CustomHttpClient(String openviduUrl, String openviduSecret) { - this.openviduUrl = openviduUrl.replaceFirst("/*$", ""); - this.headerAuth = "Basic " + Base64.getEncoder().encodeToString(("OPENVIDUAPP:" + openviduSecret).getBytes()); + public CustomHttpClient(String url, String user, String pass) throws Exception { + this.openviduUrl = url.replaceFirst("/*$", ""); + this.headerAuth = "Basic " + Base64.getEncoder().encodeToString((user + ":" + pass).getBytes()); SSLContext sslContext = null; try { @@ -68,46 +65,43 @@ public class CustomHttpClient { } }).build(); } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) { - Assert.fail("Error building custom HttpClient: " + e.getMessage()); + throw new Exception("Error building custom HttpClient: " + e.getMessage()); } HttpClient unsafeHttpClient = HttpClients.custom().setSSLContext(sslContext) .setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); Unirest.setHttpClient(unsafeHttpClient); } - - public void testAuthorizationError() { - try { - String wrongCredentials = "Basic " - + Base64.getEncoder().encodeToString(("OPENVIDUAPP:WRONG_SECRET").getBytes()); - Assert.assertEquals("Expected 401 status (unauthorized)", HttpStatus.SC_UNAUTHORIZED, Unirest - .get(openviduUrl + "/config").header("Authorization", wrongCredentials).asJson().getStatus()); - } catch (UnirestException e) { - Assert.fail("Error testing UNAUTHORIZED rest method: " + e.getMessage()); - } + + public int getAndReturnStatus(String path, String credentials) throws Exception { + path = openviduUrl + (path.startsWith("/") ? path : ("/" + path)); + return Unirest.get(path).header("Authorization", credentials).asJson().getStatus(); } - public JSONObject rest(HttpMethod method, String path, int status) { + + public JSONObject rest(HttpMethod method, String path, int status) throws Exception { return this.commonRest(method, path, null, status); } - public JSONObject rest(HttpMethod method, String path, String body, int status) { + public JSONObject rest(HttpMethod method, String path, String body, int status) throws Exception { return this.commonRest(method, path, body, status); } public JSONObject rest(HttpMethod method, String path, String body, int status, boolean exactReturnedFields, - String jsonReturnedValue) { + String jsonReturnedValue) throws Exception { JSONObject json = this.commonRest(method, path, body, status); JSONObject jsonObjExpected = null; jsonReturnedValue.replaceAll("'", "\""); try { jsonObjExpected = new JSONObject(jsonReturnedValue); } catch (JSONException e1) { - Assert.fail("Expected json element is a string without a JSON format: " + jsonReturnedValue); + throw new Exception("Expected json element is a string without a JSON format: " + jsonReturnedValue); } if (exactReturnedFields) { - Assert.assertEquals("Error in number of keys in JSON response to POST (" + json.toString() + ")" + path, - jsonObjExpected.length(), json.length()); + if (jsonObjExpected.length() != json.length()) { + throw new Exception( + "Error in number of keys in JSON response to POST (" + json.toString() + ")" + path); + } } for (String key : jsonObjExpected.keySet()) { Class c1 = jsonObjExpected.get(key).getClass(); @@ -116,21 +110,23 @@ public class CustomHttpClient { c1 = unifyNumberType(c1); c2 = unifyNumberType(c2); - Assert.assertTrue("Wrong class of property " + key, c1.equals(c2)); + if (!c1.equals(c2)) { + throw new Exception("Wrong class of property " + key); + } } return json; } public JSONObject rest(HttpMethod method, String path, String body, int status, boolean exactReturnedFields, - Map jsonResponse) { + Map jsonResponse) throws Exception { org.json.JSONObject json = this.commonRest(method, path, body, status); if (exactReturnedFields) { - Assert.assertEquals("Error in number of keys in JSON response to POST " + path, jsonResponse.size(), - json.length()); + if (jsonResponse.size() != json.length()) + throw new Exception("Error in number of keys in JSON response to POST " + path); } - for (Entry entry : jsonResponse.entrySet()) { + for (Map.Entry entry : jsonResponse.entrySet()) { Object value = entry.getValue(); if (value instanceof String) { @@ -146,34 +142,44 @@ public class CustomHttpClient { // COMPARE } catch (JSONException e2) { - Assert.assertEquals("JSON field " + entry.getKey() + " has not expected value", (String) value, - json.getInt(entry.getKey())); + if (((String) value) != json.getString(entry.getKey())) { + throw new Exception("JSON field " + entry.getKey() + " has not expected value. Expected: " + + value + ". Actual: " + json.getString(entry.getKey())); + } } } } else if (value instanceof Integer) { - Assert.assertEquals("JSON field " + entry.getKey() + " has not expected value", (int) value, - json.getInt(entry.getKey())); + if (((int) value) != json.getInt(entry.getKey())) { + throw new Exception("JSON field " + entry.getKey() + " has not expected value. Expected: " + value + + ". Actual: " + json.getInt(entry.getKey())); + } } else if (value instanceof Long) { - Assert.assertEquals("JSON field " + entry.getKey() + " has not expected value", (long) value, - json.getLong(entry.getKey())); + if (((long) value) != json.getLong(entry.getKey())) { + throw new Exception("JSON field " + entry.getKey() + " has not expected value. Expected: " + value + + ". Actual: " + json.getLong(entry.getKey())); + } } else if (value instanceof Double) { - Assert.assertEquals("JSON field " + entry.getKey() + " has not expected value", (double) value, - json.getDouble(entry.getKey()), 0.001); + if (((double) value) != json.getDouble(entry.getKey())) { + throw new Exception("JSON field " + entry.getKey() + " has not expected value. Expected: " + value + + ". Actual: " + json.getDouble(entry.getKey())); + } } else if (value instanceof Boolean) { - Assert.assertEquals("JSON field " + entry.getKey() + " has not expected value", (boolean) value, - json.getBoolean(entry.getKey())); + if (((boolean) value) != json.getBoolean(entry.getKey())) { + throw new Exception("JSON field " + entry.getKey() + " has not expected value. Expected: " + value + + ". Actual: " + json.getBoolean(entry.getKey())); + } } else if (value instanceof JSONArray) { json.getJSONArray(entry.getKey()); } else if (value instanceof JSONObject) { json.getJSONObject(entry.getKey()); } else { - Assert.fail("JSON response field cannot be parsed: " + entry.toString()); + throw new Exception("JSON response field cannot be parsed: " + entry.toString()); } } return json; } - private org.json.JSONObject commonRest(HttpMethod method, String path, String body, int status) { + private org.json.JSONObject commonRest(HttpMethod method, String path, String body, int status) throws Exception { HttpResponse jsonResponse = null; org.json.JSONObject json = null; path = openviduUrl + (path.startsWith("/") ? path : ("/" + path)); @@ -214,12 +220,16 @@ public class CustomHttpClient { } } catch (UnirestException e) { log.error(e.getMessage()); - Assert.fail("Error sending request to " + path + ": " + e.getMessage()); + throw new Exception("Error sending request to " + path + ": " + e.getMessage()); } if (jsonResponse.getStatus() == 500) { log.error("Internal Server Error: {}", jsonResponse.getBody().toString()); } - Assert.assertEquals(path + " expected to return status " + status, status, jsonResponse.getStatus()); + + if (status != jsonResponse.getStatus()) { + throw new Exception(path + " expected to return status " + status + " but got " + jsonResponse.getStatus()); + } + return json; } diff --git a/openvidu-test-e2e/src/test/java/io/openvidu/test/e2e/utils/CustomWebhook.java b/openvidu-test-browsers/src/main/java/io/openvidu/test/browsers/utils/CustomWebhook.java similarity index 98% rename from openvidu-test-e2e/src/test/java/io/openvidu/test/e2e/utils/CustomWebhook.java rename to openvidu-test-browsers/src/main/java/io/openvidu/test/browsers/utils/CustomWebhook.java index 6fc9999a..420fa85d 100644 --- a/openvidu-test-e2e/src/test/java/io/openvidu/test/e2e/utils/CustomWebhook.java +++ b/openvidu-test-browsers/src/main/java/io/openvidu/test/browsers/utils/CustomWebhook.java @@ -15,7 +15,7 @@ * */ -package io.openvidu.test.e2e.utils; +package io.openvidu.test.browsers.utils; import java.util.Collections; import java.util.Map; diff --git a/openvidu-test-e2e/src/test/java/io/openvidu/test/e2e/OpenViduTestAppE2eTest.java b/openvidu-test-e2e/src/test/java/io/openvidu/test/e2e/OpenViduTestAppE2eTest.java index eff3fe59..c9818a31 100644 --- a/openvidu-test-e2e/src/test/java/io/openvidu/test/e2e/OpenViduTestAppE2eTest.java +++ b/openvidu-test-e2e/src/test/java/io/openvidu/test/e2e/OpenViduTestAppE2eTest.java @@ -31,6 +31,7 @@ import java.nio.file.Paths; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; +import java.util.Base64; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; @@ -101,9 +102,9 @@ import io.openvidu.test.browsers.ChromeAndroidUser; import io.openvidu.test.browsers.ChromeUser; import io.openvidu.test.browsers.FirefoxUser; import io.openvidu.test.browsers.OperaUser; +import io.openvidu.test.browsers.utils.CustomHttpClient; +import io.openvidu.test.browsers.utils.CustomWebhook; import io.openvidu.test.e2e.utils.CommandLineExecutor; -import io.openvidu.test.e2e.utils.CustomHttpClient; -import io.openvidu.test.e2e.utils.CustomWebhook; import io.openvidu.test.e2e.utils.MultimediaFileMetadata; import io.openvidu.test.e2e.utils.Unzipper; @@ -2355,10 +2356,13 @@ public class OpenViduTestAppE2eTest { log.info("REST API test"); - CustomHttpClient restClient = new CustomHttpClient(OPENVIDU_URL, OPENVIDU_SECRET); + CustomHttpClient restClient = new CustomHttpClient(OPENVIDU_URL, "OPENVIDUAPP", OPENVIDU_SECRET); // 401 - restClient.testAuthorizationError(); + String wrongCredentials = "Basic " + + Base64.getEncoder().encodeToString(("OPENVIDUAPP:WRONG_SECRET").getBytes()); + Assert.assertEquals("Expected unauthorized status", HttpStatus.SC_UNAUTHORIZED, + restClient.getAndReturnStatus("/config", wrongCredentials)); /** GET /api/sessions (before session created) **/ restClient.rest(HttpMethod.GET, "/api/sessions/NOT_EXISTS", HttpStatus.SC_NOT_FOUND); @@ -2609,7 +2613,8 @@ public class OpenViduTestAppE2eTest { restClient.rest(HttpMethod.DELETE, "/api/sessions/CUSTOM_SESSION_ID", HttpStatus.SC_NO_CONTENT); // GET /api/sessions should return empty again - restClient.rest(HttpMethod.GET, "/api/sessions", null, HttpStatus.SC_OK, true, ImmutableMap.of("numberOfElements", new Integer(0), "content", new org.json.JSONArray())); + restClient.rest(HttpMethod.GET, "/api/sessions", null, HttpStatus.SC_OK, true, + ImmutableMap.of("numberOfElements", new Integer(0), "content", new org.json.JSONArray())); /** GET /config **/ restClient.rest(HttpMethod.GET, "/config", null, HttpStatus.SC_OK, true, @@ -2771,7 +2776,7 @@ public class OpenViduTestAppE2eTest { log.info("Webhook test"); CountDownLatch initLatch = new CountDownLatch(1); - CustomWebhook.main(new String[0], initLatch); + io.openvidu.test.browsers.utils.CustomWebhook.main(new String[0], initLatch); try {