Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved the validation error messages on export template parsing #1113

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,25 @@ private Map<Integer, ExportTemplateColumnDef> parseExportTemplateV1(List<String>

if (token2.startsWith(DATA_COLUMN_PREFIX)) {
columnMap.put(i, createProfileNodeDef(header, token2));
} else if ((token2.isEmpty()) || (token2.startsWith(DOUBLE_QUOTES))) {
columnMap.put(i, createConstantStringDef(header, token2));
} else {
} else if (isExpressionForDataModification(token2)) {
columnMap.put(i, createDataModifierDef(header, token2));
} else {
columnMap.put(i, createConstantStringDef(header, token2));
}
}
return columnMap;
}

private boolean isExpressionForDataModification(String expressionParam) {
List<String> operations = Arrays.stream(
ExportTemplateColumnDef.DataModification.values()).map(v -> v.toString() + OPENING_BRACKET).
collect(Collectors.toList());
String expression = expressionParam.trim();

List<String> possibleOperations = operations.stream().filter(op -> expression.startsWith(op)).collect(Collectors.toList());
return possibleOperations.size() > 0;
}

private ExportTemplateColumnDef createDataModifierDef(String header, String param2) {

assertDataModifierSyntaxValid(param2);
Expand Down Expand Up @@ -168,10 +178,11 @@ private ExportTemplateColumnDef createConstantStringDef(String header, String pa
if (param2.isEmpty()) {
return new ConstantStringColumnDef("", header);
} else {
if (!param2.endsWith(DOUBLE_QUOTES)) {
throw new ExportTemplateParseException("The line with a constant value ('" + param2 + "') in template definition does not have closing quotes");
if (param2.startsWith(DOUBLE_QUOTES) && param2.endsWith(DOUBLE_QUOTES)) {
return new ConstantStringColumnDef(param2.substring(1, param2.length() - 1), header);
} else {
throw new ExportTemplateParseException("The line with a constant value ('" + param2 + "') is not fully enclosed in quotes");
}
return new ConstantStringColumnDef(param2.substring(1, param2.length() - 1), header);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public void should_treat_missing_value_as_empty_string_constant_in_export_templa
ExportTemplate template = builder.buildExportTemplate(tempFile.getAbsolutePath());
assertNotNull(template);
assertTrue(template.getColumnOrderMap().get(1) instanceof ConstantStringColumnDef);
assertEquals("", template.getColumnOrderMap().get(1).getDataValue());
assertTrue(template.getColumnOrderMap().get(2) instanceof DataModifierColumnDef);
assertEquals("FILE_PATH", template.getColumnOrderMap().get(2).getOriginalColumnName());
}
Expand Down Expand Up @@ -166,7 +167,37 @@ public void should_throw_an_exception_if_the_constant_string_value_does_not_have
Files.write(tempFile.toPath(), data, StandardOpenOption.WRITE);

ExportTemplateParseException ex = assertThrows(ExportTemplateParseException.class, () -> builder.buildExportTemplate(tempFile.getAbsolutePath()));
assertEquals("The line with a constant value ('\"English') in template definition does not have closing quotes", ex.getMessage());
assertEquals("The line with a constant value ('\"English') is not fully enclosed in quotes", ex.getMessage());
}

@Test
public void should_throw_an_exception_if_the_constant_string_value_does_not_have_opening_double_quotes() throws IOException {
ExportTemplateBuilder builder = new ExportTemplateBuilder();
File tempFile = temporaryFolder.newFile("export-task-test-default-encoding");
List<String> data = Arrays.asList(
"version 1.0",
"",
"Language: English\"",
"");
Files.write(tempFile.toPath(), data, StandardOpenOption.WRITE);

ExportTemplateParseException ex = assertThrows(ExportTemplateParseException.class, () -> builder.buildExportTemplate(tempFile.getAbsolutePath()));
assertEquals("The line with a constant value ('English\"') is not fully enclosed in quotes", ex.getMessage());
}

@Test
public void should_throw_an_exception_if_the_constant_string_value_does_not_have_fully_enclosing_quotes() throws IOException {
ExportTemplateBuilder builder = new ExportTemplateBuilder();
File tempFile = temporaryFolder.newFile("export-task-test-default-encoding");
List<String> data = Arrays.asList(
"version 1.0",
"",
"Language: En\"gli\"sh",
"");
Files.write(tempFile.toPath(), data, StandardOpenOption.WRITE);

ExportTemplateParseException ex = assertThrows(ExportTemplateParseException.class, () -> builder.buildExportTemplate(tempFile.getAbsolutePath()));
assertEquals("The line with a constant value ('En\"gli\"sh') is not fully enclosed in quotes", ex.getMessage());
}

@Test
Expand Down Expand Up @@ -196,7 +227,7 @@ public void should_throw_an_exception_when_the_operation_is_unknown() throws IOE
Files.write(tempFile.toPath(), data, StandardOpenOption.WRITE);

ExportTemplateParseException ex = assertThrows(ExportTemplateParseException.class, () -> builder.buildExportTemplate(tempFile.getAbsolutePath()));
assertEquals("Undefined operation 'Lower' encountered in export template", ex.getMessage());
assertEquals("The line with a constant value ('Lower($ID)') is not fully enclosed in quotes", ex.getMessage());
}

@Test
Expand Down Expand Up @@ -228,6 +259,21 @@ public void should_support_colon_in_the_constant_string_value() throws IOExcepti
assertEquals("http://www.knowingwhere.com", template.getColumnOrderMap().get(0).getDataValue());
}

