added more test cases

This commit is contained in:
devender.kumar 2019-11-02 21:40:55 +01:00
parent fb34b40870
commit 344846c9ca
3 changed files with 16 additions and 9 deletions

View File

@ -15,10 +15,6 @@ public class ExtendedPersistenceContextUserService {
@PersistenceContext(type = PersistenceContextType.EXTENDED) @PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager entityManager; private EntityManager entityManager;
public EntityManager getEntityManager() {
return entityManager;
}
@Transactional @Transactional
public User insertWithTransaction(User user) { public User insertWithTransaction(User user) {
entityManager.persist(user); entityManager.persist(user);

View File

@ -15,10 +15,6 @@ public class TransctionPersistenceContextUserService {
@PersistenceContext @PersistenceContext
private EntityManager entityManager; private EntityManager entityManager;
public EntityManager getEntityManager() {
return entityManager;
}
@Transactional @Transactional
public User insertWithTransaction(User user) { public User insertWithTransaction(User user) {
entityManager.persist(user); entityManager.persist(user);

View File

@ -40,7 +40,7 @@ public class PersistenceContextIntegrationTest {
} }
@Test(expected = HttpServerErrorException.class) @Test(expected = HttpServerErrorException.class)
public void testThatWhenUserSaveWithOutTransactionInPersistenceContextTypeTransactionThenShouldFailOnPersist() { public void testThatWhenUserSaveWithOutTransactionInPersistenceContextTypeTransactionThenShouldWhtowTransactionRequiredExceptionOnPersist() {
User user = new User(122L, "Devender", "admin"); User user = new User(122L, "Devender", "admin");
restTemplate.postForEntity(buildRequestUrl("v4/user/"), user, User.class); restTemplate.postForEntity(buildRequestUrl("v4/user/"), user, User.class);
} }
@ -55,6 +55,21 @@ public class PersistenceContextIntegrationTest {
assertThat(userFromTransctionPersistenceContext, Is.is(IsNull.nullValue())); assertThat(userFromTransctionPersistenceContext, Is.is(IsNull.nullValue()));
} }
@Test(expected = HttpServerErrorException.class)
public void testThatWhenAddUserWithSameIdentifierInPersistenceContextThenShouldThrowException() {
User user = new User(126L, "Devender", "admin");
restTemplate.postForEntity(buildRequestUrl("v2/user/"), user, User.class);
restTemplate.postForEntity(buildRequestUrl("v2/user/"), user, User.class);
}
@Test
public void testThatWhenUserSavedWithExtendedPersistenceContextWithTransactionThenUserShouldSaveEntityIntoDB() {
User user = new User(127L, "Devender", "admin");
restTemplate.postForEntity(buildRequestUrl("v3/user/"), user, User.class);
User userFromDB = restTemplate.getForObject(buildRequestUrl("v1/user/127"), User.class);
assertThat(userFromDB, Is.is(IsNull.notNullValue()));
}
@Test @Test
public void testThatWhenUserSavedWithExtendedPersistenceContextWithTransactionThenUserShouldFlushCachedEntityIntoDB() { public void testThatWhenUserSavedWithExtendedPersistenceContextWithTransactionThenUserShouldFlushCachedEntityIntoDB() {
User user = new User(124L, "Devender", "admin"); User user = new User(124L, "Devender", "admin");