Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import java.util.Optional;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.javaoperatorsdk.operator.config.loader.ConfigProvider;

/**
Expand All @@ -34,6 +37,8 @@
*/
public class PropertiesConfigProvider implements ConfigProvider {

private static final Logger log = LoggerFactory.getLogger(PropertiesConfigProvider.class);

private final Properties properties;

/** Returns a {@link PropertiesConfigProvider} backed by {@link System#getProperties()}. */
Expand All @@ -44,7 +49,18 @@ public static PropertiesConfigProvider systemProperties() {
/**
* Loads properties from the given file path.
*
* @throws UncheckedIOException if the file cannot be read
* @throws UncheckedIOException if the file cannot be read. Does not throw an exception if the
* file does not exist.
*/
public PropertiesConfigProvider(String path) {
this(Path.of(path));
}

/**
* Loads properties from the given file path.
*
* @throws UncheckedIOException if the file cannot be read. Does not throw an exception if the
* file does not exist.
*/
public PropertiesConfigProvider(Path path) {
this.properties = load(path);
Expand All @@ -68,6 +84,11 @@ public <T> Optional<T> getValue(String key, Class<T> type) {
}

private static Properties load(Path path) {
if (!Files.exists(path)) {
log.warn("{} does not exist", path);
return new Properties();
}

try (InputStream in = Files.newInputStream(path)) {
Properties props = new Properties();
props.load(in);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import java.util.Map;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.javaoperatorsdk.operator.config.loader.ConfigProvider;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -39,14 +42,27 @@
*/
public class YamlConfigProvider implements ConfigProvider {

private static final Logger log = LoggerFactory.getLogger(YamlConfigProvider.class);

private static final ObjectMapper MAPPER = new ObjectMapper(new YAMLFactory());

private final Map<String, Object> data;

/**
* Loads YAML from the given file path.
*
* @throws UncheckedIOException if the file cannot be read
* @throws UncheckedIOException if the file cannot be read. Does not throw an exception if the
* file does not exist.
*/
public YamlConfigProvider(String path) {
this(Path.of(path));
}

/**
* Loads YAML from the given file path.
*
* @throws UncheckedIOException if the file cannot be read. Does not throw an exception if the
* file does not exist.
*/
public YamlConfigProvider(Path path) {
this.data = load(path);
Expand Down Expand Up @@ -79,6 +95,11 @@ public <T> Optional<T> getValue(String key, Class<T> type) {

@SuppressWarnings("unchecked")
private static Map<String, Object> load(Path path) {
if (!Files.exists(path)) {
log.warn("{} does not exist", path);
return Map.of();
}

try (InputStream in = Files.newInputStream(path)) {
Map<String, Object> result = MAPPER.readValue(in, Map.class);
return result != null ? result : Map.of();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package io.javaoperatorsdk.operator.config.loader.provider;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
Expand All @@ -27,7 +26,6 @@
import org.junit.jupiter.api.io.TempDir;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

class PropertiesConfigProviderTest {
Expand Down Expand Up @@ -120,10 +118,9 @@ void loadsFromFile(@TempDir Path dir) throws IOException {
}

@Test
void throwsUncheckedIOExceptionForMissingFile(@TempDir Path dir) {
void returnsEmptyForNonExistingFile(@TempDir Path dir) {
Path missing = dir.resolve("does-not-exist.properties");
assertThatExceptionOfType(UncheckedIOException.class)
.isThrownBy(() -> new PropertiesConfigProvider(missing))
.withMessageContaining("does-not-exist.properties");
var provider = new PropertiesConfigProvider(missing);
assertThat(provider.getValue("any.key", String.class)).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package io.javaoperatorsdk.operator.config.loader.provider;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
Expand All @@ -27,7 +26,6 @@
import org.junit.jupiter.api.io.TempDir;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

class YamlConfigProviderTest {
Expand Down Expand Up @@ -135,10 +133,9 @@ void loadsFromFile(@TempDir Path dir) throws IOException {
}

@Test
void throwsUncheckedIOExceptionForMissingFile(@TempDir Path dir) {
void returnsEmptyForNonExistingFile(@TempDir Path dir) {
Path missing = dir.resolve("does-not-exist.yaml");
assertThatExceptionOfType(UncheckedIOException.class)
.isThrownBy(() -> new YamlConfigProvider(missing))
.withMessageContaining("does-not-exist.yaml");
var provider = new YamlConfigProvider(missing);
assertThat(provider.getValue("any.key", String.class)).isEmpty();
}
}
Loading