|
| 1 | +/* |
| 2 | + * Copyright 2013-2025 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.reactor.client.v3.spacequotadefinition; |
| 18 | + |
| 19 | +import org.cloudfoundry.client.v3.Link; |
| 20 | +import org.cloudfoundry.client.v3.Pagination; |
| 21 | +import org.cloudfoundry.client.v3.Relationship; |
| 22 | +import org.cloudfoundry.client.v3.ToManyRelationship; |
| 23 | +import org.cloudfoundry.client.v3.ToOneRelationship; |
| 24 | +import org.cloudfoundry.client.v3.spacequotadefinitions.Apps; |
| 25 | +import org.cloudfoundry.client.v3.spacequotadefinitions.CreateSpaceQuotaDefinitionRequest; |
| 26 | +import org.cloudfoundry.client.v3.spacequotadefinitions.CreateSpaceQuotaDefinitionResponse; |
| 27 | +import org.cloudfoundry.client.v3.spacequotadefinitions.DeleteSpaceQuotaDefinitionRequest; |
| 28 | +import org.cloudfoundry.client.v3.spacequotadefinitions.GetSpaceQuotaDefinitionRequest; |
| 29 | +import org.cloudfoundry.client.v3.spacequotadefinitions.GetSpaceQuotaDefinitionResponse; |
| 30 | +import org.cloudfoundry.client.v3.spacequotadefinitions.ListSpaceQuotaDefinitionsRequest; |
| 31 | +import org.cloudfoundry.client.v3.spacequotadefinitions.ListSpaceQuotaDefinitionsResponse; |
| 32 | +import org.cloudfoundry.client.v3.spacequotadefinitions.Routes; |
| 33 | +import org.cloudfoundry.client.v3.spacequotadefinitions.Services; |
| 34 | +import org.cloudfoundry.client.v3.spacequotadefinitions.SpaceQuotaDefinitionRelationships; |
| 35 | +import org.cloudfoundry.client.v3.spacequotadefinitions.SpaceQuotaDefinitionResource; |
| 36 | +import org.cloudfoundry.client.v3.spacequotadefinitions.UpdateSpaceQuotaDefinitionRequest; |
| 37 | +import org.cloudfoundry.client.v3.spacequotadefinitions.UpdateSpaceQuotaDefinitionResponse; |
| 38 | +import org.cloudfoundry.reactor.InteractionContext; |
| 39 | +import org.cloudfoundry.reactor.TestRequest; |
| 40 | +import org.cloudfoundry.reactor.TestResponse; |
| 41 | +import org.cloudfoundry.reactor.client.AbstractClientApiTest; |
| 42 | +import org.jetbrains.annotations.NotNull; |
| 43 | +import org.junit.jupiter.api.Test; |
| 44 | +import reactor.test.StepVerifier; |
| 45 | + |
| 46 | +import java.time.Duration; |
| 47 | +import java.util.Collections; |
| 48 | + |
| 49 | +import static io.netty.handler.codec.http.HttpMethod.DELETE; |
| 50 | +import static io.netty.handler.codec.http.HttpMethod.GET; |
| 51 | +import static io.netty.handler.codec.http.HttpMethod.PATCH; |
| 52 | +import static io.netty.handler.codec.http.HttpMethod.POST; |
| 53 | +import static io.netty.handler.codec.http.HttpResponseStatus.ACCEPTED; |
| 54 | +import static io.netty.handler.codec.http.HttpResponseStatus.OK; |
| 55 | + |
| 56 | +class ReactorSpaceQuotaDefinitionsV3Test extends AbstractClientApiTest { |
| 57 | + |
| 58 | + public static final String EXPECTED_SPACE_QUOTA_ID_1 = "f919ef8a-e333-472a-8172-baaf2c30d301"; |
| 59 | + |
| 60 | + private final ReactorSpaceQuotaDefinitionsV3 spaceQuotaDefinitionsV3 = |
| 61 | + new ReactorSpaceQuotaDefinitionsV3( |
| 62 | + CONNECTION_CONTEXT, this.root, TOKEN_PROVIDER, Collections.emptyMap()); |
| 63 | + |
| 64 | + @Test |
| 65 | + void create() { |
| 66 | + mockRequest( |
| 67 | + InteractionContext.builder() |
| 68 | + .request( |
| 69 | + TestRequest.builder() |
| 70 | + .method(POST) |
| 71 | + .path("/space_quotas") |
| 72 | + .payload( |
| 73 | + "fixtures/client/v3/space_quotas/POST_request.json") |
| 74 | + .build()) |
| 75 | + .response( |
| 76 | + TestResponse.builder() |
| 77 | + .status(OK) |
| 78 | + .payload( |
| 79 | + "fixtures/client/v3/space_quotas/POST_response.json") |
| 80 | + .build()) |
| 81 | + .build()); |
| 82 | + |
| 83 | + SpaceQuotaDefinitionRelationships relationships = SpaceQuotaDefinitionRelationships |
| 84 | + .builder() |
| 85 | + .organization(ToOneRelationship.builder() |
| 86 | + .data(Relationship.builder() |
| 87 | + .id("9b370018-c38e-44c9-86d6-155c76801104") |
| 88 | + .build()) |
| 89 | + .build()) |
| 90 | + .spaces(ToManyRelationship.builder() |
| 91 | + .data(Collections.singletonList(Relationship.builder() |
| 92 | + .id("dcfd6a55-62b9-496e-a26f-0064cec076bf") |
| 93 | + .build())) |
| 94 | + .build()) |
| 95 | + .build(); |
| 96 | + this.spaceQuotaDefinitionsV3 |
| 97 | + .create(CreateSpaceQuotaDefinitionRequest.builder().name("my-quota").relationships(relationships).build()) |
| 98 | + .as(StepVerifier::create) |
| 99 | + .expectNext( |
| 100 | + CreateSpaceQuotaDefinitionResponse |
| 101 | + .builder() |
| 102 | + .from(expectedSpaceQuotaDefinitionResource1()) |
| 103 | + .build() |
| 104 | + ) |
| 105 | + .expectComplete() |
| 106 | + .verify(Duration.ofSeconds(5)); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void delete() { |
| 111 | + mockRequest( |
| 112 | + InteractionContext.builder() |
| 113 | + .request( |
| 114 | + TestRequest.builder() |
| 115 | + .method(DELETE) |
| 116 | + .path("/space_quotas/test-space-quota-id") |
| 117 | + .build()) |
| 118 | + .response( |
| 119 | + TestResponse.builder() |
| 120 | + .status(ACCEPTED) |
| 121 | + .header( |
| 122 | + "Location", |
| 123 | + "https://api.example.org/v3/jobs/test-job-id") |
| 124 | + .build()) |
| 125 | + .build()); |
| 126 | + |
| 127 | + this.spaceQuotaDefinitionsV3 |
| 128 | + .delete( |
| 129 | + DeleteSpaceQuotaDefinitionRequest.builder() |
| 130 | + .spaceQuotaDefinitionId("test-space-quota-id") |
| 131 | + .build()) |
| 132 | + .as(StepVerifier::create) |
| 133 | + .expectNext("test-job-id") |
| 134 | + .expectComplete() |
| 135 | + .verify(Duration.ofSeconds(5)); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + void get() { |
| 140 | + mockRequest( |
| 141 | + InteractionContext.builder() |
| 142 | + .request( |
| 143 | + TestRequest.builder() |
| 144 | + .method(GET) |
| 145 | + .path("/space_quotas/" + EXPECTED_SPACE_QUOTA_ID_1) |
| 146 | + .build()) |
| 147 | + .response( |
| 148 | + TestResponse.builder() |
| 149 | + .status(OK) |
| 150 | + .payload( |
| 151 | + "fixtures/client/v3/space_quotas/GET_{id}_response.json") |
| 152 | + .build()) |
| 153 | + .build()); |
| 154 | + |
| 155 | + this.spaceQuotaDefinitionsV3 |
| 156 | + .get( |
| 157 | + GetSpaceQuotaDefinitionRequest.builder() |
| 158 | + .spaceQuotaDefinitionId(EXPECTED_SPACE_QUOTA_ID_1) |
| 159 | + .build()) |
| 160 | + .as(StepVerifier::create) |
| 161 | + .expectNext( |
| 162 | + GetSpaceQuotaDefinitionResponse.builder() |
| 163 | + .from(expectedSpaceQuotaDefinitionResource1()) |
| 164 | + .build()) |
| 165 | + .expectComplete() |
| 166 | + .verify(Duration.ofSeconds(5)); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + void list() { |
| 171 | + mockRequest( |
| 172 | + InteractionContext.builder() |
| 173 | + .request(TestRequest.builder().method(GET).path("/space_quotas").build()) |
| 174 | + .response( |
| 175 | + TestResponse.builder() |
| 176 | + .status(OK) |
| 177 | + .payload( |
| 178 | + "fixtures/client/v3/space_quotas/GET_response.json") |
| 179 | + .build()) |
| 180 | + .build()); |
| 181 | + |
| 182 | + this.spaceQuotaDefinitionsV3 |
| 183 | + .list(ListSpaceQuotaDefinitionsRequest.builder().build()) |
| 184 | + .as(StepVerifier::create) |
| 185 | + .expectNext( |
| 186 | + ListSpaceQuotaDefinitionsResponse.builder() |
| 187 | + .pagination( |
| 188 | + Pagination.builder() |
| 189 | + .totalResults(2) |
| 190 | + .totalPages(1) |
| 191 | + .first( |
| 192 | + Link.builder() |
| 193 | + .href( |
| 194 | + "https://api.example.org/v3/space_quotas?page=1&per_page=50") |
| 195 | + .build()) |
| 196 | + .last( |
| 197 | + Link.builder() |
| 198 | + .href( |
| 199 | + "https://api.example.org/v3/space_quotas?page=1&per_page=50") |
| 200 | + .build()) |
| 201 | + .build()) |
| 202 | + .resource( |
| 203 | + SpaceQuotaDefinitionResource |
| 204 | + .builder() |
| 205 | + .from(expectedSpaceQuotaDefinitionResource1()).build() |
| 206 | + ) |
| 207 | + .resource( |
| 208 | + SpaceQuotaDefinitionResource |
| 209 | + .builder() |
| 210 | + .from(expectedSpaceQuotaDefinitionResource2()).build() |
| 211 | + ) |
| 212 | + .build()) |
| 213 | + .expectComplete() |
| 214 | + .verify(Duration.ofSeconds(5)); |
| 215 | + } |
| 216 | + |
| 217 | + @Test |
| 218 | + void update() { |
| 219 | + mockRequest( |
| 220 | + InteractionContext.builder() |
| 221 | + .request( |
| 222 | + TestRequest.builder() |
| 223 | + .method(PATCH) |
| 224 | + .path("/space_quotas/" + EXPECTED_SPACE_QUOTA_ID_1) |
| 225 | + .payload( |
| 226 | + "fixtures/client/v3/space_quotas/PATCH_{id}_request.json") |
| 227 | + .build()) |
| 228 | + .response( |
| 229 | + TestResponse.builder() |
| 230 | + .status(OK) |
| 231 | + .payload( |
| 232 | + "fixtures/client/v3/space_quotas/PATCH_{id}_response.json") |
| 233 | + .build()) |
| 234 | + .build()); |
| 235 | + |
| 236 | + this.spaceQuotaDefinitionsV3 |
| 237 | + .update( |
| 238 | + UpdateSpaceQuotaDefinitionRequest.builder() |
| 239 | + .spaceQuotaDefinitionId(EXPECTED_SPACE_QUOTA_ID_1) |
| 240 | + .build()) |
| 241 | + .as(StepVerifier::create) |
| 242 | + .expectNext( |
| 243 | + UpdateSpaceQuotaDefinitionResponse.builder() |
| 244 | + .from(expectedSpaceQuotaDefinitionResource1()) |
| 245 | + .build()) |
| 246 | + .expectComplete() |
| 247 | + .verify(Duration.ofSeconds(5)); |
| 248 | + } |
| 249 | + |
| 250 | + @NotNull |
| 251 | + private static SpaceQuotaDefinitionResource expectedSpaceQuotaDefinitionResource1() { |
| 252 | + return buildSpaceQuotaDefinitionResource(EXPECTED_SPACE_QUOTA_ID_1, "my-quota", "9b370018-c38e-44c9-86d6-155c76801104", "dcfd6a55-62b9-496e-a26f-0064cec076bf"); |
| 253 | + } |
| 254 | + |
| 255 | + private static SpaceQuotaDefinitionResource expectedSpaceQuotaDefinitionResource2() { |
| 256 | + return buildSpaceQuotaDefinitionResource("bb49bf20-ad98-4729-93ae-38fbc564b630", "my-quota-2", "9b370018-c38e-44c9-86d6-155c76801104", null); |
| 257 | + } |
| 258 | + |
| 259 | + @NotNull |
| 260 | + private static SpaceQuotaDefinitionResource buildSpaceQuotaDefinitionResource(String id, String name, String relatedOrganizationId, String relatedSpaceId) { |
| 261 | + |
| 262 | + Apps apps = Apps.builder() |
| 263 | + .totalMemoryInMb(5120) |
| 264 | + .perProcessMemoryInMb(1024) |
| 265 | + .totalInstances(10) |
| 266 | + .perAppTasks(5) |
| 267 | + .build(); |
| 268 | + Services services = Services.builder() |
| 269 | + .isPaidServicesAllowed(true) |
| 270 | + .totalServiceInstances(10) |
| 271 | + .totalServiceKeys(20) |
| 272 | + .build(); |
| 273 | + Routes routes = Routes.builder() |
| 274 | + .totalRoutes(8) |
| 275 | + .totalReservedPorts(4) |
| 276 | + .build(); |
| 277 | + |
| 278 | + ToOneRelationship organizationRelationship = ToOneRelationship.builder() |
| 279 | + .data( |
| 280 | + Relationship |
| 281 | + .builder() |
| 282 | + .id(relatedOrganizationId) |
| 283 | + .build()) |
| 284 | + .build(); |
| 285 | + ToManyRelationship spaceRelationships = ToManyRelationship.builder().data(Collections.emptyList()).build(); |
| 286 | + if (relatedSpaceId != null) { |
| 287 | + spaceRelationships = ToManyRelationship.builder() |
| 288 | + .data( |
| 289 | + Collections.singletonList( |
| 290 | + Relationship |
| 291 | + .builder() |
| 292 | + .id(relatedSpaceId) |
| 293 | + .build())) |
| 294 | + .build(); |
| 295 | + } |
| 296 | + SpaceQuotaDefinitionRelationships relationships = |
| 297 | + SpaceQuotaDefinitionRelationships |
| 298 | + .builder() |
| 299 | + .organization(organizationRelationship) |
| 300 | + .spaces(spaceRelationships) |
| 301 | + .build(); |
| 302 | + |
| 303 | + return SpaceQuotaDefinitionResource.builder() |
| 304 | + .createdAt("2016-05-04T17:00:41Z") |
| 305 | + .id(id) |
| 306 | + .link( |
| 307 | + "self", |
| 308 | + Link.builder() |
| 309 | + .href("https://api.example.org/v3/space_quotas/" + id) |
| 310 | + .build()) |
| 311 | + .link("organization", |
| 312 | + Link.builder() |
| 313 | + .href("https://api.example.org/v3/organizations/" + relatedOrganizationId) |
| 314 | + .build()) |
| 315 | + .name(name) |
| 316 | + .updatedAt("2016-05-04T18:00:41Z") |
| 317 | + .apps(apps) |
| 318 | + .services(services) |
| 319 | + .routes(routes) |
| 320 | + .relationships(relationships) |
| 321 | + .build(); |
| 322 | + } |
| 323 | +} |
0 commit comments