diff --git a/src/main/java/org/breedinginsight/services/geno/impl/GigwaGenotypeServiceImpl.java b/src/main/java/org/breedinginsight/services/geno/impl/GigwaGenotypeServiceImpl.java index a6dc67a4b..7c2ba98b0 100644 --- a/src/main/java/org/breedinginsight/services/geno/impl/GigwaGenotypeServiceImpl.java +++ b/src/main/java/org/breedinginsight/services/geno/impl/GigwaGenotypeServiceImpl.java @@ -224,6 +224,21 @@ public ImportResponse submitGenotypeData(UUID userId, UUID programId, UUID submi progress.setId(upload.getImporterProgressId()); upload.setProgress(progress); + //Query all filenames to avoid duplicate file uploads + String fileName = uploadedFile.getFilename(); + List existingImports = getGenotypeImports(programId); + for (GenotypeImportDetails existingImport : existingImports) { + if (existingImport.getGenotypingFileName().equals(fileName)){ + ImportResponse response = new ImportResponse(); + upload.getProgress().setStatuscode((short)HttpStatus.BAD_REQUEST.getCode()); + upload.getProgress().setMessage(String.format("Import cannot proceed. File name '%s' already exists in the database.", fileName)); + importDAO.updateProgress(upload.getProgress()); + response.setImportId(upload.getId()); + response.setProgress(progress); + return response; + } + } + String gigwaAuthToken; try { gigwaAuthToken = getAuthToken(); diff --git a/src/test/java/org/breedinginsight/services/geno/impl/GigwaGenotypeServiceImplIntegrationTest.java b/src/test/java/org/breedinginsight/services/geno/impl/GigwaGenotypeServiceImplIntegrationTest.java index e0cf269ac..20d1ea6fd 100644 --- a/src/test/java/org/breedinginsight/services/geno/impl/GigwaGenotypeServiceImplIntegrationTest.java +++ b/src/test/java/org/breedinginsight/services/geno/impl/GigwaGenotypeServiceImplIntegrationTest.java @@ -561,6 +561,39 @@ public void testSubmitInvalidHeader() throws ApiException { assertEquals("Header row is not valid VCF format", response.getProgress().getMessage()); } +@Test + public void testSubmitSameFileName() throws ApiException, IOException { + UUID programId = UUID.fromString("29162e85-e739-4f19-9fd0-0c377ed59956"); + String programKey = "TESTSUBMITDUPNAME"; + UUID submissionId = UUID.randomUUID(); + setupMocksForSubmitGenoData(programId, submissionId, buildSamplesFromValidVcf()); + + // First upload should be accepted for asynchronous processing. + AtomicReference importResponse = new AtomicReference<>(); + assertTimeout(Duration.of(2, ChronoUnit.MINUTES), () -> importResponse.set(submitGenoData(programId, programKey, submissionId, "sample.vcf")), "Upload did not complete within the time period"); + + ImportResponse response = importResponse.get(); + assertNotNull(response); + assertNotNull(response.getProgress()); + assertEquals((short) HttpStatus.ACCEPTED.getCode(), + response.getProgress().getStatuscode(), + "Error importing geno file: " + response.getProgress().getMessage()); + + // The DAO is mocked: explicitly simulate the completed import before resubmitting. + doReturn(List.of(GenotypeImportDetails.builder() + .genotypingFileName("sample.vcf") + .build())) + .when(genotypeImportDAO).getGenotypeImportsByProgramId(programId); + AtomicReference importResponseTwo = new AtomicReference<>(); + assertTimeout(Duration.of(2, ChronoUnit.MINUTES), () -> importResponseTwo.set(submitGenoData(programId, programKey, submissionId, "sample.vcf")), "Upload did not complete within the time period"); + + ImportResponse responseTwo = importResponseTwo.get(); + assertNotNull(responseTwo); + assertNotNull(responseTwo.getProgress()); + assertEquals((short) HttpStatus.BAD_REQUEST.getCode(), responseTwo.getProgress().getStatuscode()); + assertEquals("Import cannot proceed. File name 'sample.vcf' already exists in the database.", responseTwo.getProgress().getMessage()); + } + @Test public void testSubmitMissingSubmissionSamples() throws ApiException { UUID programId = UUID.fromString("29162e85-e739-4f19-9fd0-0c377ed59956");