Search in sources :

Example 11 with PersistentStep

use of org.talend.dataprep.preparation.store.PersistentStep in project data-prep by Talend.

the class PreparationCleanerTest method shouldNotRemovePreparationActions_sharedByMultiplePreparation.

@Test
public void shouldNotRemovePreparationActions_sharedByMultiplePreparation() throws Exception {
    // given
    final String version = "1.2.3";
    final PreparationActions content = new PreparationActions();
    List<Action> actions = new ArrayList<>();
    content.append(actions);
    content.setAppVersion(version);
    when(repository.list(PreparationActions.class)).thenReturn(Stream.of(content));
    when(repository.get(content.id(), PreparationActions.class)).thenReturn(content);
    // 2 preparations, with each, one step that shares the same action
    final Step stepFirstPreparation = new Step(Step.ROOT_STEP.getId(), content.getId(), version);
    final Step stepSecondPreparation = new Step(Step.ROOT_STEP.getId(), content.getId(), version);
    // add the steps to the repository
    when(repository.exist(eq(PersistentStep.class), eq(TqlBuilder.eq("contentId", content.id())))).thenReturn(true);
    when(repository.list(Step.class)).thenReturn(Stream.of(stepFirstPreparation, stepSecondPreparation));
    when(repository.list(eq(PersistentStep.class))).thenReturn(Stream.of(stepFirstPreparation, stepSecondPreparation).map(s -> {
        final PersistentStep persistentStep = new PersistentStep();
        persistentStep.setId(s.id());
        persistentStep.setContent(s.getContent());
        return persistentStep;
    }));
    Preparation firstPreparation = new Preparation("1", null, stepFirstPreparation.getId(), version);
    firstPreparation.setSteps(Collections.singletonList(stepFirstPreparation));
    Preparation secondPreparation = new Preparation("2", null, stepSecondPreparation.getId(), version);
    secondPreparation.setSteps(Collections.singletonList(stepSecondPreparation));
    // add the preparations to the repository
    when(repository.list(Preparation.class)).thenReturn(Stream.of(firstPreparation, secondPreparation));
    // when
    cleaner.removeOrphanSteps();
    // then
    verify(repository, never()).remove(eq(content));
}
Also used : InjectMocks(org.mockito.InjectMocks) Preparation(org.talend.dataprep.api.preparation.Preparation) Arrays(java.util.Arrays) TqlBuilder(org.talend.tql.api.TqlBuilder) Mock(org.mockito.Mock) BaseMaintenanceTest(org.talend.dataprep.maintenance.BaseMaintenanceTest) PersistentStep(org.talend.dataprep.preparation.store.PersistentStep) Test(org.junit.Test) Step(org.talend.dataprep.api.preparation.Step) PreparationRepository(org.talend.dataprep.preparation.store.PreparationRepository) ArrayList(java.util.ArrayList) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) PreparationActions(org.talend.dataprep.api.preparation.PreparationActions) Action(org.talend.dataprep.api.preparation.Action) Collections(java.util.Collections) PersistentStep(org.talend.dataprep.preparation.store.PersistentStep) Action(org.talend.dataprep.api.preparation.Action) Preparation(org.talend.dataprep.api.preparation.Preparation) ArrayList(java.util.ArrayList) PreparationActions(org.talend.dataprep.api.preparation.PreparationActions) PersistentStep(org.talend.dataprep.preparation.store.PersistentStep) Step(org.talend.dataprep.api.preparation.Step) BaseMaintenanceTest(org.talend.dataprep.maintenance.BaseMaintenanceTest) Test(org.junit.Test)

Example 12 with PersistentStep

use of org.talend.dataprep.preparation.store.PersistentStep in project data-prep by Talend.

the class PreparationCleanerTest method shouldNotRemovePreparationActions_ownedByOnePreparation.

