Search in sources :

Example 1 with ProgressReporter

use of com.enonic.xp.task.ProgressReporter in project xp by enonic.

the class ImportListenerImplTest method test.

@Test
void test() {
    final ProgressReporter progressReporter = mock(ProgressReporter.class);
    final ImportListenerImpl listener = new ImportListenerImpl(progressReporter);
    listener.nodeResolved(Integer.MAX_VALUE);
    listener.nodeImported(1);
    listener.nodeImported(2);
    final InOrder inOrder = inOrder(progressReporter);
    inOrder.verify(progressReporter).progress(1, Integer.MAX_VALUE);
    inOrder.verify(progressReporter).progress(3, Integer.MAX_VALUE);
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) ProgressReporter(com.enonic.xp.task.ProgressReporter) Test(org.junit.jupiter.api.Test)

Example 2 with ProgressReporter

use of com.enonic.xp.task.ProgressReporter in project xp by enonic.

the class VacuumTaskHandlerTest method vacuum.

@Test
public void vacuum() throws Exception {
    final TaskId taskId = TaskId.from("task");
    Mockito.when(vacuumService.vacuum(isA(VacuumParameters.class))).thenReturn(VacuumResult.create().build());
    TaskProgressReporterContext.withContext((id, progressReporter) -> runFunction("/test/VacuumTaskHandlerTest.js", "vacuum")).run(taskId, progressReporter);
    Mockito.verify(vacuumService, Mockito.times(1)).vacuum(paramsCaptor.capture());
    assertEquals("PT2S", paramsCaptor.getValue().getAgeThreshold().toString());
    assertEquals(2, paramsCaptor.getValue().getTaskNames().size());
    assertTrue(paramsCaptor.getValue().getTaskNames().containsAll(Set.of("a", "b")));
    assertNotNull(paramsCaptor.getValue().getVacuumListener());
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) VacuumResult(com.enonic.xp.vacuum.VacuumResult) Mock(org.mockito.Mock) VacuumParameters(com.enonic.xp.vacuum.VacuumParameters) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Set(java.util.Set) TaskProgressReporterContext(com.enonic.xp.task.TaskProgressReporterContext) VacuumService(com.enonic.xp.vacuum.VacuumService) Captor(org.mockito.Captor) TaskId(com.enonic.xp.task.TaskId) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ScriptTestSupport(com.enonic.xp.testing.ScriptTestSupport) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ProgressReporter(com.enonic.xp.task.ProgressReporter) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ArgumentMatchers.isA(org.mockito.ArgumentMatchers.isA) TaskId(com.enonic.xp.task.TaskId) VacuumParameters(com.enonic.xp.vacuum.VacuumParameters) Test(org.junit.jupiter.api.Test)

Example 3 with ProgressReporter

use of com.enonic.xp.task.ProgressReporter in project xp by enonic.

the class VacuumTaskHandler method execute.

public void execute() {
    final ProgressReporter progressReporter = TaskProgressReporterContext.current();
    final VacuumResult result = vacuumService.vacuum(VacuumParameters.create().ageThreshold(ageThreshold != null ? Duration.parse(ageThreshold) : null).taskNames(tasks == null || tasks.isEmpty() ? DEFAULT_VACUUM_TASKS : tasks).vacuumListener(new VacuumListenerImpl(progressReporter)).build());
    try {
        progressReporter.info(MAPPER.writeValueAsString(MAPPER.createObjectNode().putPOJO("taskResults", result.getResults())));
    } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
    }
}
Also used : VacuumResult(com.enonic.xp.vacuum.VacuumResult) ProgressReporter(com.enonic.xp.task.ProgressReporter) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) VacuumListenerImpl(com.enonic.xp.app.system.listener.VacuumListenerImpl)

Example 4 with ProgressReporter

use of com.enonic.xp.task.ProgressReporter in project xp by enonic.

the class VacuumTaskHandlerTest method vacuumDefaultParams.

@Test
public void vacuumDefaultParams() throws Exception {
    final TaskId taskId = TaskId.from("task");
    Mockito.when(vacuumService.vacuum(isA(VacuumParameters.class))).thenReturn(VacuumResult.create().build());
    TaskProgressReporterContext.withContext((id, progressReporter) -> runFunction("/test/VacuumTaskHandlerTest.js", "vacuumDefault")).run(taskId, progressReporter);
    Mockito.verify(vacuumService, Mockito.times(1)).vacuum(paramsCaptor.capture());
    assertNull(paramsCaptor.getValue().getAgeThreshold());
    assertEquals(2, paramsCaptor.getValue().getTaskNames().size());
    assertTrue(paramsCaptor.getValue().getTaskNames().containsAll(Set.of("SegmentVacuumTask", "VersionTableVacuumTask")));
    assertNotNull(paramsCaptor.getValue().getVacuumListener());
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) VacuumResult(com.enonic.xp.vacuum.VacuumResult) Mock(org.mockito.Mock) VacuumParameters(com.enonic.xp.vacuum.VacuumParameters) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) Set(java.util.Set) TaskProgressReporterContext(com.enonic.xp.task.TaskProgressReporterContext) VacuumService(com.enonic.xp.vacuum.VacuumService) Captor(org.mockito.Captor) TaskId(com.enonic.xp.task.TaskId) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ScriptTestSupport(com.enonic.xp.testing.ScriptTestSupport) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ProgressReporter(com.enonic.xp.task.ProgressReporter) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ArgumentMatchers.isA(org.mockito.ArgumentMatchers.isA) TaskId(com.enonic.xp.task.TaskId) VacuumParameters(com.enonic.xp.vacuum.VacuumParameters) Test(org.junit.jupiter.api.Test)

Example 5 with ProgressReporter

use of com.enonic.xp.task.ProgressReporter in project xp by enonic.

the class ExportListenerImplTest method test.

@Test
void test() {
    final ProgressReporter progressReporter = mock(ProgressReporter.class);
    final ExportListenerImpl listener = new ExportListenerImpl(progressReporter);
    listener.nodeResolved(Integer.MAX_VALUE);
    listener.nodeExported(1);
    listener.nodeExported(2);
    final InOrder inOrder = inOrder(progressReporter);
    inOrder.verify(progressReporter).progress(1, Integer.MAX_VALUE);
    inOrder.verify(progressReporter).progress(3, Integer.MAX_VALUE);
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) ProgressReporter(com.enonic.xp.task.ProgressReporter) Test(org.junit.jupiter.api.Test)

Aggregations

ProgressReporter (com.enonic.xp.task.ProgressReporter)5 Test (org.junit.jupiter.api.Test)4 VacuumResult (com.enonic.xp.vacuum.VacuumResult)3 TaskId (com.enonic.xp.task.TaskId)2 TaskProgressReporterContext (com.enonic.xp.task.TaskProgressReporterContext)2 ScriptTestSupport (com.enonic.xp.testing.ScriptTestSupport)2 VacuumParameters (com.enonic.xp.vacuum.VacuumParameters)2 VacuumService (com.enonic.xp.vacuum.VacuumService)2 Set (java.util.Set)2 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)2 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)2 Assertions.assertNull (org.junit.jupiter.api.Assertions.assertNull)2 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)2 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 ArgumentMatchers.isA (org.mockito.ArgumentMatchers.isA)2 Captor (org.mockito.Captor)2 InOrder (org.mockito.InOrder)2 Mock (org.mockito.Mock)2 Mockito (org.mockito.Mockito)2