Skip to content

Commit

Permalink
#1109 fix chunking of custom objects (#1113)
Browse files Browse the repository at this point in the history
  • Loading branch information
lojzatran authored Nov 13, 2023
1 parent 6ec0d3b commit c0cbae0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public CompletionStage<Optional<String>> fetchCachedCustomObjectId(
@Override
public CompletionStage<Set<CustomObject>> fetchMatchingCustomObjects(
@Nonnull final Set<CustomObjectCompositeIdentifier> identifiers) {
return super.fetchMatchingResources(
getKeys(identifiers), this::keyMapper, (keysNotCached) -> createQuery(identifiers));
return super.fetchMatchingResources(getKeys(identifiers), this::keyMapper, this::createQuery);
}

@Nonnull
Expand Down Expand Up @@ -142,8 +141,10 @@ CompletionStage<Optional<CustomObject>> executeCreateCommand(
}

@Nonnull
private ByProjectKeyCustomObjectsGet createQuery(
@Nonnull final Set<CustomObjectCompositeIdentifier> identifiers) {
private ByProjectKeyCustomObjectsGet createQuery(@Nonnull final Set<String> keys) {
final Set<CustomObjectCompositeIdentifier> identifiers =
keys.stream().map(CustomObjectCompositeIdentifier::of).collect(Collectors.toSet());

final String whereQuery =
identifiers.stream()
.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -234,6 +235,55 @@ void fetchCustomObject_WithKeyAndContainer_ShouldFetchCustomObject() {
verify(byProjectKeyCustomObjectsByContainerByKeyGet).execute();
}

@Test
void fetchMatchingCustomObjects_WithManyCustomObjects_ShouldChunkAndFetchCustomObjects() {
final ArgumentCaptor<String> requestArgumentCaptor = ArgumentCaptor.forClass(String.class);
final Set<CustomObjectCompositeIdentifier> 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<CustomObjectPagedQueryResponse> 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);
Expand Down

0 comments on commit c0cbae0

Please sign in to comment.