Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix URL to URI #8448

Merged
merged 2 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion java/server/src/org/openqa/selenium/grid/commands/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ protected void execute(Config config) {
handler.addHandler(distributor);

Router router = new Router(tracer, clientFactory, sessions, distributor);
GraphqlHandler graphqlHandler = new GraphqlHandler(distributor, serverOptions.getExternalUri().toString());
GraphqlHandler graphqlHandler = new GraphqlHandler(distributor, serverOptions.getExternalUri());
HttpHandler readinessCheck = req -> {
boolean ready = router.isReady() && bus.isReady();
return new HttpResponse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected void execute(Config config) {
};

BaseServerOptions serverOptions = new BaseServerOptions(config);
GraphqlHandler graphqlHandler = new GraphqlHandler(distributor, serverOptions.getExternalUri().toString());
GraphqlHandler graphqlHandler = new GraphqlHandler(distributor, serverOptions.getExternalUri());
HttpHandler httpHandler = combine(
router,
Route.prefix("/wd/hub").to(combine(router)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URI;
import java.util.Objects;
import java.util.concurrent.ExecutionException;

Expand All @@ -50,12 +51,12 @@ public class GraphqlHandler implements HttpHandler {
public static final String GRID_SCHEMA = "/org/openqa/selenium/grid/graphql/selenium-grid-schema.graphqls";
public static final Json JSON = new Json();
private final Distributor distributor;
private final String publicUrl;
private final URI publicUri;
private final GraphQL graphQl;

public GraphqlHandler(Distributor distributor, String publicUrl) {
public GraphqlHandler(Distributor distributor, URI publicUri) {
this.distributor = Objects.requireNonNull(distributor);
this.publicUrl = Objects.requireNonNull(publicUrl);
this.publicUri = Objects.requireNonNull(publicUri);

GraphQLSchema schema = new SchemaGenerator()
.makeExecutableSchema(buildTypeDefinitionRegistry(), buildRuntimeWiring());
Expand Down Expand Up @@ -103,7 +104,7 @@ private RuntimeWiring buildRuntimeWiring() {
.scalar(Types.Uri)
.scalar(Types.Url)
.type("GridQuery", typeWiring -> typeWiring
.dataFetcher("grid", new GridData(distributor, publicUrl)))
.dataFetcher("grid", new GridData(distributor, publicUri)))
.build();
}

Expand Down
19 changes: 14 additions & 5 deletions java/server/src/org/openqa/selenium/grid/graphql/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,32 @@
import org.openqa.selenium.grid.distributor.Distributor;
import org.openqa.selenium.internal.Require;

import java.net.URI;
import java.util.List;
import java.util.function.Supplier;

public class Grid {

private final String url;
private final URI uri;
private final Supplier<DistributorStatus> distributorStatus;

public Grid(Distributor distributor, String url) {
public Grid(Distributor distributor, URI uri) {
Require.nonNull("Distributor", distributor);
this.url = Require.nonNull("Grid's public URL", url);
this.uri = Require.nonNull("Grid's public URI", uri);

this.distributorStatus = Suppliers.memoize(distributor::getStatus);
}

public String getUrl() {
return url;
public URI getUri() {
return uri;
}

public String getHost() {
shucon marked this conversation as resolved.
Show resolved Hide resolved
return uri.getHost();
}

public int getPort() {
shucon marked this conversation as resolved.
Show resolved Hide resolved
return uri.getPort();
}

public List<Node> getNodes() {
Expand Down
10 changes: 6 additions & 4 deletions java/server/src/org/openqa/selenium/grid/graphql/GridData.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@
import org.openqa.selenium.grid.distributor.Distributor;
import org.openqa.selenium.internal.Require;

import java.net.URI;

public class GridData implements DataFetcher {
private final Distributor distributor;
private final String publicUrl;
private final URI publicUri;

public GridData(Distributor distributor, String publicUrl) {
public GridData(Distributor distributor, URI publicUri) {
this.distributor = Require.nonNull("Distributor", distributor);
this.publicUrl = Require.nonNull("Grid's public URL", publicUrl);
this.publicUri = Require.nonNull("Grid's public URI", publicUri);
}

@Override
public Object get(DataFetchingEnvironment environment) {
return new Grid(distributor, publicUrl);
return new Grid(distributor, publicUri);
}
}
4 changes: 4 additions & 0 deletions java/server/src/org/openqa/selenium/grid/graphql/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ public class Node {

private final UUID id;
private final URI uri;
private final String host;
shucon marked this conversation as resolved.
Show resolved Hide resolved
private final boolean isUp;
private final int maxSession;
private final int port;
shucon marked this conversation as resolved.
Show resolved Hide resolved

public Node(UUID id, URI uri, boolean isUp , int maxSession) {
this.id = Require.nonNull("Node id", id);
this.uri = Require.nonNull("Node uri", uri);
this.isUp = isUp;
this.maxSession = Require.nonNull("Node maxSession", maxSession);
this.host = uri.getHost();
shucon marked this conversation as resolved.
Show resolved Hide resolved
this.port = uri.getPort();
}

public UUID getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ schema {
}

scalar Uri
scalar Url
shucon marked this conversation as resolved.
Show resolved Hide resolved

enum Status {
UP
Expand All @@ -14,6 +13,8 @@ enum Status {
type Node {
id: ID!
uri: Uri!
host: String!
shucon marked this conversation as resolved.
Show resolved Hide resolved
port: Int!
status: Status!
maxSession: Int!
}
Expand All @@ -23,7 +24,9 @@ type GridQuery {
}

type Grid {
url: Url!
uri: Uri!
host: String!
shucon marked this conversation as resolved.
Show resolved Hide resolved
port: Int!
nodes: [Node!]!
totalSlots: Int
usedSlots: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected void execute(Config config) {
distributorUrl
);

GraphqlHandler graphqlHandler = new GraphqlHandler(distributor, serverOptions.getExternalUri().toString());
GraphqlHandler graphqlHandler = new GraphqlHandler(distributor, serverOptions.getExternalUri());

Route handler = Route.combine(
new Router(tracer, clientFactory, sessions, distributor),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@

public class GraphqlHandlerTest {

private final String publicUrl = "http://example.com/grid-o-matic";
private final URI publicUri = new URI("http://example.com/grid-o-matic");
private Distributor distributor;
private Tracer tracer;
private EventBus events;

public GraphqlHandlerTest() throws URISyntaxException {
}

@Before
public void setupGrid() {
tracer = DefaultTestTracer.createTracer();
Expand All @@ -69,17 +72,17 @@ public void setupGrid() {
}

@Test
public void shouldBeAbleToGetGridUrl() {
GraphqlHandler handler = new GraphqlHandler(distributor, publicUrl);
public void shouldBeAbleToGetGridUri() {
GraphqlHandler handler = new GraphqlHandler(distributor, publicUri);

Map<String, Object> topLevel = executeQuery(handler, "query { grid { url } }");
Map<String, Object> topLevel = executeQuery(handler, "query { grid { uri } }");

assertThat(topLevel).isEqualTo(Map.of("data", Map.of("grid", Map.of("url", publicUrl))));
assertThat(topLevel).isEqualTo(Map.of("data", Map.of("grid", Map.of("uri", publicUri))));
}

@Test
public void shouldReturnAnEmptyListForNodesIfNoneAreRegistered() {
GraphqlHandler handler = new GraphqlHandler(distributor, publicUrl);
GraphqlHandler handler = new GraphqlHandler(distributor, publicUri);

Map<String, Object> topLevel = executeQuery(handler, "query { grid { nodes { uri } } }");

Expand All @@ -92,7 +95,7 @@ public void shouldReturnAnEmptyListForNodesIfNoneAreRegistered() {
public void shouldBeAbleToGetUrlsOfAllNodes() throws URISyntaxException {
Capabilities stereotype = new ImmutableCapabilities("cheese", "stilton");
String nodeUri = "http://localhost:5556";
Node node = LocalNode.builder(tracer, events, new URI(nodeUri), new URI(publicUrl), null)
Node node = LocalNode.builder(tracer, events, new URI(nodeUri), publicUri, null)
.add(stereotype, new SessionFactory() {
@Override
public Optional<ActiveSession> apply(CreateSessionRequest createSessionRequest) {
Expand All @@ -105,9 +108,9 @@ public boolean test(Capabilities capabilities) {
}
})
.build();
distributor.add(node);
distributor.add(node);String

GraphqlHandler handler = new GraphqlHandler(distributor, publicUrl);
GraphqlHandler handler = new GraphqlHandler(distributor, publicUri);
Map<String, Object> topLevel = executeQuery(handler, "query { grid { nodes { uri } } }");

assertThat(topLevel)
Expand Down