Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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";
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -543,6 +545,31 @@ private Map<String, ImportPreviewStatistics> getStatisticsMap(List<BrAPIImport>

}

/**
* Validates the name of the given Germplasm, ensuring it does not contain any slash ("/") characters.
* <p>
* 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.
* </p>
*
* @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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpResponse<String>> call = importTestUtils.uploadDataFile(file, Map.of(GERM_LIST_NAME, "Bad Germplasm Names"), true, client, validProgram, germplasmMappingId);
HttpResponse<String> response = call.blockingFirst();
assertEquals(HttpStatus.ACCEPTED, response.getStatus());
String importId = JsonParser.parseString(response.body()).getAsJsonObject().getAsJsonObject("result").get("importId").getAsString();

HttpResponse<String> 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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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,,
Loading