-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJdbcAttachmentDataSourceTest.java
More file actions
97 lines (75 loc) · 3.17 KB
/
Copy pathJdbcAttachmentDataSourceTest.java
File metadata and controls
97 lines (75 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package it.aboutbits.springboot.emailservice.lib.application;
import it.aboutbits.springboot.emailservice.lib.exception.AttachmentException;
import it.aboutbits.springboot.emailservice.support.database.WithPostgres;
import org.jspecify.annotations.NullMarked;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import java.io.ByteArrayInputStream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@SpringBootTest
@WithPostgres
@NullMarked
class JdbcAttachmentDataSourceTest {
@Autowired
JdbcTemplate jdbcTemplate;
JdbcAttachmentDataSource attachmentDataSource;
@BeforeEach
void setup() {
attachmentDataSource = new JdbcAttachmentDataSource(jdbcTemplate);
}
@Test
void givenPayload_store_shouldBeReadableAgain() throws Exception {
var payload = new byte[]{1, 2, 3, 4, 5};
var fileReference = attachmentDataSource.storeAttachmentPayload(new ByteArrayInputStream(payload));
try (var stored = attachmentDataSource.getAttachmentPayload(fileReference)) {
assertThat(stored.readAllBytes()).isEqualTo(payload);
}
}
@Test
void givenPayload_store_shouldClosePayloadStream() throws Exception {
var payload = new TrackingInputStream(new byte[]{1, 2, 3});
attachmentDataSource.storeAttachmentPayload(payload);
assertThat(payload.closed).isTrue();
}
@Test
void givenMultiplePayloads_store_shouldReturnDistinctReferences() throws Exception {
var first = attachmentDataSource.storeAttachmentPayload(new ByteArrayInputStream(new byte[]{1}));
var second = attachmentDataSource.storeAttachmentPayload(new ByteArrayInputStream(new byte[]{2}));
assertThat(first).isNotEqualTo(second);
}
@Test
void givenUnknownReference_get_shouldFail() {
assertThatExceptionOfType(AttachmentException.class).isThrownBy(
() -> attachmentDataSource.getAttachmentPayload(1L)
);
}
@Test
void givenStoredPayload_release_shouldRemoveIt() throws Exception {
var fileReference = attachmentDataSource.storeAttachmentPayload(new ByteArrayInputStream(new byte[]{1, 2, 3}));
attachmentDataSource.releaseAttachment(fileReference);
assertThatExceptionOfType(AttachmentException.class).isThrownBy(
() -> attachmentDataSource.getAttachmentPayload(fileReference)
);
}
@Test
void givenUnknownReference_release_shouldBeIdempotent() {
assertThatCode(
() -> attachmentDataSource.releaseAttachment(1L)
).doesNotThrowAnyException();
}
private static final class TrackingInputStream extends ByteArrayInputStream {
private boolean closed = false;
private TrackingInputStream(byte[] buf) {
super(buf);
}
@Override
public void close() {
closed = true;
}
}
}