|
| 1 | +/* |
| 2 | + * Copyright 2013-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.cloudfoundry.client.v3; |
| 18 | + |
| 19 | +import org.cloudfoundry.AbstractIntegrationTest; |
| 20 | +import org.cloudfoundry.CloudFoundryVersion; |
| 21 | +import org.cloudfoundry.IfCloudFoundryVersion; |
| 22 | +import org.cloudfoundry.client.CloudFoundryClient; |
| 23 | +import org.cloudfoundry.client.v3.organizationquotadefinitions.Apps; |
| 24 | +import org.cloudfoundry.client.v3.organizationquotadefinitions.CreateOrganizationQuotaDefinitionRequest; |
| 25 | +import org.cloudfoundry.client.v3.organizationquotadefinitions.CreateOrganizationQuotaDefinitionResponse; |
| 26 | +import org.cloudfoundry.client.v3.organizationquotadefinitions.DeleteOrganizationQuotaDefinitionRequest; |
| 27 | +import org.cloudfoundry.client.v3.organizationquotadefinitions.GetOrganizationQuotaDefinitionRequest; |
| 28 | +import org.cloudfoundry.client.v3.organizationquotadefinitions.GetOrganizationQuotaDefinitionResponse; |
| 29 | +import org.cloudfoundry.client.v3.organizationquotadefinitions.ListOrganizationQuotaDefinitionsRequest; |
| 30 | +import org.cloudfoundry.client.v3.organizationquotadefinitions.OrganizationQuotaDefinitionResource; |
| 31 | +import org.cloudfoundry.client.v3.organizationquotadefinitions.Routes; |
| 32 | +import org.cloudfoundry.client.v3.organizationquotadefinitions.Services; |
| 33 | +import org.cloudfoundry.client.v3.organizationquotadefinitions.UpdateOrganizationQuotaDefinitionRequest; |
| 34 | +import org.cloudfoundry.util.JobUtils; |
| 35 | +import org.cloudfoundry.util.PaginationUtils; |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | +import org.springframework.beans.factory.annotation.Autowired; |
| 38 | +import reactor.core.publisher.Flux; |
| 39 | +import reactor.core.publisher.Mono; |
| 40 | +import reactor.test.StepVerifier; |
| 41 | + |
| 42 | +import java.time.Duration; |
| 43 | + |
| 44 | +import static org.assertj.core.api.Assertions.assertThat; |
| 45 | + |
| 46 | +@IfCloudFoundryVersion(greaterThanOrEqualTo = CloudFoundryVersion.PCF_2_8) |
| 47 | +public final class OrganizationQuotaDefinitionsTest extends AbstractIntegrationTest { |
| 48 | + |
| 49 | + @Autowired |
| 50 | + private CloudFoundryClient cloudFoundryClient; |
| 51 | + |
| 52 | + @Test |
| 53 | + public void create() { |
| 54 | + String organizationQuotaName = this.nameFactory.getOrganizationQuotaName(); |
| 55 | + this.cloudFoundryClient |
| 56 | + .organizationQuotaDefinitionsV3() |
| 57 | + .create(CreateOrganizationQuotaDefinitionRequest.builder().name(organizationQuotaName).build()) |
| 58 | + .thenMany(requestListOrganizationQuotas(this.cloudFoundryClient, organizationQuotaName)) |
| 59 | + .single() |
| 60 | + .as(StepVerifier::create) |
| 61 | + .expectNextCount(1) |
| 62 | + .expectComplete() |
| 63 | + .verify(Duration.ofMinutes(5)); |
| 64 | + |
| 65 | + deleteOrganizationQuotaId(this.cloudFoundryClient, organizationQuotaName); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void delete() { |
| 70 | + String organizationQuotaName = this.nameFactory.getOrganizationQuotaName(); |
| 71 | + |
| 72 | + createOrganizationQuotaId(this.cloudFoundryClient, organizationQuotaName) |
| 73 | + .flatMap( |
| 74 | + organizationId -> |
| 75 | + this.cloudFoundryClient |
| 76 | + .organizationQuotaDefinitionsV3() |
| 77 | + .delete( |
| 78 | + DeleteOrganizationQuotaDefinitionRequest.builder() |
| 79 | + .organizationQuotaDefinitionId(organizationId) |
| 80 | + .build()) |
| 81 | + .flatMap( |
| 82 | + job -> |
| 83 | + JobUtils.waitForCompletion( |
| 84 | + this.cloudFoundryClient, |
| 85 | + Duration.ofMinutes(5), |
| 86 | + job))) |
| 87 | + .thenMany(requestListOrganizationQuotas(this.cloudFoundryClient, organizationQuotaName)) |
| 88 | + .as(StepVerifier::create) |
| 89 | + .expectComplete() |
| 90 | + .verify(Duration.ofMinutes(5)); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void get() { |
| 95 | + String organizationQuotaName = this.nameFactory.getOrganizationQuotaName(); |
| 96 | + |
| 97 | + createOrganizationQuotaId(this.cloudFoundryClient, organizationQuotaName) |
| 98 | + .flatMap( |
| 99 | + organizationQuotaId -> |
| 100 | + this.cloudFoundryClient |
| 101 | + .organizationQuotaDefinitionsV3() |
| 102 | + .get( |
| 103 | + GetOrganizationQuotaDefinitionRequest.builder() |
| 104 | + .organizationQuotaDefinitionId(organizationQuotaId) |
| 105 | + .build())) |
| 106 | + .map(GetOrganizationQuotaDefinitionResponse::getName) |
| 107 | + .as(StepVerifier::create) |
| 108 | + .expectNext(organizationQuotaName) |
| 109 | + .expectComplete() |
| 110 | + .verify(Duration.ofMinutes(5)); |
| 111 | + |
| 112 | + deleteOrganizationQuotaId(this.cloudFoundryClient, organizationQuotaName); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void list() { |
| 117 | + String organizationQuotaName = this.nameFactory.getOrganizationQuotaName(); |
| 118 | + |
| 119 | + requestCreateOrganizationQuota(this.cloudFoundryClient, organizationQuotaName) |
| 120 | + .thenMany( |
| 121 | + PaginationUtils.requestClientV3Resources( |
| 122 | + page -> |
| 123 | + this.cloudFoundryClient |
| 124 | + .organizationQuotaDefinitionsV3() |
| 125 | + .list( |
| 126 | + ListOrganizationQuotaDefinitionsRequest.builder() |
| 127 | + .page(page) |
| 128 | + .build()))) |
| 129 | + .filter(resource -> organizationQuotaName.equals(resource.getName())) |
| 130 | + .as(StepVerifier::create) |
| 131 | + .expectNextCount(1) |
| 132 | + .expectComplete() |
| 133 | + .verify(Duration.ofMinutes(5)); |
| 134 | + |
| 135 | + deleteOrganizationQuotaId(this.cloudFoundryClient, organizationQuotaName); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void update() { |
| 140 | + String organizationQuotaName = this.nameFactory.getOrganizationQuotaName(); |
| 141 | + int totalMemoryLimit = 64 * 1024; // 64 GB |
| 142 | + |
| 143 | + createOrganizationQuotaId(this.cloudFoundryClient, organizationQuotaName) |
| 144 | + .flatMap( |
| 145 | + organizationQuotaId -> |
| 146 | + this.cloudFoundryClient |
| 147 | + .organizationQuotaDefinitionsV3() |
| 148 | + .update( |
| 149 | + UpdateOrganizationQuotaDefinitionRequest.builder() |
| 150 | + .organizationQuotaDefinitionId(organizationQuotaId) |
| 151 | + .apps(Apps.builder().totalMemoryInMb(totalMemoryLimit).build()) |
| 152 | + .routes(Routes.builder().totalRoutes(100).build()) |
| 153 | + .services(Services.builder().isPaidServicesAllowed(true).totalServiceInstances(100).build()) |
| 154 | + .build())) |
| 155 | + .thenMany(requestListOrganizationQuotas(this.cloudFoundryClient, organizationQuotaName)) |
| 156 | + .as(StepVerifier::create) |
| 157 | + .consumeNextWith( |
| 158 | + organizationQuotaDefinitionResource -> { |
| 159 | + assertThat(organizationQuotaDefinitionResource.getApps().getTotalMemoryInMb()).isEqualTo(totalMemoryLimit); |
| 160 | + assertThat(organizationQuotaDefinitionResource.getRoutes().getTotalRoutes()).isEqualTo(100); |
| 161 | + assertThat(organizationQuotaDefinitionResource.getServices().getTotalServiceInstances()).isEqualTo(100); |
| 162 | + }) |
| 163 | + .expectComplete() |
| 164 | + .verify(Duration.ofMinutes(5)); |
| 165 | + |
| 166 | + deleteOrganizationQuotaId(this.cloudFoundryClient, organizationQuotaName); |
| 167 | + } |
| 168 | + |
| 169 | + private static Mono<String> createOrganizationQuotaId( |
| 170 | + CloudFoundryClient cloudFoundryClient, String organizationQuotaName) { |
| 171 | + return requestCreateOrganizationQuota(cloudFoundryClient, organizationQuotaName) |
| 172 | + .map(CreateOrganizationQuotaDefinitionResponse::getId); |
| 173 | + } |
| 174 | + |
| 175 | + private static Mono<String> getOrganizationQuotaId( |
| 176 | + CloudFoundryClient cloudFoundryClient, String organizationQuotaName) { |
| 177 | + return requestListOrganizationQuotas(cloudFoundryClient, organizationQuotaName) |
| 178 | + .filter(organizationQuotaDefinitionResource -> organizationQuotaName.equals(organizationQuotaDefinitionResource.getName())) |
| 179 | + .single() |
| 180 | + .map(OrganizationQuotaDefinitionResource::getId); |
| 181 | + } |
| 182 | + |
| 183 | + private static Mono<CreateOrganizationQuotaDefinitionResponse> requestCreateOrganizationQuota( |
| 184 | + CloudFoundryClient cloudFoundryClient, String organizationQuotaName) { |
| 185 | + return cloudFoundryClient |
| 186 | + .organizationQuotaDefinitionsV3() |
| 187 | + .create(CreateOrganizationQuotaDefinitionRequest.builder().name(organizationQuotaName).build()); |
| 188 | + } |
| 189 | + |
| 190 | + private static Flux<OrganizationQuotaDefinitionResource> requestListOrganizationQuotas( |
| 191 | + CloudFoundryClient cloudFoundryClient, String organizationName) { |
| 192 | + return PaginationUtils.requestClientV3Resources( |
| 193 | + page -> |
| 194 | + cloudFoundryClient |
| 195 | + .organizationQuotaDefinitionsV3() |
| 196 | + .list( |
| 197 | + ListOrganizationQuotaDefinitionsRequest.builder() |
| 198 | + .name(organizationName) |
| 199 | + .page(page) |
| 200 | + .build())); |
| 201 | + } |
| 202 | + |
| 203 | + private static void deleteOrganizationQuotaId(CloudFoundryClient cloudFoundryClient, String organizationQuotaName) { |
| 204 | + |
| 205 | + getOrganizationQuotaId(cloudFoundryClient, organizationQuotaName) |
| 206 | + .flatMap( |
| 207 | + organizationId -> |
| 208 | + cloudFoundryClient |
| 209 | + .organizationQuotaDefinitionsV3() |
| 210 | + .delete( |
| 211 | + DeleteOrganizationQuotaDefinitionRequest.builder() |
| 212 | + .organizationQuotaDefinitionId(organizationId) |
| 213 | + .build()) |
| 214 | + .flatMap( |
| 215 | + job -> |
| 216 | + JobUtils.waitForCompletion( |
| 217 | + cloudFoundryClient, |
| 218 | + Duration.ofMinutes(5), |
| 219 | + job))) |
| 220 | + .block(Duration.ofMinutes(5)); |
| 221 | + } |
| 222 | +} |
0 commit comments