From c0cbae042eae734c13d1b909624ee46dea9f4a61 Mon Sep 17 00:00:00 2001 From: Lam Tran Date: Mon, 13 Nov 2023 13:43:56 +0000 Subject: [PATCH] #1109 fix chunking of custom objects (#1113) --- .../impl/CustomObjectServiceImpl.java | 9 ++-- .../impl/CustomObjectServiceImplTest.java | 50 +++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/commercetools/sync/services/impl/CustomObjectServiceImpl.java b/src/main/java/com/commercetools/sync/services/impl/CustomObjectServiceImpl.java index 4d3f81920a..79933b7f47 100644 --- a/src/main/java/com/commercetools/sync/services/impl/CustomObjectServiceImpl.java +++ b/src/main/java/com/commercetools/sync/services/impl/CustomObjectServiceImpl.java @@ -70,8 +70,7 @@ public CompletionStage> fetchCachedCustomObjectId( @Override public CompletionStage> fetchMatchingCustomObjects( @Nonnull final Set identifiers) { - return super.fetchMatchingResources( - getKeys(identifiers), this::keyMapper, (keysNotCached) -> createQuery(identifiers)); + return super.fetchMatchingResources(getKeys(identifiers), this::keyMapper, this::createQuery); } @Nonnull @@ -142,8 +141,10 @@ CompletionStage> executeCreateCommand( } @Nonnull - private ByProjectKeyCustomObjectsGet createQuery( - @Nonnull final Set identifiers) { + private ByProjectKeyCustomObjectsGet createQuery(@Nonnull final Set keys) { + final Set identifiers = + keys.stream().map(CustomObjectCompositeIdentifier::of).collect(Collectors.toSet()); + final String whereQuery = identifiers.stream() .map( diff --git a/src/test/java/com/commercetools/sync/services/impl/CustomObjectServiceImplTest.java b/src/test/java/com/commercetools/sync/services/impl/CustomObjectServiceImplTest.java index f4709a7109..25cb4a3fa7 100644 --- a/src/test/java/com/commercetools/sync/services/impl/CustomObjectServiceImplTest.java +++ b/src/test/java/com/commercetools/sync/services/impl/CustomObjectServiceImplTest.java @@ -40,6 +40,7 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; @SuppressWarnings("unchecked") class CustomObjectServiceImplTest { @@ -234,6 +235,55 @@ void fetchCustomObject_WithKeyAndContainer_ShouldFetchCustomObject() { verify(byProjectKeyCustomObjectsByContainerByKeyGet).execute(); } + @Test + void fetchMatchingCustomObjects_WithManyCustomObjects_ShouldChunkAndFetchCustomObjects() { + final ArgumentCaptor requestArgumentCaptor = ArgumentCaptor.forClass(String.class); + final Set customObjectCompositeIdentifiers = new HashSet<>(); + for (int i = 0; i < 500; i++) { + final String key = RandomStringUtils.random(15, true, true); + final String container = RandomStringUtils.random(15, true, true); + customObjectCompositeIdentifiers.add(CustomObjectCompositeIdentifier.of(key, container)); + } + + final ByProjectKeyCustomObjectsGet byProjectKeyCustomObjectsGet = + mock(ByProjectKeyCustomObjectsGet.class); + + when(client.customObjects()).thenReturn(mock(ByProjectKeyCustomObjectsRequestBuilder.class)); + when(client.customObjects().get()).thenReturn(byProjectKeyCustomObjectsGet); + when(byProjectKeyCustomObjectsGet.withWhere(anyString())) + .thenReturn(byProjectKeyCustomObjectsGet); + when(byProjectKeyCustomObjectsGet.withPredicateVar(anyString(), anyString())) + .thenReturn(byProjectKeyCustomObjectsGet, byProjectKeyCustomObjectsGet); + when(byProjectKeyCustomObjectsGet.withLimit(anyInt())).thenReturn(byProjectKeyCustomObjectsGet); + when(byProjectKeyCustomObjectsGet.withWithTotal(anyBoolean())) + .thenReturn(byProjectKeyCustomObjectsGet); + when(byProjectKeyCustomObjectsGet.withSort(anyString())) + .thenReturn(byProjectKeyCustomObjectsGet); + when(byProjectKeyCustomObjectsGet.withSort(anyString())) + .thenReturn(byProjectKeyCustomObjectsGet); + + final ApiHttpResponse apiHttpResponse = + mock(ApiHttpResponse.class); + final CustomObjectPagedQueryResponse customObjectPagedQueryResponse = + mock(CustomObjectPagedQueryResponse.class); + when(byProjectKeyCustomObjectsGet.execute()) + .thenReturn(CompletableFuture.completedFuture(apiHttpResponse)); + when(apiHttpResponse.getBody()).thenReturn(customObjectPagedQueryResponse); + when(customObjectPagedQueryResponse.getResults()).thenReturn(Collections.emptyList()); + + // test + service + .fetchMatchingCustomObjects(customObjectCompositeIdentifiers) + .toCompletableFuture() + .join(); + + // assertions + verify(byProjectKeyCustomObjectsGet, times(2)).execute(); + verify(byProjectKeyCustomObjectsGet, times(2)).withWhere(requestArgumentCaptor.capture()); + assertThat(requestArgumentCaptor.getAllValues().get(0)) + .isNotEqualTo(requestArgumentCaptor.getAllValues().get(1)); + } + @Test void createCustomObject_WithDraft_ShouldCreateCustomObject() { final CustomObject mock = mock(CustomObject.class);