fix chat creating/updating 404

This commit is contained in:
Stepan Usatiuk
2024-01-04 18:56:04 +01:00
parent 2e89af5adf
commit 63f2a1b598
3 changed files with 30 additions and 5 deletions

View File

@@ -47,10 +47,14 @@ public class ApiExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(DataIntegrityViolationException.class) @ExceptionHandler(DataIntegrityViolationException.class)
protected ResponseEntity<Object> handleDataIntegrityViolationException(DataIntegrityViolationException ex, WebRequest request) { protected ResponseEntity<Object> handleDataIntegrityViolationException(DataIntegrityViolationException ex, WebRequest request) {
if (ex.getRootCause() instanceof ConstraintViolationException) {
return handleConstraintViolation((ConstraintViolationException) ex.getRootCause(), request);
} else {
return handleExceptionInternal(ex, return handleExceptionInternal(ex,
new ErrorTo(List.of("Something is wrong with your request"), HttpStatus.BAD_REQUEST.value()), new ErrorTo(List.of("Something is wrong with your request"), HttpStatus.BAD_REQUEST.value()),
new HttpHeaders(), HttpStatus.BAD_REQUEST, request); new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
} }
}
@ExceptionHandler(AuthenticationException.class) @ExceptionHandler(AuthenticationException.class)
protected ResponseEntity<Object> handleAuthenticationException(AuthenticationException ex, WebRequest request) { protected ResponseEntity<Object> handleAuthenticationException(AuthenticationException ex, WebRequest request) {

View File

@@ -48,7 +48,11 @@ public class ChatServiceImpl implements ChatService {
chat.setCreator(entityManager.getReference(Person.class, authentication.getName())); chat.setCreator(entityManager.getReference(Person.class, authentication.getName()));
chat.setMembers(Arrays.stream(chatCreateTo.memberUuids()).map( chat.setMembers(Arrays.stream(chatCreateTo.memberUuids()).map(
p -> entityManager.getReference(Person.class, p) p -> {
if (entityManager.find(Person.class, p) == null)
throw new NotFoundException("User with uuid " + p + " not found");
return entityManager.getReference(Person.class, p);
}
).toList()); ).toList());
chat.setName(chatCreateTo.name()); chat.setName(chatCreateTo.name());
@@ -67,7 +71,11 @@ public class ChatServiceImpl implements ChatService {
throw new BadInputException("Chat must have members other than its creator"); throw new BadInputException("Chat must have members other than its creator");
chat.setMembers(new ArrayList<>(Arrays.stream(chatCreateTo.memberUuids()).map( chat.setMembers(new ArrayList<>(Arrays.stream(chatCreateTo.memberUuids()).map(
p -> entityManager.getReference(Person.class, p) p -> {
if (entityManager.find(Person.class, p) == null)
throw new NotFoundException("User with uuid " + p + " not found");
return entityManager.getReference(Person.class, p);
}
).toList())); ).toList()));
chat.setName(chatCreateTo.name()); chat.setName(chatCreateTo.name());

View File

@@ -44,6 +44,19 @@ public class ChatControllerTest extends DemoDataDbTest {
Assertions.assertTrue(chatRepository.findByName("chatnew").isPresent()); Assertions.assertTrue(chatRepository.findByName("chatnew").isPresent());
} }
@Test
void chatCreateWithBadUuid404() {
var response = restTemplate.exchange(addr + "/chat", HttpMethod.POST,
new HttpEntity<>(new ChatCreateTo("chatnew", new String[]{person1.getUuid(), "asdfasdfasdf"}), createAuthHeaders(person1Auth)),
ErrorTo.class);
Assertions.assertNotNull(response);
Assertions.assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
var toResponse = response.getBody();
Assertions.assertNotNull(toResponse);
}
@Test @Test
void shouldGetChat() { void shouldGetChat() {
for (TokenResponseTo t : new TokenResponseTo[]{person1Auth, person2Auth}) { for (TokenResponseTo t : new TokenResponseTo[]{person1Auth, person2Auth}) {