Skip to content

Commit

Permalink
Fix and prevent code style issues in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkobor committed Sep 2, 2020
1 parent df8c555 commit 3543bcc
Show file tree
Hide file tree
Showing 16 changed files with 335 additions and 309 deletions.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ jacocoTestReport {
)
}

detekt {
input = files(
"src/main/kotlin",
"src/test/kotlin"
)
}

build {
dependsOn("detekt")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,41 @@ import io.micronaut.context.ApplicationContext
import io.micronaut.context.env.PropertySource
import io.micronaut.context.exceptions.BeanInstantiationException

class AdminAuthConfigTest : BehaviorSpec({
given("an AdminAuthConfig bean") {
`when`("password is less than 12 characters long") {
val properties = PropertySource.of(
"test",
mapOf(
"admin-auth.username" to "test-user",
"admin-auth.password" to "tooShortPas"
class AdminAuthConfigTest : BehaviorSpec(
{
given("an AdminAuthConfig bean") {
`when`("password is less than 12 characters long") {
val properties = PropertySource.of(
"test",
mapOf(
"admin-auth.username" to "test-user",
"admin-auth.password" to "tooShortPas"
)
)
)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
}
exceptionToMessage(exception) shouldContain "password - size must be between 12"
}
exceptionToMessage(exception) shouldContain "password - size must be between 12"
}
}

`when`("username or password is blank") {
val properties = PropertySource.of(
"test",
mapOf(
"admin-auth.username" to "",
"admin-auth.password" to ""
`when`("username or password is blank") {
val properties = PropertySource.of(
"test",
mapOf(
"admin-auth.username" to "",
"admin-auth.password" to ""
)
)
)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
}
exceptionToMessage(exception) shouldContain "username - must not be blank"
exceptionToMessage(exception) shouldContain "password - must not be blank"
}
exceptionToMessage(exception) shouldContain "username - must not be blank"
exceptionToMessage(exception) shouldContain "password - must not be blank"
}
}
}
})
)
52 changes: 27 additions & 25 deletions src/test/kotlin/com/kuvaszuptime/kuvasz/config/AppConfigTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,39 @@ import io.micronaut.context.ApplicationContext
import io.micronaut.context.env.PropertySource
import io.micronaut.context.exceptions.BeanInstantiationException

class AppConfigTest : BehaviorSpec({
given("an AppConfig bean") {
`when`("there is a data-retention-days parameter with a null value") {
val properties = PropertySource.of(
"test",
mapOf(
"app-config.data-retention-days" to "null"
class AppConfigTest : BehaviorSpec(
{
given("an AppConfig bean") {
`when`("there is a data-retention-days parameter with a null value") {
val properties = PropertySource.of(
"test",
mapOf(
"app-config.data-retention-days" to "null"
)
)
)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
}
exceptionToMessage(exception) shouldContain
"Error resolving property value [app-config.data-retention-days]"
}
exceptionToMessage(exception) shouldContain
"Error resolving property value [app-config.data-retention-days]"
}
}

`when`("there is a data-retention-days parameter with an exceptionally low value") {
val properties = PropertySource.of(
"test",
mapOf(
"app-config.data-retention-days" to "6"
`when`("there is a data-retention-days parameter with an exceptionally low value") {
val properties = PropertySource.of(
"test",
mapOf(
"app-config.data-retention-days" to "6"
)
)
)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
}
exceptionToMessage(exception) shouldContain "dataRetentionDays - must be greater than or equal to 7"
}
exceptionToMessage(exception) shouldContain "dataRetentionDays - must be greater than or equal to 7"
}
}
}
})
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@ import io.micronaut.context.ApplicationContext
import io.micronaut.context.env.PropertySource
import io.micronaut.context.exceptions.BeanInstantiationException

class SMTPMailerConfigTest : BehaviorSpec({
given("an SMTPMailerConfig bean") {
`when`("the SMTP host does not exists") {
val properties = PropertySource.of(
"test",
mapOf(
"handler-config.smtp-event-handler.enabled" to "true",
"smtp-config.host" to "localhost",
"smtp-config.port" to "123"
class SMTPMailerConfigTest : BehaviorSpec(
{
given("an SMTPMailerConfig bean") {
`when`("the SMTP host does not exists") {
val properties = PropertySource.of(
"test",
mapOf(
"handler-config.smtp-event-handler.enabled" to "true",
"smtp-config.host" to "localhost",
"smtp-config.port" to "123"
)
)
)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
}
exceptionToMessage(exception) shouldContain "Error when trying to open connection to the server"
}
exceptionToMessage(exception) shouldContain "Error when trying to open connection to the server"
}
}
}
})
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,41 @@ import io.micronaut.context.ApplicationContext
import io.micronaut.context.env.PropertySource
import io.micronaut.context.exceptions.BeanInstantiationException

class SlackEventHandlerConfigTest : BehaviorSpec({
given("an SlackEventHandlerConfig bean") {
`when`("there is no webhook URL in the configuration") {
val properties = PropertySource.of(
"test",
mapOf(
"handler-config.slack-event-handler.enabled" to "true"
class SlackEventHandlerConfigTest : BehaviorSpec(
{
given("an SlackEventHandlerConfig bean") {
`when`("there is no webhook URL in the configuration") {
val properties = PropertySource.of(
"test",
mapOf(
"handler-config.slack-event-handler.enabled" to "true"
)
)
)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
}
exceptionToMessage(exception) shouldContain
"Bean definition [com.kuvaszuptime.kuvasz.handlers.SlackEventHandler] could not be loaded"
}
exceptionToMessage(exception) shouldContain
"Bean definition [com.kuvaszuptime.kuvasz.handlers.SlackEventHandler] could not be loaded"
}
}

`when`("there the webhookUrl is not a valid URI") {
val properties = PropertySource.of(
"test",
mapOf(
"handler-config.slack-event-handler.enabled" to "true",
"handler-config.slack-event-handler.webhook-url" to "jklfdjaklfjdalfda"
`when`("there the webhookUrl is not a valid URI") {
val properties = PropertySource.of(
"test",
mapOf(
"handler-config.slack-event-handler.enabled" to "true",
"handler-config.slack-event-handler.webhook-url" to "jklfdjaklfjdalfda"
)
)
)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
}
exceptionToMessage(exception) shouldContain
"Bean definition [com.kuvaszuptime.kuvasz.handlers.SlackEventHandler] could not be loaded"
}
exceptionToMessage(exception) shouldContain
"Bean definition [com.kuvaszuptime.kuvasz.handlers.SlackEventHandler] could not be loaded"
}
}
}
})
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,63 @@ import io.micronaut.context.ApplicationContext
import io.micronaut.context.env.PropertySource
import io.micronaut.context.exceptions.BeanInstantiationException

class TelegramEventHandlerConfigTest : BehaviorSpec({
given("a TelegramEventHandlerConfig bean") {
`when`("there is no API token in the configuration") {
val properties = PropertySource.of(
"test",
mapOf(
"handler-config.telegram-event-handler.enabled" to "true",
"handler-config.telegram-event-handler.chat-id" to "chat-id"
class TelegramEventHandlerConfigTest : BehaviorSpec(
{
given("a TelegramEventHandlerConfig bean") {
`when`("there is no API token in the configuration") {
val properties = PropertySource.of(
"test",
mapOf(
"handler-config.telegram-event-handler.enabled" to "true",
"handler-config.telegram-event-handler.chat-id" to "chat-id"
)
)
)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
}
exceptionToMessage(exception) shouldContain
"Bean definition [com.kuvaszuptime.kuvasz.handlers.TelegramEventHandler] " +
"could not be loaded"
}
exceptionToMessage(exception) shouldContain
"Bean definition [com.kuvaszuptime.kuvasz.handlers.TelegramEventHandler] could not be loaded"
}
}

`when`("there is no chat ID in the configuration") {
val properties = PropertySource.of(
"test",
mapOf(
"handler-config.telegram-event-handler.enabled" to "true",
"handler-config.telegram-event-handler.token" to "your-token"
`when`("there is no chat ID in the configuration") {
val properties = PropertySource.of(
"test",
mapOf(
"handler-config.telegram-event-handler.enabled" to "true",
"handler-config.telegram-event-handler.token" to "your-token"
)
)
)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
}
exceptionToMessage(exception) shouldContain
"Bean definition [com.kuvaszuptime.kuvasz.handlers.TelegramEventHandler] " +
"could not be loaded"
}
exceptionToMessage(exception) shouldContain
"Bean definition [com.kuvaszuptime.kuvasz.handlers.TelegramEventHandler] could not be loaded"
}
}

`when`("chat ID and API token are empty strings") {
val properties = PropertySource.of(
"test",
mapOf(
"handler-config.telegram-event-handler.enabled" to "true",
"handler-config.telegram-event-handler.token" to "",
"handler-config.telegram-event-handler.chat-id" to ""
`when`("chat ID and API token are empty strings") {
val properties = PropertySource.of(
"test",
mapOf(
"handler-config.telegram-event-handler.enabled" to "true",
"handler-config.telegram-event-handler.token" to "",
"handler-config.telegram-event-handler.chat-id" to ""
)
)
)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
then("ApplicationContext should throw a BeanInstantiationException") {
val exception = shouldThrow<BeanInstantiationException> {
ApplicationContext.run(properties)
}
exceptionToMessage(exception) shouldContain
"Bean definition [com.kuvaszuptime.kuvasz.handlers.TelegramEventHandler] " +
"could not be loaded"
}
exceptionToMessage(exception) shouldContain
"Bean definition [com.kuvaszuptime.kuvasz.handlers.TelegramEventHandler] could not be loaded"
}
}
}
})
)
Loading

0 comments on commit 3543bcc

Please sign in to comment.