diff --git a/cli/build.gradle b/cli/build.gradle index 576b4c31..d13ebe33 100644 --- a/cli/build.gradle +++ b/cli/build.gradle @@ -10,6 +10,7 @@ dependencies { implementation project(':ownership-cli') implementation project(':lnd-cli') implementation project(':cli-base') + implementation project(':wizard-cli') implementation 'org.flywaydb:flyway-core' testImplementation testFixtures(project(':backend-transaction')) testImplementation testFixtures(project(':backend-transaction-models')) diff --git a/settings.gradle b/settings.gradle index a0e15fee..4358ceda 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,6 +1,6 @@ rootProject.name = 'bitbook' -include 'cli-base' include 'cli' +include 'cli-base' include 'backend' include 'backend-price' include 'backend-address-transactions' @@ -24,3 +24,5 @@ include 'lnd' include 'lnd-cli' include 'ownership' include 'ownership-cli' +include 'wizard' +include 'wizard-cli' diff --git a/wizard-cli/build.gradle b/wizard-cli/build.gradle new file mode 100644 index 00000000..7bab401e --- /dev/null +++ b/wizard-cli/build.gradle @@ -0,0 +1,9 @@ +plugins { + id 'bitbook.java-library-conventions' +} + +dependencies { + implementation project(':cli-base') + implementation project(':wizard') + testImplementation testFixtures(project(':backend-transaction-models')) +} \ No newline at end of file diff --git a/wizard-cli/src/main/java/de/cotto/bitbook/wizard/cli/WizardCommands.java b/wizard-cli/src/main/java/de/cotto/bitbook/wizard/cli/WizardCommands.java new file mode 100644 index 00000000..1980391a --- /dev/null +++ b/wizard-cli/src/main/java/de/cotto/bitbook/wizard/cli/WizardCommands.java @@ -0,0 +1,37 @@ +package de.cotto.bitbook.wizard.cli; + +import de.cotto.bitbook.cli.PromptChangeListener; +import de.cotto.bitbook.wizard.WizardService; +import org.springframework.shell.Availability; +import org.springframework.shell.standard.ShellComponent; +import org.springframework.shell.standard.ShellMethod; + +@ShellComponent +public class WizardCommands { + private final WizardService wizardService; + private final PromptChangeListener promptChangeListener; + + public WizardCommands(WizardService wizardService, PromptChangeListener promptChangeListener) { + this.wizardService = wizardService; + this.promptChangeListener = promptChangeListener; + } + + @ShellMethod("Start the wizard which helps you complete the ownership information") + public void wizard() { + wizardService.enableWizard(); + promptChangeListener.changePrompt("wizard$ "); + } + + @ShellMethod("Exit the wizard") + public void exitWizard() { + wizardService.disableWizard(); + promptChangeListener.changePromptToDefault(); + } + + public Availability exitWizardAvailability() { + if (wizardService.isEnabled()) { + return Availability.available(); + } + return Availability.unavailable("wizard is not active"); + } +} diff --git a/wizard-cli/src/test/java/de/cotto/bitbook/wizard/cli/WizardCommandsTest.java b/wizard-cli/src/test/java/de/cotto/bitbook/wizard/cli/WizardCommandsTest.java new file mode 100644 index 00000000..71f16f7c --- /dev/null +++ b/wizard-cli/src/test/java/de/cotto/bitbook/wizard/cli/WizardCommandsTest.java @@ -0,0 +1,68 @@ +package de.cotto.bitbook.wizard.cli; + +import de.cotto.bitbook.cli.PromptChangeListener; +import de.cotto.bitbook.wizard.WizardService; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class WizardCommandsTest { + @InjectMocks + private WizardCommands wizardCommands; + + @Mock + private WizardService wizardService; + + @Mock + private PromptChangeListener promptChangeListener; + + @Nested + class WizardDisabled { + @Test + void enables_wizard() { + wizardCommands.wizard(); + verify(wizardService).enableWizard(); + } + + @Test + void exit_wizard_command_initially_not_available() { + assertThat(wizardCommands.exitWizardAvailability().isAvailable()).isEqualTo(false); + assertThat(wizardCommands.exitWizardAvailability().getReason()).isEqualTo("wizard is not active"); + } + } + + @Nested + class WizardEnabled { + @Test + void changes_prompt() { + wizardCommands.wizard(); + verify(promptChangeListener).changePrompt("wizard$ "); + } + + @Test + void exit_wizard_command_available() { + when(wizardService.isEnabled()).thenReturn(true); + assertThat(wizardCommands.exitWizardAvailability().isAvailable()).isEqualTo(true); + } + + @Test + void exit_wizard_changes_prompt_to_default() { + wizardCommands.exitWizard(); + verify(promptChangeListener).changePromptToDefault(); + } + + @Test + void exit_wizard_notifies_service() { + wizardCommands.exitWizard(); + verify(wizardService).disableWizard(); + } + } +} \ No newline at end of file diff --git a/wizard/build.gradle b/wizard/build.gradle new file mode 100644 index 00000000..ba412183 --- /dev/null +++ b/wizard/build.gradle @@ -0,0 +1,8 @@ +plugins { + id 'bitbook.java-library-conventions' +} + +dependencies { + implementation project(':ownership') + testImplementation testFixtures(project(':backend-transaction-models')) +} \ No newline at end of file diff --git a/wizard/src/main/java/de/cotto/bitbook/wizard/WizardService.java b/wizard/src/main/java/de/cotto/bitbook/wizard/WizardService.java new file mode 100644 index 00000000..e3e3d81e --- /dev/null +++ b/wizard/src/main/java/de/cotto/bitbook/wizard/WizardService.java @@ -0,0 +1,24 @@ +package de.cotto.bitbook.wizard; + +import org.springframework.stereotype.Component; + +@Component +public class WizardService { + private boolean enabled; + + public WizardService() { + // default constructor + } + + public void enableWizard() { + enabled = true; + } + + public boolean isEnabled() { + return enabled; + } + + public void disableWizard() { + enabled = false; + } +} diff --git a/wizard/src/test/java/de/cotto/bitbook/wizard/WizardServiceTest.java b/wizard/src/test/java/de/cotto/bitbook/wizard/WizardServiceTest.java new file mode 100644 index 00000000..134c22a7 --- /dev/null +++ b/wizard/src/test/java/de/cotto/bitbook/wizard/WizardServiceTest.java @@ -0,0 +1,31 @@ +package de.cotto.bitbook.wizard; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(MockitoExtension.class) +class WizardServiceTest { + @InjectMocks + private WizardService wizardService; + + @Test + void isEnabled() { + assertThat(wizardService.isEnabled()).isFalse(); + } + + @Test + void enableWizard() { + wizardService.enableWizard(); + assertThat(wizardService.isEnabled()).isTrue(); + } + + @Test + void disableWizard() { + wizardService.disableWizard(); + assertThat(wizardService.isEnabled()).isFalse(); + } +} \ No newline at end of file