diff --git a/cloudfoundry-util/src/main/java/org/cloudfoundry/util/ResourceMatchingUtilsV3.java b/cloudfoundry-util/src/main/java/org/cloudfoundry/util/ResourceMatchingUtilsV3.java index 0589c5dfbd..b4327ec221 100644 --- a/cloudfoundry-util/src/main/java/org/cloudfoundry/util/ResourceMatchingUtilsV3.java +++ b/cloudfoundry-util/src/main/java/org/cloudfoundry/util/ResourceMatchingUtilsV3.java @@ -24,6 +24,7 @@ import java.util.Collection; import java.util.Enumeration; import java.util.List; +import java.util.stream.Collectors; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipFile; import org.cloudfoundry.client.CloudFoundryClient; @@ -46,6 +47,7 @@ public final class ResourceMatchingUtilsV3 { private static final Logger LOGGER = LoggerFactory.getLogger("cloudfoundry-client.resource-matching-v3"); + public static final int MAX_RESOURCES_SIZE = 5000; private ResourceMatchingUtilsV3() {} @@ -55,9 +57,9 @@ public static Mono> getMatchedResources( ? getArtifactMetadataFromDirectory(application) : getArtifactMetadataFromZip(application)) .collectList() - .flatMap( - (List artifactMetadatas) -> - requestListMatchingResources(cloudFoundryClient, artifactMetadatas)) + .flatMapMany(Flux::fromIterable) + .buffer(MAX_RESOURCES_SIZE) + .flatMap(chunk -> requestListMatchingResources(cloudFoundryClient, chunk)) .map(ListMatchingResourcesResponse::getResources) .doOnNext( matched -> @@ -68,6 +70,8 @@ public static Mono> getMatchedResources( matched.stream() .mapToInt(MatchedResource::getSize) .sum()))) + .collectList() + .map(lists -> lists.stream().flatMap(List::stream).collect(Collectors.toList())) .subscribeOn(Schedulers.boundedElastic()); } diff --git a/cloudfoundry-util/src/test/java/org/cloudfoundry/util/ResourceMatchingUtilsV3Test.java b/cloudfoundry-util/src/test/java/org/cloudfoundry/util/ResourceMatchingUtilsV3Test.java new file mode 100644 index 0000000000..542f72a5cd --- /dev/null +++ b/cloudfoundry-util/src/test/java/org/cloudfoundry/util/ResourceMatchingUtilsV3Test.java @@ -0,0 +1,57 @@ +package org.cloudfoundry.util; + +import static org.cloudfoundry.util.ResourceMatchingUtilsV3.getMatchedResources; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; +import org.cloudfoundry.client.CloudFoundryClient; +import org.cloudfoundry.client.v3.resourcematch.ListMatchingResourcesResponse; +import org.cloudfoundry.client.v3.resourcematch.MatchedResource; +import org.junit.jupiter.api.Test; +import org.springframework.core.io.ClassPathResource; +import reactor.core.publisher.Mono; + +class ResourceMatchingUtilsV3Test { + + @Test + void requestListMatchingResources2() throws IOException { + CloudFoundryClient cloudFoundryClient = mock(CloudFoundryClient.class); + when(cloudFoundryClient.resourceMatchV3()) + .thenReturn( + request -> + Mono.just( + ListMatchingResourcesResponse.builder() + .addAllResources(request.getResources()) + .build())); + + Path testApplication = new ClassPathResource("test-application.zip").getFile().toPath(); + + List result = + getMatchedResources(cloudFoundryClient, testApplication).block(); + assertNotNull(result); + assertEquals(2, result.size()); + } + + @Test + void requestListMatchingResources15001() throws IOException { + CloudFoundryClient cloudFoundryClient = mock(CloudFoundryClient.class); + when(cloudFoundryClient.resourceMatchV3()) + .thenReturn( + request -> + Mono.just( + ListMatchingResourcesResponse.builder() + .addAllResources(request.getResources()) + .build())); + Path testApplication = new ClassPathResource("15001_files.zip").getFile().toPath(); + + List result = + getMatchedResources(cloudFoundryClient, testApplication).block(); + assertNotNull(result); + assertEquals(15001, result.size()); + } +} diff --git a/cloudfoundry-util/src/test/resources/15001_files.zip b/cloudfoundry-util/src/test/resources/15001_files.zip new file mode 100644 index 0000000000..d5c7c2d0b3 Binary files /dev/null and b/cloudfoundry-util/src/test/resources/15001_files.zip differ diff --git a/cloudfoundry-util/src/test/resources/test-application.zip b/cloudfoundry-util/src/test/resources/test-application.zip new file mode 100644 index 0000000000..7393880735 Binary files /dev/null and b/cloudfoundry-util/src/test/resources/test-application.zip differ