Skip to content

Commit

Permalink
update file info checker
Browse files Browse the repository at this point in the history
  • Loading branch information
nitin-ebi committed Jul 15, 2024
1 parent e1672ae commit f2109d5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,53 +98,58 @@ public Submission initiateSubmission(SubmissionAccount submissionAccount) {
}

public void checkMetadataFileInfoMatchesWithUploadedFiles(SubmissionAccount submissionAccount, String submissionId, JsonNode metadataJson) {
Map<String, Long> metadataFileInfo = new HashMap<>();
if (metadataJson.get(METADATA_FILES_TAG) != null) {
metadataFileInfo = StreamSupport.stream(metadataJson.get(METADATA_FILES_TAG).spliterator(), false)
.collect(Collectors.toMap(
dataNode -> dataNode.get(METADATA_FILE_NAME).asText(),
dataNode -> dataNode.get(METADATA_FILE_SIZE).asLong()
));
}
if (metadataFileInfo.isEmpty()) {
throw new MetadataFileInfoMismatchException("Metadata json file does not have any file info");
}

String directoryToList = String.format("%s/%s", submissionAccount.getId(), submissionId);
String uploadedFilesInfo = globusDirectoryProvisioner.listSubmittedFiles(directoryToList);
if (uploadedFilesInfo.isEmpty()) {
throw new MetadataFileInfoMismatchException("Failed to retrieve any file info from submission directory.");
} else {
Map<String, Long> globusFileInfo = new HashMap<>();
try {
ObjectMapper mapper = new ObjectMapper();
ObjectNode globusFileInfoJson = (ObjectNode) mapper.readTree(uploadedFilesInfo);
Map<String, Long> globusFileInfo = new HashMap<>();
if (globusFileInfoJson.get(GLOBUS_FILES_TAG) != null) {
globusFileInfo = StreamSupport.stream(globusFileInfoJson.get(GLOBUS_FILES_TAG).spliterator(), false)
.filter(dataNode -> dataNode.get(GLOBUS_FILE_NAME).asText().endsWith(".vcf") || dataNode.get(GLOBUS_FILE_NAME).asText().endsWith(".vcf.gz"))
.collect(Collectors.toMap(
dataNode -> dataNode.get(GLOBUS_FILE_NAME).asText(),
dataNode -> dataNode.get(GLOBUS_FILE_SIZE).asLong()
));
}
} catch (JsonProcessingException ex) {
throw new MetadataFileInfoMismatchException("Error parsing fileInfo from Submission Directory");
}

Map<String, Long> metadataFileInfo = StreamSupport.stream(metadataJson.get(METADATA_FILES_TAG).spliterator(), false)
.collect(Collectors.toMap(
dataNode -> dataNode.get(METADATA_FILE_NAME).asText(),
dataNode -> dataNode.get(METADATA_FILE_SIZE).asLong()
));

List<String> missingFileList = new ArrayList<>();
String fileSizeMismatchInfo = "";

for (Map.Entry<String, Long> fileEntry : metadataFileInfo.entrySet()) {
String fileName = fileEntry.getKey();
Long metadataFileSize = fileEntry.getValue();
if (globusFileInfo.containsKey(fileName)) {
Long fileSizeInGlobus = globusFileInfo.get(fileName);
if (!metadataFileSize.equals(fileSizeInGlobus)) {
fileSizeMismatchInfo += fileName + ": metadata json file size (" + metadataFileSize + ") is not equal to uploaded file size (" + fileSizeInGlobus + ")\n";
}
} else {
missingFileList.add(fileName);
List<String> missingFileList = new ArrayList<>();
String fileSizeMismatchInfo = "";

for (Map.Entry<String, Long> fileEntry : metadataFileInfo.entrySet()) {
String fileName = fileEntry.getKey();
Long metadataFileSize = fileEntry.getValue();
if (globusFileInfo.containsKey(fileName)) {
Long fileSizeInGlobus = globusFileInfo.get(fileName);
if (!metadataFileSize.equals(fileSizeInGlobus)) {
fileSizeMismatchInfo += fileName + ": metadata json file size (" + metadataFileSize + ") is not equal to uploaded file size (" + fileSizeInGlobus + ")\n";
}
} else {
missingFileList.add(fileName);
}
}

if (!missingFileList.isEmpty() || !fileSizeMismatchInfo.isEmpty()) {
String missingFileMsg = missingFileList.isEmpty() ? "" : "There are some files mentioned in metadata json but not uploaded. Files : " + String.join(", ", missingFileList) + "\n";
String fileSizeMismatchMsg = fileSizeMismatchInfo.isEmpty() ? "" : "There are some files mentioned in metadata json whose size does not match with the files uploaded.\n" + fileSizeMismatchInfo;
throw new MetadataFileInfoMismatchException(missingFileMsg + fileSizeMismatchMsg);
}
} catch (JsonProcessingException ex) {
throw new MetadataFileInfoMismatchException("Error parsing fileInfo from Submission Directory");
if (!missingFileList.isEmpty() || !fileSizeMismatchInfo.isEmpty()) {
String missingFileMsg = missingFileList.isEmpty() ? "" : "There are some files mentioned in metadata json but not uploaded. Files : " + String.join(", ", missingFileList) + "\n";
String fileSizeMismatchMsg = fileSizeMismatchInfo.isEmpty() ? "" : "There are some files mentioned in metadata json whose size does not match with the files uploaded.\n" + fileSizeMismatchInfo;
throw new MetadataFileInfoMismatchException(missingFileMsg + fileSizeMismatchMsg);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,35 @@ public void testUploadMetadataJsonAndMarkUploaded() throws Exception {
assertThat(submissionDetails.getMetadataJson().get("project").get("description").asText()).isEqualTo(projectDescription);
}

@Test
@Transactional
public void testMarkSubmissionUploadNoFileInfoInMetadatajson() throws Exception {
String userToken = "webinUserToken";
SubmissionAccount submissionAccount = getWebinUserAccount();
String submissionId = createNewSubmissionEntry(submissionAccount);

when(webinTokenService.getWebinUserAccountFromToken(anyString())).thenReturn(submissionAccount);
doNothing().when(mailSender).sendEmail(anyString(), anyString(), anyString());
when(globusDirectoryProvisioner.listSubmittedFiles(submissionAccount.getId() + "/" + submissionId)).thenReturn("");

// create metadata json
ObjectMapper mapper = new ObjectMapper();
ObjectNode metadataRootNode = mapper.createObjectNode();
ArrayNode filesArrayNode = mapper.createArrayNode();
metadataRootNode.put("files", filesArrayNode);


HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setBearerAuth(userToken);
mvc.perform(put("/v1/submission/" + submissionId + "/uploaded")
.headers(httpHeaders)
.content(mapper.writeValueAsString(metadataRootNode))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(content().string("Metadata json file does not have any file info"));
}


@Test
@Transactional
public void testMarkSubmissionUploadErrorGettingInfoFromGlobus() throws Exception {
Expand Down

0 comments on commit f2109d5

Please sign in to comment.