Search in sources :

Example 1 with Usecase

use of org.qi4j.api.usecase.Usecase in project qi4j-sdk by Qi4j.

the class SchedulerTest method testTaskWithoutScheduling.

@Test
public void testTaskWithoutScheduling() throws UnitOfWorkCompletionException {
    Usecase usecase = UsecaseBuilder.newUsecase("testTask");
    String taskId;
    try (UnitOfWork uow = module.newUnitOfWork(usecase)) {
        FooTask task = createFooTask(uow, "TestTask", BAZAR);
        taskId = task.identity().get();
        task.run();
        uow.complete();
    }
    try (UnitOfWork uow = module.newUnitOfWork(usecase)) {
        FooTask task = uow.get(FooTask.class, taskId);
        assertThat(task.output().get(), equalTo(BAR));
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Usecase(org.qi4j.api.usecase.Usecase) Test(org.junit.Test)

Example 2 with Usecase

use of org.qi4j.api.usecase.Usecase in project qi4j-sdk by Qi4j.

the class TestApplication method prepareTest.

@Before
public void prepareTest() throws Exception {
    logger.info(name.getMethodName());
    Usecase usecase = UsecaseBuilder.newUsecase("Usecase:" + name);
    UnitOfWork uow = module.newUnitOfWork(usecase);
    populateTestData();
    ServiceReference<RouteSpecificationFactoryService> routeSpecFactoryServiceRef = module.findService(RouteSpecificationFactoryService.class);
    routeSpecFactory = routeSpecFactoryServiceRef.get();
    // Separate test suites in console output
    System.out.println();
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Usecase(org.qi4j.api.usecase.Usecase) RouteSpecificationFactoryService(org.qi4j.sample.dcicargo.sample_b.data.factory.RouteSpecificationFactoryService) Before(org.junit.Before)

Example 3 with Usecase

use of org.qi4j.api.usecase.Usecase in project qi4j-sdk by Qi4j.

the class SchedulerTest method testMinutely.

@Test
public void testMinutely() throws Exception {
    Usecase usecase = UsecaseBuilder.newUsecase("testMinutely");
    UnitOfWork uow = module.newUnitOfWork(usecase);
    try {
        Scheduler scheduler = module.<Scheduler>findService(Scheduler.class).get();
        DateTime start = new DateTime();
        FooTask task = createFooTask(uow, "TestMinutely", Constants.BAZAR);
        String taskIdentity = task.identity().get();
        DateTime expectedRun = start.withMillisOfSecond(0).withSecondOfMinute(0).plusMinutes(1);
        scheduler.scheduleCron(task, "@minutely", true);
        uow.complete();
        LOGGER.info("Task scheduled on {} to be run at {}", start.getMillis(), expectedRun.getMillis());
        // waiting a little more
        Thread.sleep(new Interval(start, expectedRun).toDurationMillis() + 5000);
        usecase = UsecaseBuilder.newUsecase("testMinutely");
        uow = module.newUnitOfWork(usecase);
        task = uow.get(FooTask.class, taskIdentity);
        assertNotNull(task);
        assertEquals(Constants.BAR, task.output().get());
        Timeline timeline = module.<Timeline>findService(Timeline.class).get();
        DateTime now = new DateTime();
        // Queries returning past records
        assertEquals(2, Iterables.count(timeline.getLastRecords(5)));
        assertEquals(2, Iterables.count(timeline.getRecords(start.getMillis(), now.getMillis())));
        // Queries returning future records
        assertEquals(4, Iterables.count(timeline.getNextRecords(4)));
        assertEquals(5, Iterables.count(timeline.getRecords(now.getMillis() + 100, now.plusMinutes(5).getMillis())));
        // Queries returning mixed past and future records
        assertEquals(7, Iterables.count(timeline.getRecords(start.getMillis(), now.plusMinutes(5).getMillis())));
        uow.complete();
    } finally {
        if (uow.isOpen()) {
            uow.discard();
        }
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Timeline(org.qi4j.library.scheduler.timeline.Timeline) Usecase(org.qi4j.api.usecase.Usecase) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Test(org.junit.Test)

Example 4 with Usecase

use of org.qi4j.api.usecase.Usecase in project qi4j-sdk by Qi4j.

the class SchedulerTest method testTask.

@Test
public void testTask() throws UnitOfWorkCompletionException, InterruptedException {
    Usecase usecase = UsecaseBuilder.newUsecase("testTask");
    UnitOfWork uow = module.newUnitOfWork(usecase);
    FooTask task = createFooTask(uow, "TestTask", Constants.BAZAR);
    String taskId = task.identity().get();
    task.run();
    uow.complete();
    usecase = UsecaseBuilder.newUsecase("testTask");
    uow = module.newUnitOfWork(usecase);
    task = uow.get(FooTask.class, taskId);
    assertEquals(Constants.BAR, task.output().get());
    Thread.sleep(10 * 1000);
    uow.complete();
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Usecase(org.qi4j.api.usecase.Usecase) Test(org.junit.Test)

Example 5 with Usecase

use of org.qi4j.api.usecase.Usecase in project qi4j-sdk by Qi4j.

the class SchedulerTest method testOnce.

@Test
public void testOnce() throws UnitOfWorkCompletionException, InterruptedException {
    Usecase usecase = UsecaseBuilder.newUsecase("testOnce");
    UnitOfWork uow = module.newUnitOfWork(usecase);
    try {
        Scheduler scheduler = module.<Scheduler>findService(Scheduler.class).get();
        FooTask task = createFooTask(uow, "TestOnce", Constants.BAZAR);
        String taskIdentity = task.identity().get();
        scheduler.scheduleOnce(task, 4, true);
        uow.complete();
        Thread.sleep(5000);
        usecase = UsecaseBuilder.newUsecase("testOnce");
        uow = module.newUnitOfWork(usecase);
        task = uow.get(FooTask.class, taskIdentity);
        assertNotNull(task);
        assertEquals(Constants.BAR, task.output().get());
        uow.complete();
    } finally {
        if (uow.isOpen()) {
            uow.discard();
        }
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Usecase(org.qi4j.api.usecase.Usecase) Test(org.junit.Test)

Aggregations

Usecase (org.qi4j.api.usecase.Usecase)12 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)7 Test (org.junit.Test)5 Before (org.junit.Before)2 EntityReference (org.qi4j.api.entity.EntityReference)2 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)2 EntityStoreUnitOfWork (org.qi4j.spi.entitystore.EntityStoreUnitOfWork)2 ResourceException (org.restlet.resource.ResourceException)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 DateTime (org.joda.time.DateTime)1 Interval (org.joda.time.Interval)1 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)1 EntityComposite (org.qi4j.api.entity.EntityComposite)1 EntityDescriptor (org.qi4j.api.entity.EntityDescriptor)1 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)1 ConcurrentEntityModificationException (org.qi4j.api.unitofwork.ConcurrentEntityModificationException)1 UsecaseBuilder (org.qi4j.api.usecase.UsecaseBuilder)1