Search in sources :

Example 1 with StaleJob

use of gov.cms.ab2d.common.dto.StaleJob in project ab2d by CMSgov.

the class FileDeletionServiceTest method testDontCleanOutDir.

@Test
void testDontCleanOutDir(@TempDir File tmpDir) throws IOException {
    AuditMockJob newJob = new AuditMockJob(new StaleJob(UUID.randomUUID().toString(), job.getOrganization()), SUCCESSFUL, OffsetDateTime.now().minusDays(1));
    jobAuditClientMock.update(job);
    ReflectionTestUtils.setField(fileDeletionService, "efsMount", tmpDir.getAbsolutePath());
    String jobId = newJob.getJobUuid();
    String miscString = "Hello";
    // Create the directories
    List<Path> files = new ArrayList<>();
    Path jobDir = Files.createDirectory(Path.of(tmpDir.getAbsolutePath(), jobId));
    Path finishedDir = Files.createDirectory(Path.of(tmpDir.getAbsolutePath(), jobId, "finished"));
    Path streamDir = Files.createDirectory(Path.of(tmpDir.getAbsolutePath(), jobId, "streaming"));
    // Create the files
    files.add(Files.createFile(Path.of(tmpDir.getAbsolutePath(), jobId, "tstfile.ndjson")));
    files.add(Files.createFile(Path.of(tmpDir.getAbsolutePath(), jobId, "tstfile.txt")));
    files.add(Files.createFile(Path.of(tmpDir.getAbsolutePath(), jobId, "finished", "tstfile.ndjson")));
    files.add(Files.createFile(Path.of(tmpDir.getAbsolutePath(), jobId, "streaming", "tstfile.ndjson")));
    files.add(Files.createFile(Path.of(tmpDir.getAbsolutePath(), jobId, "streaming", "tstfile.txt")));
    // Write data to the files
    for (Path file : files) {
        Files.writeString(file, miscString);
    }
    files.add(jobDir);
    files.add(finishedDir);
    files.add(streamDir);
    fileDeletionService.deleteFiles();
    List<Path> remaining = files.stream().filter(f -> f.toFile().exists()).collect(Collectors.toList());
    remaining.forEach(System.out::println);
    assertEquals(8, remaining.size());
    assertEquals(3, remaining.stream().filter(f -> f.toFile().isDirectory()).count());
    assertEquals(2, remaining.stream().filter(f -> f.toFile().isFile()).filter(f -> f.toFile().getName().equals("tstfile.txt")).count());
}
Also used : Path(java.nio.file.Path) CoreMatchers.is(org.hamcrest.CoreMatchers.is) BeforeEach(org.junit.jupiter.api.BeforeEach) UtilMethods(gov.cms.ab2d.eventlogger.utils.UtilMethods) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) Autowired(org.springframework.beans.factory.annotation.Autowired) FileTime(java.nio.file.attribute.FileTime) StaleJob(gov.cms.ab2d.common.dto.StaleJob) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Path(java.nio.file.Path) MethodSource(org.junit.jupiter.params.provider.MethodSource) TestPropertySource(org.springframework.test.context.TestPropertySource) ContractSearchEvent(gov.cms.ab2d.eventlogger.events.ContractSearchEvent) UUID(java.util.UUID) Arguments(org.junit.jupiter.params.provider.Arguments) FileSystemUtils(org.springframework.util.FileSystemUtils) Collectors(java.util.stream.Collectors) LoggerEventRepository(gov.cms.ab2d.eventlogger.reports.sql.LoggerEventRepository) Test(org.junit.jupiter.api.Test) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Stream(java.util.stream.Stream) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) SpringBootApp(gov.cms.ab2d.audit.SpringBootApp) TempDir(org.junit.jupiter.api.io.TempDir) FAILED(gov.cms.ab2d.common.model.JobStatus.FAILED) LocalDate(java.time.LocalDate) IN_PROGRESS(gov.cms.ab2d.common.model.JobStatus.IN_PROGRESS) ReloadEvent(gov.cms.ab2d.eventlogger.events.ReloadEvent) PdpClient(gov.cms.ab2d.common.model.PdpClient) ApiResponseEvent(gov.cms.ab2d.eventlogger.events.ApiResponseEvent) AB2DPostgresqlContainer(gov.cms.ab2d.common.util.AB2DPostgresqlContainer) ApiRequestEvent(gov.cms.ab2d.eventlogger.events.ApiRequestEvent) ErrorEvent(gov.cms.ab2d.eventlogger.events.ErrorEvent) PostgreSQLContainer(org.testcontainers.containers.PostgreSQLContainer) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) SUCCESSFUL(gov.cms.ab2d.common.model.JobStatus.SUCCESSFUL) LoggableEvent(gov.cms.ab2d.eventlogger.LoggableEvent) CANCELLED(gov.cms.ab2d.common.model.JobStatus.CANCELLED) DataSetup(gov.cms.ab2d.common.util.DataSetup) JobStatusChangeEvent(gov.cms.ab2d.eventlogger.events.JobStatusChangeEvent) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) Container(org.testcontainers.junit.jupiter.Container) FileEvent(gov.cms.ab2d.eventlogger.events.FileEvent) Files(java.nio.file.Files) AuditMockJob(gov.cms.ab2d.audit.dto.AuditMockJob) OS(org.junit.jupiter.api.condition.OS) Testcontainers(org.testcontainers.junit.jupiter.Testcontainers) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) IOException(java.io.IOException) File(java.io.File) DisplayName(org.junit.jupiter.api.DisplayName) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ChronoUnit(java.time.temporal.ChronoUnit) JobAuditClientMock(gov.cms.ab2d.audit.remote.JobAuditClientMock) Paths(java.nio.file.Paths) Assertions(org.junit.jupiter.api.Assertions) AuditMockJob(gov.cms.ab2d.audit.dto.AuditMockJob) ArrayList(java.util.ArrayList) StaleJob(gov.cms.ab2d.common.dto.StaleJob) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with StaleJob

