Skip to content

Commit

Permalink
Simple config API
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Apr 9, 2024
1 parent 71d36fa commit 27b78d0
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- simple-config ConfigParams
- simple-config-microprofile ConfigParamsMicroprofile

## [1.1.0 - 2024-04-05]

### Added
Expand Down
21 changes: 21 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
<native-helper-graalvm-version>1.1.1</native-helper-graalvm-version>
<jakarta-ws-rs-api-version>3.1.0</jakarta-ws-rs-api-version>
<microprofile-openapi-api-version>3.1.1</microprofile-openapi-api-version>
<microprofile-config-api-version>3.1</microprofile-config-api-version>
<microprofile-rest-client-api-version>3.0.1</microprofile-rest-client-api-version>
<fj-service-helper-version>${project.version}</fj-service-helper-version>
<simple-config-version>${fj-service-helper-version}</simple-config-version>
<simple-config-microprofile-version>${fj-service-helper-version}</simple-config-microprofile-version>
<emp-service-model-version>${fj-service-helper-version}</emp-service-model-version>
<emp-exception-mapper-version>${fj-service-helper-version}</emp-exception-mapper-version>
<data-service-base-version>${fj-service-helper-version}</data-service-base-version>
Expand Down Expand Up @@ -135,12 +138,30 @@
<version>${microprofile-openapi-api-version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.microprofile.config</groupId>
<artifactId>microprofile-config-api</artifactId>
<version>${microprofile-config-api-version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.microprofile.rest.client</groupId>
<artifactId>microprofile-rest-client-api</artifactId>
<version>${microprofile-rest-client-api-version}</version>
</dependency>

<dependency>
<groupId>org.fugerit.java</groupId>
<artifactId>simple-config</artifactId>
<version>${simple-config-version}</version>
</dependency>

<dependency>
<groupId>org.fugerit.java</groupId>
<artifactId>simple-config-microprofile</artifactId>
<version>${simple-config-microprofile-version}</version>
</dependency>

<dependency>
<groupId>org.fugerit.java</groupId>
<artifactId>emp-service-model</artifactId>
Expand Down
12 changes: 12 additions & 0 deletions simple-config-microprofile/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
</build>

<dependencies>

<dependency>
<groupId>org.fugerit.java</groupId>
<artifactId>simple-config</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.microprofile.config</groupId>
<artifactId>microprofile-config-api</artifactId>
<scope>provided</scope>
</dependency>

</dependencies>

<organization>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.fugerit.java.simple.config.microprofile;

import org.eclipse.microprofile.config.ConfigProvider;
import org.fugerit.java.simple.config.ConfigParams;

import java.util.Optional;

public class ConfigParamsMicroprofile implements ConfigParams {


@Override
public String getValue(String name) {
return ConfigProvider.getConfig().getValue( name, String.class );
}

@Override
public Optional<String> getOptionalValue(String name) {
return ConfigProvider.getConfig().getOptionalValue( name, String.class );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package test.org.fugerit.java.simple.config.microprofile;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.ConfigValue;
import org.eclipse.microprofile.config.spi.ConfigBuilder;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.Converter;
import org.fugerit.java.simple.config.ConfigParams;
import org.fugerit.java.simple.config.ConfigParamsDefault;
import org.fugerit.java.simple.config.microprofile.ConfigParamsMicroprofile;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.Properties;

class TestConfigParamsMicroprofile {

@Test
void testConfigParamsMicroprofile() throws IOException {
Properties configProperties = new Properties();
try (InputStream is = new FileInputStream( "src/test/resources/testconfig.properties" ) ) {
configProperties.load( is );
}
ConfigParamsDefault configHelper = new ConfigParamsDefault( configProperties );
ConfigProviderResolver.setInstance(new ConfigProviderResolver() {
@Override
public Config getConfig() {
return new Config() {
@Override
public <T> T getValue(String s, Class<T> aClass) {
if ( aClass == String.class ) {
return (T) configHelper.getValue( s );
} else {
return null;
}
}
@Override
public ConfigValue getConfigValue(String s) {
return null;
}
@Override
public <T> Optional<T> getOptionalValue(String s, Class<T> aClass) {
if ( aClass == String.class ) {
return (Optional<T>) configHelper.getOptionalValue( s );
} else {
return null;
}
}
@Override
public Iterable<String> getPropertyNames() {
return null;
}
@Override
public Iterable<ConfigSource> getConfigSources() {
return null;
}
@Override
public <T> Optional<Converter<T>> getConverter(Class<T> aClass) {
return Optional.empty();
}
@Override
public <T> T unwrap(Class<T> aClass) {
return null;
}
};
}
@Override
public Config getConfig(ClassLoader classLoader) {
return null;
}
@Override
public ConfigBuilder getBuilder() {
return null;
}
@Override
public void registerConfig(Config config, ClassLoader classLoader) {
}
@Override
public void releaseConfig(Config config) {
}
});
// actual testing
ConfigParams config = new ConfigParamsMicroprofile();
String value1 = config.getValue( "testconfig.param1" );
Assertions.assertEquals( "value1", value1 );
Optional<String> value2 = config.getOptionalValue( "testconfig.param2" );
Assertions.assertEquals( "value2", value2.get() );
Optional<String> valueX = config.getOptionalValue( "testconfig.paramX" );
Assertions.assertFalse( valueX.isPresent() );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.slf4j.simpleLogger.defaultLogLevel=debug
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# config params default test params
testconfig.param1=value1
testconfig.param2=value2
testconfig.param3=value3
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.fugerit.java.simple.config;

import java.util.Optional;

public interface ConfigParams {

String getValue( String name );

Optional<String> getOptionalValue( String name );

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.fugerit.java.simple.config;

import lombok.extern.slf4j.Slf4j;

import java.util.Optional;
import java.util.Properties;

@Slf4j
public class ConfigParamsDefault implements ConfigParams {

private String configNamespace;

private Properties configProperties;

public ConfigParamsDefault(String configNamespace, Properties configProperties) {
this.configNamespace = configNamespace;
this.configProperties = configProperties;
}

public ConfigParamsDefault(Properties configProperties) {
this( "", configProperties );
}

@Override
public String getValue(String name) {
String key = this.configNamespace+name;
String value = this.configProperties.getProperty( this.configNamespace+name );
log.debug( "key : {}, value : {}", key, value );
return value;
}

@Override
public Optional<String> getOptionalValue(String name) {
String value = this.getValue( name );
if ( value == null ) {
return Optional.empty();
} else {
return Optional.of( value );
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package teat.org.fugerit.java.simple.config;

import org.fugerit.java.simple.config.ConfigParamsDefault;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.Properties;

class TestConfigParamsDefault {

@Test
void testConfigParams() throws IOException {
try (InputStream is = new FileInputStream( "src/test/resources/testconfig.properties" ) ) {
Properties configProperties = new Properties();
configProperties.load( is );
ConfigParamsDefault config = new ConfigParamsDefault( configProperties );
String value1 = config.getValue( "testconfig.param1" );
Assertions.assertEquals( "value1", value1 );
Optional<String> value2 = config.getOptionalValue( "testconfig.param2" );
Assertions.assertEquals( "value2", value2.get() );
Optional<String> valueX = config.getOptionalValue( "testconfig.paramX" );
Assertions.assertFalse( valueX.isPresent() );
}
}

}

1 change: 1 addition & 0 deletions simple-config/src/test/resources/simplelogger.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.slf4j.simpleLogger.defaultLogLevel=debug
4 changes: 4 additions & 0 deletions simple-config/src/test/resources/testconfig.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# config params default test params
testconfig.param1=value1
testconfig.param2=value2
testconfig.param3=value3

0 comments on commit 27b78d0

Please sign in to comment.