Skip to content

Commit 592948c

Browse files
committed
[bugfix] minor bugfixes during review
1 parent 03fe1d1 commit 592948c

2 files changed

Lines changed: 32 additions & 48 deletions

File tree

src/main/java/com/evolvedbinary/bblValidator/service/CsvValidationService.java

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import uk.gov.nationalarchives.csv.validator.api.java.*;
99

1010
import java.nio.charset.Charset;
11+
import java.nio.charset.StandardCharsets;
1112
import java.nio.file.Path;
1213
import java.util.ArrayList;
14+
import java.util.Collections;
1315
import java.util.List;
1416

1517
import static uk.gov.nationalarchives.csv.validator.api.CsvValidator$.MODULE$;
@@ -19,39 +21,31 @@
1921
public class CsvValidationService {
2022

2123
private static final Logger LOG = LoggerFactory.getLogger(CsvValidationService.class);
22-
private static final int DEFAULT_MAX_CHARS_PER_CELL = 8096;
2324

2425
@Inject
2526
private SchemaService schemaService;
2627

2728
public ValidationResult validateCsvFile(Path csvFilePath, String schemaId) {
29+
String schemaFilePath = String.valueOf(schemaService.getSchemaFilePath(schemaId));
30+
Charset csvEncoding = StandardCharsets.UTF_8;
31+
boolean validateUtf8Encoding = true;
32+
Charset csvSchemaEncoding = StandardCharsets.UTF_8;
33+
boolean failFast = false;
34+
List<Substitution> pathSubstitutions = Collections.emptyList();
35+
boolean enforceCaseSensitivePathChecks = false;
36+
boolean trace = false;
37+
boolean skipFileChecks = false;
38+
39+
CsvValidatorJavaBridge.ValidationRequest validationRequest = new CsvValidatorJavaBridge.ValidationRequest(
40+
csvFilePath.toString(), csvEncoding, validateUtf8Encoding, schemaFilePath,
41+
csvSchemaEncoding, true, failFast, pathSubstitutions,
42+
enforceCaseSensitivePathChecks, trace, null, skipFileChecks, -1);
43+
2844
long startTime = System.currentTimeMillis();
29-
try {
30-
String schemaFilePath = String.valueOf(schemaService.getSchemaFilePath(schemaId));
31-
Charset csvEncoding = MODULE$.DEFAULT_ENCODING();
32-
boolean validateUtf8Encoding = csvEncoding.name().equals("UTF-8");
33-
Charset csvSchemaEncoding = MODULE$.DEFAULT_ENCODING();
34-
boolean failFast = false;
35-
List<Substitution> pathSubstitutions = new ArrayList<>();
36-
boolean enforceCaseSensitivePathChecks = false;
37-
boolean trace = false;
38-
boolean skipFileChecks = false;
39-
40-
CsvValidatorJavaBridge.ValidationRequest validationRequest = new CsvValidatorJavaBridge.ValidationRequest(
41-
csvFilePath.toString(), csvEncoding, validateUtf8Encoding, schemaFilePath,
42-
csvSchemaEncoding, true, failFast, pathSubstitutions,
43-
enforceCaseSensitivePathChecks, trace, null, skipFileChecks, DEFAULT_MAX_CHARS_PER_CELL);
44-
45-
CsvValidatorJavaBridge.ValidationResult result = CsvValidatorJavaBridge.validate(validationRequest);
46-
List<FailMessage> errors = result.errors();
47-
long executionTime = System.currentTimeMillis() - startTime;
48-
return processValidationMessages(errors, executionTime);
49-
50-
} catch (Exception e) {
51-
long executionTime = System.currentTimeMillis() - startTime;
52-
LOG.error("Error validating CSV file: {}", csvFilePath, e);
53-
return ValidationResult.error("Validation failed: " + e.getMessage(), executionTime);
54-
}
45+
CsvValidatorJavaBridge.ValidationResult result = CsvValidatorJavaBridge.validate(validationRequest);
46+
List<FailMessage> errors = result.errors();
47+
long executionTime = System.currentTimeMillis() - startTime;
48+
return processValidationMessages(errors, executionTime);
5549
}
5650

5751

src/main/java/com/evolvedbinary/bblValidator/service/SchemaService.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ public void loadSchemas() {
6464
* Otherwise, it's resolved relative to the current working directory (startup location).
6565
*/
6666
private Path resolveSchemaPath() {
67-
if (schemaDirectory.startsWith("/")) {
67+
Path schemaPath = Paths.get(schemaDirectory);
68+
if (schemaPath.isAbsolute()) {
6869
// Absolute path
69-
return Paths.get(schemaDirectory);
70+
return schemaPath;
7071
} else {
71-
// Relative to current working directory
72-
return Paths.get(System.getProperty("user.dir"), schemaDirectory);
72+
// Relative to current working
73+
Path applicationDir = Paths.get(System.getProperty("user.dir"));
74+
return schemaPath.relativize(applicationDir);
7375
}
7476
}
7577

@@ -109,27 +111,15 @@ private void loadSchemaMetadata(Path metadataPath) throws IOException {
109111
}
110112

111113
public List<SchemaInfo> listSchemas() {
112-
return new ArrayList<>(schemas);
114+
return schemas;
113115
}
114116

115-
public String getSchema(String schemaId) throws Exception {
116-
String content = schemaContents.get(schemaId);
117-
118-
if (content == null) {
119-
throw new Exception("Schema not found: " + schemaId);
120-
}
121-
122-
return content;
117+
public String getSchema(String schemaId) {
118+
return schemaContents.get(schemaId);
123119
}
124120

125-
public Path getSchemaFilePath(String schemaId) throws Exception {
126-
Path filePath = schemaFilePaths.get(schemaId);
127-
128-
if (filePath == null) {
129-
throw new Exception("Schema file path not found: " + schemaId);
130-
}
131-
132-
return filePath;
121+
public Path getSchemaFilePath(String schemaId) {
122+
return schemaFilePaths.get(schemaId);
133123
}
134124
}
135125

0 commit comments

Comments
 (0)