Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@

import java.time.Duration;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.cloudfoundry.AbstractIntegrationTest;
import org.cloudfoundry.client.CloudFoundryClient;
import org.cloudfoundry.client.v3.featureflags.GetFeatureFlagRequest;
import org.cloudfoundry.client.v3.featureflags.GetFeatureFlagResponse;
import org.cloudfoundry.client.v3.featureflags.ListFeatureFlagsRequest;
import org.cloudfoundry.client.v3.featureflags.UpdateFeatureFlagRequest;
import org.cloudfoundry.client.v3.featureflags.UpdateFeatureFlagResponse;
import org.cloudfoundry.util.PaginationUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

public final class FeatureFlagsV3Test extends AbstractIntegrationTest {
Expand Down Expand Up @@ -82,82 +86,37 @@ public void list() {
public void updateCustomErrorMessage() {
String newMessage = "Integration test custom error message";

this.cloudFoundryClient
.featureFlagsV3()
.get(GetFeatureFlagRequest.builder().name(FLAG_NAME).build())
.flatMap(
withRestoredFeatureFlag(
original ->
this.cloudFoundryClient
.featureFlagsV3()
.update(
UpdateFeatureFlagRequest.builder()
.name(FLAG_NAME)
.customErrorMessage(newMessage)
.build())
.flatMap(
updated ->
this.cloudFoundryClient
.featureFlagsV3()
.update(
UpdateFeatureFlagRequest
.builder()
.name(FLAG_NAME)
.customErrorMessage(
original
.getCustomErrorMessage())
.build())
.thenReturn(
updated
.getCustomErrorMessage())))
update(
UpdateFeatureFlagRequest.builder()
.name(FLAG_NAME)
.customErrorMessage(newMessage)
.build()))
.map(UpdateFeatureFlagResponse::getCustomErrorMessage)
.as(StepVerifier::create)
.expectNext(newMessage)
.expectComplete()
.verify(Duration.ofMinutes(8));
.verify(Duration.ofMinutes(5));
}

@Test
public void updateCustomErrorMessageToNull() {
String tempMessage = "Integration test custom error message";

this.cloudFoundryClient
.featureFlagsV3()
.get(GetFeatureFlagRequest.builder().name(FLAG_NAME).build())
.flatMap(
withRestoredFeatureFlag(
original ->
this.cloudFoundryClient
.featureFlagsV3()
.update(
update(
UpdateFeatureFlagRequest.builder()
.name(FLAG_NAME)
.customErrorMessage(tempMessage)
.build())
.flatMap(
ignored ->
this.cloudFoundryClient
.featureFlagsV3()
.update(
UpdateFeatureFlagRequest
.builder()
.name(FLAG_NAME)
.clearCustomErrorMessage(
true)
.build()))
.flatMap(
cleared -> {
UpdateFeatureFlagRequest.Builder restore =
UpdateFeatureFlagRequest.builder()
.name(FLAG_NAME);
if (original.getCustomErrorMessage() != null) {
restore.customErrorMessage(
original.getCustomErrorMessage());
} else {
restore.clearCustomErrorMessage(true);
}
return this.cloudFoundryClient
.featureFlagsV3()
.update(restore.build())
.thenReturn(cleared);
}))
.then(
update(
UpdateFeatureFlagRequest.builder()
.name(FLAG_NAME)
.clearCustomErrorMessage(true)
.build())))
.as(StepVerifier::create)
.consumeNextWith(cleared -> assertThat(cleared.getCustomErrorMessage()).isNull())
.expectComplete()
Expand All @@ -166,45 +125,17 @@ public void updateCustomErrorMessageToNull() {

@Test
public void update() {
this.cloudFoundryClient
.featureFlagsV3()
.get(GetFeatureFlagRequest.builder().name(FLAG_NAME).build())
.flatMap(
withRestoredFeatureFlag(
original ->
this.cloudFoundryClient
.featureFlagsV3()
.update(
update(
UpdateFeatureFlagRequest.builder()
.name(FLAG_NAME)
.enabled(!original.getEnabled())
.build())
.map(
toggled ->
toggled.getEnabled()
!= original.getEnabled())
.doFinally(
signal ->
this.cloudFoundryClient
.featureFlagsV3()
.update(
UpdateFeatureFlagRequest
.builder()
.name(FLAG_NAME)
.enabled(
original
.getEnabled())
.build())
.as(StepVerifier::create)
.consumeNextWith(
r ->
assertThat(
r
.getEnabled())
.isEqualTo(
original
.getEnabled()))
.expectComplete()
.verify(Duration.ofMinutes(5))))
!toggled.getEnabled()
.equals(original.getEnabled())))
.as(StepVerifier::create)
.expectNext(true)
.expectComplete()
Expand All @@ -215,42 +146,14 @@ public void update() {
public void updateMessage() {
String newMessage = "Integration test updateMessage";

this.cloudFoundryClient
.featureFlagsV3()
.get(GetFeatureFlagRequest.builder().name(FLAG_NAME).build())
.flatMap(
withRestoredFeatureFlag(
original ->
this.cloudFoundryClient
.featureFlagsV3()
.update(
UpdateFeatureFlagRequest.builder()
.name(FLAG_NAME)
.customErrorMessage(newMessage)
.build())
.map(updated -> updated.getCustomErrorMessage())
.doFinally(
signal ->
this.cloudFoundryClient
.featureFlagsV3()
.update(
UpdateFeatureFlagRequest
.builder()
.name(FLAG_NAME)
.customErrorMessage(
original
.getCustomErrorMessage())
.build())
.as(StepVerifier::create)
.consumeNextWith(
r ->
assertThat(
r
.getCustomErrorMessage())
.isEqualTo(
original
.getCustomErrorMessage()))
.expectComplete()
.verify(Duration.ofMinutes(5))))
update(
UpdateFeatureFlagRequest.builder()
.name(FLAG_NAME)
.customErrorMessage(newMessage)
.build()))
.map(UpdateFeatureFlagResponse::getCustomErrorMessage)
.as(StepVerifier::create)
.expectNext(newMessage)
.expectComplete()
Expand All @@ -259,47 +162,55 @@ public void updateMessage() {

@Test
public void updateNothing() {
this.cloudFoundryClient
.featureFlagsV3()
.get(GetFeatureFlagRequest.builder().name(FLAG_NAME).build())
.flatMap(
withRestoredFeatureFlag(
original ->
this.cloudFoundryClient
.featureFlagsV3()
.update(
UpdateFeatureFlagRequest.builder()
.name(FLAG_NAME)
.build())
update(UpdateFeatureFlagRequest.builder().name(FLAG_NAME).build())
.map(
unchanged ->
unchanged.getEnabled()
== original.getEnabled())
.doFinally(
signal ->
this.cloudFoundryClient
.featureFlagsV3()
.update(
UpdateFeatureFlagRequest
.builder()
.name(FLAG_NAME)
.enabled(
original
.getEnabled())
.build())
.as(StepVerifier::create)
.consumeNextWith(
r ->
assertThat(
r
.getEnabled())
.isEqualTo(
original
.getEnabled()))
.expectComplete()
.verify(Duration.ofMinutes(5))))
unchanged
.getEnabled()
.equals(
original
.getEnabled())
&& Objects.equals(
unchanged
.getCustomErrorMessage(),
original
.getCustomErrorMessage())))
.as(StepVerifier::create)
.expectNext(true)
.expectComplete()
.verify(Duration.ofMinutes(5));
}

private Mono<UpdateFeatureFlagResponse> update(UpdateFeatureFlagRequest request) {
return this.cloudFoundryClient.featureFlagsV3().update(request);
}

/**
* Runs {@code test} with the current state of {@link #FLAG_NAME} and restores that state
* afterwards, also when {@code test} fails or is cancelled. The restore is part of the returned
* {@link Mono}, so it never blocks a reactor thread.
*/
private <T> Mono<T> withRestoredFeatureFlag(Function<GetFeatureFlagResponse, Mono<T>> test) {
return Mono.usingWhen(
this.cloudFoundryClient
.featureFlagsV3()
.get(GetFeatureFlagRequest.builder().name(FLAG_NAME).build()),
test,
this::restore,
(original, error) -> restore(original),
this::restore);
}

private Mono<Void> restore(GetFeatureFlagResponse original) {
UpdateFeatureFlagRequest.Builder request =
UpdateFeatureFlagRequest.builder().name(FLAG_NAME).enabled(original.getEnabled());
if (original.getCustomErrorMessage() != null) {
request.customErrorMessage(original.getCustomErrorMessage());
} else {
request.clearCustomErrorMessage(true);
}
return update(request.build()).then();
}
}
Loading