Skip to content

Commit

Permalink
[java] Refactoring tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Sep 6, 2018
1 parent f658da9 commit 1407346
Showing 1 changed file with 56 additions and 56 deletions.
112 changes: 56 additions & 56 deletions java/client/test/org/openqa/selenium/remote/W3CRemoteDriverTest.java
Expand Up @@ -17,16 +17,14 @@

package org.openqa.selenium.remote;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static java.util.Collections.EMPTY_MAP;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assume.assumeNotNull;
import static org.openqa.selenium.json.Json.MAP_TYPE;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import org.junit.Test;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
Expand All @@ -50,23 +48,17 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class W3CRemoteDriverTest {

@Test
public void mustSpecifyAtLeastOneSetOfOptions() {
RemoteWebDriverBuilder builder = RemoteWebDriver.builder();

try {
builder.build();
fail("This is unexpected");
} catch (SessionNotCreatedException expected) {
// Fine
}
assertThatExceptionOfType(SessionNotCreatedException.class)
.isThrownBy(() -> RemoteWebDriver.builder().build());
}

@Test
Expand All @@ -76,22 +68,24 @@ public void settingAGlobalCapabilityCountsAsAnOption() {

List<Capabilities> capabilities = listCapabilities(builder);

assertEquals(1, capabilities.size());
assertEquals("cheese", capabilities.get(0).getBrowserName());
assertThat(capabilities).hasSize(1);
assertThat(capabilities.get(0).getBrowserName()).isEqualTo("cheese");
}

@Test
public void simpleCaseShouldBeADropIn() {
List<Capabilities> caps =
List<Capabilities> capabilities =
listCapabilities(RemoteWebDriver.builder().addAlternative(new FirefoxOptions()));

assertEquals(1, caps.size());
assertEquals("firefox", caps.get(0).getBrowserName());
assertThat(capabilities).hasSize(1);
assertThat(capabilities.get(0).getBrowserName()).isEqualTo("firefox");
}

@Test(expected = IllegalArgumentException.class)
@Test
public void requireAllOptionsAreW3CCompatible() {
RemoteWebDriver.builder().addAlternative(new ImmutableCapabilities("unknownOption", "cake"));
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> RemoteWebDriver.builder()
.addAlternative(new ImmutableCapabilities("unknownOption", "cake")));
}

@Test(expected = IllegalArgumentException.class)
Expand All @@ -113,29 +107,34 @@ public void ensureEachOfTheKeyOptionTypesAreSafe() {

@Test
public void shouldAllowMetaDataToBeSet() {
Map<String, String> expected = ImmutableMap.of("cheese", "brie");
Map<String, String> expected = singletonMap("cheese", "brie");
RemoteWebDriverBuilder builder = RemoteWebDriver.builder()
.addAlternative(new InternetExplorerOptions())
.addMetadata("cloud:options", expected);

Map<String, Object> payload = getPayload(builder);

assertEquals(expected, payload.get("cloud:options"));
assertThat(payload.get("cloud:options")).isEqualTo(expected);
}

@Test(expected = IllegalArgumentException.class)
@Test
public void doesNotAllowFirstMatchToBeUsedAsAMetadataNameAsItIsConfusing() {
RemoteWebDriver.builder().addMetadata("firstMatch", new HashMap<>());
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> RemoteWebDriver.builder().addMetadata("firstMatch", EMPTY_MAP));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void doesNotAllowAlwaysMatchToBeUsedAsAMetadataNameAsItIsConfusing() {
RemoteWebDriver.builder().addMetadata("alwaysMatch", ImmutableList.of(ImmutableMap.of()));
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> RemoteWebDriver.builder()
.addMetadata("alwaysMatch", singletonList(EMPTY_MAP)));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void doesNotAllowCapabilitiesToBeUsedAsAMetadataName() {
RemoteWebDriver.builder().addMetadata("capabilities", ImmutableList.of(ImmutableMap.of()));
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> RemoteWebDriver.builder()
.addMetadata("capabilities", singletonList(EMPTY_MAP)));
}

@Test
Expand All @@ -149,9 +148,9 @@ public void shouldAllowCapabilitiesToBeSetGlobally() {
// a requirement. Get the capabilities and check each of them.
List<Capabilities> allCaps = listCapabilities(builder);

assertEquals(2, allCaps.size());
assertEquals("brie", allCaps.get(0).getCapability("se:cheese"));
assertEquals("brie", allCaps.get(1).getCapability("se:cheese"));
assertThat(allCaps).hasSize(2);
assertThat(allCaps.get(0).getCapability("se:cheese")).isEqualTo("brie");
assertThat(allCaps.get(1).getCapability("se:cheese")).isEqualTo("brie");
}

