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

View File

@@ -48,7 +48,11 @@ public class ChatServiceImpl implements ChatService {
chat.setCreator(entityManager.getReference(Person.class, authentication.getName()));
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());
chat.setName(chatCreateTo.name());
@@ -67,7 +71,11 @@ public class ChatServiceImpl implements ChatService {
throw new BadInputException("Chat must have members other than its creator");
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()));
chat.setName(chatCreateTo.name());

View File

@@ -44,6 +44,19 @@ public class ChatControllerTest extends DemoDataDbTest {
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
void shouldGetChat() {
for (TokenResponseTo t : new TokenResponseTo[]{person1Auth, person2Auth}) {