Skip to content

Commit

Permalink
[grid] Migrate RegistrationRequest.toJson to not use GSON
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Mar 6, 2018
1 parent 41f1681 commit 22d2498
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 72 deletions.
21 changes: 9 additions & 12 deletions java/server/src/org/openqa/grid/common/RegistrationRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
import org.openqa.grid.common.exception.GridConfigurationException;
import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;
import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration.CollectionOfDesiredCapabilitiesDeSerializer;
import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration.CollectionOfDesiredCapabilitiesSerializer;
import org.openqa.selenium.MutableCapabilities;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
* Helper to register to the grid. Using JSON to exchange the object between the node and the hub.
Expand Down Expand Up @@ -117,17 +118,13 @@ public GridNodeConfiguration getConfiguration() {
return configuration;
}

public JsonObject toJson() {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(new TypeToken<List<MutableCapabilities>>(){}.getType(),
new CollectionOfDesiredCapabilitiesSerializer());

// note: it's very important that nulls are serialized for this type.
return builder.serializeNulls()
.excludeFieldsWithoutExposeAnnotation()
.create()
.toJsonTree(this, RegistrationRequest.class)
.getAsJsonObject();
public Map<String, Object> toJson() {
Map<String, Object> json = new TreeMap<>();
json.put("class", getClass());
json.put("name", getName());
json.put("description", getDescription());
json.put("configuration", getConfiguration());
return json;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.Platform;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.DesiredCapabilities;

Expand Down Expand Up @@ -67,7 +68,7 @@ public void testToJson() {
req.getConfiguration().capabilities.add(cap);
}

String json = req.toJson().toString();
String json = new Json().toJson(req.toJson());

RegistrationRequest req2 = RegistrationRequest.fromJson(json);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

import com.google.gson.JsonObject;

import org.openqa.selenium.json.Json;
import org.openqa.testing.FakeHttpServletRequest;
import org.openqa.testing.FakeHttpServletResponse;
import org.openqa.testing.UrlInfo;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
Expand All @@ -41,14 +43,16 @@ protected static UrlInfo createUrl(String path) {

protected FakeHttpServletResponse sendCommand(String method, String commandPath)
throws IOException, ServletException {
return sendCommand(method, commandPath, null);
return sendCommand(method, commandPath, (Map<String, Object>) null);
}

protected FakeHttpServletResponse sendCommand(String method, String commandPath,
JsonObject parameters) throws IOException, ServletException {
protected FakeHttpServletResponse sendCommand(
String method,
String commandPath,
Map<String, Object> parameters) throws IOException, ServletException {
FakeHttpServletRequest request = new FakeHttpServletRequest(method, createUrl(commandPath));
if (parameters != null) {
request.setBody(parameters.toString());
request.setBody(new Json().toJson(parameters));
}
FakeHttpServletResponse response = new FakeHttpServletResponse();
servlet.service(request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -34,40 +33,42 @@
import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;
import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;
import org.openqa.grid.web.Hub;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.testing.FakeHttpServletResponse;
import org.seleniumhq.jetty9.server.handler.ContextHandler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

public class RegistrationServletTest extends BaseServletTest {
private class BaseRequest {
final String name = "proxy-foo";
final String description = "a fictitious proxy";
@SerializedName( "class" )
final String clazz = BaseRequest.class.getCanonicalName();
String id;
}

private final class RequestV2 extends BaseRequest {
final Map<String, Object> configuration = new HashMap<>();
final List<DesiredCapabilities> capabilities = new ArrayList<>();
}
private Map<String, Object> requestWithoutConfig;
private Map<String, Object> grid2Request;
private Map<String, Object> grid3Request;

private final class InvalidV2Request extends BaseRequest {
final List<DesiredCapabilities> capabilities = new ArrayList<>();
}

private final class RequestV3Beta extends BaseRequest {
final GridNodeConfiguration configuration = new GridNodeConfiguration();
final List<DesiredCapabilities> capabilities = new ArrayList<>();
@Before
public void fillBaseRequest() {
// This base request contains most of the fields we normally see serialised, but lacks the
// "configuration" and "capabilities" fields.
Map<String, Object> baseRequest = new TreeMap<>();
baseRequest.put("name", "proxy-foo");
baseRequest.put("description", "a fictitious proxy");
baseRequest.put("class", "com.example.grid.BaseRequest");

requestWithoutConfig = new TreeMap<>(baseRequest);
requestWithoutConfig.put("capabilities", ImmutableList.of());

grid2Request = new TreeMap<>(baseRequest);
grid2Request.put("capabilities", ImmutableList.of());
grid2Request.put("configuration", ImmutableMap.of());

grid3Request = new TreeMap<>(baseRequest);
grid3Request.put("capabilities", ImmutableList.of());
grid3Request.put("configuration", new GridNodeConfiguration());
}

@Before
Expand Down Expand Up @@ -106,71 +107,71 @@ private void waitForServletToAddProxy() throws Exception {
*/
@Test(expected = GridConfigurationException.class)
public void testInvalidV2Registration() throws Exception {
final InvalidV2Request request = new InvalidV2Request();
request.capabilities.add(DesiredCapabilities.firefox());
request.id = "http://dummynode:1111";
final JsonObject json = new GsonBuilder().serializeNulls().create()
.toJsonTree(request, InvalidV2Request.class).getAsJsonObject();
sendCommand("POST", "/", json);
}
requestWithoutConfig.put("capabilities", ImmutableList.of(new FirefoxOptions()));
requestWithoutConfig.put("id", "http://dummynode:1111");

sendCommand("POST", "/", requestWithoutConfig);
}

/**
* Tests that the registration request servlet can process a V2 RegistrationRequest which
* contains servlets as a comma separated String.
*/
@Test
public void testLegacyV2Registration() throws Exception {
final RequestV2 request = new RequestV2();
request.configuration.put("servlets", "foo,bar,baz");
request.configuration.put("registerCycle", 30001);
request.configuration.put("proxy", null);
request.capabilities.add(DesiredCapabilities.firefox());
request.id = "http://dummynode:1234";
final JsonObject json = new GsonBuilder().serializeNulls().create()
.toJsonTree(request, RequestV2.class).getAsJsonObject();
final FakeHttpServletResponse response = sendCommand("POST", "/", json);
Map<String, Object> config = new TreeMap<>();
config.put("servlets", "foo,bar,baz");
config.put("registerCycle", 30001);
config.put("proxy", null);
grid2Request.put("configuration", config);

grid2Request.put("capabilities", ImmutableList.of(new FirefoxOptions()));
String id = "http://dummynode:1234";
grid2Request.put("id", id);

final FakeHttpServletResponse response = sendCommand("POST", "/", grid2Request);
waitForServletToAddProxy();

assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertEquals(((RegistrationServlet) servlet).getRegistry().getAllProxies().size(), 1);

final RemoteProxy proxy = ((RegistrationServlet) servlet).getRegistry().getAllProxies()
.getProxyById(request.id);
.getProxyById(id);
assertNotNull(proxy);
assertEquals(3, proxy.getConfig().servlets.size());
assertEquals(1, proxy.getConfig().capabilities.size());
assertEquals(30001, proxy.getConfig().registerCycle.intValue());
assertEquals(request.id, proxy.getConfig().id);
assertEquals(id, proxy.getConfig().id);
}


/**
* Tests that the registration request servlet can process a V2 RegistrationRequest from
* a 3.0.0-beta node.
* a 3.x node.
*/
@Test
public void testLegacyV3BetaRegistration() throws Exception {
final RequestV3Beta request = new RequestV3Beta();
request.configuration.capabilities.clear();
request.configuration.proxy = null;
request.capabilities.add(DesiredCapabilities.firefox());
request.id = "http://dummynode:2345";
final JsonObject json = new GsonBuilder().serializeNulls().create()
.toJsonTree(request, RequestV3Beta.class).getAsJsonObject();
final FakeHttpServletResponse response = sendCommand("POST", "/", json);
GridNodeConfiguration config = new GridNodeConfiguration();
config.capabilities.clear();
config.proxy = null;
grid3Request.put("configuration", config);

grid3Request.put("capabilities", ImmutableList.of(new FirefoxOptions()));
String id = "http://dummynode:2345";
grid3Request.put("id", id);
final FakeHttpServletResponse response = sendCommand("POST", "/", grid3Request);
waitForServletToAddProxy();

assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertEquals(((RegistrationServlet) servlet).getRegistry().getAllProxies().size(), 1);

final RemoteProxy proxy = ((RegistrationServlet) servlet).getRegistry().getAllProxies()
.getProxyById(request.id);
.getProxyById(id);
assertNotNull(proxy);
assertEquals(0, proxy.getConfig().servlets.size());
assertEquals(1, proxy.getConfig().capabilities.size());
assertEquals(request.configuration.registerCycle.intValue(), proxy.getConfig().registerCycle.intValue());
assertEquals(request.id, proxy.getConfig().id);
assertEquals(config.registerCycle.intValue(), proxy.getConfig().registerCycle.intValue());
assertEquals(id, proxy.getConfig().id);
}


Expand Down

0 comments on commit 22d2498

Please sign in to comment.