/*
* Copyright (c) 2010-2016. Axon Framework
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.axonframework.commandhandling.model;
import org.axonframework.eventhandling.*;
import org.axonframework.messaging.unitofwork.CurrentUnitOfWork;
import org.axonframework.messaging.unitofwork.DefaultUnitOfWork;
import org.axonframework.messaging.unitofwork.UnitOfWork;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
/**
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/META-INF/spring/jpa-repository-context.xml", "/META-INF/spring/db-context.xml"})
@Transactional
public class JpaRepositoryIntegrationTest implements EventListener {
@Autowired
@Qualifier("simpleRepository")
private GenericJpaRepository<JpaAggregate> repository;
@Autowired
private EventBus eventBus;
@PersistenceContext
private EntityManager entityManager;
private final List<EventMessage> capturedEvents = new ArrayList<>();
private SubscribingEventProcessor eventProcessor;
@Before
public void setUp() {
eventProcessor = new SubscribingEventProcessor("test", new SimpleEventHandlerInvoker(this), eventBus);
eventProcessor.start();
}
@After
public void tearDown() {
eventProcessor.shutDown();
while (CurrentUnitOfWork.isStarted()) {
CurrentUnitOfWork.get().rollback();
}
}
@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 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 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()));
}
@Override
public void handle(EventMessage event) {
this.capturedEvents.add(event);
}
private UnitOfWork<?> startAndGetUnitOfWork() {
return DefaultUnitOfWork.startAndGet(null);
}
}