/** * Commits the current UnitOfWork. If no UnitOfWork was started, an {@link IllegalStateException} is thrown. * * @throws IllegalStateException if no UnitOfWork is currently started. * @see UnitOfWork#commit() */ public static void commit() { get().commit(); }
@SuppressWarnings({"unchecked"}) @Test public void testStoreAndLoadNewAggregate() throws Exception { UnitOfWork<?> uow = startAndGetUnitOfWork(); String originalId = repository.newInstance(() -> new JpaAggregate("Hello")).invoke(JpaAggregate::getIdentifier); uow.commit(); entityManager.flush(); entityManager.clear(); List<JpaAggregate> results = entityManager.createQuery("SELECT a FROM JpaAggregate a").getResultList(); assertEquals(1, results.size()); JpaAggregate aggregate = results.get(0); assertEquals(originalId, aggregate.getIdentifier()); uow = startAndGetUnitOfWork(); Aggregate<JpaAggregate> storedAggregate = repository.load(originalId); uow.commit(); assertEquals(storedAggregate.identifierAsString(), originalId); assertTrue(capturedEvents.isEmpty()); }
@Test public void testDeleteAnAggregate() { JpaAggregate agg = new JpaAggregate("First message"); entityManager.persist(agg); entityManager.flush(); entityManager.clear(); assertEquals((Long) 0L, agg.getVersion()); UnitOfWork<?> uow = startAndGetUnitOfWork(); Aggregate<JpaAggregate> aggregate = repository.load(agg.getIdentifier()); aggregate.execute(r -> r.setMessage("And again")); aggregate.execute(r -> r.setMessage("And more")); aggregate.execute(JpaAggregate::delete); uow.commit(); entityManager.flush(); entityManager.clear(); assertEquals(2, capturedEvents.size()); assertNull(entityManager.find(JpaAggregate.class, aggregate.identifierAsString())); }
@Test public void testUpdateAnAggregate() { JpaAggregate agg = new JpaAggregate("First message"); entityManager.persist(agg); entityManager.flush(); entityManager.clear(); UnitOfWork<?> uow = startAndGetUnitOfWork(); Aggregate<JpaAggregate> aggregate = repository.load(agg.getIdentifier()); aggregate.execute(r -> r.setMessage("And again")); aggregate.execute(r -> r.setMessage("And more")); uow.commit(); assertEquals((Long) 1L, aggregate.version()); assertEquals(2, capturedEvents.size()); assertNotNull(entityManager.find(JpaAggregate.class, aggregate.identifierAsString())); }
@Test public void orderInEventSourcedAggregate() { Repository<MyAggregate> repository = configuration.repository(MyAggregate.class); configuration.commandGateway().sendAndWait(command); UnitOfWork unitOfWork = DefaultUnitOfWork.startAndGet(GenericCommandMessage.asCommandMessage("loading")); MyAggregate loadedAggregate = repository.load(aggregateIdentifier).invoke(Function.identity()); unitOfWork.commit(); assertEquals(expectedDescriptions(command), loadedAggregate.getHandledCommands()); }
/** * Commits the current UnitOfWork. If no UnitOfWork was started, an {@link IllegalStateException} is thrown. * * @throws IllegalStateException if no UnitOfWork is currently started. * @see UnitOfWork#commit() */ public static void commit() { get().commit(); }
/** * Commits the current UnitOfWork. If no UnitOfWork was started, an {@link IllegalStateException} is thrown. * * @throws IllegalStateException if no UnitOfWork is currently started. * @see UnitOfWork#commit() */ public static void commit() { get().commit(); }