-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[grid] Add support for configs to be from JSON files
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
java/server/src/org/openqa/selenium/grid/config/JsonConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
java/server/test/org/openqa/selenium/grid/config/JsonConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |