Move REST client and Webhook from openvidu-test-e2e to openvidu-test-browsers repo

pull/331/head
pabloFuente 2019-10-04 15:31:24 +02:00
parent 1f47c1b9e4
commit 638176a6d4
4 changed files with 85 additions and 50 deletions

View File

@ -57,6 +57,16 @@
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${version.spring-boot}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${version.spring-boot}</version>
</dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
@ -77,6 +87,16 @@
<artifactId>selenium-firefox-driver</artifactId> <artifactId>selenium-firefox-driver</artifactId>
<version>${version.selenium}</version> <version>${version.selenium}</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.9</version>
</dependency>
</dependencies> </dependencies>
<profiles> <profiles>

View File

@ -15,7 +15,7 @@
* *
*/ */
package io.openvidu.test.e2e.utils; package io.openvidu.test.browsers.utils;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.KeyStoreException; import java.security.KeyStoreException;
@ -24,11 +24,9 @@ import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.Base64; import java.util.Base64;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy; import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
@ -37,7 +35,6 @@ import org.apache.http.ssl.SSLContextBuilder;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.junit.Assert;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -56,9 +53,9 @@ public class CustomHttpClient {
private String openviduUrl; private String openviduUrl;
private String headerAuth; private String headerAuth;
public CustomHttpClient(String openviduUrl, String openviduSecret) { public CustomHttpClient(String url, String user, String pass) throws Exception {
this.openviduUrl = openviduUrl.replaceFirst("/*$", ""); this.openviduUrl = url.replaceFirst("/*$", "");
this.headerAuth = "Basic " + Base64.getEncoder().encodeToString(("OPENVIDUAPP:" + openviduSecret).getBytes()); this.headerAuth = "Basic " + Base64.getEncoder().encodeToString((user + ":" + pass).getBytes());
SSLContext sslContext = null; SSLContext sslContext = null;
try { try {
@ -68,46 +65,43 @@ public class CustomHttpClient {
} }
}).build(); }).build();
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) { } 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) HttpClient unsafeHttpClient = HttpClients.custom().setSSLContext(sslContext)
.setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); .setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
Unirest.setHttpClient(unsafeHttpClient); Unirest.setHttpClient(unsafeHttpClient);
} }
public void testAuthorizationError() { public int getAndReturnStatus(String path, String credentials) throws Exception {
try { path = openviduUrl + (path.startsWith("/") ? path : ("/" + path));
String wrongCredentials = "Basic " return Unirest.get(path).header("Authorization", credentials).asJson().getStatus();
+ 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 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); 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); return this.commonRest(method, path, body, status);
} }
public JSONObject rest(HttpMethod method, String path, String body, int status, boolean exactReturnedFields, 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 json = this.commonRest(method, path, body, status);
JSONObject jsonObjExpected = null; JSONObject jsonObjExpected = null;
jsonReturnedValue.replaceAll("'", "\""); jsonReturnedValue.replaceAll("'", "\"");
try { try {
jsonObjExpected = new JSONObject(jsonReturnedValue); jsonObjExpected = new JSONObject(jsonReturnedValue);
} catch (JSONException e1) { } 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) { if (exactReturnedFields) {
Assert.assertEquals("Error in number of keys in JSON response to POST (" + json.toString() + ")" + path, if (jsonObjExpected.length() != json.length()) {
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()) { for (String key : jsonObjExpected.keySet()) {
Class<?> c1 = jsonObjExpected.get(key).getClass(); Class<?> c1 = jsonObjExpected.get(key).getClass();
@ -116,21 +110,23 @@ public class CustomHttpClient {
c1 = unifyNumberType(c1); c1 = unifyNumberType(c1);
c2 = unifyNumberType(c2); 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; return json;
} }
public JSONObject rest(HttpMethod method, String path, String body, int status, boolean exactReturnedFields, public JSONObject rest(HttpMethod method, String path, String body, int status, boolean exactReturnedFields,
Map<String, ?> jsonResponse) { Map<String, ?> jsonResponse) throws Exception {
org.json.JSONObject json = this.commonRest(method, path, body, status); org.json.JSONObject json = this.commonRest(method, path, body, status);
if (exactReturnedFields) { if (exactReturnedFields) {
Assert.assertEquals("Error in number of keys in JSON response to POST " + path, jsonResponse.size(), if (jsonResponse.size() != json.length())
json.length()); throw new Exception("Error in number of keys in JSON response to POST " + path);
} }
for (Entry<String, ?> entry : jsonResponse.entrySet()) { for (Map.Entry<String, ?> entry : jsonResponse.entrySet()) {
Object value = entry.getValue(); Object value = entry.getValue();
if (value instanceof String) { if (value instanceof String) {
@ -146,34 +142,44 @@ public class CustomHttpClient {
// COMPARE // COMPARE
} catch (JSONException e2) { } catch (JSONException e2) {
Assert.assertEquals("JSON field " + entry.getKey() + " has not expected value", (String) value, if (((String) value) != json.getString(entry.getKey())) {
json.getInt(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) { } else if (value instanceof Integer) {
Assert.assertEquals("JSON field " + entry.getKey() + " has not expected value", (int) value, if (((int) value) != json.getInt(entry.getKey())) {
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) { } else if (value instanceof Long) {
Assert.assertEquals("JSON field " + entry.getKey() + " has not expected value", (long) value, if (((long) value) != json.getLong(entry.getKey())) {
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) { } else if (value instanceof Double) {
Assert.assertEquals("JSON field " + entry.getKey() + " has not expected value", (double) value, if (((double) value) != json.getDouble(entry.getKey())) {
json.getDouble(entry.getKey()), 0.001); throw new Exception("JSON field " + entry.getKey() + " has not expected value. Expected: " + value
+ ". Actual: " + json.getDouble(entry.getKey()));
}
} else if (value instanceof Boolean) { } else if (value instanceof Boolean) {
Assert.assertEquals("JSON field " + entry.getKey() + " has not expected value", (boolean) value, if (((boolean) value) != json.getBoolean(entry.getKey())) {
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) { } else if (value instanceof JSONArray) {
json.getJSONArray(entry.getKey()); json.getJSONArray(entry.getKey());
} else if (value instanceof JSONObject) { } else if (value instanceof JSONObject) {
json.getJSONObject(entry.getKey()); json.getJSONObject(entry.getKey());
} else { } else {
Assert.fail("JSON response field cannot be parsed: " + entry.toString()); throw new Exception("JSON response field cannot be parsed: " + entry.toString());
} }
} }
return json; 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<JsonNode> jsonResponse = null; HttpResponse<JsonNode> jsonResponse = null;
org.json.JSONObject json = null; org.json.JSONObject json = null;
path = openviduUrl + (path.startsWith("/") ? path : ("/" + path)); path = openviduUrl + (path.startsWith("/") ? path : ("/" + path));
@ -214,12 +220,16 @@ public class CustomHttpClient {
} }
} catch (UnirestException e) { } catch (UnirestException e) {
log.error(e.getMessage()); 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) { if (jsonResponse.getStatus() == 500) {
log.error("Internal Server Error: {}", jsonResponse.getBody().toString()); 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; return json;
} }

View File

@ -15,7 +15,7 @@
* *
*/ */
package io.openvidu.test.e2e.utils; package io.openvidu.test.browsers.utils;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;

View File

@ -31,6 +31,7 @@ import java.nio.file.Paths;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Base64;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; 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.ChromeUser;
import io.openvidu.test.browsers.FirefoxUser; import io.openvidu.test.browsers.FirefoxUser;
import io.openvidu.test.browsers.OperaUser; 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.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.MultimediaFileMetadata;
import io.openvidu.test.e2e.utils.Unzipper; import io.openvidu.test.e2e.utils.Unzipper;
@ -2355,10 +2356,13 @@ public class OpenViduTestAppE2eTest {
log.info("REST API test"); log.info("REST API test");
CustomHttpClient restClient = new CustomHttpClient(OPENVIDU_URL, OPENVIDU_SECRET); CustomHttpClient restClient = new CustomHttpClient(OPENVIDU_URL, "OPENVIDUAPP", OPENVIDU_SECRET);
// 401 // 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) **/ /** GET /api/sessions (before session created) **/
restClient.rest(HttpMethod.GET, "/api/sessions/NOT_EXISTS", HttpStatus.SC_NOT_FOUND); 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); restClient.rest(HttpMethod.DELETE, "/api/sessions/CUSTOM_SESSION_ID", HttpStatus.SC_NO_CONTENT);
// GET /api/sessions should return empty again // 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 **/ /** GET /config **/
restClient.rest(HttpMethod.GET, "/config", null, HttpStatus.SC_OK, true, restClient.rest(HttpMethod.GET, "/config", null, HttpStatus.SC_OK, true,
@ -2771,7 +2776,7 @@ public class OpenViduTestAppE2eTest {
log.info("Webhook test"); log.info("Webhook test");
CountDownLatch initLatch = new CountDownLatch(1); CountDownLatch initLatch = new CountDownLatch(1);
CustomWebhook.main(new String[0], initLatch); io.openvidu.test.browsers.utils.CustomWebhook.main(new String[0], initLatch);
try { try {