🏗️ Add Guice module to instantiate all classes
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 13 Nov 2024 21:36:00 +0000 (22:36 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 13 Nov 2024 21:36:00 +0000 (22:36 +0100)
src/main/kotlin/net/pterodactylus/rhynodge/engine/RhynodgeModule.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/rhynodge/engine/RhynodgeModuleTest.kt [new file with mode: 0644]

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 (file)
index 0000000..6b1220a
--- /dev/null
@@ -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 (file)
index 0000000..9acbc8f
--- /dev/null
@@ -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)
+}