|
| 1 | +package com.baeldung.dao.repositories; |
| 2 | + |
| 3 | +import com.baeldung.domain.Person; |
| 4 | +import org.junit.Test; |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.runner.RunWith; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; |
| 9 | +import org.springframework.dao.DataIntegrityViolationException; |
| 10 | +import org.springframework.test.context.junit4.SpringRunner; |
| 11 | + |
| 12 | +import java.util.Optional; |
| 13 | + |
| 14 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 15 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 16 | + |
| 17 | +@RunWith(SpringRunner.class) |
| 18 | +@DataJpaTest |
| 19 | +public class PersonInsertRepositoryIntegrationTest { |
| 20 | + |
| 21 | + private static final Long ID = 1L; |
| 22 | + private static final String FIRST_NAME = "firstname"; |
| 23 | + private static final String LAST_NAME = "lastname"; |
| 24 | + private static final Person PERSON = new Person(ID, FIRST_NAME, LAST_NAME); |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private PersonQueryRepository personQueryRepository; |
| 28 | + |
| 29 | + @Autowired |
| 30 | + private PersonEntityManagerRepository personEntityManagerRepository; |
| 31 | + |
| 32 | + @BeforeEach |
| 33 | + public void clearDB() { |
| 34 | + personQueryRepository.deleteAll(); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void givenPersonEntity_whenInsertWithNativeQuery_ThenPersonIsPersisted() { |
| 39 | + insertPerson(); |
| 40 | + |
| 41 | + assertPersonPersisted(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void givenPersonEntity_whenInsertedTwiceWithNativeQuery_thenDataIntegrityViolationExceptionIsThrown() { |
| 46 | + assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy(() -> { |
| 47 | + insertPerson(); |
| 48 | + insertPerson(); |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void givenPersonEntity_whenInsertWithQueryAnnotation_thenPersonIsPersisted() { |
| 54 | + insertPersonWithQueryAnnotation(); |
| 55 | + |
| 56 | + assertPersonPersisted(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void givenPersonEntity_whenInsertedTwiceWithQueryAnnotation_thenDataIntegrityViolationExceptionIsThrown() { |
| 61 | + assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy(() -> { |
| 62 | + insertPersonWithQueryAnnotation(); |
| 63 | + insertPersonWithQueryAnnotation(); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void givenPersonEntity_whenInsertWithEntityManager_thenPersonIsPersisted() { |
| 69 | + insertPersonWithEntityManager(); |
| 70 | + |
| 71 | + assertPersonPersisted(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void givenPersonEntity_whenInsertedTwiceWithEntityManager_thenDataIntegrityViolationExceptionIsThrown() { |
| 76 | + assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy(() -> { |
| 77 | + insertPersonWithEntityManager(); |
| 78 | + insertPersonWithEntityManager(); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + private void insertPerson() { |
| 83 | + personQueryRepository.insert(PERSON); |
| 84 | + } |
| 85 | + |
| 86 | + private void insertPersonWithQueryAnnotation() { |
| 87 | + personQueryRepository.insertWithAnnotation(ID, FIRST_NAME, LAST_NAME); |
| 88 | + } |
| 89 | + |
| 90 | + private void insertPersonWithEntityManager() { |
| 91 | + personEntityManagerRepository.insert(new Person(ID, FIRST_NAME, LAST_NAME)); |
| 92 | + } |
| 93 | + |
| 94 | + private void assertPersonPersisted() { |
| 95 | + Optional<Person> personOptional = personQueryRepository.findById(PERSON.getId()); |
| 96 | + |
| 97 | + assertThat(personOptional.isPresent()).isTrue(); |
| 98 | + assertThat(personOptional.get().getId()).isEqualTo(PERSON.getId()); |
| 99 | + assertThat(personOptional.get().getFirstName()).isEqualTo(PERSON.getFirstName()); |
| 100 | + assertThat(personOptional.get().getLastName()).isEqualTo(PERSON.getLastName()); |
| 101 | + } |
| 102 | +} |
0 commit comments