feat(persons): add year range bounds validation (> 0) to PersonService
birthYear and deathYear must be positive integers; extracted shared validateYears() method for reuse in createPerson. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -70,11 +70,21 @@ public class PersonService {
|
|||||||
return personRepository.save(person);
|
return personRepository.save(person);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
private void validateYears(Integer birthYear, Integer deathYear) {
|
||||||
public Person updatePerson(UUID id, PersonUpdateDTO dto) {
|
if (birthYear != null && birthYear <= 0) {
|
||||||
if (dto.getBirthYear() != null && dto.getDeathYear() != null && dto.getBirthYear() > dto.getDeathYear()) {
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Geburtsjahr muss eine positive Zahl sein");
|
||||||
|
}
|
||||||
|
if (deathYear != null && deathYear <= 0) {
|
||||||
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Todesjahr muss eine positive Zahl sein");
|
||||||
|
}
|
||||||
|
if (birthYear != null && deathYear != null && birthYear > deathYear) {
|
||||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Geburtsjahr darf nicht nach dem Todesjahr liegen");
|
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Geburtsjahr darf nicht nach dem Todesjahr liegen");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Person updatePerson(UUID id, PersonUpdateDTO dto) {
|
||||||
|
validateYears(dto.getBirthYear(), dto.getDeathYear());
|
||||||
Person person = personRepository.findById(id)
|
Person person = personRepository.findById(id)
|
||||||
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Person nicht gefunden"));
|
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Person nicht gefunden"));
|
||||||
person.setFirstName(dto.getFirstName());
|
person.setFirstName(dto.getFirstName());
|
||||||
|
|||||||
@@ -267,6 +267,56 @@ class PersonServiceTest {
|
|||||||
assertThat(result.getDeathYear()).isEqualTo(1900);
|
assertThat(result.getDeathYear()).isEqualTo(1900);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ─── Phase 1.3: Year range bounds (> 0) ──────────────────────────────────
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void updatePerson_throwsBadRequest_whenBirthYearIsZero() {
|
||||||
|
UUID id = UUID.randomUUID();
|
||||||
|
|
||||||
|
PersonUpdateDTO dto = new PersonUpdateDTO();
|
||||||
|
dto.setFirstName("Anna"); dto.setLastName("Alt"); dto.setBirthYear(0);
|
||||||
|
assertThatThrownBy(() -> personService.updatePerson(id, dto))
|
||||||
|
.isInstanceOf(ResponseStatusException.class)
|
||||||
|
.extracting(e -> ((ResponseStatusException) e).getStatusCode().value())
|
||||||
|
.isEqualTo(400);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void updatePerson_throwsBadRequest_whenBirthYearIsNegative() {
|
||||||
|
UUID id = UUID.randomUUID();
|
||||||
|
|
||||||
|
PersonUpdateDTO dto = new PersonUpdateDTO();
|
||||||
|
dto.setFirstName("Anna"); dto.setLastName("Alt"); dto.setBirthYear(-5);
|
||||||
|
assertThatThrownBy(() -> personService.updatePerson(id, dto))
|
||||||
|
.isInstanceOf(ResponseStatusException.class)
|
||||||
|
.extracting(e -> ((ResponseStatusException) e).getStatusCode().value())
|
||||||
|
.isEqualTo(400);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void updatePerson_throwsBadRequest_whenDeathYearIsZero() {
|
||||||
|
UUID id = UUID.randomUUID();
|
||||||
|
|
||||||
|
PersonUpdateDTO dto = new PersonUpdateDTO();
|
||||||
|
dto.setFirstName("Anna"); dto.setLastName("Alt"); dto.setDeathYear(0);
|
||||||
|
assertThatThrownBy(() -> personService.updatePerson(id, dto))
|
||||||
|
.isInstanceOf(ResponseStatusException.class)
|
||||||
|
.extracting(e -> ((ResponseStatusException) e).getStatusCode().value())
|
||||||
|
.isEqualTo(400);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void updatePerson_throwsBadRequest_whenDeathYearIsNegative() {
|
||||||
|
UUID id = UUID.randomUUID();
|
||||||
|
|
||||||
|
PersonUpdateDTO dto = new PersonUpdateDTO();
|
||||||
|
dto.setFirstName("Anna"); dto.setLastName("Alt"); dto.setDeathYear(-10);
|
||||||
|
assertThatThrownBy(() -> personService.updatePerson(id, dto))
|
||||||
|
.isInstanceOf(ResponseStatusException.class)
|
||||||
|
.extracting(e -> ((ResponseStatusException) e).getStatusCode().value())
|
||||||
|
.isEqualTo(400);
|
||||||
|
}
|
||||||
|
|
||||||
// ─── findCorrespondents ──────────────────────────────────────────────────
|
// ─── findCorrespondents ──────────────────────────────────────────────────
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user