@Test
Expand All @@ -165,9 +164,9 @@ public void shouldSetCapabilityToOptionsAddedAfterTheCallToSetCapabilities() {
// a requirement. Get the capabilities and check each of them.
List<Capabilities> allCaps = listCapabilities(builder);

assertEquals(2, allCaps.size());
assertEquals("brie", allCaps.get(0).getCapability("se:cheese"));
assertEquals("brie", allCaps.get(1).getCapability("se:cheese"));
assertThat(allCaps).hasSize(2);
assertThat(allCaps.get(0).getCapability("se:cheese")).isEqualTo("brie");
assertThat(allCaps.get(1).getCapability("se:cheese")).isEqualTo("brie");
}

@Test
Expand All @@ -181,8 +180,8 @@ public void additionalCapabilitiesOverrideOnesSetOnCapabilitiesAlready() {

List<Capabilities> allCaps = listCapabilities(builder);

assertEquals(1, allCaps.size());
assertEquals("brie", allCaps.get(0).getCapability("se:cheese"));
assertThat(allCaps).hasSize(1);
assertThat(allCaps.get(0).getCapability("se:cheese")).isEqualTo("brie");
}

@Test
Expand All @@ -195,8 +194,8 @@ public void ifARemoteUrlIsGivenThatIsUsedForTheSession() throws MalformedURLExce

RemoteWebDriverBuilder.Plan plan = builder.getPlan();

assertFalse(plan.isUsingDriverService());
assertEquals(expected, plan.getRemoteHost());
assertThat(plan.isUsingDriverService()).isFalse();
assertThat(plan.getRemoteHost()).isEqualTo(expected);
}

static class FakeDriverService extends DriverService {
Expand All @@ -215,16 +214,17 @@ public void shouldUseADriverServiceIfGivenOneRegardlessOfOtherChoices() throws I

RemoteWebDriverBuilder.Plan plan = builder.getPlan();

assertTrue(plan.isUsingDriverService());
assertEquals(expected, plan.getDriverService());
assertThat(plan.isUsingDriverService()).isTrue();
assertThat(plan.getDriverService()).isEqualTo(expected);
}

@Test(expected = IllegalArgumentException.class)
public void settingBothDriverServiceAndUrlIsAnError() throws IOException {
RemoteWebDriver.builder()
.addAlternative(new InternetExplorerOptions())
.url("http://example.com/cheese/peas/wd")
.withDriverService(new FakeDriverService());
@Test
public void settingBothDriverServiceAndUrlIsAnError() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> RemoteWebDriver.builder()
.addAlternative(new InternetExplorerOptions())
.url("http://example.com/cheese/peas/wd")
.withDriverService(new FakeDriverService()));
}

@Test
Expand Down Expand Up @@ -265,8 +265,8 @@ public void shouldDetectDriverServicesAndUseThoseIfNoOtherChoiceMade() {

RemoteWebDriverBuilder.Plan plan = RemoteWebDriver.builder().addAlternative(caps).getPlan();

assertTrue(plan.isUsingDriverService());
assertEquals(expectedServiceClass, plan.getDriverService().getClass());
assertThat(plan.isUsingDriverService()).isTrue();
assertThat(plan.getDriverService().getClass()).isEqualTo(expectedServiceClass);
}

@Test
Expand All @@ -277,8 +277,8 @@ public void oneOfWillClearOutTheCurrentlySetCapabilities() {

List<Capabilities> allCaps = listCapabilities(builder);

assertEquals(1, allCaps.size());
assertEquals("firefox", allCaps.get(0).getBrowserName());
assertThat(allCaps).hasSize(1);
assertThat(allCaps.get(0).getBrowserName()).isEqualTo("firefox");
}

private List<Capabilities> listCapabilities(RemoteWebDriverBuilder builder) {
Expand All @@ -288,19 +288,19 @@ private List<Capabilities> listCapabilities(RemoteWebDriverBuilder builder) {

@SuppressWarnings("unchecked")
Map<String, Object> always =
(Map<String, Object>) value.getOrDefault("alwaysMatch", ImmutableMap.of());
(Map<String, Object>) value.getOrDefault("alwaysMatch", EMPTY_MAP);
Capabilities alwaysMatch = new ImmutableCapabilities(always);

@SuppressWarnings("unchecked")
Collection<Map<String, Object>> firstMatch =
(Collection<Map<String, Object>>)
value.getOrDefault("firstMatch", ImmutableList.of(ImmutableMap.of()));
value.getOrDefault("firstMatch", singletonList(EMPTY_MAP));

return firstMatch
.parallelStream()
.map(ImmutableCapabilities::new)
.map(alwaysMatch::merge)
.collect(ImmutableList.toImmutableList());
.collect(Collectors.toList());
}

private Map<String, Object> getPayload(RemoteWebDriverBuilder builder) {
Expand Down

0 comments on commit 1407346

Please sign in to comment.