From 52c504e20aaa2381abd5d87968c2863f8e85f288 Mon Sep 17 00:00:00 2001 From: Oleksandr Vayda Date: Wed, 27 Jan 2021 23:36:11 +0100 Subject: [PATCH] commons #54 Configuration utils: EnvironmentConfiguration extension to support UPPER_SNAKE_CASE for environment variables --- ...perSnakeCaseEnvironmentConfiguration.scala | 38 ++++++++++++ ...nakeCaseEnvironmentConfigurationSpec.scala | 58 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/main/scala/za/co/absa/commons/config/UpperSnakeCaseEnvironmentConfiguration.scala create mode 100644 src/test/scala/za/co/absa/commons/config/UpperSnakeCaseEnvironmentConfigurationSpec.scala diff --git a/src/main/scala/za/co/absa/commons/config/UpperSnakeCaseEnvironmentConfiguration.scala b/src/main/scala/za/co/absa/commons/config/UpperSnakeCaseEnvironmentConfiguration.scala new file mode 100644 index 0000000..63ba292 --- /dev/null +++ b/src/main/scala/za/co/absa/commons/config/UpperSnakeCaseEnvironmentConfiguration.scala @@ -0,0 +1,38 @@ +/* + * Copyright 2021 ABSA Group Limited + * + * 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 za.co.absa.commons.config + +import org.apache.commons.configuration.EnvironmentConfiguration +import org.apache.commons.lang.StringUtils +import za.co.absa.commons.config.UpperSnakeCaseEnvironmentConfiguration.toUpperSnake + +class UpperSnakeCaseEnvironmentConfiguration extends EnvironmentConfiguration { + + override def getProperty(key: String): AnyRef = super.getProperty(toUpperSnake(key)) + + override def containsKey(key: String): Boolean = super.containsKey(toUpperSnake(key)) +} + +object UpperSnakeCaseEnvironmentConfiguration { + private def toUpperSnake(key: String) = { + key + .split("[\\W_]") + .flatMap(StringUtils.splitByCharacterTypeCamelCase) + .map(_.toUpperCase) + .mkString("_") + } +} diff --git a/src/test/scala/za/co/absa/commons/config/UpperSnakeCaseEnvironmentConfigurationSpec.scala b/src/test/scala/za/co/absa/commons/config/UpperSnakeCaseEnvironmentConfigurationSpec.scala new file mode 100644 index 0000000..967b6c9 --- /dev/null +++ b/src/test/scala/za/co/absa/commons/config/UpperSnakeCaseEnvironmentConfigurationSpec.scala @@ -0,0 +1,58 @@ +/* + * Copyright 2021 ABSA Group Limited + * + * 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 za.co.absa.commons.config + +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import za.co.absa.commons.scalatest.EnvFixture + +class UpperSnakeCaseEnvironmentConfigurationSpec + extends AnyFlatSpec + with Matchers + with EnvFixture { + + it should "act as normal when given a key in format 'MY_VAR_NAME'" in { + setEnv("FOO_BAR_BAZ_42_QUX", "right") + (new UpperSnakeCaseEnvironmentConfiguration).containsKey("FOO_BAR_BAZ_42_QUX") should be(true) + (new UpperSnakeCaseEnvironmentConfiguration).getString("FOO_BAR_BAZ_42_QUX") should equal("right") + } + + it should "ignore lower-case keys" in { + setEnv("foo.barBaz42.Qux", "wrong") + (new UpperSnakeCaseEnvironmentConfiguration).containsKey("foo.barBaz42.Qux") should be(false) + (new UpperSnakeCaseEnvironmentConfiguration).getString("foo.barBaz42.Qux") should be(null) + } + + it should "convert keys per the environment variable naming convention, i.e 'my.varName' to 'MY_VAR_NAME'" in { + setEnv("FOO_BAR_BAZ_42_QUX", "right") + (new UpperSnakeCaseEnvironmentConfiguration).containsKey("foo.barBaz42.Qux") should be(true) + (new UpperSnakeCaseEnvironmentConfiguration).getString("foo.barBaz42.Qux") should equal("right") + } + + it should "fix issue Spline-616" in { + setEnv("SPLINE_DATABASE_CONNECTION_URL", "right") + (new UpperSnakeCaseEnvironmentConfiguration).containsKey("spline.database.connectionUrl") should be(true) + (new UpperSnakeCaseEnvironmentConfiguration).getString("spline.database.connectionUrl") should equal("right") + } + + it should "return null if non of the keys found" in { + (new UpperSnakeCaseEnvironmentConfiguration).containsKey("foo.barBaz42.Qux") should be(false) + (new UpperSnakeCaseEnvironmentConfiguration).getString("foo.barBaz42.Qux") should be(null) + } + +} +