1919import static org .assertj .core .api .Assertions .assertThat ;
2020
2121import java .time .Duration ;
22+ import java .util .Collections ;
23+ import java .util .List ;
24+ import java .util .stream .Collectors ;
2225import org .cloudfoundry .AbstractIntegrationTest ;
2326import org .cloudfoundry .CloudFoundryVersion ;
2427import org .cloudfoundry .IfCloudFoundryVersion ;
2528import org .cloudfoundry .client .CloudFoundryClient ;
2629import org .cloudfoundry .client .v3 .organizations .CreateOrganizationRequest ;
2730import org .cloudfoundry .client .v3 .organizations .Organization ;
28- import org .cloudfoundry .client .v3 .quotas .*;
31+ import org .cloudfoundry .client .v3 .quotas .Apps ;
32+ import org .cloudfoundry .client .v3 .quotas .Routes ;
33+ import org .cloudfoundry .client .v3 .quotas .Services ;
2934import org .cloudfoundry .client .v3 .quotas .spaces .*;
3035import org .cloudfoundry .client .v3 .spaces .CreateSpaceRequest ;
3136import org .cloudfoundry .client .v3 .spaces .Space ;
@@ -105,7 +110,7 @@ public void create() {
105110 public void createWithSpaceRelationship () {
106111 String spaceQuotaName = this .nameFactory .getQuotaDefinitionName ();
107112 SpaceQuotaRelationships spaceQuotaRelationships =
108- createSpaceQuotaRelationships (organizationId , spaceId );
113+ createSpaceQuotaRelationships (organizationId , Collections . singletonList ( spaceId ) );
109114
110115 Apps spaceQuotaAppLimits =
111116 Apps .builder ()
@@ -277,6 +282,72 @@ public void delete() {
277282 .verify (Duration .ofMinutes (5 ));
278283 }
279284
285+ @ Test
286+ public void apply () {
287+ String spaceQuotaName = this .nameFactory .getQuotaDefinitionName ();
288+
289+ Relationship spaceRelationship1 = Relationship .builder ().id (spaceId ).build ();
290+ ToManyRelationship spaceRelationships =
291+ ToManyRelationship .builder ().data (spaceRelationship1 ).build ();
292+
293+ createSpaceQuotaId (this .cloudFoundryClient , spaceQuotaName , organizationId )
294+ .flatMap (
295+ spaceQuotaId -> {
296+ ApplySpaceQuotaRequest applySpaceQuotaRequest =
297+ ApplySpaceQuotaRequest .builder ()
298+ .spaceQuotaId (spaceQuotaId )
299+ .spaceRelationships (spaceRelationships )
300+ .build ();
301+ return this .cloudFoundryClient
302+ .spaceQuotasV3 ()
303+ .apply (applySpaceQuotaRequest );
304+ })
305+ .as (StepVerifier ::create )
306+ .consumeNextWith (
307+ applySpaceQuotaResponse -> {
308+ List <Relationship > spaceRelationshipsData =
309+ applySpaceQuotaResponse .spaceRelationships ().getData ();
310+ assertThat (spaceRelationshipsData .size ()).isEqualTo (1 );
311+ assertThat (spaceRelationshipsData .get (0 ).getId ()).isEqualTo (spaceId );
312+ })
313+ .expectComplete ()
314+ .verify (Duration .ofMinutes (5 ));
315+ }
316+
317+ @ Test
318+ public void remove () {
319+ String spaceQuotaName = this .nameFactory .getQuotaDefinitionName ();
320+
321+ // create a space quota in org with "organizationId" that is pre-associated to the space
322+ // with "spaceId"
323+ createSpaceQuotaId (
324+ this .cloudFoundryClient ,
325+ spaceQuotaName ,
326+ organizationId ,
327+ Collections .singletonList (spaceId ))
328+ .flatMap (
329+ spaceQuotaId -> {
330+ RemoveSpaceQuotaRequest removeSpaceQuotaRequest =
331+ RemoveSpaceQuotaRequest .builder ()
332+ .spaceQuotaId (spaceQuotaId )
333+ .spaceId (spaceId )
334+ .build ();
335+ return this .cloudFoundryClient
336+ .spaceQuotasV3 ()
337+ .remove (removeSpaceQuotaRequest );
338+ })
339+ .thenMany (requestListSpaceQuotas (this .cloudFoundryClient , spaceQuotaName ))
340+ .as (StepVerifier ::create )
341+ .consumeNextWith (
342+ spaceQuotaResource -> {
343+ List <Relationship > spaceRelationshipsData =
344+ spaceQuotaResource .getRelationships ().getSpaces ().getData ();
345+ assertThat (spaceRelationshipsData .size ()).isEqualTo (0 );
346+ })
347+ .expectComplete ()
348+ .verify (Duration .ofMinutes (5 ));
349+ }
350+
280351 private static Organization createOrganization (
281352 CloudFoundryClient cloudFoundryClient , String orgName ) {
282353 return cloudFoundryClient
@@ -314,15 +385,20 @@ private static SpaceQuotaRelationships createSpaceQuotaRelationships(String orgG
314385
315386 @ NotNull
316387 private static SpaceQuotaRelationships createSpaceQuotaRelationships (
317- String orgGuid , String spaceGuid ) {
388+ String orgGuid , List < String > spaceGuids ) {
318389 ToOneRelationship organizationRelationship =
319390 ToOneRelationship .builder ()
320391 .data (Relationship .builder ().id (orgGuid ).build ())
321392 .build ();
393+
394+ // iterate over spaceGuids to create Relationship objects
395+ List <Relationship > spaceRelationshipsData =
396+ spaceGuids .stream ()
397+ .map (spaceGuid -> Relationship .builder ().id (spaceGuid ).build ())
398+ .collect (Collectors .toList ());
399+
322400 ToManyRelationship spaceRelationships =
323- ToManyRelationship .builder ()
324- .data (Relationship .builder ().id (spaceGuid ).build ())
325- .build ();
401+ ToManyRelationship .builder ().data (spaceRelationshipsData ).build ();
326402 return SpaceQuotaRelationships .builder ()
327403 .organization (organizationRelationship )
328404 .spaces (spaceRelationships )
@@ -347,6 +423,31 @@ private static Mono<CreateSpaceQuotaResponse> createSpaceQuota(
347423 .build ());
348424 }
349425
426+ private static Mono <String > createSpaceQuotaId (
427+ CloudFoundryClient cloudFoundryClient ,
428+ String spaceQuotaName ,
429+ String orgGuid ,
430+ List <String > spaceGuids ) {
431+ return createSpaceQuota (cloudFoundryClient , spaceQuotaName , orgGuid , spaceGuids )
432+ .map (CreateSpaceQuotaResponse ::getId );
433+ }
434+
435+ private static Mono <CreateSpaceQuotaResponse > createSpaceQuota (
436+ CloudFoundryClient cloudFoundryClient ,
437+ String spaceQuotaName ,
438+ String orgGuid ,
439+ List <String > spaceGuids ) {
440+ SpaceQuotaRelationships spaceQuotaRelationships =
441+ createSpaceQuotaRelationships (orgGuid , spaceGuids );
442+ return cloudFoundryClient
443+ .spaceQuotasV3 ()
444+ .create (
445+ CreateSpaceQuotaRequest .builder ()
446+ .name (spaceQuotaName )
447+ .relationships (spaceQuotaRelationships )
448+ .build ());
449+ }
450+
350451 private static Flux <SpaceQuotaResource > requestListSpaceQuotas (
351452 CloudFoundryClient cloudFoundryClient , String spaceName ) {
352453 return PaginationUtils .requestClientV3Resources (
0 commit comments