use of gov.cms.ab2d.common.dto.StaleJob in project ab2d by CMSgov.

the class FileDeletionServiceTest method init.

@BeforeEach
public void init() {
    pathsToDelete = new ArrayList<>();
    PdpClient pdpClient = dataSetup.setupPdpClient(List.of());
    // Connected to a job that is finished and has expired
    job = new AuditMockJob(new StaleJob(UUID.randomUUID().toString(), pdpClient.getOrganization()), SUCCESSFUL, OffsetDateTime.now().minusDays(4));
    jobAuditClientMock.update(job);
    // Connected to a job, but in progress
    jobInProgress = new AuditMockJob(new StaleJob(UUID.randomUUID().toString(), pdpClient.getOrganization()), IN_PROGRESS, null);
    jobAuditClientMock.update(jobInProgress);
    // Connected to a job that is finished where the file has yet to expire
    jobNotExpiredYet = new AuditMockJob(new StaleJob(UUID.randomUUID().toString(), pdpClient.getOrganization()), SUCCESSFUL, OffsetDateTime.now().minusHours(55));
    jobAuditClientMock.update(jobNotExpiredYet);
    jobCancelled = new AuditMockJob(new StaleJob(UUID.randomUUID().toString(), pdpClient.getOrganization()), CANCELLED, null);
    jobAuditClientMock.update(jobCancelled);
    jobFailed = new AuditMockJob(new StaleJob(UUID.randomUUID().toString(), pdpClient.getOrganization()), FAILED, null);
    jobAuditClientMock.update(jobFailed);
    efsMount = tmpDirFolder.toPath().toString();
    ReflectionTestUtils.setField(fileDeletionService, "efsMount", efsMount);
}
Also used : AuditMockJob(gov.cms.ab2d.audit.dto.AuditMockJob) PdpClient(gov.cms.ab2d.common.model.PdpClient) StaleJob(gov.cms.ab2d.common.dto.StaleJob) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with StaleJob

use of gov.cms.ab2d.common.dto.StaleJob in project ab2d by CMSgov.

the class JobServiceTest method checkForExpirations.

@Test
void checkForExpirations() {
    Job job = createJobAllContracts(NDJSON_FIRE_CONTENT_TYPE);
    job.setStatus(FAILED);
    jobRepository.save(job);
    dataSetup.queueForCleanup(job);
    List<StaleJob> staleJobs = jobService.checkForExpiration(of(job.getJobUuid()), 1);
    assertFalse(staleJobs.isEmpty());
    assertEquals(1, staleJobs.size());
    staleJobs = jobService.checkForExpiration(Collections.emptyList(), 1);
    assertTrue(staleJobs.isEmpty());
}
Also used : StaleJob(gov.cms.ab2d.common.dto.StaleJob) StaleJob(gov.cms.ab2d.common.dto.StaleJob) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with StaleJob

