-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ccb8e2f
commit 0d0be91
Showing
8 changed files
with
274 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
cloud-spring/src/test/java/org/incendo/cloud/spring/ApplicationIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2023 Incendo | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
package org.incendo.cloud.spring; | ||
|
||
import cloud.commandframework.Command; | ||
import cloud.commandframework.CommandBean; | ||
import cloud.commandframework.CommandProperties; | ||
import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator; | ||
import cloud.commandframework.internal.CommandNode; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
/** | ||
* Test that spins up a custom spring application to make sure that things wire up like they're supposed to. | ||
*/ | ||
@SpringBootTest | ||
class ApplicationIntegrationTest { | ||
|
||
@Autowired | ||
private ApplicationContext applicationContext; | ||
|
||
@Autowired | ||
private SpringCommandManager<TestCommandSender> springCommandManager; | ||
|
||
@Test | ||
@DisplayName("Verify that the application started") | ||
void test() { | ||
assertThat(this.applicationContext).isNotNull(); | ||
assertThat(this.springCommandManager).isNotNull(); | ||
} | ||
|
||
@Test | ||
@DisplayName("Verify that the command execution coordinator was overridden") | ||
void testCommandExecutionCoordinator() { | ||
assertThat(this.springCommandManager.commandExecutionCoordinator()) | ||
.isInstanceOf(AsynchronousCommandExecutionCoordinator.class); | ||
} | ||
|
||
@Test | ||
@DisplayName("Verify that the command bean was registered") | ||
void testCommandRegistration() { | ||
// Act | ||
final CommandNode<TestCommandSender> commandNode = this.springCommandManager.commandTree().getNamedNode("test"); | ||
|
||
// Assert | ||
assertThat(commandNode).isNotNull(); | ||
} | ||
|
||
|
||
@SpringBootApplication | ||
static class TestApplication { | ||
} | ||
|
||
@TestConfiguration | ||
static class TestConfig { | ||
|
||
@Bean | ||
@NonNull CommandSenderSupplier<TestCommandSender> commandSenderSupplier() { | ||
return TestCommandSender::new; | ||
} | ||
|
||
@Bean | ||
@NonNull SpringCommandExecutionCoordinatorResolver<TestCommandSender> commandExecutionCoordinatorResolver() { | ||
return AsynchronousCommandExecutionCoordinator.<TestCommandSender>builder().withAsynchronousParsing().build()::apply; | ||
} | ||
|
||
@Bean | ||
@NonNull TestCommandBean commandBean() { | ||
return new TestCommandBean(); | ||
} | ||
} | ||
|
||
static class TestCommandSender { | ||
|
||
} | ||
|
||
static class TestCommandBean extends CommandBean<TestCommandSender> { | ||
|
||
@Override | ||
protected @NonNull CommandProperties properties() { | ||
return CommandProperties.of("test"); | ||
} | ||
|
||
@Override | ||
protected Command.Builder<TestCommandSender> configure(final Command.Builder<TestCommandSender> builder) { | ||
return builder; | ||
} | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
cloud-spring/src/test/java/org/incendo/cloud/spring/ShellIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// | ||
// MIT License | ||
// | ||
// Copyright (c) 2023 Incendo | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
// | ||
package org.incendo.cloud.spring; | ||
|
||
import cloud.commandframework.Command; | ||
import cloud.commandframework.CommandBean; | ||
import cloud.commandframework.CommandDescription; | ||
import cloud.commandframework.CommandProperties; | ||
import cloud.commandframework.arguments.flags.CommandFlag; | ||
import cloud.commandframework.context.CommandContext; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.incendo.cloud.spring.config.CloudSpringConfig; | ||
import org.incendo.cloud.spring.registrar.BeanRegistrar; | ||
import org.incendo.cloud.spring.registrar.CommandRegistrationCoordinator; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.shell.test.ShellAssertions; | ||
import org.springframework.shell.test.ShellTestClient; | ||
import org.springframework.shell.test.autoconfigure.ShellTest; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
import static cloud.commandframework.arguments.standard.IntegerParser.integerParser; | ||
import static cloud.commandframework.arguments.standard.StringParser.stringParser; | ||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
@Import(CloudSpringConfig.class) | ||
@ContextConfiguration(classes = { | ||
SpringCommandRegistrationHandler.class, | ||
SpringCommandManager.class, | ||
ShellIntegrationTest.TestConfig.class | ||
}) | ||
@ShellTest | ||
public class ShellIntegrationTest { | ||
|
||
@Autowired | ||
private ShellTestClient client; | ||
|
||
@Test | ||
void testCommandParsing() { | ||
// Arrange | ||
final ShellTestClient.InteractiveShellSession session = this.client.interactive().run(); | ||
await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> { | ||
ShellAssertions.assertThat(session.screen()).containsText("shell"); | ||
}); | ||
|
||
// Act | ||
session.write(session.writeSequence().text("help").carriageReturn().build()); | ||
session.write(session.writeSequence().text("command abc --flag 123").carriageReturn().build()); | ||
|
||
// Assert | ||
await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> { | ||
assertThat(TestCommand.EXECUTED.get()).isTrue(); | ||
}); | ||
|
||
final CommandContext<SpringCommandSender> context = TestCommand.CONTEXT.get(); | ||
assertThat(context.<String>get("string")).isEqualTo("abc"); | ||
assertThat(context.flags().<Integer>get("flag")).isEqualTo(123); | ||
} | ||
|
||
|
||
@SpringBootApplication | ||
static class TestApplication { | ||
|
||
} | ||
|
||
@TestConfiguration | ||
static class TestConfig { | ||
|
||
@Bean | ||
@NonNull CommandRegistrationCoordinator commandRegistrationCoordinator( | ||
final @NonNull SpringCommandManager<SpringCommandSender> springCommandManager | ||
) { | ||
return new CommandRegistrationCoordinator( | ||
springCommandManager, | ||
List.of(new BeanRegistrar<>(List.of(this.testCommand()))) | ||
); | ||
} | ||
|
||
@Bean | ||
@NonNull TestCommand testCommand() { | ||
return new TestCommand(); | ||
} | ||
} | ||
|
||
static class TestCommand extends CommandBean<SpringCommandSender> { | ||
|
||
private static final AtomicBoolean EXECUTED = new AtomicBoolean(false); | ||
private static final AtomicReference<CommandContext<SpringCommandSender>> CONTEXT = new AtomicReference<>(null); | ||
|
||
@Override | ||
protected @NonNull CommandProperties properties() { | ||
return CommandProperties.of("command"); | ||
} | ||
|
||
@Override | ||
protected Command.Builder<SpringCommandSender> configure(final Command.Builder<SpringCommandSender> builder) { | ||
return builder.commandDescription(CommandDescription.commandDescription("Description!")) | ||
.required("string", stringParser()) | ||
.flag(CommandFlag.builder("flag").withComponent(integerParser())); | ||
} | ||
|
||
@Override | ||
public void execute(final @NonNull CommandContext<SpringCommandSender> commandContext) { | ||
EXECUTED.set(true); | ||
CONTEXT.set(commandContext); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters