4
This commit is contained in:
@@ -4,6 +4,7 @@ import com.example.mtusi_messenger.exception.ApiException;
|
||||
import com.example.mtusi_messenger.model.Attachment;
|
||||
import com.example.mtusi_messenger.model.Chat;
|
||||
import com.example.mtusi_messenger.repository.AttachmentRepository;
|
||||
import com.example.mtusi_messenger.queue.AttachmentJobPublisher;
|
||||
import com.example.mtusi_messenger.storage.ObjectStorage;
|
||||
import com.example.mtusi_messenger.storage.ObjectStorageException;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -34,6 +35,7 @@ class AttachmentServiceTest {
|
||||
private AttachmentRepository repository;
|
||||
private ChatService chats;
|
||||
private InMemoryStorage storage;
|
||||
private AttachmentJobPublisher publisher;
|
||||
private AttachmentService service;
|
||||
private byte[] png;
|
||||
|
||||
@@ -42,6 +44,7 @@ class AttachmentServiceTest {
|
||||
repository = mock(AttachmentRepository.class);
|
||||
chats = mock(ChatService.class);
|
||||
storage = new InMemoryStorage();
|
||||
publisher = mock(AttachmentJobPublisher.class);
|
||||
TransactionTemplate transaction = mock(TransactionTemplate.class);
|
||||
when(transaction.execute(any())).thenAnswer(invocation -> {
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -50,14 +53,14 @@ class AttachmentServiceTest {
|
||||
});
|
||||
when(chats.getAccessible(chatId, ownerId)).thenReturn(
|
||||
new Chat(chatId, "test", List.of(ownerId), Instant.now()));
|
||||
when(repository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
service = new AttachmentService(repository, chats, storage, transaction,
|
||||
10_485_760, 40_000_000, 3, 10_000);
|
||||
when(repository.saveQueued(any(), any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
service = new AttachmentService(repository, chats, storage, publisher, transaction,
|
||||
10_485_760, 40_000_000, 3, 10_000, false);
|
||||
png = Files.readAllBytes(Path.of("tests", "fixtures", "sample.png"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void uploadsValidPngUsingServerGeneratedObjectKey() {
|
||||
void stagesValidPngAndPublishesDurableJob() {
|
||||
Attachment attachment = service.upload(chatId, ownerId, "photo.png", "image/png",
|
||||
png.length, new ByteArrayInputStream(png));
|
||||
|
||||
@@ -65,9 +68,13 @@ class AttachmentServiceTest {
|
||||
assertEquals("image/png", attachment.contentType());
|
||||
assertEquals(png.length, attachment.sizeBytes());
|
||||
assertTrue(attachment.objectKey().matches("attachments/[0-9a-f-]+/original"));
|
||||
assertTrue(attachment.stagingKey().matches("staging/[0-9a-f-]+/original"));
|
||||
assertEquals("queued", attachment.status());
|
||||
assertFalse(attachment.objectKey().contains("photo.png"));
|
||||
assertArrayEquals(png, storage.objects.get(attachment.objectKey()));
|
||||
verify(repository).save(attachment);
|
||||
assertArrayEquals(png, storage.objects.get(attachment.stagingKey()));
|
||||
assertFalse(storage.objects.containsKey(attachment.objectKey()));
|
||||
verify(repository).saveQueued(eq(attachment), any());
|
||||
verify(publisher).publish(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -108,7 +115,7 @@ class AttachmentServiceTest {
|
||||
assertThrows(ObjectStorageException.class, () -> service.upload(
|
||||
chatId, ownerId, "photo.png", "image/png", png.length,
|
||||
new ByteArrayInputStream(png)));
|
||||
verify(repository, never()).save(any());
|
||||
verify(repository, never()).saveQueued(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -130,12 +137,12 @@ class AttachmentServiceTest {
|
||||
|
||||
assertEquals(HttpStatus.BAD_REQUEST, error.status());
|
||||
assertTrue(storage.objects.isEmpty());
|
||||
verify(repository, never()).save(any());
|
||||
verify(repository, never()).saveQueued(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void databaseFailureCompensatesAlreadyWrittenS3Object() {
|
||||
when(repository.save(any())).thenThrow(new IllegalStateException("database commit failed"));
|
||||
when(repository.saveQueued(any(), any())).thenThrow(new IllegalStateException("database commit failed"));
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> service.upload(
|
||||
chatId, ownerId, "photo.png", "image/png", png.length,
|
||||
|
||||
Reference in New Issue
Block a user