-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
### What changes were proposed in this pull request? + Add ServerConfigTest to keep consistency check in the graviton.conf file. + Use the JAVA reflect method to get all property names from `ServerConfig.java` and `Configs.java` files. ### Why are the changes needed? Because property names in the `graviton.conf.template` file need to be kept consistent with `com/datastrato/graviton/Configs.java` and `com/datastrato/graviton/server/ServerConfig.java`, Otherwise, Graviton will not be able to read `graviton.conf` file correctly. Fix: #390 ### Does this PR introduce _any_ user-facing change? N/A ### How was this patch tested? Added `ServerConfigTest` in the test cases.
- Loading branch information
Showing
3 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
server/src/test/java/com/datastrato/graviton/server/TestServerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2023 Datastrato. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
package com.datastrato.graviton.server; | ||
|
||
import com.datastrato.graviton.Configs; | ||
import com.datastrato.graviton.config.ConfigEntry; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.reflect.Field; | ||
import java.nio.file.Files; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class TestServerConfig { | ||
@Test | ||
public void checkGravitonConfFile() | ||
throws NoSuchFieldException, IllegalAccessException, IOException { | ||
// Load all program config keys from `ServerConfig` and `Configs` into a map | ||
Map<String, String> configKeyMap = new HashMap<>(); | ||
configKeyMap.putAll(getConfigEntryFromClass(ServerConfig.class)); | ||
configKeyMap.putAll(getConfigEntryFromClass(Configs.class)); | ||
|
||
// Load all config keys from `graviton.conf.template` into a map | ||
Properties properties = new Properties(); | ||
String confFile = | ||
System.getenv("GRAVITON_ROOT_DIR") | ||
+ File.separator | ||
+ "conf" | ||
+ File.separator | ||
+ "graviton.conf.template"; | ||
InputStream in = Files.newInputStream(new File(confFile).toPath()); | ||
properties.load(in); | ||
|
||
// Check if all config keys from `graviton.conf.template` are defined in `ServerConfig` and | ||
// `Configs` | ||
for (Map.Entry<Object, Object> entry : properties.entrySet()) { | ||
String propKey = (String) entry.getKey(); | ||
Assertions.assertTrue( | ||
configKeyMap.containsKey(propKey), | ||
"Config key " + propKey + " is not defined in ConfigEntry"); | ||
} | ||
} | ||
|
||
// Get all ConfigEntry member variables from a config class | ||
private Map<String, String> getConfigEntryFromClass(Class<?> configClazz) | ||
throws NoSuchFieldException, IllegalAccessException { | ||
Map<String, String> configKeyMap = new HashMap<>(); | ||
// Get all fields | ||
Field[] fields = configClazz.getDeclaredFields(); | ||
for (Field field : fields) { | ||
String fieldName = field.getName(); | ||
Class<?> fieldType = field.getType(); | ||
|
||
if (!(fieldType == ConfigEntry.class)) { | ||
continue; | ||
} | ||
Field memberConfigEntry = configClazz.getDeclaredField(fieldName); | ||
memberConfigEntry.setAccessible(true); | ||
|
||
// Get all ConfigEntry member variables | ||
ConfigEntry<?> configEntry = (ConfigEntry<?>) memberConfigEntry.get(null); | ||
String configEntryKey = configEntry.getKey(); | ||
configKeyMap.put(configEntryKey, fieldName); | ||
} | ||
return configKeyMap; | ||
} | ||
} |