BAEL-1509 jpa attribute converters (#3599)

* fix converter

* fix converter

* fix converter

* fix converter + more tests
This commit is contained in:
Marcos Lopez Gonzalez 2018-02-24 08:15:52 +01:00 committed by Grzegorz Piwowarek
parent 53627ae439
commit 2399b4d817
2 changed files with 156 additions and 12 deletions

View File

@ -11,31 +11,49 @@ public class PersonNameConverter implements AttributeConverter<PersonName, Strin
private static final String SEPARATOR = ", ";
@Override
public String convertToDatabaseColumn(PersonName person) {
public String convertToDatabaseColumn(PersonName personName) {
if (personName == null) {
return null;
}
StringBuilder sb = new StringBuilder();
if (person.getSurname() != null) {
sb.append(person.getSurname());
if (personName.getSurname() != null && !personName.getSurname()
.isEmpty()) {
sb.append(personName.getSurname());
sb.append(SEPARATOR);
}
if (person.getName() != null) {
sb.append(person.getName());
if (personName.getName() != null && !personName.getName()
.isEmpty()) {
sb.append(personName.getName());
}
return sb.toString();
}
@Override
public PersonName convertToEntityAttribute(String dbPerson) {
String[] pieces = dbPerson.split(SEPARATOR);
public PersonName convertToEntityAttribute(String dbPersonName) {
if (dbPersonName == null || dbPersonName.isEmpty()) {
return null;
}
if (pieces == null || pieces.length != 2) {
String[] pieces = dbPersonName.split(SEPARATOR);
if (pieces == null || pieces.length == 0) {
return null;
}
PersonName personName = new PersonName();
personName.setSurname(pieces[0]);
personName.setName(pieces[1]);
String firstPiece = !pieces[0].isEmpty() ? pieces[0] : null;
if (dbPersonName.contains(SEPARATOR)) {
personName.setSurname(firstPiece);
if (pieces.length >= 2 && pieces[1] != null && !pieces[1].isEmpty()) {
personName.setName(pieces[1]);
}
} else {
personName.setName(firstPiece);
}
return personName;
}

View File

@ -71,4 +71,130 @@ public class PersonNameConverterTest {
.getSurname(), surname);
}
@Test
public void givenPersonNameNull_WhenSaving_ThenNullStored() {
final String name = null;
final String surname = null;
PersonName personName = new PersonName();
personName.setName(name);
personName.setSurname(surname);
Person person = new Person();
person.setPersonName(personName);
Long id = (Long) session.save(person);
session.flush();
session.clear();
String dbPersonName = (String) session.createNativeQuery("select p.personName from PersonTable p where p.id = :id")
.setParameter("id", id)
.getSingleResult();
assertEquals("", dbPersonName);
Person dbPerson = session.createNativeQuery("select * from PersonTable p where p.id = :id", Person.class)
.setParameter("id", id)
.getSingleResult();
assertEquals(dbPerson.getPersonName(), null);
}
@Test
public void givenPersonNameWithoutName_WhenSaving_ThenNotNameStored() {
final String name = null;
final String surname = "surname";
PersonName personName = new PersonName();
personName.setName(name);
personName.setSurname(surname);
Person person = new Person();
person.setPersonName(personName);
Long id = (Long) session.save(person);
session.flush();
session.clear();
String dbPersonName = (String) session.createNativeQuery("select p.personName from PersonTable p where p.id = :id")
.setParameter("id", id)
.getSingleResult();
assertEquals("surname, ", dbPersonName);
Person dbPerson = session.createNativeQuery("select * from PersonTable p where p.id = :id", Person.class)
.setParameter("id", id)
.getSingleResult();
assertEquals(dbPerson.getPersonName()
.getName(), name);
assertEquals(dbPerson.getPersonName()
.getSurname(), surname);
}
@Test
public void givenPersonNameWithoutSurName_WhenSaving_ThenNotSurNameStored() {
final String name = "name";
final String surname = null;
PersonName personName = new PersonName();
personName.setName(name);
personName.setSurname(surname);
Person person = new Person();
person.setPersonName(personName);
Long id = (Long) session.save(person);
session.flush();
session.clear();
String dbPersonName = (String) session.createNativeQuery("select p.personName from PersonTable p where p.id = :id")
.setParameter("id", id)
.getSingleResult();
assertEquals("name", dbPersonName);
Person dbPerson = session.createNativeQuery("select * from PersonTable p where p.id = :id", Person.class)
.setParameter("id", id)
.getSingleResult();
assertEquals(dbPerson.getPersonName()
.getName(), name);
assertEquals(dbPerson.getPersonName()
.getSurname(), surname);
}
@Test
public void givenPersonNameEmptyFields_WhenSaving_ThenFielsNotStored() {
final String name = "";
final String surname = "";
PersonName personName = new PersonName();
personName.setName(name);
personName.setSurname(surname);
Person person = new Person();
person.setPersonName(personName);
Long id = (Long) session.save(person);
session.flush();
session.clear();
String dbPersonName = (String) session.createNativeQuery("select p.personName from PersonTable p where p.id = :id")
.setParameter("id", id)
.getSingleResult();
assertEquals("", dbPersonName);
Person dbPerson = session.createNativeQuery("select * from PersonTable p where p.id = :id", Person.class)
.setParameter("id", id)
.getSingleResult();
assertEquals(dbPerson.getPersonName(), null);
}
}