diff options
Diffstat (limited to 'core/utils/provider')
4 files changed, 163 insertions, 0 deletions
diff --git a/core/utils/provider/pom.xml b/core/utils/provider/pom.xml index e3a805f82..a94183479 100644 --- a/core/utils/provider/pom.xml +++ b/core/utils/provider/pom.xml @@ -46,4 +46,20 @@ <scope>test</scope> </dependency> </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <environmentVariables> + <MYSQL_USER>dummyUser</MYSQL_USER> + <MYSQL_PASSWORD>dummyPassword</MYSQL_PASSWORD> + <MYSQL_DATABASE>dummyDatabase</MYSQL_DATABASE> + </environmentVariables> + </configuration> + </plugin> + </plugins> + </build> </project> diff --git a/core/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/common/EnvProperties.java b/core/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/common/EnvProperties.java new file mode 100644 index 000000000..2e9f2673d --- /dev/null +++ b/core/utils/provider/src/main/java/org/onap/ccsdk/sli/core/utils/common/EnvProperties.java @@ -0,0 +1,61 @@ +package org.onap.ccsdk.sli.core.utils.common; + +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.util.Enumeration; +import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class EnvProperties extends Properties { + + @Override + public synchronized void load(Reader reader) throws IOException { + super.load(reader); + resolveAllValues(); + } + + @Override + public synchronized void load(InputStream inStream) throws IOException { + super.load(inStream); + resolveAllValues(); + } + + @Override + public synchronized Object setProperty(String key, String value) { + return super.setProperty(key, EnvProperties.resolveValue(value)); + } + + private void resolveAllValues() { + Enumeration<?> propNames = propertyNames(); + + while (propNames.hasMoreElements()) { + String propName = (String) propNames.nextElement(); + super.setProperty(propName, EnvProperties.resolveValue(getProperty(propName))); + } + + } + + public static String resolveValue(String value) { + if (value == null) { + return null; + } + + Pattern p = Pattern.compile("\\$\\{(\\w+)((?:\\:\\-)([^\\}]*))?\\}"); + Matcher m = p.matcher(value); + + StringBuffer sb = new StringBuffer(); + while (m.find()) { + String envVarName = null == m.group(1) ? m.group(2) : m.group(1); + String envVarDefault = null == m.group(3) ? "" : m.group(3); + String envVarValue = System.getenv(envVarName); + + m.appendReplacement(sb, + null == envVarValue ? Matcher.quoteReplacement(envVarDefault) : Matcher.quoteReplacement(envVarValue)); + } + m.appendTail(sb); + return sb.toString(); + + } +} diff --git a/core/utils/provider/src/test/java/org/onap/ccsdk/sli/core/utils/common/EnvPropertiesTest.java b/core/utils/provider/src/test/java/org/onap/ccsdk/sli/core/utils/common/EnvPropertiesTest.java new file mode 100644 index 000000000..bee432577 --- /dev/null +++ b/core/utils/provider/src/test/java/org/onap/ccsdk/sli/core/utils/common/EnvPropertiesTest.java @@ -0,0 +1,59 @@ +package org.onap.ccsdk.sli.core.utils.common; + +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; + +public class EnvPropertiesTest { + + @Test + public void loadStreamTest() throws IOException { + InputStream testStr = getClass().getResourceAsStream("/svclogic.properties"); + + Properties props = new EnvProperties(); + props.load(testStr); + + assertEquals("jdbc", props.getProperty("org.onap.ccsdk.sli.dbtype")); + String dbUrl = "jdbc:derby:memory:"+System.getenv("MYSQL_DATABASE")+";create=true"; + assertEquals(dbUrl, props.getProperty("org.onap.ccsdk.sli.jdbc.url")); + assertEquals("org.apache.derby.jdbc.EmbeddedDriver", props.getProperty("org.onap.ccsdk.sli.jdbc.driver")); + assertEquals(System.getenv("MYSQL_DATABASE"), props.getProperty("org.onap.ccsdk.sli.jdbc.database")); + assertEquals(System.getenv("MYSQL_USER"), props.getProperty("org.onap.ccsdk.sli.jdbc.user") ); + assertEquals(System.getenv("MYSQL_PASSWORD"), props.getProperty("org.onap.ccsdk.sli.jdbc.password")); + } + + @Test + public void loadReaderTest() throws IOException { + InputStream testStr = getClass().getResourceAsStream("/svclogic.properties"); + BufferedReader testReader = new BufferedReader(new InputStreamReader(testStr)); + + Properties props = new EnvProperties(); + props.load(testReader); + + assertEquals("jdbc", props.getProperty("org.onap.ccsdk.sli.dbtype")); + String dbUrl = "jdbc:derby:memory:"+System.getenv("MYSQL_DATABASE")+";create=true"; + assertEquals(dbUrl, props.getProperty("org.onap.ccsdk.sli.jdbc.url")); + assertEquals("org.apache.derby.jdbc.EmbeddedDriver", props.getProperty("org.onap.ccsdk.sli.jdbc.driver")); + assertEquals(System.getenv("MYSQL_DATABASE"), props.getProperty("org.onap.ccsdk.sli.jdbc.database")); + assertEquals(System.getenv("MYSQL_USER"), props.getProperty("org.onap.ccsdk.sli.jdbc.user")); + assertEquals(System.getenv("MYSQL_PASSWORD"), props.getProperty("org.onap.ccsdk.sli.jdbc.password")); + } + + @Test + public void setPropertyTest() { + Properties props = new EnvProperties(); + + props.setProperty("path", "${PATH}"); + props.setProperty("dummy", "${UNSET_DUMMY:-dummyvalue}"); + assertEquals(System.getenv("PATH"), props.getProperty("path")); + assertEquals("dummyvalue", props.getProperty("dummy")); + } + +} diff --git a/core/utils/provider/src/test/resources/svclogic.properties b/core/utils/provider/src/test/resources/svclogic.properties new file mode 100644 index 000000000..0d62389f2 --- /dev/null +++ b/core/utils/provider/src/test/resources/svclogic.properties @@ -0,0 +1,27 @@ +### +# ============LICENSE_START======================================================= +# ONAP : CCSDK +# ================================================================================ +# Copyright (C) 2017 AT&T Intellectual Property. All rights +# reserved. +# ================================================================================ +# 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. +# ============LICENSE_END========================================================= +### + +org.onap.ccsdk.sli.dbtype = jdbc +org.onap.ccsdk.sli.jdbc.url=jdbc:derby:memory:${MYSQL_DATABASE};create=true +org.onap.ccsdk.sli.jdbc.driver=${MYSQL_DRIVER:-org.apache.derby.jdbc.EmbeddedDriver} +org.onap.ccsdk.sli.jdbc.database = ${MYSQL_DATABASE} +org.onap.ccsdk.sli.jdbc.user = ${MYSQL_USER} +org.onap.ccsdk.sli.jdbc.password = ${MYSQL_PASSWORD} |