Skip to content

Commit

Permalink
[java] Removing a few bits more of the magic that moves JWP to W3C.
Browse files Browse the repository at this point in the history
  • Loading branch information
diemol committed Dec 8, 2022
1 parent d9e5d28 commit 1ee7e1f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 66 deletions.
57 changes: 3 additions & 54 deletions java/src/org/openqa/selenium/remote/CapabilitiesUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,14 @@
import com.google.common.collect.ImmutableSet;

import org.openqa.selenium.AcceptedW3CCapabilityKeys;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.session.CapabilitiesFilter;
import org.openqa.selenium.remote.session.CapabilityTransform;
import org.openqa.selenium.remote.session.ProxyTransform;
import org.openqa.selenium.remote.session.StripAnyPlatform;
import org.openqa.selenium.remote.session.W3CPlatformNameNormaliser;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -48,10 +43,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.openqa.selenium.remote.CapabilityType.PLATFORM;
import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME;
import static org.openqa.selenium.remote.CapabilityType.PROXY;

public class CapabilitiesUtils {

private static final Predicate<String> ACCEPTED_W3C_PATTERNS = new AcceptedW3CCapabilityKeys();
Expand All @@ -60,12 +51,6 @@ private CapabilitiesUtils() {
// Helper class
}

public static Stream<Capabilities> makeW3CSafe(Capabilities possiblyInvalidCapabilities) {
Require.nonNull("Capabilities", possiblyInvalidCapabilities);

return makeW3CSafe(possiblyInvalidCapabilities.asMap()).map(ImmutableCapabilities::new);
}

public static Stream<Map<String, Object>> makeW3CSafe(Map<String, Object> possiblyInvalidCapabilities) {
Require.nonNull("Capabilities", possiblyInvalidCapabilities);

Expand All @@ -75,13 +60,12 @@ public static Stream<Map<String, Object>> makeW3CSafe(Map<String, Object> possib
// then add magic to generate each of the w3c capabilities. For the sake of simplicity, we're
// going to make the (probably wrong) assumption we can hold all of the firstMatch values and
// alwaysMatch value in memory at the same time.
Map<String, Object> oss = convertOssToW3C(possiblyInvalidCapabilities);
Stream<Map<String, Object>> fromOss;
Set<String> usedKeys = new HashSet<>();

// Are there any values we care want to pull out into a mapping of their own?
List<Map<String, Object>> firsts = adapters.stream()
.map(adapter -> adapter.apply(oss))
.map(adapter -> adapter.apply(possiblyInvalidCapabilities))
.filter(Objects::nonNull)
.filter(map -> !map.isEmpty())
.map(
Expand All @@ -97,7 +81,7 @@ public static Stream<Map<String, Object>> makeW3CSafe(Map<String, Object> possib
}

// Are there any remaining unused keys?
Map<String, Object> always = oss.entrySet().stream()
Map<String, Object> always = possiblyInvalidCapabilities.entrySet().stream()
.filter(entry -> !usedKeys.contains(entry.getKey()))
.filter(entry -> entry.getValue() != null)
.collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
Expand All @@ -114,42 +98,7 @@ public static Stream<Map<String, Object>> makeW3CSafe(Map<String, Object> possib
return fromOss;
}

private static Map<String, Object> convertOssToW3C(Map<String, Object> capabilities) {
Map<String, Object> toReturn = new TreeMap<>(capabilities);

// Platform name
if (capabilities.containsKey(PLATFORM) && !capabilities.containsKey(PLATFORM_NAME)) {
toReturn.put(PLATFORM_NAME, String.valueOf(capabilities.get(PLATFORM)));
}

if (capabilities.containsKey(PROXY)) {
Map<String, Object> proxyMap = getProxyFromCapabilities(capabilities);
if (proxyMap.containsKey("noProxy")) {
Map<String, Object> w3cProxyMap = new HashMap<>(proxyMap);
Object rawData = proxyMap.get("noProxy");
if (rawData instanceof String) {
w3cProxyMap.put("noProxy", Arrays.asList(((String) rawData).split(",\\s*")));
}
toReturn.put(CapabilityType.PROXY, w3cProxyMap);
}
}

return toReturn;
}

private static Map<String, Object> getProxyFromCapabilities(Map<String, Object> capabilities) {
Object rawProxy = capabilities.get(CapabilityType.PROXY);
if (rawProxy instanceof Proxy) {
return ((Proxy) rawProxy).toJson();
} else if (rawProxy instanceof Map) {
//noinspection unchecked
return (Map<String, Object>) rawProxy;
} else {
return new HashMap<>();
}
}

private static Map<String, Object> applyTransforms(Map<String, Object> caps) {
public static Map<String, Object> applyTransforms(Map<String, Object> caps) {
Queue<Map.Entry<String, Object>> toExamine = new LinkedList<>(caps.entrySet());
Set<String> seenKeys = new HashSet<>();
Map<String, Object> toReturn = new TreeMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

package org.openqa.selenium.remote.session;

import static java.util.Collections.singleton;

import org.openqa.selenium.Proxy;

import java.util.AbstractMap;
import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;

import static java.util.Collections.singleton;

public class ProxyTransform implements CapabilityTransform {

@Override
Expand All @@ -43,9 +43,7 @@ public Collection<Map.Entry<String, Object>> apply(Map.Entry<String, Object> ent
proxy = new TreeMap<>((Map<String, Object>) rawProxy);
}
if (proxy.containsKey("proxyType")) {
proxy.put(
"proxyType",
String.valueOf(proxy.get("proxyType")).toLowerCase());
proxy.put("proxyType", String.valueOf(proxy.get("proxyType")).toLowerCase());
}
return singleton(new AbstractMap.SimpleImmutableEntry<>(entry.getKey(), proxy));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@

package org.openqa.selenium.remote.session;

import static java.util.Collections.singleton;
import static org.openqa.selenium.remote.CapabilityType.PLATFORM;
import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME;

import java.util.Collection;
import java.util.Map;

import static java.util.Collections.singleton;
import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME;

public class StripAnyPlatform implements CapabilityTransform {

@Override
public Collection<Map.Entry<String, Object>> apply(Map.Entry<String, Object> entry) {
if (!(PLATFORM.equals(entry.getKey()) || PLATFORM_NAME.equals(entry.getKey()))) {
if (!(PLATFORM_NAME.equals(entry.getKey()))) {
return singleton(entry);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.openqa.selenium.remote;

import com.google.common.collect.ImmutableMap;

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Capabilities;
Expand Down Expand Up @@ -213,11 +214,12 @@ void convertEverythingToFirstMatchOnlyIfPayloadContainsAlwaysMatchSectionAndOssC
singletonMap("browserName", "foo"),
singletonMap("browserName", "firefox")))));

assertEquals(asList(
assertEquals(
asList(
// From OSS
new ImmutableCapabilities("browserName", "firefox", "platform", "WINDOWS"),
// Generated from OSS
new ImmutableCapabilities("browserName", "firefox", "platformName", "windows"),
new ImmutableCapabilities("browserName", "firefox"),
// From the actual W3C capabilities
new ImmutableCapabilities("browserName", "foo", "platformName", "macos"),
new ImmutableCapabilities("browserName", "firefox", "platformName", "macos")),
Expand Down

0 comments on commit 1ee7e1f

Please sign in to comment.