|
| 1 | +package com.bobocode; |
| 2 | + |
| 3 | +import com.bobocode.exception.QueryHelperException; |
| 4 | +import com.bobocode.model.Account; |
| 5 | +import com.bobocode.util.EntityManagerUtil; |
| 6 | +import com.bobocode.util.TestDataGenerator; |
| 7 | +import org.junit.jupiter.api.AfterAll; |
| 8 | +import org.junit.jupiter.api.BeforeAll; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import javax.persistence.EntityManagerFactory; |
| 12 | +import javax.persistence.Persistence; |
| 13 | + |
| 14 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 15 | +import static org.hamcrest.Matchers.*; |
| 16 | +import static org.junit.jupiter.api.Assertions.fail; |
| 17 | + |
| 18 | + |
| 19 | +public class QueryHelperTest { |
| 20 | + private static EntityManagerFactory entityManagerFactory; |
| 21 | + private static EntityManagerUtil emUtil; |
| 22 | + private static QueryHelper queryHelper; |
| 23 | + |
| 24 | + @BeforeAll |
| 25 | + public static void setup() { |
| 26 | + entityManagerFactory = Persistence.createEntityManagerFactory("Account"); |
| 27 | + emUtil = new EntityManagerUtil(entityManagerFactory); |
| 28 | + queryHelper = new QueryHelper(entityManagerFactory); |
| 29 | + } |
| 30 | + |
| 31 | + @AfterAll |
| 32 | + static void destroy() { |
| 33 | + entityManagerFactory.close(); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testQueryHelperReturnsResult() { |
| 38 | + Account account = saveRandomAccount(); |
| 39 | + Long accountId = account.getId(); |
| 40 | + |
| 41 | + Account foundAccount = queryHelper.readWithinTx(entityManager -> entityManager.find(Account.class, accountId)); |
| 42 | + |
| 43 | + assertThat(foundAccount, notNullValue()); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testQueryHelperUsesReadOnly() { |
| 48 | + Account account = saveRandomAccount(); |
| 49 | + Long accountId = account.getId(); |
| 50 | + |
| 51 | + tryToUpdateFirstName(accountId); |
| 52 | + Account foundAccount = queryHelper.readWithinTx(entityManager -> entityManager.find(Account.class, accountId)); |
| 53 | + |
| 54 | + assertThat(foundAccount.getFirstName(), not(equalTo("XXX"))); |
| 55 | + assertThat(foundAccount.getFirstName(), equalTo(account.getFirstName())); |
| 56 | + } |
| 57 | + |
| 58 | + private Account saveRandomAccount() { |
| 59 | + Account account = TestDataGenerator.generateAccount(); |
| 60 | + emUtil.performWithinTx(entityManager -> entityManager.persist(account)); |
| 61 | + return account; |
| 62 | + } |
| 63 | + |
| 64 | + private void tryToUpdateFirstName(Long accountId) { |
| 65 | + queryHelper.readWithinTx(entityManager -> { |
| 66 | + Account managedAccount = entityManager.find(Account.class, accountId); |
| 67 | + managedAccount.setFirstName("XXX"); |
| 68 | + return managedAccount; |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testQueryHelperThrowsException() { |
| 75 | + Account account = TestDataGenerator.generateAccount(); |
| 76 | + emUtil.performWithinTx(entityManager -> entityManager.persist(account)); |
| 77 | + try { |
| 78 | + queryHelper.readWithinTx(entityManager -> { |
| 79 | + Account managedAccount = entityManager.find(Account.class, account.getId()); |
| 80 | + throwException(); |
| 81 | + return managedAccount; |
| 82 | + }); |
| 83 | + fail("Exception should be thrown"); |
| 84 | + } catch (Exception e) { |
| 85 | + assertThat(e.getClass(), equalTo(QueryHelperException.class)); |
| 86 | + assertThat(e.getMessage(), containsString("Transaction is rolled back")); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private void throwException() { |
| 91 | + throw new RuntimeException("Runtime error"); |
| 92 | + } |
| 93 | +} |
0 commit comments