@Test
public void should_support_constant_value_which_includes_operation_name() throws IOException {
ExportTemplateBuilder builder = new ExportTemplateBuilder();
File tempFile = temporaryFolder.newFile("export-task-test-default-encoding");
List<String> data = Arrays.asList(
"version 1.0",
" MyWebsite : \"LCASE($ID)\"",
"");
Files.write(tempFile.toPath(), data, StandardOpenOption.WRITE);

ExportTemplate template = builder.buildExportTemplate(tempFile.getAbsolutePath());
assertEquals("MyWebsite", template.getColumnOrderMap().get(0).getHeaderLabel());
assertEquals("LCASE($ID)", template.getColumnOrderMap().get(0).getDataValue());
}

@Test
public void should_throw_an_exception_when_an_operation_cannot_be_located() throws IOException {
ExportTemplateBuilder builder = new ExportTemplateBuilder();
Expand All @@ -239,7 +285,7 @@ public void should_throw_an_exception_when_an_operation_cannot_be_located() thro
Files.write(tempFile.toPath(), data, StandardOpenOption.WRITE);

ExportTemplateParseException ex = assertThrows(ExportTemplateParseException.class, () -> builder.buildExportTemplate(tempFile.getAbsolutePath()));
assertEquals("Invalid syntax in data modifier expression 'Crown Copyright (C)', expecting '$' after '('", ex.getMessage());
assertEquals("The line with a constant value ('Crown Copyright (C)') is not fully enclosed in quotes", ex.getMessage());
}

@Test
Expand All @@ -253,7 +299,7 @@ public void should_throw_an_exception_when_a_constant_is_not_enclosed_in_double_
Files.write(tempFile.toPath(), data, StandardOpenOption.WRITE);

ExportTemplateParseException ex = assertThrows(ExportTemplateParseException.class, () -> builder.buildExportTemplate(tempFile.getAbsolutePath()));
assertEquals("Invalid syntax in data modifier expression 'Crown Copyright', expecting exactly one occurrence of '('", ex.getMessage());
assertEquals("The line with a constant value ('Crown Copyright') is not fully enclosed in quotes", ex.getMessage());
}

@Test
Expand Down
Loading
Loading