BAEL-2090 Insert with JPA

* Adding inserts for JPA objects repositories

* Refactor tests

* Small refactor

* Small refactor

* Remove redundant @Repository annotation

* Refactor tests

* Change to simple entity manager implementation

* Refactor changes

* Remove Spring repositories, Add simple implementation using EntityManager

* Remove redundant Interface

* Remove redundant files

* Update persistence-modules/spring-data-jpa/src/main/java/com/baeldung/dao/repositories/impl/PersonInsertRepository.java

Co-Authored-By: FrancoCorleone <jedfra6@gmail.com>
This commit is contained in:
FrancoCorleone
2018-12-29 00:18:30 +01:00
committed by Emily Cheyne
parent a1b1e02e71
commit 2c801888e7
8 changed files with 35 additions and 116 deletions
@@ -1,21 +1,24 @@
package com.baeldung.dao.repositories;
import com.baeldung.dao.repositories.impl.PersonInsertRepository;
import com.baeldung.domain.Person;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Optional;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@RunWith(SpringRunner.class)
@DataJpaTest
@Import(PersonInsertRepository.class)
public class PersonInsertRepositoryIntegrationTest {
private static final Long ID = 1L;
@@ -24,43 +27,23 @@ public class PersonInsertRepositoryIntegrationTest {
private static final Person PERSON = new Person(ID, FIRST_NAME, LAST_NAME);
@Autowired
private PersonQueryRepository personQueryRepository;
private PersonInsertRepository personInsertRepository;
@Autowired
private PersonEntityManagerRepository personEntityManagerRepository;
@BeforeEach
public void clearDB() {
personQueryRepository.deleteAll();
}
private EntityManager entityManager;
@Test
public void givenPersonEntity_whenInsertWithNativeQuery_ThenPersonIsPersisted() {
insertPerson();
insertWithQuery();
assertPersonPersisted();
}
@Test
public void givenPersonEntity_whenInsertedTwiceWithNativeQuery_thenDataIntegrityViolationExceptionIsThrown() {
assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy(() -> {
insertPerson();
insertPerson();
});
}
@Test
public void givenPersonEntity_whenInsertWithQueryAnnotation_thenPersonIsPersisted() {
insertPersonWithQueryAnnotation();
assertPersonPersisted();
}
@Test
public void givenPersonEntity_whenInsertedTwiceWithQueryAnnotation_thenDataIntegrityViolationExceptionIsThrown() {
assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy(() -> {
insertPersonWithQueryAnnotation();
insertPersonWithQueryAnnotation();
public void givenPersonEntity_whenInsertedTwiceWithNativeQuery_thenPersistenceExceptionExceptionIsThrown() {
assertThatExceptionOfType(PersistenceException.class).isThrownBy(() -> {
insertWithQuery();
insertWithQuery();
});
}
@@ -72,31 +55,27 @@ public class PersonInsertRepositoryIntegrationTest {
}
@Test
public void givenPersonEntity_whenInsertedTwiceWithEntityManager_thenDataIntegrityViolationExceptionIsThrown() {
assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy(() -> {
public void givenPersonEntity_whenInsertedTwiceWithEntityManager_thenEntityExistsExceptionIsThrown() {
assertThatExceptionOfType(EntityExistsException.class).isThrownBy(() -> {
insertPersonWithEntityManager();
insertPersonWithEntityManager();
});
}
private void insertPerson() {
personQueryRepository.insert(PERSON);
}
private void insertPersonWithQueryAnnotation() {
personQueryRepository.insertWithAnnotation(ID, FIRST_NAME, LAST_NAME);
private void insertWithQuery() {
personInsertRepository.insertWithQuery(PERSON);
}
private void insertPersonWithEntityManager() {
personEntityManagerRepository.insert(new Person(ID, FIRST_NAME, LAST_NAME));
personInsertRepository.insertWithEntityManager(new Person(ID, FIRST_NAME, LAST_NAME));
}
private void assertPersonPersisted() {
Optional<Person> personOptional = personQueryRepository.findById(PERSON.getId());
Person person = entityManager.find(Person.class, ID);
assertThat(personOptional.isPresent()).isTrue();
assertThat(personOptional.get().getId()).isEqualTo(PERSON.getId());
assertThat(personOptional.get().getFirstName()).isEqualTo(PERSON.getFirstName());
assertThat(personOptional.get().getLastName()).isEqualTo(PERSON.getLastName());
assertThat(person).isNotNull();
assertThat(person.getId()).isEqualTo(PERSON.getId());
assertThat(person.getFirstName()).isEqualTo(PERSON.getFirstName());
assertThat(person.getLastName()).isEqualTo(PERSON.getLastName());
}
}