Skip to content

Commit

Permalink
Manage all hardcode properties in one class + Add tests to ensure all…
Browse files Browse the repository at this point in the history
… hardcoded properties exist #1438
  • Loading branch information
JamesChenX committed Apr 17, 2024
1 parent a4bbbf1 commit 9158d5d
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@
import im.turms.server.common.infra.property.env.common.logging.FileLoggingProperties;
import im.turms.server.common.infra.property.env.common.logging.LoggingProperties;

import static im.turms.server.common.infra.property.HardcodedPropertyNameConst.TURMS_CLUSTER_NODE_ID;
import static im.turms.server.common.infra.property.HardcodedPropertyNameConst.TURMS_LOGGING_CONSOLE_ENABLED;
import static im.turms.server.common.infra.property.HardcodedPropertyNameConst.TURMS_LOGGING_CONSOLE_LEVEL;
import static im.turms.server.common.infra.property.HardcodedPropertyNameConst.TURMS_LOGGING_FILE_COMPRESSION_ENABLED;
import static im.turms.server.common.infra.property.HardcodedPropertyNameConst.TURMS_LOGGING_FILE_ENABLED;
import static im.turms.server.common.infra.property.HardcodedPropertyNameConst.TURMS_LOGGING_FILE_FILE_PATH;
import static im.turms.server.common.infra.property.HardcodedPropertyNameConst.TURMS_LOGGING_FILE_LEVEL;
import static im.turms.server.common.infra.property.HardcodedPropertyNameConst.TURMS_LOGGING_FILE_MAX_FILES;
import static im.turms.server.common.infra.property.HardcodedPropertyNameConst.TURMS_LOGGING_FILE_MAX_FILE_SIZE_MB;

/**
* @author James Chen
*/
Expand Down Expand Up @@ -99,34 +109,34 @@ private void configureContextForLogging(ApplicationEnvironmentPreparedEvent even
+ applicationClassName);
}
};
Node.initNodeId(env.getProperty("turms.cluster.node.id", String.class));
Node.initNodeId(env.getProperty(TURMS_CLUSTER_NODE_ID, String.class));

