From d355316fbb8597dff1a01a0564224515a762b9ce Mon Sep 17 00:00:00 2001 From: Nick <53413353+nickpalladino@users.noreply.github.com> Date: Wed, 23 Apr 2025 16:27:08 -0400 Subject: [PATCH 1/2] Add germplasm name validation --- .../germplasm/GermplasmProcessor.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/java/org/breedinginsight/brapps/importer/services/processors/germplasm/GermplasmProcessor.java b/src/main/java/org/breedinginsight/brapps/importer/services/processors/germplasm/GermplasmProcessor.java index d4d2389a8..bdf1f8d69 100644 --- a/src/main/java/org/breedinginsight/brapps/importer/services/processors/germplasm/GermplasmProcessor.java +++ b/src/main/java/org/breedinginsight/brapps/importer/services/processors/germplasm/GermplasmProcessor.java @@ -91,6 +91,7 @@ public class GermplasmProcessor implements Processor { public static String missingParentalGIDsMsg = "The following parental GIDs were not found in the database: %s"; public static String missingParentalEntryNoMsg = "The following parental entry numbers were not found in the database: %s"; public static String badBreedMethodsMsg = "Invalid breeding method"; + public static String badGermplasmNameMsg = "Germplasm name cannot contain /"; public static String missingEntryNumbersMsg = "Either all or none of the germplasm must have entry numbers"; public static String duplicateEntryNoMsg = "Entry numbers must be unique. Duplicated entry numbers found: %s"; public static String circularDependency = "Circular dependency in the pedigree tree"; @@ -357,6 +358,7 @@ private void processNewGermplasm(Germplasm germplasm, ValidationErrors validatio } } + validateGermplasmName(germplasm, i+2, validationErrors); validatePedigree(germplasm, i + 2, validationErrors); BrAPIGermplasm newGermplasm = germplasm.constructBrAPIGermplasm(program, breedingMethod, user, commit, BRAPI_REFERENCE_SOURCE, nextVal, importListId); @@ -543,6 +545,31 @@ private Map getStatisticsMap(List } + /** + * Validates the name of the given Germplasm, ensuring it does not contain any slash ("/") characters. + *

+ * If the germplasm name contains a "/", a new {@link ValidationError} with status + * {@code 422 Unprocessable Entity} is created and added to the provided {@code ValidationErrors} object. + * This method does not throw an exception; instead, it records validation failures by mutating + * the {@code validationErrors} parameter. + *

+ * + * @param germplasm + * the {@link Germplasm} instance whose name is to be validated; must not be {@code null} + * @param rowNumber + * the row index (for example, in a spreadsheet or CSV file) corresponding to this + * germplasm entry; used when reporting errors + * @param validationErrors + * the {@link ValidationErrors} collector into which any detected errors will be added; + * this object is modified by this method to record validation issues; must not be {@code null} + */ + private void validateGermplasmName(Germplasm germplasm, Integer rowNumber, ValidationErrors validationErrors) { + if (germplasm.getGermplasmName().contains("/")) { + ValidationError error = new ValidationError("Germplasm Name", badGermplasmNameMsg, HttpStatus.UNPROCESSABLE_ENTITY); + validationErrors.addError(rowNumber, error); + } + } + private void validatePedigree(Germplasm germplasm, Integer rowNumber, ValidationErrors validationErrors) { String femaleParentEntryNo = germplasm.getFemaleParentEntryNo(); String maleParentEntryNo = germplasm.getMaleParentEntryNo(); From c68b47f2d0f90490263e53402abeb708ccf57979 Mon Sep 17 00:00:00 2001 From: Nick <53413353+nickpalladino@users.noreply.github.com> Date: Wed, 23 Apr 2025 16:27:47 -0400 Subject: [PATCH 2/2] Add test for germplasm name validation --- .../importer/GermplasmFileImportTest.java | 29 +++++++++++++++++++ .../germplasm_import/bad_germplasm_names.csv | 4 +++ 2 files changed, 33 insertions(+) create mode 100644 src/test/resources/files/germplasm_import/bad_germplasm_names.csv diff --git a/src/test/java/org/breedinginsight/brapps/importer/GermplasmFileImportTest.java b/src/test/java/org/breedinginsight/brapps/importer/GermplasmFileImportTest.java index 13d60a714..ac2860441 100644 --- a/src/test/java/org/breedinginsight/brapps/importer/GermplasmFileImportTest.java +++ b/src/test/java/org/breedinginsight/brapps/importer/GermplasmFileImportTest.java @@ -542,6 +542,35 @@ public void badBreedingMethods() { assertEquals("Breeding Method", firstError.get("field").getAsString()); } + /** + * Test for BI-2185 adding validation for germplasm names to not allow / characters + */ + @Test + @SneakyThrows + public void badGermplasmNames() { + File file = new File("src/test/resources/files/germplasm_import/bad_germplasm_names.csv"); + Flowable> call = importTestUtils.uploadDataFile(file, Map.of(GERM_LIST_NAME, "Bad Germplasm Names"), true, client, validProgram, germplasmMappingId); + HttpResponse response = call.blockingFirst(); + assertEquals(HttpStatus.ACCEPTED, response.getStatus()); + String importId = JsonParser.parseString(response.body()).getAsJsonObject().getAsJsonObject("result").get("importId").getAsString(); + + HttpResponse upload = importTestUtils.getUploadedFile(importId, client, validProgram, germplasmMappingId); + JsonObject result = JsonParser.parseString(upload.body()).getAsJsonObject().getAsJsonObject("result"); + assertEquals(422, result.getAsJsonObject("progress").get("statuscode").getAsInt()); + + JsonArray errorList = result + .getAsJsonObject("progress") + .getAsJsonArray("rowErrors"); + + assertEquals(3, errorList.size()); + + JsonObject firstError = errorList + .get(0).getAsJsonObject() + .getAsJsonArray("errors").get(0).getAsJsonObject(); + assertEquals("Germplasm name cannot contain /", firstError.get("errorMessage").getAsString()); + assertEquals("Germplasm Name", firstError.get("field").getAsString()); + } + @Test @SneakyThrows public void someEntryNumbersError() { diff --git a/src/test/resources/files/germplasm_import/bad_germplasm_names.csv b/src/test/resources/files/germplasm_import/bad_germplasm_names.csv new file mode 100644 index 000000000..22f177757 --- /dev/null +++ b/src/test/resources/files/germplasm_import/bad_germplasm_names.csv @@ -0,0 +1,4 @@ +GID,Germplasm Name,Breeding Method,Source,Female Parent GID,Male Parent GID,Entry No,Female Parent Entry No,Male Parent Entry No,External UID,Synonyms +,Germplasm 1/Germplasm 2,BCR,Test,,,1,,,, +,Germplasm3 / Germaplasm4,BCR,Test,,,2,,,, +,Germplasm5/Germplasm6/Germplasm7,BCR,Test,,,3,1,2,, \ No newline at end of file