Skip to content

Commit c0cbae0

Browse files
authored
#1109 fix chunking of custom objects (#1113)
1 parent 6ec0d3b commit c0cbae0

2 files changed

Lines changed: 55 additions & 4 deletions

File tree

src/main/java/com/commercetools/sync/services/impl/CustomObjectServiceImpl.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ public CompletionStage<Optional<String>> fetchCachedCustomObjectId(
7070
@Override
7171
public CompletionStage<Set<CustomObject>> fetchMatchingCustomObjects(
7272
@Nonnull final Set<CustomObjectCompositeIdentifier> identifiers) {
73-
return super.fetchMatchingResources(
74-
getKeys(identifiers), this::keyMapper, (keysNotCached) -> createQuery(identifiers));
73+
return super.fetchMatchingResources(getKeys(identifiers), this::keyMapper, this::createQuery);
7574
}
7675

7776
@Nonnull
@@ -142,8 +141,10 @@ CompletionStage<Optional<CustomObject>> executeCreateCommand(
142141
}
143142

144143
@Nonnull
145-
private ByProjectKeyCustomObjectsGet createQuery(
146-
@Nonnull final Set<CustomObjectCompositeIdentifier> identifiers) {
144+
private ByProjectKeyCustomObjectsGet createQuery(@Nonnull final Set<String> keys) {
145+
final Set<CustomObjectCompositeIdentifier> identifiers =
146+
keys.stream().map(CustomObjectCompositeIdentifier::of).collect(Collectors.toSet());
147+
147148
final String whereQuery =
148149
identifiers.stream()
149150
.map(

src/test/java/com/commercetools/sync/services/impl/CustomObjectServiceImplTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.junit.jupiter.api.AfterEach;
4141
import org.junit.jupiter.api.BeforeEach;
4242
import org.junit.jupiter.api.Test;
43+
import org.mockito.ArgumentCaptor;
4344

4445
@SuppressWarnings("unchecked")
4546
class CustomObjectServiceImplTest {
@@ -234,6 +235,55 @@ void fetchCustomObject_WithKeyAndContainer_ShouldFetchCustomObject() {
234235
verify(byProjectKeyCustomObjectsByContainerByKeyGet).execute();
235236
}
236237

238+
@Test
239+
void fetchMatchingCustomObjects_WithManyCustomObjects_ShouldChunkAndFetchCustomObjects() {
240+
final ArgumentCaptor<String> requestArgumentCaptor = ArgumentCaptor.forClass(String.class);
241+
final Set<CustomObjectCompositeIdentifier> customObjectCompositeIdentifiers = new HashSet<>();
242+
for (int i = 0; i < 500; i++) {
243+
final String key = RandomStringUtils.random(15, true, true);
244+
final String container = RandomStringUtils.random(15, true, true);
245+
customObjectCompositeIdentifiers.add(CustomObjectCompositeIdentifier.of(key, container));
246+
}
247+
248+
final ByProjectKeyCustomObjectsGet byProjectKeyCustomObjectsGet =
249+
mock(ByProjectKeyCustomObjectsGet.class);
250+
251+
when(client.customObjects()).thenReturn(mock(ByProjectKeyCustomObjectsRequestBuilder.class));
252+
when(client.customObjects().get()).thenReturn(byProjectKeyCustomObjectsGet);
253+
when(byProjectKeyCustomObjectsGet.withWhere(anyString()))
254+
.thenReturn(byProjectKeyCustomObjectsGet);
255+
when(byProjectKeyCustomObjectsGet.withPredicateVar(anyString(), anyString()))
256+
.thenReturn(byProjectKeyCustomObjectsGet, byProjectKeyCustomObjectsGet);
257+
when(byProjectKeyCustomObjectsGet.withLimit(anyInt())).thenReturn(byProjectKeyCustomObjectsGet);
258+
when(byProjectKeyCustomObjectsGet.withWithTotal(anyBoolean()))
259+
.thenReturn(byProjectKeyCustomObjectsGet);
260+
when(byProjectKeyCustomObjectsGet.withSort(anyString()))
261+
.thenReturn(byProjectKeyCustomObjectsGet);
262+
when(byProjectKeyCustomObjectsGet.withSort(anyString()))
263+
.thenReturn(byProjectKeyCustomObjectsGet);
264+
265+
final ApiHttpResponse<CustomObjectPagedQueryResponse> apiHttpResponse =
266+
mock(ApiHttpResponse.class);
267+
final CustomObjectPagedQueryResponse customObjectPagedQueryResponse =
268+
mock(CustomObjectPagedQueryResponse.class);
269+
when(byProjectKeyCustomObjectsGet.execute())
270+
.thenReturn(CompletableFuture.completedFuture(apiHttpResponse));
271+
when(apiHttpResponse.getBody()).thenReturn(customObjectPagedQueryResponse);
272+
when(customObjectPagedQueryResponse.getResults()).thenReturn(Collections.emptyList());
273+
274+
// test
275+
service
276+
.fetchMatchingCustomObjects(customObjectCompositeIdentifiers)
277+
.toCompletableFuture()
278+
.join();
279+
280+
// assertions
281+
verify(byProjectKeyCustomObjectsGet, times(2)).execute();
282+
verify(byProjectKeyCustomObjectsGet, times(2)).withWhere(requestArgumentCaptor.capture());
283+
assertThat(requestArgumentCaptor.getAllValues().get(0))
284+
.isNotEqualTo(requestArgumentCaptor.getAllValues().get(1));
285+
}
286+
237287
@Test
238288
void createCustomObject_WithDraft_ShouldCreateCustomObject() {
239289
final CustomObject mock = mock(CustomObject.class);

0 commit comments

Comments
 (0)