Code-Optimierungen
This commit is contained in:
@@ -307,13 +307,13 @@ public class StartConfigurationValidator {
|
|||||||
* or exists and is a directory.
|
* or exists and is a directory.
|
||||||
*/
|
*/
|
||||||
private void validateOptionalExistingDirectory(Path directoryPath, String fieldName, List<String> errors) {
|
private void validateOptionalExistingDirectory(Path directoryPath, String fieldName, List<String> errors) {
|
||||||
if (directoryPath != null && !directoryPath.toString().isBlank() && Files.exists(directoryPath)) {
|
if (directoryPath != null
|
||||||
if (!Files.isDirectory(directoryPath)) {
|
&& !directoryPath.toString().isBlank()
|
||||||
errors.add("- " + fieldName + ": exists but is not a directory: " + directoryPath);
|
&& Files.exists(directoryPath)
|
||||||
}
|
&& !Files.isDirectory(directoryPath)) {
|
||||||
}
|
errors.add("- " + fieldName + ": exists but is not a directory: " + directoryPath);
|
||||||
// If it doesn't exist yet, that's acceptable - we don't auto-create
|
}
|
||||||
|
// If it doesn't exist yet, that's acceptable - we don't auto-create
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import java.nio.file.Path;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
import java.util.function.UnaryOperator;
|
||||||
|
|
||||||
import de.gecheckt.pdf.umbenenner.application.config.provider.MultiProviderConfiguration;
|
import de.gecheckt.pdf.umbenenner.application.config.provider.MultiProviderConfiguration;
|
||||||
import de.gecheckt.pdf.umbenenner.application.config.startup.StartConfiguration;
|
import de.gecheckt.pdf.umbenenner.application.config.startup.StartConfiguration;
|
||||||
@@ -45,7 +46,7 @@ public class PropertiesConfigurationPortAdapter implements ConfigurationPort {
|
|||||||
*
|
*
|
||||||
* @param environmentLookup a function that looks up environment variables by name
|
* @param environmentLookup a function that looks up environment variables by name
|
||||||
*/
|
*/
|
||||||
public PropertiesConfigurationPortAdapter(Function<String, String> environmentLookup) {
|
public PropertiesConfigurationPortAdapter(UnaryOperator<String> environmentLookup) {
|
||||||
this(environmentLookup, Paths.get(DEFAULT_CONFIG_FILE_PATH));
|
this(environmentLookup, Paths.get(DEFAULT_CONFIG_FILE_PATH));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +70,7 @@ public class PropertiesConfigurationPortAdapter implements ConfigurationPort {
|
|||||||
* @param environmentLookup a function that looks up environment variables by name
|
* @param environmentLookup a function that looks up environment variables by name
|
||||||
* @param configFilePath the path to the configuration properties file
|
* @param configFilePath the path to the configuration properties file
|
||||||
*/
|
*/
|
||||||
public PropertiesConfigurationPortAdapter(Function<String, String> environmentLookup, Path configFilePath) {
|
public PropertiesConfigurationPortAdapter(UnaryOperator<String> environmentLookup, Path configFilePath) {
|
||||||
this.environmentLookup = environmentLookup;
|
this.environmentLookup = environmentLookup;
|
||||||
this.configFilePath = configFilePath;
|
this.configFilePath = configFilePath;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
|||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.function.Function;
|
import java.util.function.UnaryOperator;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@@ -26,7 +26,7 @@ import de.gecheckt.pdf.umbenenner.application.config.provider.AiProviderFamily;
|
|||||||
*/
|
*/
|
||||||
class PropertiesConfigurationPortAdapterTest {
|
class PropertiesConfigurationPortAdapterTest {
|
||||||
|
|
||||||
private Function<String, String> emptyEnvLookup;
|
private UnaryOperator<String> emptyEnvLookup;
|
||||||
|
|
||||||
@TempDir
|
@TempDir
|
||||||
Path tempDir;
|
Path tempDir;
|
||||||
@@ -84,7 +84,7 @@ class PropertiesConfigurationPortAdapterTest {
|
|||||||
void loadConfiguration_rejectsBlankApiKeyWhenEnvVarIsNull() throws Exception {
|
void loadConfiguration_rejectsBlankApiKeyWhenEnvVarIsNull() throws Exception {
|
||||||
Path configFile = createConfigFile("no-api-key.properties");
|
Path configFile = createConfigFile("no-api-key.properties");
|
||||||
|
|
||||||
Function<String, String> envLookup = key -> null;
|
UnaryOperator<String> envLookup = key -> null;
|
||||||
|
|
||||||
PropertiesConfigurationPortAdapter adapter = new PropertiesConfigurationPortAdapter(envLookup, configFile);
|
PropertiesConfigurationPortAdapter adapter = new PropertiesConfigurationPortAdapter(envLookup, configFile);
|
||||||
|
|
||||||
@@ -98,7 +98,7 @@ class PropertiesConfigurationPortAdapterTest {
|
|||||||
void loadConfiguration_rejectsBlankApiKeyWhenEnvVarIsEmpty() throws Exception {
|
void loadConfiguration_rejectsBlankApiKeyWhenEnvVarIsEmpty() throws Exception {
|
||||||
Path configFile = createConfigFile("no-api-key.properties");
|
Path configFile = createConfigFile("no-api-key.properties");
|
||||||
|
|
||||||
Function<String, String> envLookup = key -> "";
|
UnaryOperator<String> envLookup = key -> "";
|
||||||
|
|
||||||
PropertiesConfigurationPortAdapter adapter = new PropertiesConfigurationPortAdapter(envLookup, configFile);
|
PropertiesConfigurationPortAdapter adapter = new PropertiesConfigurationPortAdapter(envLookup, configFile);
|
||||||
|
|
||||||
@@ -112,7 +112,7 @@ class PropertiesConfigurationPortAdapterTest {
|
|||||||
void loadConfiguration_rejectsBlankApiKeyWhenEnvVarIsBlank() throws Exception {
|
void loadConfiguration_rejectsBlankApiKeyWhenEnvVarIsBlank() throws Exception {
|
||||||
Path configFile = createConfigFile("no-api-key.properties");
|
Path configFile = createConfigFile("no-api-key.properties");
|
||||||
|
|
||||||
Function<String, String> envLookup = key -> " ";
|
UnaryOperator<String> envLookup = key -> " ";
|
||||||
|
|
||||||
PropertiesConfigurationPortAdapter adapter = new PropertiesConfigurationPortAdapter(envLookup, configFile);
|
PropertiesConfigurationPortAdapter adapter = new PropertiesConfigurationPortAdapter(envLookup, configFile);
|
||||||
|
|
||||||
@@ -126,7 +126,7 @@ class PropertiesConfigurationPortAdapterTest {
|
|||||||
void loadConfiguration_envVarOverridesPropertiesApiKey() throws Exception {
|
void loadConfiguration_envVarOverridesPropertiesApiKey() throws Exception {
|
||||||
Path configFile = createConfigFile("valid-config.properties");
|
Path configFile = createConfigFile("valid-config.properties");
|
||||||
|
|
||||||
Function<String, String> envLookup = key -> {
|
UnaryOperator<String> envLookup = key -> {
|
||||||
if (MultiProviderConfigurationParser.ENV_OPENAI_API_KEY.equals(key)) {
|
if (MultiProviderConfigurationParser.ENV_OPENAI_API_KEY.equals(key)) {
|
||||||
return "env-api-key-override";
|
return "env-api-key-override";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user