ConsoleLoggingProperties consoleLoggingProperties = ConsoleLoggingProperties.builder()
.enabled(env.getProperty("turms.logging.console.enabled",
.enabled(env.getProperty(TURMS_LOGGING_CONSOLE_ENABLED,
Boolean.class,
ConsoleLoggingProperties.DEFAULT_VALUE_ENABLED))
.level(env.getProperty("turms.logging.console.level",
.level(env.getProperty(TURMS_LOGGING_CONSOLE_LEVEL,
LogLevel.class,
ConsoleLoggingProperties.DEFAULT_VALUE_LEVEL))
.build();
FileLoggingProperties fileLoggingProperties = FileLoggingProperties.builder()
.enabled(env.getProperty("turms.logging.file.enabled",
.enabled(env.getProperty(TURMS_LOGGING_FILE_ENABLED,
Boolean.class,
FileLoggingProperties.DEFAULT_VALUE_ENABLED))
.level(env.getProperty("turms.logging.file.level",
.level(env.getProperty(TURMS_LOGGING_FILE_LEVEL,
LogLevel.class,
FileLoggingProperties.DEFAULT_VALUE_LEVEL))
.filePath(env.getProperty("turms.logging.file.filePath",
.filePath(env.getProperty(TURMS_LOGGING_FILE_FILE_PATH,
String.class,
FileLoggingProperties.DEFAULT_VALUE_FILE_PATH))
.maxFiles(env.getProperty("turms.logging.file.maxFiles",
.maxFiles(env.getProperty(TURMS_LOGGING_FILE_MAX_FILES,
Integer.class,
FileLoggingProperties.DEFAULT_VALUE_MAX_FILES))
.maxFileSizeMb(env.getProperty("turms.logging.file.maxFileSizeMb",
.maxFileSizeMb(env.getProperty(TURMS_LOGGING_FILE_MAX_FILE_SIZE_MB,
Integer.class,
FileLoggingProperties.DEFAULT_VALUE_FILE_SIZE_MB))
.compression(new FileLoggingCompressionProperties(
env.getProperty("turms.logging.file.compression.enabled",
env.getProperty(TURMS_LOGGING_FILE_COMPRESSION_ENABLED,
Boolean.class,
FileLoggingCompressionProperties.DEFAULT_VALUE_ENABLED)))
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2019 The Turms Project
* https://github.com/turms-im/turms
*
* 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.
*/

package im.turms.server.common.infra.property;

/**
* Define all hardcoded property names here to manage them in one place.
* <p>
* We need these hardcoded property names because we need to use them before Spring Boot prepares
* properties.
*
* @author James Chen
*/
public final class HardcodedPropertyNameConst {

public static final String TURMS_CLUSTER_NODE_ID = "turms.cluster.node.id";

public static final String TURMS_LOGGING_CONSOLE_ENABLED = "turms.logging.console.enabled";
public static final String TURMS_LOGGING_CONSOLE_LEVEL = "turms.logging.console.level";
public static final String TURMS_LOGGING_FILE_ENABLED = "turms.logging.file.enabled";
public static final String TURMS_LOGGING_FILE_LEVEL = "turms.logging.file.level";
public static final String TURMS_LOGGING_FILE_FILE_PATH = "turms.logging.file.file-path";
public static final String TURMS_LOGGING_FILE_MAX_FILES = "turms.logging.file.max-files";
public static final String TURMS_LOGGING_FILE_MAX_FILE_SIZE_MB =
"turms.logging.file.max-file-size-mb";
public static final String TURMS_LOGGING_FILE_COMPRESSION_ENABLED =
"turms.logging.file.compression.enabled";

private HardcodedPropertyNameConst() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (C) 2019 The Turms Project
* https://github.com/turms-im/turms
*
* 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.
*/

package unit.im.turms.server.common.infra.property;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Optional;

import org.junit.jupiter.api.Test;

import im.turms.server.common.infra.lang.ClassUtil;
import im.turms.server.common.infra.lang.StringUtil;
import im.turms.server.common.infra.property.HardcodedPropertyNameConst;
import im.turms.server.common.infra.property.TurmsProperties;

/**
* @author James Chen
*/
class HardcodedPropertyNameConstTests {

@Test
void allPropertiesShouldBePresent() {
Field[] fields = HardcodedPropertyNameConst.class.getDeclaredFields();
for (Field field : fields) {
testField(field);
}
}

private static void testField(Field field) {
int modifiers = field.getModifiers();
if (!Modifier.isStatic(modifiers)) {
return;
}
Object value;
try {
value = field.get(null);
} catch (IllegalAccessException e) {
throw new RuntimeException(
"Failed to get the value of: "
+ field.getName(),
e);
}
if (!(value instanceof String valueStr)) {
throw new RuntimeException(
"The field value is not a string: "
+ field.getName());
}
String[] parts = valueStr.split("\\.");
String firstPart = parts[0];
if (!TurmsProperties.PROPERTIES_PREFIX.equals(firstPart)) {
throw new RuntimeException(
"The first part of the value of the field ("
+ field.getName()
+ ") is not \""
+ TurmsProperties.PROPERTIES_PREFIX
+ "\". Actual: \""
+ firstPart
+ "\"");
}
int length = parts.length;
Class<?> currentPropertyClass = TurmsProperties.class;
for (int i = 1; i < length; i++) {
String part = parts[i];
Optional<Field> matchedField = ClassUtil.getNonStaticFields(currentPropertyClass)
.stream()
.filter(f -> StringUtil.lowerCamelToLowerHyphenLatin1(f.getName())
.equals(part))
.findFirst();
if (matchedField.isEmpty()) {
throw new RuntimeException(
"The value of the field ("
+ field.getName()
+ ") is not found");
}
currentPropertyClass = matchedField.get()
.getType();
}
}
}

0 comments on commit 9158d5d

Please sign in to comment.