@Test
public void shouldNotRemovePreparationActions_ownedByOnePreparation() throws Exception {
    // given
    final String version = "1.2.3";
    final PreparationActions content = new PreparationActions();
    List<Action> actions = new ArrayList<>();
    content.append(actions);
    content.setAppVersion(version);
    when(repository.list(PreparationActions.class)).thenReturn(Stream.of(content));
    when(repository.get(content.id(), PreparationActions.class)).thenReturn(content);
    // 2 preparations, with each, one step that shares the same action
    final Step stepFirstPreparation = new Step(Step.ROOT_STEP.getId(), content.getId(), version);
    final Step stepSecondPreparation = new Step(Step.ROOT_STEP.getId(), content.getId(), version);
    // add the steps to the repository
    when(repository.list(Step.class)).thenReturn(Stream.of(stepFirstPreparation, stepSecondPreparation));
    when(repository.list(eq(PersistentStep.class))).thenReturn(Stream.of(stepFirstPreparation, stepSecondPreparation).map(s -> {
        final PersistentStep persistentStep = new PersistentStep();
        persistentStep.setId(s.id());
        persistentStep.setContent(s.getContent());
        return persistentStep;
    }));
    Preparation firstPreparation = new Preparation("1", null, stepFirstPreparation.getId(), version);
    firstPreparation.setSteps(Collections.singletonList(stepFirstPreparation));
    Preparation secondPreparation = new Preparation("2", null, stepSecondPreparation.getId(), version);
    secondPreparation.setSteps(Collections.singletonList(stepSecondPreparation));
    // add the preparations to the repository
    when(repository.exist(eq(PersistentStep.class), any())).thenReturn(true);
    // Remove first preparation
    when(repository.list(Preparation.class)).thenReturn(Stream.of(secondPreparation));
    // when
    cleaner.removeOrphanSteps();
    // then
    // when the first preparation is removed, the shared action is not deleted
    verify(repository, never()).remove(eq(content));
}
Also used : InjectMocks(org.mockito.InjectMocks) Preparation(org.talend.dataprep.api.preparation.Preparation) Arrays(java.util.Arrays) TqlBuilder(org.talend.tql.api.TqlBuilder) Mock(org.mockito.Mock) BaseMaintenanceTest(org.talend.dataprep.maintenance.BaseMaintenanceTest) PersistentStep(org.talend.dataprep.preparation.store.PersistentStep) Test(org.junit.Test) Step(org.talend.dataprep.api.preparation.Step) PreparationRepository(org.talend.dataprep.preparation.store.PreparationRepository) ArrayList(java.util.ArrayList) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) PreparationActions(org.talend.dataprep.api.preparation.PreparationActions) Action(org.talend.dataprep.api.preparation.Action) Collections(java.util.Collections) PersistentStep(org.talend.dataprep.preparation.store.PersistentStep) Action(org.talend.dataprep.api.preparation.Action) Preparation(org.talend.dataprep.api.preparation.Preparation) ArrayList(java.util.ArrayList) PreparationActions(org.talend.dataprep.api.preparation.PreparationActions) PersistentStep(org.talend.dataprep.preparation.store.PersistentStep) Step(org.talend.dataprep.api.preparation.Step) BaseMaintenanceTest(org.talend.dataprep.maintenance.BaseMaintenanceTest) Test(org.junit.Test)

Example 13 with PersistentStep

use of org.talend.dataprep.preparation.store.PersistentStep in project data-prep by Talend.

the class PreparationCleanerTest method removeOrphanSteps_should_remove_orphan_step_after_at_least_X_hours.

@Test
public void removeOrphanSteps_should_remove_orphan_step_after_at_least_X_hours() {
    // given
    final String version = "1.2.3";
    final Step firstStep = new Step(Step.ROOT_STEP.id(), "first", version);
    final Step secondStep = new Step(firstStep.id(), "second", version);
    final Step orphanStep = new Step(secondStep.id(), "orphan", version);
    when(repository.list(eq(Step.class))).thenReturn(Stream.of(firstStep, secondStep, orphanStep));
    when(repository.list(eq(PersistentStep.class))).thenReturn(Stream.of(firstStep, secondStep, orphanStep).map(s -> {
        final PersistentStep persistentStep = new PersistentStep();
        persistentStep.setId(s.id());
        persistentStep.setContent(s.getContent());
        return persistentStep;
    }));
    final Preparation preparation = new Preparation("#123", "1", secondStep.id(), version);
    preparation.setSteps(Arrays.asList(secondStep, firstStep));
    when(repository.list(eq(Preparation.class))).thenReturn(Stream.of(preparation));
    // when
    cleaner.removeOrphanSteps();
    // then
    verify(repository, never()).remove(eq(firstStep));
    verify(repository, never()).remove(eq(secondStep));
    verify(repository, times(1)).remove(eq(orphanStep));
}
Also used : InjectMocks(org.mockito.InjectMocks) Preparation(org.talend.dataprep.api.preparation.Preparation) Arrays(java.util.Arrays) TqlBuilder(org.talend.tql.api.TqlBuilder) Mock(org.mockito.Mock) BaseMaintenanceTest(org.talend.dataprep.maintenance.BaseMaintenanceTest) PersistentStep(org.talend.dataprep.preparation.store.PersistentStep) Test(org.junit.Test) Step(org.talend.dataprep.api.preparation.Step) PreparationRepository(org.talend.dataprep.preparation.store.PreparationRepository) ArrayList(java.util.ArrayList) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) PreparationActions(org.talend.dataprep.api.preparation.PreparationActions) Action(org.talend.dataprep.api.preparation.Action) Collections(java.util.Collections) PersistentStep(org.talend.dataprep.preparation.store.PersistentStep) Preparation(org.talend.dataprep.api.preparation.Preparation) PersistentStep(org.talend.dataprep.preparation.store.PersistentStep) Step(org.talend.dataprep.api.preparation.Step) BaseMaintenanceTest(org.talend.dataprep.maintenance.BaseMaintenanceTest) Test(org.junit.Test)

