Skip to content

Commit

Permalink
[grid] Add support for configs to be from JSON files
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Mar 30, 2020
1 parent 0173ece commit b61e170
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ java_library(
"//java/server/test/org/openqa/selenium:__subpackages__",
],
deps = [
"//java/client/src/org/openqa/selenium/json",
artifact("com.google.guava:guava"),
artifact("io.ous:jtoml"),
],
Expand Down
60 changes: 60 additions & 0 deletions java/server/src/org/openqa/selenium/grid/config/JsonConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.config;

import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonException;

import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static org.openqa.selenium.json.Json.MAP_TYPE;

public class JsonConfig implements Config {

private static final Json JSON = new Json();
private final Config delegate;

public JsonConfig(Reader reader) {
Objects.requireNonNull(reader, "JSON source must be set.");

try {
delegate = new MapConfig(JSON.toType(reader, MAP_TYPE));
} catch (JsonException e) {
throw new ConfigException("Unable to parse input", e);
}
}

public static Config from(Path path) {
try (Reader reader = Files.newBufferedReader(path)) {
return new JsonConfig(reader);
} catch (IOException e) {
throw new ConfigException("Unable to read JSON.", e);
}
}

@Override
public Optional<List<String>> getAll(String section, String option) {
return delegate.getAll(section, option);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.config;

import org.junit.Test;

import java.io.StringReader;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

public class JsonConfigTest {

@Test
public void shouldUseATableAsASection() {
String raw = "{\"cheeses\": {\"selected\": \"brie\"}}";
Config config = new JsonConfig(new StringReader(raw));

assertThat(config.get("cheeses", "selected")).isEqualTo(Optional.of("brie"));
}
}

0 comments on commit b61e170

Please sign in to comment.