From 3dadbde5eabd5de14c68add442a1eb835598e703 Mon Sep 17 00:00:00 2001 From: Mikko Karjalainen Date: Fri, 3 Aug 2018 17:31:24 +0100 Subject: [PATCH] Remove SystemSettings & related classes. --- .../styx/api/configuration/Setting.java | 42 ----- .../api/configuration/SystemSettings.java | 162 ------------------ .../styx/api/configuration/LocationTest.java | 52 ------ .../styx/api/configuration/StringTest.java | 38 ---- .../api/configuration/SystemSettingsTest.java | 48 ------ 5 files changed, 342 deletions(-) delete mode 100644 components/api/src/main/java/com/hotels/styx/api/configuration/Setting.java delete mode 100644 components/api/src/main/java/com/hotels/styx/api/configuration/SystemSettings.java delete mode 100644 components/api/src/test/java/com/hotels/styx/api/configuration/LocationTest.java delete mode 100644 components/api/src/test/java/com/hotels/styx/api/configuration/StringTest.java delete mode 100644 components/api/src/test/java/com/hotels/styx/api/configuration/SystemSettingsTest.java diff --git a/components/api/src/main/java/com/hotels/styx/api/configuration/Setting.java b/components/api/src/main/java/com/hotels/styx/api/configuration/Setting.java deleted file mode 100644 index 2f429684b5..0000000000 --- a/components/api/src/main/java/com/hotels/styx/api/configuration/Setting.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - Copyright (C) 2013-2018 Expedia Inc. - - Licensed 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 com.hotels.styx.api.configuration; - -import java.util.Optional; - -/** - * Represents a System environment variable necessary for bootstrapping the application. - * - * @param type of the system environment value - * @see SystemSettings.String - * @see SystemSettings.Location - */ -public interface Setting { - - /** - * Return the key of the setting. - * - * @return the key of the setting - */ - String name(); - - /** - * Return the value defined using the system variable. - * - * @return the value defined using the system variable - */ - Optional value(); -} diff --git a/components/api/src/main/java/com/hotels/styx/api/configuration/SystemSettings.java b/components/api/src/main/java/com/hotels/styx/api/configuration/SystemSettings.java deleted file mode 100644 index 930bab8321..0000000000 --- a/components/api/src/main/java/com/hotels/styx/api/configuration/SystemSettings.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - Copyright (C) 2013-2018 Expedia Inc. - - Licensed 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 com.hotels.styx.api.configuration; - - -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Optional; - -import static com.hotels.styx.api.io.ResourceFactory.newResource; -import static java.lang.String.format; -import static java.nio.file.Files.isReadable; - -/** - * System Settings for Styx proxy. - */ -public final class SystemSettings { - private SystemSettings() { - } - - /** - * Return the value associated with the setting. - * - * @param setting a setting - * @param setting value type - * @return the value associated with the setting - * @throws com.hotels.styx.api.configuration.NoSystemPropertyDefined if there is no associated value - */ - public static T valueOf(Setting setting) { - return setting.value().orElseThrow(() -> new NoSystemPropertyDefined(setting.name())); - } - - /** - * Return the value associated with the setting, or default value if no value is associated. - * - * @param setting a setting - * @param defaultValue default value - * @param setting value type - * @return the value associated with the setting - */ - public static T valueOf(Setting setting, T defaultValue) { - return setting.value().orElse(defaultValue); - } - - /** - * Convenient class for implementing settings. - * - * @param type of the value to read from system property - */ - public abstract static class SystemSetting implements Setting { - private final java.lang.String systemVariable; - - /** - * Constructs a system setting from the specified system variable. - * - * @param systemVariable the system variable - */ - protected SystemSetting(java.lang.String systemVariable) { - this.systemVariable = systemVariable; - } - - @Override - public Optional value() { - java.lang.String value = System.getProperty(systemVariable); - - return Optional.ofNullable(value).map(this::convert); - } - - @Override - public java.lang.String name() { - return systemVariable; - } - - /** - * Convert the value to the target type T. - * - * @param value the value to convert - * @return the value converted to the target type - */ - protected abstract T convert(java.lang.String value); - } - - /** - * A System setting representing a {@link com.hotels.styx.api.Resource}. - */ - public static class Resource extends SystemSetting { - /** - * Create a Resource setting from the specified systemVariable. - * - * @param systemVariable the variable name to read from - */ - public Resource(java.lang.String systemVariable) { - super(systemVariable); - } - - @Override - protected com.hotels.styx.api.Resource convert(java.lang.String value) { - return newResource(value); - } - } - - /** - * A System setting representing a file/directory location. - */ - public static class Location extends SystemSetting { - /** - * Create a Location setting from the specified systemVariable. - * - * @param systemVariable the variable name to read from - */ - public Location(java.lang.String systemVariable) { - super(systemVariable); - } - - @Override - protected Path convert(java.lang.String value) { - Path location = Paths.get(removeFilePrefix(value)); - - if (!isReadable(location)) { - throw new ConfigurationException(format("%s=%s is not a readable configuration path.", name(), location)); - } - - return location; - } - - private java.lang.String removeFilePrefix(java.lang.String value) { - return value.replaceFirst("file:", ""); - } - } - - /** - * Simple string-type System Setting. - */ - public static class String extends SystemSetting { - /** - * Creates a String system setting from the specified systemVariable. - * - * @param systemVariable the variable name to read from - */ - public String(java.lang.String systemVariable) { - super(systemVariable); - } - - @Override - protected java.lang.String convert(java.lang.String value) { - return value; - } - } -} diff --git a/components/api/src/test/java/com/hotels/styx/api/configuration/LocationTest.java b/components/api/src/test/java/com/hotels/styx/api/configuration/LocationTest.java deleted file mode 100644 index 1fe1746e1a..0000000000 --- a/components/api/src/test/java/com/hotels/styx/api/configuration/LocationTest.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - Copyright (C) 2013-2018 Expedia Inc. - - Licensed 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 com.hotels.styx.api.configuration; - -import org.testng.annotations.AfterTest; -import org.testng.annotations.Test; - -import static com.hotels.styx.support.ClassPathResourceUtils.getResource; -import static com.hotels.styx.support.matchers.IsOptional.isPresent; -import static java.lang.System.setProperty; -import static org.hamcrest.MatcherAssert.assertThat; - -public class LocationTest { - - private static final String CONFIG_PATH = "CONFIG_PATH"; - - @AfterTest - protected void clearProperty() { - System.clearProperty(CONFIG_PATH); - } - - @Test(expectedExceptions = ConfigurationException.class) - public void failsIfTheLocationSpecifiedIsNotReadableFilePath() { - setProperty(CONFIG_PATH, "/path/that/does/not/exist"); - SystemSettings.Location location = new SystemSettings.Location(CONFIG_PATH); - location.value(); - } - - @Test - public void returnsTheConfiguredLocationPath() { - setProperty(CONFIG_PATH, logPath("/conf/logback/logback.xml")); - SystemSettings.Location location = new SystemSettings.Location(CONFIG_PATH); - assertThat(location.value(), isPresent()); - } - - private String logPath(String name) { - return getResource(LocationTest.class, name); - } -} \ No newline at end of file diff --git a/components/api/src/test/java/com/hotels/styx/api/configuration/StringTest.java b/components/api/src/test/java/com/hotels/styx/api/configuration/StringTest.java deleted file mode 100644 index e01a897f1d..0000000000 --- a/components/api/src/test/java/com/hotels/styx/api/configuration/StringTest.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - Copyright (C) 2013-2018 Expedia Inc. - - Licensed 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 com.hotels.styx.api.configuration; - -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; - -public class StringTest { - static final String SYSTEM_VAR = "SYSTEM_VAR"; - - @BeforeMethod - public void clearSystemProperties() { - System.clearProperty(SYSTEM_VAR); - } - - @Test - public void returnsTheSystemEnvAsString() { - System.setProperty(SYSTEM_VAR, "someValue"); - SystemSettings.String stringSetting = new SystemSettings.String(SYSTEM_VAR); - assertThat(stringSetting.value().get(), is("someValue")); - } -} diff --git a/components/api/src/test/java/com/hotels/styx/api/configuration/SystemSettingsTest.java b/components/api/src/test/java/com/hotels/styx/api/configuration/SystemSettingsTest.java deleted file mode 100644 index 9614e65854..0000000000 --- a/components/api/src/test/java/com/hotels/styx/api/configuration/SystemSettingsTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - Copyright (C) 2013-2018 Expedia Inc. - - Licensed 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 com.hotels.styx.api.configuration; - -import org.testng.annotations.Test; - -import static java.lang.System.clearProperty; -import static java.lang.System.setProperty; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; - -public class SystemSettingsTest { - static final Setting ENVIRONMENT = new SystemSettings.String("CONFIG_ENV"); - - @Test(expectedExceptions = NoSystemPropertyDefined.class, expectedExceptionsMessageRegExp = "No system property CONFIG_ENV has been defined.") - public void failsIfTheSystemSettingIsNotSet() { - SystemSettings.valueOf(ENVIRONMENT); - } - - @Test - public void returnsTheDefaultValueIfTheSystemSettingIsNotSet() { - assertThat(SystemSettings.valueOf(ENVIRONMENT, "test-lon"), is("test-lon")); - } - - @Test - public void returnsTheSystemSettingConfigured() { - setProperty("CONFIG_ENV", "test-lon"); - - try { - assertThat(SystemSettings.valueOf(ENVIRONMENT), is("test-lon")); - } finally { - clearProperty("CONFIG_ENV"); - } - } -} \ No newline at end of file