test 3

 @Test

void should_handle_job_status_ko_during_dump_download() throws Exception {

    // Given

    ImportDataEvent event = new ImportDataEvent(

        this, almRepository, DUMPSET_NAME, ENVIRONMENT, superTask, dump

    );

    

    when(dumpAvailabilityDao.findByPerimeterAndNameAndDatabase(

        almRepository.getPerimeter(), DUMPSET_NAME, almRepository.getDbName()))

        .thenReturn(Optional.empty());

        

    when(dumpDao.findByPerimeterAndNameAndEnvironment(

        almRepository.getPerimeter(), DUMPSET_NAME, ENVIRONMENT))

        .thenReturn(Optional.of(givenDumpEntity()));

        

    List<String> fileList = List.of("file1.sql", "file2.sql");

    when(dumpHelper.getListOfDumpFiles(any(DumpSetDto.class))).thenReturn(fileList);

    

    when(JsonHelper.toObject(eq(DumpSetDto.class), anyString())).thenReturn(dumpSetDto);

    

    // First file succeeds, second file fails

    when(garliqOcpmService.transferDumpToS3(

        any(), anyString(), any(), any(), any()))

        .thenReturn(JobStatus.OK, JobStatus.KO);

        

    when(almRepositoryDao.findByDbUsername(anyString()))

        .thenReturn(Optional.of(givenRepositoryEntity()));

        

    when(almRepositoryMapper.toAlmRepository(any()))

        .thenReturn(almRepository);

        

    when(dumpAvailabilityDao.findByPerimeterAndNameAndDatabase(

        almRepository.getPerimeter(), dumpSetDto.getName(), almRepository.getDbName()))

        .thenReturn(Optional.of(dumpAvailabilityEntity));


    doNothing().when(taskService).createFailedTask(any(), anyString(), any(), anyString());

    doNothing().when(almRepositoryService).updateStateToFailure(anyString());


    // When

    yourService.handleImportDataEvent(event);


    // Then

    // Verify updateRepositoryStateToFailure is called TWICE:

    // 1. First time directly in the if(jobStatus.equals(JobStatus.KO)) block

    // 2. Second time in the catch block that catches the GarliqException

    verify(taskService, times(2)).createFailedTask(

        eq(superTask), 

        eq(almRepository.getDbUsername()), 

        eq(TaskType.DOWNLOAD_DUMP), 

        anyString()

    );

    

    verify(almRepositoryService, times(2)).updateStateToFailure(almRepository.getDbUsername());

    

    // Verify DumpState was updated to INCOMPLETE

    verify(dumpAvailabilityDao).findByPerimeterAndNameAndDatabase(

        almRepository.getPerimeter(), dumpSetDto.getName(), almRepository.getDbName());

    verify(dumpAvailabilityEntity).setState(DumpState.INCOMPLETE);

    verify(dumpAvailabilityDao).save(dumpAvailabilityEntity);

    

    // Verify importDumpFromOcpm was never called due to the exception

    verify(yourService, never()).importDumpFromOcpm(any(), any(), any(), any(), any());

    

    // Verify no further processing of files happened after encountering the KO status

    // We have 2 files, but only the first file should be processed successfully

    // and the second file triggers JobStatus.KO

    verify(garliqOcpmService, times(2)).transferDumpToS3(any(), anyString(), any(), any(), any());

}

Comments

Popular posts from this blog

Unit Tests in Spring Boot