use of gov.cms.ab2d.common.dto.StaleJob in project ab2d by CMSgov.

the class EventUtilsTest method buildStaleFileEvent.

/*
     * A trivial test to get Sonar happy.
     */
@Test
void buildStaleFileEvent() {
    StaleJob staleJob = new StaleJob("job_uuid", "bogus incorporated");
    FileEvent fileEvent = EventUtils.getStaleFileEvent(staleJob, null, null);
    assertNotNull(fileEvent);
}
Also used : FileEvent(gov.cms.ab2d.eventlogger.events.FileEvent) StaleJob(gov.cms.ab2d.common.dto.StaleJob) Test(org.junit.jupiter.api.Test)

Example 5 with StaleJob

use of gov.cms.ab2d.common.dto.StaleJob in project ab2d by CMSgov.

the class FileDeletionServiceTest method testCleanOutDir.

@Test
void testCleanOutDir(@TempDir File tmpDir) throws IOException {
    ReflectionTestUtils.setField(fileDeletionService, "efsMount", tmpDir.getAbsolutePath());
    String jobId = job.getJobUuid();
    String miscString = "Hello";
    // Create the directories
    List<Path> files = new ArrayList<>();
    Path jobDir = Files.createDirectory(Path.of(tmpDir.getAbsolutePath(), jobId));
    Path finishedDir = Files.createDirectory(Path.of(tmpDir.getAbsolutePath(), jobId, "finished"));
    Path streamDir = Files.createDirectory(Path.of(tmpDir.getAbsolutePath(), jobId, "streaming"));
    // Create the files
    files.add(Files.createFile(Path.of(tmpDir.getAbsolutePath(), jobId, "tstfile.ndjson")));
    files.add(Files.createFile(Path.of(tmpDir.getAbsolutePath(), jobId, "tstfile.txt")));
    files.add(Files.createFile(Path.of(tmpDir.getAbsolutePath(), jobId, "finished", "tstfile.ndjson")));
    files.add(Files.createFile(Path.of(tmpDir.getAbsolutePath(), jobId, "streaming", "tstfile.ndjson")));
    files.add(Files.createFile(Path.of(tmpDir.getAbsolutePath(), jobId, "streaming", "tstfile.txt")));
    // Write data to the files
    for (Path file : files) {
        Files.writeString(file, miscString);
        changeFileCreationDate(file);
    }
    files.add(jobDir);
    files.add(finishedDir);
    files.add(streamDir);
    StaleJob staleJob = new StaleJob(job.getJobUuid(), job.getOrganization());
    fileDeletionService.deleteNdjsonFilesAndDirectory(staleJob, jobDir);
    List<Path> remaining = files.stream().filter(f -> f.toFile().exists()).collect(Collectors.toList());
    // There should be 4 remaining path - the top job directory, the streaming dir, the top level txt file and the s
    // streaming txt file
    assertEquals(4, remaining.size());
    assertEquals(2, remaining.stream().filter(f -> f.toFile().isDirectory()).count());
    assertEquals(2, remaining.stream().filter(f -> f.toFile().isFile()).filter(f -> f.toFile().getName().equals("tstfile.txt")).count());
    remaining.forEach(System.out::println);
}
Also used : Path(java.nio.file.Path) CoreMatchers.is(org.hamcrest.CoreMatchers.is) BeforeEach(org.junit.jupiter.api.BeforeEach) UtilMethods(gov.cms.ab2d.eventlogger.utils.UtilMethods) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) Autowired(org.springframework.beans.factory.annotation.Autowired) FileTime(java.nio.file.attribute.FileTime) StaleJob(gov.cms.ab2d.common.dto.StaleJob) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Path(java.nio.file.Path) MethodSource(org.junit.jupiter.params.provider.MethodSource) TestPropertySource(org.springframework.test.context.TestPropertySource) ContractSearchEvent(gov.cms.ab2d.eventlogger.events.ContractSearchEvent) UUID(java.util.UUID) Arguments(org.junit.jupiter.params.provider.Arguments) FileSystemUtils(org.springframework.util.FileSystemUtils) Collectors(java.util.stream.Collectors) LoggerEventRepository(gov.cms.ab2d.eventlogger.reports.sql.LoggerEventRepository) Test(org.junit.jupiter.api.Test) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Stream(java.util.stream.Stream) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) SpringBootApp(gov.cms.ab2d.audit.SpringBootApp) TempDir(org.junit.jupiter.api.io.TempDir) FAILED(gov.cms.ab2d.common.model.JobStatus.FAILED) LocalDate(java.time.LocalDate) IN_PROGRESS(gov.cms.ab2d.common.model.JobStatus.IN_PROGRESS) ReloadEvent(gov.cms.ab2d.eventlogger.events.ReloadEvent) PdpClient(gov.cms.ab2d.common.model.PdpClient) ApiResponseEvent(gov.cms.ab2d.eventlogger.events.ApiResponseEvent) AB2DPostgresqlContainer(gov.cms.ab2d.common.util.AB2DPostgresqlContainer) ApiRequestEvent(gov.cms.ab2d.eventlogger.events.ApiRequestEvent) ErrorEvent(gov.cms.ab2d.eventlogger.events.ErrorEvent) PostgreSQLContainer(org.testcontainers.containers.PostgreSQLContainer) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) SUCCESSFUL(gov.cms.ab2d.common.model.JobStatus.SUCCESSFUL) LoggableEvent(gov.cms.ab2d.eventlogger.LoggableEvent) CANCELLED(gov.cms.ab2d.common.model.JobStatus.CANCELLED) DataSetup(gov.cms.ab2d.common.util.DataSetup) JobStatusChangeEvent(gov.cms.ab2d.eventlogger.events.JobStatusChangeEvent) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) Container(org.testcontainers.junit.jupiter.Container) FileEvent(gov.cms.ab2d.eventlogger.events.FileEvent) Files(java.nio.file.Files) AuditMockJob(gov.cms.ab2d.audit.dto.AuditMockJob) OS(org.junit.jupiter.api.condition.OS) Testcontainers(org.testcontainers.junit.jupiter.Testcontainers) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) IOException(java.io.IOException) File(java.io.File) DisplayName(org.junit.jupiter.api.DisplayName) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ChronoUnit(java.time.temporal.ChronoUnit) JobAuditClientMock(gov.cms.ab2d.audit.remote.JobAuditClientMock) Paths(java.nio.file.Paths) Assertions(org.junit.jupiter.api.Assertions) ArrayList(java.util.ArrayList) StaleJob(gov.cms.ab2d.common.dto.StaleJob) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

