|
| 1 | +// |
| 2 | +// MIT License |
| 3 | +// |
| 4 | +// Copyright (c) 2024 Incendo |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in all |
| 14 | +// copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +// SOFTWARE. |
| 23 | +// |
| 24 | +package org.incendo.cloud.execution; |
| 25 | + |
| 26 | +import java.util.concurrent.CompletableFuture; |
| 27 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 28 | +import org.incendo.cloud.CommandManager; |
| 29 | +import org.incendo.cloud.TestCommandSender; |
| 30 | +import org.incendo.cloud.context.CommandContext; |
| 31 | +import org.junit.jupiter.api.BeforeEach; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | + |
| 34 | +import static com.google.common.truth.Truth.assertThat; |
| 35 | +import static org.incendo.cloud.util.TestUtils.createManager; |
| 36 | + |
| 37 | +class FutureCommandExecutionCoordinatorTest { |
| 38 | + |
| 39 | + private CommandManager<TestCommandSender> commandManager; |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + void setup() { |
| 43 | + this.commandManager = createManager(); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void testExecution() { |
| 48 | + // Arrange |
| 49 | + this.commandManager.command(this.commandManager.commandBuilder("command").futureHandler(this::handler)); |
| 50 | + |
| 51 | + // Act |
| 52 | + final CommandResult<TestCommandSender> result = |
| 53 | + this.commandManager.commandExecutor().executeCommand(new TestCommandSender(), "command").join(); |
| 54 | + |
| 55 | + // Assert |
| 56 | + assertThat(result.commandContext().<String>get("string")).isEqualTo("hello"); |
| 57 | + } |
| 58 | + |
| 59 | + private @NonNull CompletableFuture<Void> handler(final @NonNull CommandContext<TestCommandSender> context) { |
| 60 | + return CompletableFuture.supplyAsync(() -> "hello").thenAccept(string -> context.store("string", string)); |
| 61 | + } |
| 62 | +} |
0 commit comments