Example 14 with PersistentStep

use of org.talend.dataprep.preparation.store.PersistentStep in project data-prep by Talend.

the class PreparationCleanerTest method shouldRemovePreparationActions_noPreparation.

@Test
public void shouldRemovePreparationActions_noPreparation() throws Exception {
    // given
    final String version = "1.2.3";
    final PreparationActions content = new PreparationActions();
    List<Action> actions = new ArrayList<>();
    content.append(actions);
    content.setAppVersion(version);
    when(repository.list(PreparationActions.class)).thenReturn(Stream.of(content));
    when(repository.get(content.id(), PreparationActions.class)).thenReturn(content);
    // 2 preparations, with each, one step that shares the same action
    final Step stepFirstPreparation = new Step(Step.ROOT_STEP.getId(), content.getId(), version);
    final Step stepSecondPreparation = new Step(Step.ROOT_STEP.getId(), content.getId(), version);
    // add the steps to the repository
    when(repository.list(Step.class)).thenReturn(Stream.of(stepFirstPreparation, stepSecondPreparation));
    when(repository.list(eq(PersistentStep.class))).thenReturn(Stream.of(stepFirstPreparation, stepSecondPreparation).map(s -> {
        final PersistentStep persistentStep = new PersistentStep();
        persistentStep.setId(s.id());
        persistentStep.setContent(s.getContent());
        return persistentStep;
    }));
    Preparation firstPreparation = new Preparation("1", null, stepFirstPreparation.getId(), version);
    firstPreparation.setSteps(Collections.singletonList(stepFirstPreparation));
    Preparation secondPreparation = new Preparation("2", null, stepSecondPreparation.getId(), version);
    secondPreparation.setSteps(Collections.singletonList(stepSecondPreparation));
    // when
    when(repository.exist(eq(PersistentStep.class), eq(TqlBuilder.eq("contentId", content.id())))).thenReturn(false);
    // Remove first and second preparations
    when(repository.list(Preparation.class)).thenReturn(Stream.empty());
    cleaner.removeOrphanSteps();
    // then
    // 2 steps content
    verify(repository, times(2)).remove(eq(content));
}
Also used : InjectMocks(org.mockito.InjectMocks) Preparation(org.talend.dataprep.api.preparation.Preparation) Arrays(java.util.Arrays) TqlBuilder(org.talend.tql.api.TqlBuilder) Mock(org.mockito.Mock) BaseMaintenanceTest(org.talend.dataprep.maintenance.BaseMaintenanceTest) PersistentStep(org.talend.dataprep.preparation.store.PersistentStep) Test(org.junit.Test) Step(org.talend.dataprep.api.preparation.Step) PreparationRepository(org.talend.dataprep.preparation.store.PreparationRepository) ArrayList(java.util.ArrayList) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) PreparationActions(org.talend.dataprep.api.preparation.PreparationActions) Action(org.talend.dataprep.api.preparation.Action) Collections(java.util.Collections) PersistentStep(org.talend.dataprep.preparation.store.PersistentStep) Action(org.talend.dataprep.api.preparation.Action) Preparation(org.talend.dataprep.api.preparation.Preparation) ArrayList(java.util.ArrayList) PreparationActions(org.talend.dataprep.api.preparation.PreparationActions) PersistentStep(org.talend.dataprep.preparation.store.PersistentStep) Step(org.talend.dataprep.api.preparation.Step) BaseMaintenanceTest(org.talend.dataprep.maintenance.BaseMaintenanceTest) Test(org.junit.Test)

Aggregations

PersistentStep (org.talend.dataprep.preparation.store.PersistentStep)14 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)9 Action (org.talend.dataprep.api.preparation.Action)9 PreparationActions (org.talend.dataprep.api.preparation.PreparationActions)9 PreparationRepository (org.talend.dataprep.preparation.store.PreparationRepository)9 Preparation (org.talend.dataprep.api.preparation.Preparation)8 Step (org.talend.dataprep.api.preparation.Step)7 BaseMaintenanceTest (org.talend.dataprep.maintenance.BaseMaintenanceTest)7 Arrays (java.util.Arrays)6 Collections (java.util.Collections)6 List (java.util.List)6 Stream (java.util.stream.Stream)6 InjectMocks (org.mockito.InjectMocks)6 Mock (org.mockito.Mock)6 Mockito (org.mockito.Mockito)6 TqlBuilder (org.talend.tql.api.TqlBuilder)6 DataSetMetadata (org.talend.dataprep.api.dataset.DataSetMetadata)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 PostConstruct (javax.annotation.PostConstruct)1