StaleJob (gov.cms.ab2d.common.dto.StaleJob)6 AuditMockJob (gov.cms.ab2d.audit.dto.AuditMockJob)3 PdpClient (gov.cms.ab2d.common.model.PdpClient)3 FileEvent (gov.cms.ab2d.eventlogger.events.FileEvent)3 File (java.io.File)3 Test (org.junit.jupiter.api.Test)3 SpringBootApp (gov.cms.ab2d.audit.SpringBootApp)2 JobAuditClientMock (gov.cms.ab2d.audit.remote.JobAuditClientMock)2 CANCELLED (gov.cms.ab2d.common.model.JobStatus.CANCELLED)2 FAILED (gov.cms.ab2d.common.model.JobStatus.FAILED)2 IN_PROGRESS (gov.cms.ab2d.common.model.JobStatus.IN_PROGRESS)2 SUCCESSFUL (gov.cms.ab2d.common.model.JobStatus.SUCCESSFUL)2 AB2DPostgresqlContainer (gov.cms.ab2d.common.util.AB2DPostgresqlContainer)2 DataSetup (gov.cms.ab2d.common.util.DataSetup)2 LoggableEvent (gov.cms.ab2d.eventlogger.LoggableEvent)2 ApiRequestEvent (gov.cms.ab2d.eventlogger.events.ApiRequestEvent)2 ApiResponseEvent (gov.cms.ab2d.eventlogger.events.ApiResponseEvent)2 ContractSearchEvent (gov.cms.ab2d.eventlogger.events.ContractSearchEvent)2 ErrorEvent (gov.cms.ab2d.eventlogger.events.ErrorEvent)2 JobStatusChangeEvent (gov.cms.ab2d.eventlogger.events.JobStatusChangeEvent)2