Skip to content

Commit

Permalink
Merge two GlobalNodePath (#34323)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Jan 12, 2025
1 parent e78d0bd commit 4475e3c
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,72 +30,139 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class GlobalNodePath {

private static final String RULES_NODE = "rules";
private static final String RULES_NODE = "/rules";

private static final String PROPS_NODE = "props";
private static final String PROPS_NODE = "/props";

private static final String VERSIONS = "versions";
private static final String VERSIONS_NODE = "versions";

private static final String ACTIVE_VERSION_SUFFIX = "/active_version$";
private static final String ACTIVE_VERSION_NODE = "active_version";

private static final String IDENTIFIER_PATTERN = "(\\w+)";

private static final String VERSION_PATTERN = "(\\d+)";

/**
* Get version.
* Get global rule root path.
*
* @param ruleName rule name
* @param rulePath rule path
* @return version
* @return global rule root path
*/
public static Optional<String> getVersion(final String ruleName, final String rulePath) {
Pattern pattern = Pattern.compile(getVersionsNode(ruleName) + "/(\\d+)$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(rulePath);
return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty();
public static String getRuleRootPath() {
return RULES_NODE;
}

private static String getVersionsNode(final String ruleName) {
return String.join("/", "", RULES_NODE, ruleName, VERSIONS);
/**
* Get global rule path.
*
* @param ruleTypeName rule type name
* @return global rule path
*/
public static String getRulePath(final String ruleTypeName) {
return String.join("/", getRuleRootPath(), ruleTypeName);
}

/**
* Is rule active version path.
* Get global rule versions path.
*
* @param rulePath rule path
* @return true or false
* @param ruleTypeName rule type name
* @return global rule versions path
*/
public static boolean isRuleActiveVersionPath(final String rulePath) {
Pattern pattern = Pattern.compile(getRuleNameNode() + "/(\\w+)" + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(rulePath);
return matcher.find();
public static String getRuleVersionsPath(final String ruleTypeName) {
return String.join("/", getRulePath(ruleTypeName), VERSIONS_NODE);
}

/**
* Is props active version path.
* Get global rule version path.
*
* @param propsPath props path
* @return true or false
* @param ruleTypeName rule type name
* @param version version
* @return global rule version path
*/
public static boolean isPropsActiveVersionPath(final String propsPath) {
Pattern pattern = Pattern.compile(getPropsNode() + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(propsPath);
return matcher.find();
public static String getRuleVersionPath(final String ruleTypeName, final String version) {
return String.join("/", getRuleVersionsPath(ruleTypeName), version);
}

/**
* Get global rule active version path.
*
* @param ruleTypeName rule type name
* @return global rule active version path
*/
public static String getRuleActiveVersionPath(final String ruleTypeName) {
return String.join("/", getRulePath(ruleTypeName), ACTIVE_VERSION_NODE);
}

/**
* Get properties path.
*
* @return properties path
*/
public static String getPropsRootPath() {
return PROPS_NODE;
}

/**
* Get properties versions path.
*
* @return properties versions path
*/
public static String getPropsVersionsPath() {
return String.join("/", getPropsRootPath(), VERSIONS_NODE);
}

/**
* Get properties version path.
*
* @param version version
* @return properties version path
*/
public static String getPropsVersionPath(final String version) {
return String.join("/", getPropsVersionsPath(), version);
}

/**
* Get properties active version path.
*
* @return properties active version path
*/
public static String getPropsActiveVersionPath() {
return String.join("/", getPropsRootPath(), ACTIVE_VERSION_NODE);
}

private static String getPropsNode() {
return String.join("/", "", PROPS_NODE);
/**
* Find rule type name from active version.
*
* @param path path to be found
* @return found rule type name
*/
public static Optional<String> findRuleTypeNameFromActiveVersion(final String path) {
Pattern pattern = Pattern.compile(getRuleActiveVersionPath(IDENTIFIER_PATTERN) + "$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty();
}

/**
* Get rule name.
* Find version.
*
* @param rulePath rule path
* @return rule name
* @param ruleTypeName rule type name
* @param path path to be found
* @return found version
*/
public static Optional<String> getRuleName(final String rulePath) {
Pattern pattern = Pattern.compile(getRuleNameNode() + "/(\\w+)" + ACTIVE_VERSION_SUFFIX, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(rulePath);
public static Optional<String> findVersion(final String ruleTypeName, final String path) {
Pattern pattern = Pattern.compile(getRuleVersionPath(ruleTypeName, VERSION_PATTERN) + "$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty();
}

private static String getRuleNameNode() {
return String.join("/", "", RULES_NODE);
/**
* Is props active version path.
*
* @param propsPath props path
* @return true or false
*/
public static boolean isPropsActiveVersionPath(final String propsPath) {
Pattern pattern = Pattern.compile(getPropsActiveVersionPath() + "$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(propsPath);
return matcher.find();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private Optional<YamlRuleConfiguration> swapToYamlRuleConfiguration(final Collec
final Class<? extends YamlRuleConfiguration> toBeSwappedType, final RepositoryTupleEntity tupleEntity) {
if (YamlGlobalRuleConfiguration.class.isAssignableFrom(toBeSwappedType)) {
for (RepositoryTuple each : repositoryTuples) {
if (GlobalNodePath.getVersion(tupleEntity.value(), each.getKey()).isPresent()) {
if (GlobalNodePath.findVersion(tupleEntity.value(), each.getKey()).isPresent()) {
return Optional.of(YamlEngine.unmarshal(each.getValue(), toBeSwappedType));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,66 @@

import java.util.Optional;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class GlobalNodePathTest {

@Test
void assertGetVersion() {
Optional<String> actual = GlobalNodePath.getVersion("transaction", "/rules/transaction/versions/0");
assertTrue(actual.isPresent());
assertThat(actual.get(), is("0"));
void assertGetRuleRootPath() {
assertThat(GlobalNodePath.getRuleRootPath(), is("/rules"));
}

@Test
void assertIsRuleActiveVersionPath() {
assertTrue(GlobalNodePath.isRuleActiveVersionPath("/rules/transaction/active_version"));
void assertGetRulePath() {
assertThat(GlobalNodePath.getRulePath("foo_rule"), is("/rules/foo_rule"));
}

@Test
void assertIsPropsActiveVersionPath() {
assertTrue(GlobalNodePath.isPropsActiveVersionPath("/props/active_version"));
void assertGetRuleVersionsPath() {
assertThat(GlobalNodePath.getRuleVersionsPath("foo_rule"), is("/rules/foo_rule/versions"));
}

@Test
void assertGetRuleVersionPath() {
assertThat(GlobalNodePath.getRuleVersionPath("foo_rule", "0"), is("/rules/foo_rule/versions/0"));
}

@Test
void assertGetRuleActiveVersionPath() {
assertThat(GlobalNodePath.getRuleActiveVersionPath("foo_rule"), is("/rules/foo_rule/active_version"));
}

@Test
void assertGetPropsRootPath() {
assertThat(GlobalNodePath.getPropsRootPath(), is("/props"));
}

@Test
void assertGetRuleName() {
Optional<String> actual = GlobalNodePath.getRuleName("/rules/transaction/active_version");
void assertGetPropsVersionsPath() {
assertThat(GlobalNodePath.getPropsVersionsPath(), is("/props/versions"));
}

@Test
void assertGetPropsVersionPath() {
assertThat(GlobalNodePath.getPropsVersionPath("0"), is("/props/versions/0"));
}

@Test
void assertGetPropsActiveVersionPath() {
assertThat(GlobalNodePath.getPropsActiveVersionPath(), is("/props/active_version"));
}

@Test
void assertFindVersion() {
Optional<String> actual = GlobalNodePath.findVersion("foo_rule", "/rules/foo_rule/versions/0");
assertTrue(actual.isPresent());
assertThat(actual.get(), is("transaction"));
assertThat(actual.get(), is("0"));
}

@Test
void assertGetRuleNameWhenNotFound() {
Optional<String> actual = GlobalNodePath.getRuleName("/invalid/transaction/active_version");
assertFalse(actual.isPresent());
void assertIsPropsActiveVersionPath() {
assertTrue(GlobalNodePath.isPropsActiveVersionPath("/props/active_version"));
}
}

This file was deleted.

Loading

0 comments on commit 4475e3c

Please sign in to comment.