Skip to content

Commit

Permalink
fix: For path lenghts errors make the error message a little more cle…
Browse files Browse the repository at this point in the history
…ar, add the possible cause of duplicate paths.

Add test for duplicate paths.
  • Loading branch information
credmond-git committed Feb 28, 2024
1 parent a72a5ca commit 8c1853b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,18 @@ public String description() {
/**
* Mismatched path lengths received for path, this could be because a node is both a leaf and an object.
*/
public static class MismatchedPathLength extends ValidationError {
public static class PathLengthErrors extends ValidationError {
private final String paths;

public MismatchedPathLength(String paths) {
public PathLengthErrors(String paths) {
super(ValidationLevel.ERROR);
this.paths = paths;
}

@Override
public String description() {
return "Mismatched path lengths received for path: " + paths + ", this could be because a node is both a leaf and an object";
return "Parsing path length errors for path: " + paths +
", there could be several causes such as: duplicate paths, or a node is both a leaf and an object";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ GResultOf<ConfigNode> buildConfigTree(List<Pair<List<Token>, ConfigValue>> token
return GResultOf.result(new LeafNode(configValue.getValue()));
}

// result any mis-matched path's, this is most like when a path is both a leaf and an object and an array.
List<ValidationError> mismatchedPathLengthErrors = getMismatchedPathLengthErrors(tokens, index, currentPath);
// result any mis-matched path's, this is most like when a path is both a duplicate, or leaf and an object and an array.
List<ValidationError> mismatchedPathLengthErrors = getPathLengthErrors(tokens, index, currentPath);
if (!mismatchedPathLengthErrors.isEmpty()) {
return GResultOf.errors(mismatchedPathLengthErrors);
}
Expand Down Expand Up @@ -250,16 +250,16 @@ private List<ValidationError> validateArrayDuplicateLeafIndex(Map<Token,

// We can not have paths with a different lengths, as this means we are at
// both a leaf and object for the same group of tokens.
private List<ValidationError> getMismatchedPathLengthErrors(List<Pair<List<Token>, ConfigValue>> tokens,
int index, String currentPath) {
List<Pair<List<Token>, ConfigValue>> nodesWithMismatchedPathLengths =
private List<ValidationError> getPathLengthErrors(List<Pair<List<Token>, ConfigValue>> tokens,
int index, String currentPath) {
List<Pair<List<Token>, ConfigValue>> nodesWithSamePathLengths =
tokens.stream()
.filter(tokenPair -> tokenPair.getFirst().size() < index + 1)
.collect(Collectors.toList());

List<ValidationError> errorList = new ArrayList<>();
if (!nodesWithMismatchedPathLengths.isEmpty()) {
errorList.add(new ValidationError.MismatchedPathLength(currentPath));
if (!nodesWithSamePathLengths.isEmpty()) {
errorList.add(new ValidationError.PathLengthErrors(currentPath));
}

return errorList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ void loadSourceWithRealDependenciesErrorParsing() throws GestaltException {
Assertions.assertTrue(results.hasErrors());

Assertions.assertEquals(1, results.getErrors().size());
Assertions.assertEquals("Mismatched path lengths received for path: db, this could be because a node is both a leaf " +
"and an object", results.getErrors().get(0).description());
Assertions.assertEquals("Parsing path length errors for path: db, there could be several causes such as: duplicate paths, " +
"or a node is both a leaf and an object", results.getErrors().get(0).description());
}

private static class MapConfigSourceWarn extends TestMapConfigSource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,31 @@ public void testValidateMismatchedPathLength() {
List<ValidationError> results = resultsOf.getErrors();
assertEquals(1, results.size());
assertEquals(ValidationLevel.ERROR, results.get(0).level());
assertEquals("Mismatched path lengths received for path: db.hosts, " +
"this could be because a node is both a leaf and an object", results.get(0).description());
assertEquals("Parsing path length errors for path: db.hosts, there could be several causes such as: duplicate paths, " +
"or a node is both a leaf and an object", results.get(0).description());
}

@Test
public void testValidateDuplicatePathLength() {
MapConfigParser mapConfigParser = new MapConfigParser();

List<Pair<List<Token>, ConfigValue>> test = List.of(
new Pair<>(List.of(new ObjectToken("db"), new ObjectToken("connections")), new ConfigValue("10")),
new Pair<>(List.of(new ObjectToken("db"), new ObjectToken("user")), new ConfigValue("test")),
new Pair<>(List.of(new ObjectToken("db"), new ObjectToken("password")), new ConfigValue("password")),
new Pair<>(List.of(new ObjectToken("db"), new ObjectToken("hosts")), new ConfigValue("hostDB1")),
new Pair<>(List.of(new ObjectToken("db"), new ObjectToken("hosts")), new ConfigValue("hostDB2"))
);

GResultOf<ConfigNode> resultsOf = mapConfigParser.buildConfigTree(test, 0, true);
assertFalse(resultsOf.hasResults());
assertTrue(resultsOf.hasErrors());
assertNull(resultsOf.results());
List<ValidationError> results = resultsOf.getErrors();
assertEquals(1, results.size());
assertEquals(ValidationLevel.ERROR, results.get(0).level());
assertEquals("Parsing path length errors for path: db.hosts, there could be several causes such as: duplicate paths, " +
"or a node is both a leaf and an object", results.get(0).description());
}

@Test
Expand All @@ -340,8 +363,8 @@ public void testValidateMismatchedPathLengthWarnings() {
List<ValidationError> results = resultsOf.getErrors();
assertEquals(1, results.size());
assertEquals(ValidationLevel.ERROR, results.get(0).level());
assertEquals("Mismatched path lengths received for path: db.hosts, " +
"this could be because a node is both a leaf and an object", results.get(0).description());
assertEquals("Parsing path length errors for path: db.hosts, there could be several causes such as: duplicate paths, " +
"or a node is both a leaf and an object", results.get(0).description());
}

@Test
Expand All @@ -363,8 +386,8 @@ public void testValidateMismatchedPathLength2() {
List<ValidationError> results = resultsOf.getErrors();
assertEquals(1, results.size());
assertEquals(ValidationLevel.ERROR, results.get(0).level());
assertEquals("Mismatched path lengths received for path: db, this could be because " +
"a node is both a leaf and an object", results.get(0).description());
assertEquals("Parsing path length errors for path: db, there could be several causes such as: duplicate paths, " +
"or a node is both a leaf and an object", results.get(0).description());
}

@Test
Expand Down

0 comments on commit 8c1853b

Please sign in to comment.