Skip to content

Commit

Permalink
Add state to e2e availability tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jvalkeal committed May 23, 2024
1 parent cd7add3 commit b954211
Showing 1 changed file with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 the original author or authors.
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,9 +21,11 @@
import org.springframework.shell.command.CommandRegistration;
import org.springframework.shell.command.annotation.Command;
import org.springframework.shell.command.annotation.CommandAvailability;
import org.springframework.shell.command.annotation.Option;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellMethodAvailability;
import org.springframework.shell.standard.ShellOption;
import org.springframework.stereotype.Component;

public class AvailabilityCommands {
Expand Down Expand Up @@ -65,6 +67,28 @@ public String testAvailability3LegacyAnnotation(
public Availability testAvailability3LegacyAnnotationAvailability3() {
return Availability.unavailable("not available 3");
}

private boolean connected = false;

@ShellMethod(key = LEGACY_ANNO + "availability-set", group = GROUP)
public void testAvailabilitySetLegacyAnnotation(
@ShellOption(value = "connected") boolean connected
) {
this.connected = connected;
}

@ShellMethod(key = LEGACY_ANNO + "availability-use", group = GROUP)
@ShellMethodAvailability("testAvailabilityLegacyAnnotationConnected")
public void testAvailabilityUseLegacyAnnotation(
) {
}

public Availability testAvailabilityLegacyAnnotationConnected() {
return connected
? Availability.available()
: Availability.unavailable("you are not connected");
}

}

@Command(command = BaseE2ECommands.ANNO, group = BaseE2ECommands.GROUP)
Expand All @@ -81,6 +105,29 @@ public String testAvailability1Annotation(
public AvailabilityProvider testAvailability1AnnotationAvailability() {
return () -> Availability.unavailable("not available");
}

private boolean connected = false;

@Command(command = "availability-set")
public void testAvailabilitySetAnnotation(
@Option(longNames = "connected") boolean connected
) {
this.connected = connected;
}

@Command(command = "availability-use")
@CommandAvailability(provider = "testAvailabilityAnnotationConnected")
public void testAvailabilityUseAnnotation(
) {
}

@Bean
public AvailabilityProvider testAvailabilityAnnotationConnected() {
return () -> connected
? Availability.available()
: Availability.unavailable("you are not connected");
}

}

@Component
Expand Down Expand Up @@ -119,5 +166,46 @@ public CommandRegistration testAvailability2Registration() {
AvailabilityProvider testAvailability2AnnotationAvailability() {
return () -> Availability.unavailable("not available");
}

private boolean connected = false;

@Bean
public CommandRegistration testAvailabilitySetRegistration() {
return getBuilder()
.command(REG, "availability-set")
.group(GROUP)
.withOption()
.longNames("connected")
.required()
.type(boolean.class)
.and()
.withTarget()
.consumer(ctx -> {
boolean connected = ctx.getOptionValue("connected");
this.connected = connected;
})
.and()
.build();
}

@Bean
public CommandRegistration testAvailabilityUseRegistration() {
return getBuilder()
.command(REG, "availability-use")
.group(GROUP)
.availability(testAvailabilityRegistrationConnected())
.withTarget()
.consumer(ctx -> {
})
.and()
.build();
}

public AvailabilityProvider testAvailabilityRegistrationConnected() {
return () -> connected
? Availability.available()
: Availability.unavailable("you are not connected");
}

}
}

0 comments on commit b954211

Please sign in to comment.