Skip to content

Commit

Permalink
[Grid] 1) Fixing 'servlets' and 'withoutServlets' loading from JSON,
Browse files Browse the repository at this point in the history
2) Fixing 'host' to be always '0.0.0.0' if not specified explicitly
  • Loading branch information
barancev committed Aug 8, 2018
1 parent 4965564 commit 4ee517f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;
import org.openqa.selenium.json.Json;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class GridHubCliOptions extends CommonGridCliOptions {

Expand Down Expand Up @@ -67,11 +69,24 @@ public GridHubCliOptions parse(String[] args) {
}

private static IDefaultProvider defaults(String json) {
Map<String, Object> map = (Map<String, Object>) new Json().toType(json, Map.class);
Map<String, Object> map = new Json().toType(json, Map.class);
map.remove("custom");
return optionName -> {
String option = optionName.replaceAll("-", "");
return map.containsKey(option) && map.get(option) != null ? map.get(option).toString() : null;
if (map.containsKey(option) && map.get(option) != null) {
if (map.get(option) instanceof List) {
List<?> l = (List<?>) map.get(option);
if (l.isEmpty()) {
return null;
} else {
return l.stream().map(Object::toString).collect(Collectors.joining(","));
}
} else {
return map.get(option).toString();
}
} else {
return null;
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public StandaloneConfiguration(CommonJsonConfiguration jsonConfig) {
this.role = ROLE;
this.debug = jsonConfig.getDebug();
this.log = jsonConfig.getLog();
this.host = jsonConfig.getHost();
this.host = jsonConfig.getHost() != null ? jsonConfig.getHost() : "0.0.0.0";
this.port = jsonConfig.getPort();
this.timeout = jsonConfig.getTimeout();
this.browserTimeout = jsonConfig.getBrowserTimeout();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class GridHubConfigurationTest {

static final Integer DEFAULT_TIMEOUT = StandaloneConfigurationTest.DEFAULT_TIMEOUT;
static final Integer DEFAULT_BROWSER_TIMEOUT = StandaloneConfigurationTest.DEFAULT_BROWSER_TIMEOUT;
static final String DEFAULT_HOST = StandaloneConfigurationTest.DEFAULT_HOST;
static final Integer DEFAULT_PORT = StandaloneConfigurationTest.DEFAULT_PORT;
static final Boolean DEFAULT_DEBUG_TOGGLE = StandaloneConfigurationTest.DEFAULT_DEBUG_TOGGLE;

Expand All @@ -52,7 +53,21 @@ public class GridHubConfigurationTest {

@Test
public void testDefaults() {
GridHubConfiguration ghc = new GridHubConfiguration();
checkDefaults(new GridHubConfiguration());
}

@Test
public void testDefaultsFromConfig() {
checkDefaults(new GridHubConfiguration(
HubJsonConfiguration.loadFromResourceOrFile(GridHubConfiguration.DEFAULT_HUB_CONFIG_FILE)));
}

@Test
public void testDefaultsFromCli() {
checkDefaults(new GridHubCliOptions.Parser().parse(new String[]{}).toConfiguration());
}

private void checkDefaults(GridHubConfiguration ghc) {
// these values come from the GridHubConfiguration class
assertEquals(DEFAULT_PORT, ghc.port);
assertEquals(GridHubConfiguration.ROLE, ghc.role);
Expand All @@ -64,7 +79,7 @@ public void testDefaults() {

// these values come from the GridConfiguration base class
assertEquals(DEFAULT_CLEANUP_CYCLE, ghc.cleanUpCycle);
assertNull(ghc.host);
assertEquals(DEFAULT_HOST, ghc.host);
assertNull(ghc.maxSession);
assertNotNull(ghc.custom);
assertTrue(ghc.custom.isEmpty());
Expand All @@ -81,33 +96,6 @@ public void testDefaults() {
assertNull(ghc.log);
}

@Test
public void testConstructorEqualsDefaultConfig() {
GridHubConfiguration actual = new GridHubConfiguration();
GridHubConfiguration expected =
GridHubConfiguration.loadFromJSON(GridHubConfiguration.DEFAULT_HUB_CONFIG_FILE);

assertEquals(expected.role, actual.role);
assertEquals(expected.port, actual.port);
assertEquals(expected.capabilityMatcher.getClass().getCanonicalName(),
actual.capabilityMatcher.getClass().getCanonicalName());
assertEquals(expected.newSessionWaitTimeout, actual.newSessionWaitTimeout);
assertEquals(expected.throwOnCapabilityNotPresent, actual.throwOnCapabilityNotPresent);
assertEquals(expected.hubConfig, actual.hubConfig);
assertEquals(expected.prioritizer, actual.prioritizer);
assertEquals(expected.cleanUpCycle, actual.cleanUpCycle);
assertEquals(expected.host, actual.host);
assertEquals(expected.maxSession, actual.maxSession);
assertEquals(expected.custom.size(), actual.custom.size());
assertEquals(expected.servlets.size(), actual.servlets.size());
assertEquals(expected.withoutServlets.size(), actual.withoutServlets.size());
assertEquals(expected.timeout, actual.timeout);
assertEquals(expected.browserTimeout, actual.browserTimeout);
assertEquals(expected.debug, actual.debug);
assertEquals(expected.jettyMaxThreads, actual.jettyMaxThreads);
assertEquals(expected.log, actual.log);
}

@Test
public void testLoadFromJson() throws IOException {
GridHubConfiguration ghc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class GridNodeConfigurationTest {

static final Integer DEFAULT_TIMEOUT = StandaloneConfigurationTest.DEFAULT_TIMEOUT;
static final Integer DEFAULT_BROWSER_TIMEOUT = StandaloneConfigurationTest.DEFAULT_BROWSER_TIMEOUT;
static final String DEFAULT_HOST = StandaloneConfigurationTest.DEFAULT_HOST;
static final Integer DEFAULT_PORT = -1;
static final Boolean DEFAULT_DEBUG_TOGGLE = StandaloneConfigurationTest.DEFAULT_DEBUG_TOGGLE;

Expand All @@ -64,6 +65,60 @@ public class GridNodeConfigurationTest {
static final Integer DEFAULT_DOWN_POLLING_LIMIT = 2;
static final String DEFAULT_PROXY = "org.openqa.grid.selenium.proxy.DefaultRemoteProxy";

@Test
public void testDefaults() {
checkDefaults(new GridNodeConfiguration());
}

@Test
public void testConstructorEqualsDefaultConfig() {
checkDefaults(new GridNodeConfiguration(
NodeJsonConfiguration.loadFromResourceOrFile(GridNodeConfiguration.DEFAULT_NODE_CONFIG_FILE)));
}

@Test
public void testDefaultsFromCli() {
checkDefaults(new GridNodeCliOptions.Parser().parse(new String[]{}).toConfiguration());
}

private void checkDefaults(GridNodeConfiguration gnc) {
assertEquals(GridNodeConfiguration.ROLE, gnc.role);
assertEquals(DEFAULT_HOST, gnc.host);
assertEquals(DEFAULT_PORT, gnc.port);
assertEquals(DEFAULT_NODE_STATUS_CHECK_TIMEOUT, gnc.nodeStatusCheckTimeout);
assertEquals(DEFAULT_POLLING_INTERVAL, gnc.nodePolling);
assertEquals(DEFAULT_PROXY, gnc.proxy);
assertEquals(DEFAULT_REGISTER_TOGGLE, gnc.register);
assertEquals(DEFAULT_REGISTER_CYCLE, gnc.registerCycle);
assertEquals(DEFAULT_HUB, gnc.hub);
assertEquals(DEFAULT_MAX_SESSION, gnc.maxSession);
assertFalse(gnc.capabilities.isEmpty());
assertEquals(4, gnc.capabilities.size());
assertNull(gnc.id);
assertEquals(DEFAULT_DOWN_POLLING_LIMIT, gnc.downPollingLimit);
assertNull(gnc.hubHost);
assertNull(gnc.hubPort);
assertNull(gnc.nodeConfigFile);
assertEquals(DEFAULT_UNREGISTER_DELAY, gnc.unregisterIfStillDownAfter);

assertNull(gnc.cleanUpCycle);
assertNotNull(gnc.custom);
assertTrue(gnc.custom.isEmpty());
assertNotNull(gnc.servlets);
assertTrue(gnc.servlets.isEmpty());
assertNotNull(gnc.withoutServlets);
assertTrue(gnc.withoutServlets.isEmpty());

assertEquals(DEFAULT_TIMEOUT, gnc.timeout);
assertEquals(DEFAULT_BROWSER_TIMEOUT, gnc.browserTimeout);
assertEquals(DEFAULT_DEBUG_TOGGLE, gnc.debug);
assertNull(gnc.jettyMaxThreads);
assertNull(gnc.log);

//not a @Parameter
assertNull(gnc.remoteHost);
}

@Test
public void testLoadFromJson() throws IOException {
final String configJson = "{"
Expand Down Expand Up @@ -110,83 +165,6 @@ public void testLoadFromOldJson() {
GridNodeConfiguration.loadFromJSON(configJson);
}

@Test
public void testDefaults() {
GridNodeConfiguration gnc = new GridNodeConfiguration();
assertEquals(GridNodeConfiguration.ROLE, gnc.role);
assertEquals(DEFAULT_PORT, gnc.port);
assertEquals(DEFAULT_NODE_STATUS_CHECK_TIMEOUT, gnc.nodeStatusCheckTimeout);
assertEquals(DEFAULT_POLLING_INTERVAL, gnc.nodePolling);
assertEquals(DEFAULT_PROXY, gnc.proxy);
assertEquals(DEFAULT_REGISTER_TOGGLE, gnc.register);
assertEquals(DEFAULT_REGISTER_CYCLE, gnc.registerCycle);
assertEquals(DEFAULT_HUB, gnc.hub);
assertEquals(DEFAULT_MAX_SESSION, gnc.maxSession);
assertFalse(gnc.capabilities.isEmpty());
assertEquals(4, gnc.capabilities.size());
assertNull(gnc.id);
assertEquals(DEFAULT_DOWN_POLLING_LIMIT, gnc.downPollingLimit);
assertNull(gnc.hubHost);
assertNull(gnc.hubPort);
assertNull(gnc.nodeConfigFile);
assertEquals(DEFAULT_UNREGISTER_DELAY, gnc.unregisterIfStillDownAfter);

assertNull(gnc.cleanUpCycle);
assertNull(gnc.host);
assertNotNull(gnc.custom);
assertTrue(gnc.custom.isEmpty());
assertNotNull(gnc.servlets);
assertTrue(gnc.servlets.isEmpty());
assertNotNull(gnc.withoutServlets);
assertTrue(gnc.withoutServlets.isEmpty());

assertEquals(DEFAULT_TIMEOUT, gnc.timeout);
assertEquals(DEFAULT_BROWSER_TIMEOUT, gnc.browserTimeout);
assertEquals(DEFAULT_DEBUG_TOGGLE, gnc.debug);
assertNull(gnc.jettyMaxThreads);
assertNull(gnc.log);

//not a @Parameter
assertNull(gnc.remoteHost);
}

@Test
public void testConstructorEqualsDefaultConfig() {
GridNodeConfiguration actual = new GridNodeConfiguration();
GridNodeConfiguration expected =
GridNodeConfiguration.loadFromJSON(GridNodeConfiguration.DEFAULT_NODE_CONFIG_FILE);

assertEquals(expected.role, actual.role);
assertEquals(expected.port, actual.port);
assertEquals(expected.capabilities.size(), actual.capabilities.size());

assertEquals(expected.nodeStatusCheckTimeout, actual.nodeStatusCheckTimeout);
assertEquals(expected.nodePolling, actual.nodePolling);
assertEquals(expected.proxy, actual.proxy);
assertEquals(expected.register, actual.register);
assertEquals(expected.registerCycle, actual.registerCycle);
assertEquals(expected.hub, actual.hub);

assertEquals(expected.id, actual.id);
assertEquals(expected.downPollingLimit, actual.downPollingLimit);
assertEquals(expected.hubPort, actual.hubPort);
assertEquals(expected.hubHost, actual.hubHost);
assertEquals(expected.nodeConfigFile, actual.nodeConfigFile);
assertEquals(expected.unregisterIfStillDownAfter, actual.unregisterIfStillDownAfter);

assertEquals(expected.cleanUpCycle, actual.cleanUpCycle);
assertEquals(expected.host, actual.host);
assertEquals(expected.maxSession, actual.maxSession);
assertEquals(expected.custom.size(), actual.custom.size());
assertEquals(expected.servlets.size(), actual.servlets.size());
assertEquals(expected.withoutServlets.size(), actual.withoutServlets.size());
assertEquals(expected.timeout, actual.timeout);
assertEquals(expected.browserTimeout, actual.browserTimeout);
assertEquals(expected.debug, actual.debug);
assertEquals(expected.jettyMaxThreads, actual.jettyMaxThreads);
assertEquals(expected.log, actual.log);
}

@Test
public void testAsJson() {
GridNodeConfiguration gnc = parseCliOptions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,37 @@ public class StandaloneConfigurationTest {

static final Integer DEFAULT_TIMEOUT = 1800;
static final Integer DEFAULT_BROWSER_TIMEOUT = 0;
static final String DEFAULT_HOST = "0.0.0.0";
static final Integer DEFAULT_PORT = 4444;
static final Boolean DEFAULT_DEBUG_TOGGLE = false;

@Test
public void testDefaults() {
StandaloneConfiguration sc = new StandaloneConfiguration();
checkDefaults(new StandaloneConfiguration());
}

@Test
public void testConstructorEqualsDefaultConfig() {
checkDefaults(new StandaloneConfiguration(
StandaloneJsonConfiguration.loadFromResourceOrFile(StandaloneConfiguration.DEFAULT_STANDALONE_CONFIG_FILE)));
}

@Test
public void testDefaultsFromCli() {
checkDefaults(new StandaloneCliOptions().parse(new String[]{}).toConfiguration());
}

private void checkDefaults(StandaloneConfiguration sc) {
assertEquals(StandaloneConfiguration.ROLE, sc.role);
assertEquals(DEFAULT_BROWSER_TIMEOUT, sc.browserTimeout);
assertEquals(DEFAULT_DEBUG_TOGGLE, sc.debug);
assertEquals(DEFAULT_TIMEOUT, sc.timeout);
assertEquals(DEFAULT_PORT, sc.port);
assertEquals(null, sc.host);
assertEquals(DEFAULT_HOST, sc.host);
assertNull(sc.jettyMaxThreads);
assertNull(sc.log);
}

@Test
public void testConstructorEqualsDefaultConfig() {
StandaloneConfiguration actual = new StandaloneConfiguration();
StandaloneConfiguration expected =
StandaloneConfiguration.loadFromJson(StandaloneConfiguration.DEFAULT_STANDALONE_CONFIG_FILE, StandaloneConfiguration.class);

assertEquals(expected.role, actual.role);
assertEquals(expected.browserTimeout, actual.browserTimeout);
assertEquals(expected.debug, actual.debug);
assertEquals(expected.timeout, actual.timeout);
assertEquals(expected.jettyMaxThreads, actual.jettyMaxThreads);
assertEquals(expected.log, actual.log);
assertEquals(expected.port, actual.port);
assertEquals(null, actual.host);
}

@Test
public void commandLineParsing() {
String[] args = "-timeout 32123 -browserTimeout 456".split(" ");
Expand Down

0 comments on commit 4ee517f

Please sign in to comment.