Skip to content

Commit ec85c4b

Browse files
authored
fix: make mock blobs created only once (GoogleCloudPlatform#3850) (GoogleCloudPlatform#3856)
* fix: purge channel before tests * use latch * use getUpdateTime * update resulte to long * test * test * make mock blob fixed * remove test code * add config to surefire * restore
1 parent 952b107 commit ec85c4b

File tree

1 file changed

+45
-29
lines changed

1 file changed

+45
-29
lines changed

spring-cloud-gcp-storage/src/test/java/com/google/cloud/spring/storage/integration/inbound/GcsStreamingMessageSourceTests.java

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@
2020
import static org.mockito.ArgumentMatchers.eq;
2121
import static org.mockito.BDDMockito.willAnswer;
2222
import static org.mockito.Mockito.mock;
23+
import static org.mockito.Mockito.when;
2324

2425
import com.google.cloud.PageImpl;
2526
import com.google.cloud.ReadChannel;
2627
import com.google.cloud.spring.storage.integration.GcsSessionFactory;
2728
import com.google.cloud.storage.Blob;
29+
import com.google.cloud.storage.BlobInfo;
2830
import com.google.cloud.storage.Storage;
2931
import java.io.InputStream;
30-
import java.time.OffsetDateTime;
31-
import java.time.ZoneOffset;
32+
import java.util.ArrayList;
3233
import java.util.Comparator;
3334
import java.util.List;
34-
import java.util.stream.Collectors;
35-
import java.util.stream.Stream;
35+
import org.junit.jupiter.api.BeforeAll;
3636
import org.junit.jupiter.api.Test;
3737
import org.junit.jupiter.api.extension.ExtendWith;
3838
import org.springframework.beans.factory.annotation.Autowired;
@@ -52,15 +52,34 @@
5252
import org.springframework.test.context.aot.DisabledInAotMode;
5353
import org.springframework.test.context.junit.jupiter.SpringExtension;
5454

55-
/** Tests for the streaming message source. */
55+
/**
56+
* Tests for the streaming message source.
57+
*/
5658
@ExtendWith(SpringExtension.class)
5759
@ContextConfiguration
5860
@DisabledInAotMode
5961
class GcsStreamingMessageSourceTests {
6062

61-
@Autowired private PollableChannel unsortedChannel;
63+
@Autowired
64+
private PollableChannel unsortedChannel;
65+
66+
@Autowired
67+
private PollableChannel sortedChannel;
68+
69+
private static Blob alphaBlob = mock(Blob.class);
70+
71+
private static Blob betaBlob = mock(Blob.class);
72+
73+
private static Blob gammaBlob = mock(Blob.class);
74+
75+
private static List<Blob> blobList = new ArrayList<>();
6276

63-
@Autowired private PollableChannel sortedChannel;
77+
@BeforeAll
78+
static void setUp() {
79+
blobList.add(mockBlob(gammaBlob, "gamma"));
80+
blobList.add(mockBlob(betaBlob, "beta"));
81+
blobList.add(mockBlob(alphaBlob, "alpha/alpha"));
82+
}
6483

6584
@Test
6685
void testInboundStreamingChannelAdapter() {
@@ -102,30 +121,26 @@ void testSortedInboundChannelAdapter() {
102121
assertThat(message).isNull();
103122
}
104123

105-
private static Blob createBlob(String bucket, String name) {
106-
Blob blob = mock(Blob.class);
107-
willAnswer(invocationOnMock -> bucket).given(blob).getBucket();
108-
willAnswer(invocationOnMock -> name).given(blob).getName();
109-
willAnswer(invocationOnMock -> OffsetDateTime.now(ZoneOffset.UTC)).given(blob).getUpdateTimeOffsetDateTime();
124+
private static Blob mockBlob(Blob blob, String name) {
125+
when(blob.getBucket()).thenReturn("gcsbucket");
126+
when(blob.getName()).thenReturn(name);
110127
return blob;
111128
}
112129

113-
/** Spring config for the tests. */
130+
/**
131+
* Spring config for the tests.
132+
*/
114133
@Configuration
115134
@EnableIntegration
116135
public static class Config {
117-
118-
@Bean
119-
public Storage gcsClient() {
136+
private Storage gcsClient() {
120137
Storage gcs = mock(Storage.class);
121-
122-
List<Blob> blobList = Stream.of(
123-
createBlob("gcsbucket", "gamma"),
124-
createBlob("gcsbucket", "beta"),
125-
createBlob("gcsbucket", "alpha/alpha"))
126-
.collect(Collectors.toList());
127-
128-
willAnswer(invocationOnMock -> new PageImpl<>(null, null, blobList))
138+
willAnswer(
139+
invocationOnMock ->
140+
new PageImpl<>(
141+
null,
142+
null,
143+
blobList))
129144
.given(gcs)
130145
.list(eq("gcsbucket"));
131146

@@ -144,9 +159,10 @@ public Storage gcsClient() {
144159

145160
@Bean
146161
@InboundChannelAdapter(value = "unsortedChannel", poller = @Poller(fixedDelay = "100"))
147-
public MessageSource<InputStream> unsortedChannelAdapter(Storage gcs) {
162+
public MessageSource<InputStream> unsortedChannelAdapter() {
148163
GcsStreamingMessageSource adapter =
149-
new GcsStreamingMessageSource(new RemoteFileTemplate<>(new GcsSessionFactory(gcs)));
164+
new GcsStreamingMessageSource(
165+
new RemoteFileTemplate<>(new GcsSessionFactory(gcsClient())));
150166
adapter.setRemoteDirectory("gcsbucket");
151167
adapter.setFilter(new AcceptOnceFileListFilter<>());
152168

@@ -155,11 +171,11 @@ public MessageSource<InputStream> unsortedChannelAdapter(Storage gcs) {
155171

156172
@Bean
157173
@InboundChannelAdapter(value = "sortedChannel", poller = @Poller(fixedDelay = "100"))
158-
public MessageSource<InputStream> sortedChannelAdapter(Storage gcs) {
174+
public MessageSource<InputStream> sortedChannelAdapter() {
159175
GcsStreamingMessageSource adapter =
160176
new GcsStreamingMessageSource(
161-
new RemoteFileTemplate<>(new GcsSessionFactory(gcs)),
162-
Comparator.comparing(blob -> blob.getName()));
177+
new RemoteFileTemplate<>(new GcsSessionFactory(gcsClient())),
178+
Comparator.comparing(BlobInfo::getName));
163179

164180
adapter.setRemoteDirectory("gcsbucket");
165181
adapter.setFilter(new AcceptOnceFileListFilter<>());

0 commit comments

Comments
 (0)