From 81723bdeb19db3960fd680a6db8a53e5b95c5e6d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 13 Nov 2024 22:36:00 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=8F=97=EF=B8=8F=20Add=20Guice=20module=20t?= =?utf8?q?o=20instantiate=20all=20classes?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../rhynodge/engine/RhynodgeModule.kt | 31 ++++++++++++++ .../rhynodge/engine/RhynodgeModuleTest.kt | 50 ++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/main/kotlin/net/pterodactylus/rhynodge/engine/RhynodgeModule.kt create mode 100644 src/test/kotlin/net/pterodactylus/rhynodge/engine/RhynodgeModuleTest.kt diff --git a/src/main/kotlin/net/pterodactylus/rhynodge/engine/RhynodgeModule.kt b/src/main/kotlin/net/pterodactylus/rhynodge/engine/RhynodgeModule.kt new file mode 100644 index 0000000..6b1220a --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/rhynodge/engine/RhynodgeModule.kt @@ -0,0 +1,31 @@ +package net.pterodactylus.rhynodge.engine + +import com.google.inject.AbstractModule +import com.google.inject.Provides +import jakarta.inject.Named +import net.pterodactylus.rhynodge.Action +import net.pterodactylus.rhynodge.actions.EmailAction +import net.pterodactylus.rhynodge.loader.ChainWatcher +import net.pterodactylus.rhynodge.states.StateManager + +class RhynodgeModule : AbstractModule() { + + @Provides + @Named("email") + fun getEmailAction(options: Options): Action = + EmailAction(options.smtpHostname, options.emailSender, options.emailRecipient) + + @Provides + @Named("errorEmail") + fun getErrorEmailAction(options: Options): Action = + EmailAction(options.smtpHostname, options.errorEmailSender, options.errorEmailRecipient) + + @Provides + fun getChainDirectory(options: Options): ChainWatcher.ChainDirectory = + ChainWatcher.ChainDirectory.of(options.chainDirectory) + + @Provides + fun getStateDirectory(options: Options): StateManager.StateDirectory = + StateManager.StateDirectory.of(options.stateDirectory) + +} diff --git a/src/test/kotlin/net/pterodactylus/rhynodge/engine/RhynodgeModuleTest.kt b/src/test/kotlin/net/pterodactylus/rhynodge/engine/RhynodgeModuleTest.kt new file mode 100644 index 0000000..9acbc8f --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/rhynodge/engine/RhynodgeModuleTest.kt @@ -0,0 +1,50 @@ +package net.pterodactylus.rhynodge.engine + +import com.google.inject.Guice.createInjector +import net.pterodactylus.rhynodge.loader.ChainWatcher +import net.pterodactylus.rhynodge.loader.ReactionLoader +import net.pterodactylus.rhynodge.utils.toOptional +import net.pterodactylus.util.envopt.Parser +import net.pterodactylus.util.inject.ObjectBinding +import org.junit.jupiter.api.Test + +class RhynodgeModuleTest { + + @Test + fun `can create rhynodge module`() { + RhynodgeModule() + } + + @Test + fun `chain watcher can be created`() { + injector.getInstance(ChainWatcher::class.java) + } + + @Test + fun `reaction loader can be created`() { + injector.getInstance(ReactionLoader::class.java) + } + + @Test + fun `engine can be created`() { + injector.getInstance(Engine::class.java) + } + + private val injector = createInjector( + ObjectBinding.forClass(Options::class.java).shallBe(supplyOptions()), + RhynodgeModule() + ) + +} + +private fun supplyOptions(): Options { + val environment = mapOf( + "SMTP_HOSTNAME" to "host.local", + "EMAIL_SENDER" to "send@r", + "EMAIL_RECIPIENT" to "recipi@nt", + "ERROR_EMAIL_SENDER" to "error-send@r", + "ERROR_EMAIL_RECIPIENT" to "error-recipient@nt", + ) + return Parser { environment[it].toOptional() } + .parseEnvironment(::Options) +} -- 2.7.4