diff --git a/Dockerfile b/Dockerfile index 47568b7..158b56d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM oracle/graalvm-ce:20.2.0-java11 as graalvm +FROM oracle/graalvm-ce:20.3.0-java11 as graalvm ARG VERSION="" RUN gu install native-image @@ -8,7 +8,7 @@ WORKDIR /home/app/kuvasz RUN native-image --no-server --verbose -cp build/libs/kuvasz-${VERSION}-all.jar \ --initialize-at-build-time=com.sun.mail.util.LineInputStream -FROM frolvlad/alpine-glibc:alpine-3.12_glibc-2.31 +FROM frolvlad/alpine-glibc:alpine-3.12_glibc-2.32 RUN apk --no-cache add libstdc++ EXPOSE 8080 COPY --from=graalvm /home/app/kuvasz/kuvasz /app/kuvasz diff --git a/build.gradle b/build.gradle index a7edc26..063db15 100644 --- a/build.gradle +++ b/build.gradle @@ -1,3 +1,11 @@ +buildscript { + configurations['classpath'].resolutionStrategy.eachDependency { + if (requested.group == 'org.jooq') { + useVersion jooqVersion + } + } +} + plugins { id "org.jetbrains.kotlin.jvm" version "${kotlinVersion}" id "org.jetbrains.kotlin.kapt" version "${kotlinVersion}" @@ -98,6 +106,29 @@ jacocoTestReport { ) } +jacocoTestCoverageVerification { + afterEvaluate { + classDirectories.setFrom( + fileTree("build/classes/kotlin/main") { + exclude("com/kuvaszuptime/kuvasz/Application.class") + } + ) + } + violationRules { + rule { + limit { + counter = "INSTRUCTION" + value = "COVEREDRATIO" + minimum = 0.9 + } + } + } +} + +check { + dependsOn("jacocoTestCoverageVerification") +} + detekt { input = files( "src/main/kotlin", @@ -210,7 +241,6 @@ jooq { driver = "org.postgresql.Driver" } generator { - name = "org.jooq.codegen.DefaultGenerator" target { directory = "src/jooq/java" packageName = "com.kuvaszuptime.kuvasz" diff --git a/gradle.properties b/gradle.properties index f4e7f73..80d6611 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,17 +1,17 @@ arrowDataVersion=0.11.0 -checkUpdatesVersion=1.1.0 +checkUpdatesVersion=1.1.4 detektVersion=1.14.2 jibVersion=2.6.0 jooqPluginVersion=5.2 jooqVersion=3.13.1 -kotlinCoroutinesVersion=1.3.9 -kotlinVersion=1.4.10 -logbookVersion=2.3.0 -micronautPluginVersion=1.0.5 -micronautVersion=2.1.2 +kotlinCoroutinesVersion=1.4.1 +kotlinVersion=1.4.20 +logbookVersion=2.4.1 +micronautPluginVersion=1.2.0 +micronautVersion=2.1.4 mockkVersion=1.10.2 palantirGitVersion=0.12.3 -postgresTestContainersVersion=1.14.3 +postgresTestContainersVersion=1.15.0 postgresVersion=42.2.18 shadowVersion=6.1.0 -simpleJavaMailVersion=6.4.3 +simpleJavaMailVersion=6.4.4 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index bb105ce..9216860 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ #Sat Jul 18 09:10:10 CEST 2020 -distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStorePath=wrapper/dists diff --git a/src/main/kotlin/com/kuvaszuptime/kuvasz/factories/LogbookFactory.kt b/src/main/kotlin/com/kuvaszuptime/kuvasz/factories/LogbookFactory.kt index d1cefbc..00f7da9 100644 --- a/src/main/kotlin/com/kuvaszuptime/kuvasz/factories/LogbookFactory.kt +++ b/src/main/kotlin/com/kuvaszuptime/kuvasz/factories/LogbookFactory.kt @@ -1,6 +1,8 @@ package com.kuvaszuptime.kuvasz.factories import io.micronaut.context.annotation.Factory +import org.zalando.logbook.Conditions.exclude +import org.zalando.logbook.Conditions.requestTo import org.zalando.logbook.DefaultHttpLogWriter import org.zalando.logbook.DefaultSink import org.zalando.logbook.Logbook @@ -15,6 +17,7 @@ class LogbookFactory { @Singleton fun logbook(): Logbook = Logbook.builder() + .condition(exclude(requestTo("/health"))) .strategy(SecurityStrategy()) .strategy(WithoutBodyStrategy()) .sink(DefaultSink(JsonHttpLogFormatter(), DefaultHttpLogWriter())) diff --git a/src/main/kotlin/com/kuvaszuptime/kuvasz/handlers/DatabaseEventHandler.kt b/src/main/kotlin/com/kuvaszuptime/kuvasz/handlers/DatabaseEventHandler.kt index 632d0b1..1f47432 100644 --- a/src/main/kotlin/com/kuvaszuptime/kuvasz/handlers/DatabaseEventHandler.kt +++ b/src/main/kotlin/com/kuvaszuptime/kuvasz/handlers/DatabaseEventHandler.kt @@ -6,8 +6,9 @@ import com.kuvaszuptime.kuvasz.repositories.LatencyLogRepository import com.kuvaszuptime.kuvasz.repositories.SSLEventRepository import com.kuvaszuptime.kuvasz.repositories.UptimeEventRepository import com.kuvaszuptime.kuvasz.services.EventDispatcher -import com.kuvaszuptime.kuvasz.util.transaction import io.micronaut.context.annotation.Context +import org.jooq.Configuration +import org.jooq.impl.DSL import org.slf4j.LoggerFactory @Context @@ -15,7 +16,8 @@ class DatabaseEventHandler( private val eventDispatcher: EventDispatcher, private val uptimeEventRepository: UptimeEventRepository, private val latencyLogRepository: LatencyLogRepository, - private val sslEventRepository: SSLEventRepository + private val sslEventRepository: SSLEventRepository, + private val jooqConfig: Configuration ) { companion object { private val logger = LoggerFactory.getLogger(DatabaseEventHandler::class.java) @@ -52,9 +54,9 @@ class DatabaseEventHandler( private fun handleUptimeMonitorEvent(currentEvent: UptimeMonitorEvent) { currentEvent.previousEvent?.let { previousEvent -> if (currentEvent.statusNotEquals(previousEvent)) { - uptimeEventRepository.transaction { - uptimeEventRepository.endEventById(previousEvent.id, currentEvent.dispatchedAt) - uptimeEventRepository.insertFromMonitorEvent(currentEvent) + DSL.using(jooqConfig).transaction { config -> + uptimeEventRepository.endEventById(previousEvent.id, currentEvent.dispatchedAt, config) + uptimeEventRepository.insertFromMonitorEvent(currentEvent, config) } } else { uptimeEventRepository.updateEventUpdatedAt(previousEvent.id, currentEvent.dispatchedAt) @@ -65,9 +67,9 @@ class DatabaseEventHandler( private fun handleSSLMonitorEvent(currentEvent: SSLMonitorEvent) { currentEvent.previousEvent?.let { previousEvent -> if (currentEvent.statusNotEquals(previousEvent)) { - sslEventRepository.transaction { - sslEventRepository.endEventById(previousEvent.id, currentEvent.dispatchedAt) - sslEventRepository.insertFromMonitorEvent(currentEvent) + DSL.using(jooqConfig).transaction { config -> + sslEventRepository.endEventById(previousEvent.id, currentEvent.dispatchedAt, config) + sslEventRepository.insertFromMonitorEvent(currentEvent, config) } } else { sslEventRepository.updateEventUpdatedAt(previousEvent.id, currentEvent.dispatchedAt) diff --git a/src/main/kotlin/com/kuvaszuptime/kuvasz/repositories/MonitorRepository.kt b/src/main/kotlin/com/kuvaszuptime/kuvasz/repositories/MonitorRepository.kt index 167198a..27b742e 100644 --- a/src/main/kotlin/com/kuvaszuptime/kuvasz/repositories/MonitorRepository.kt +++ b/src/main/kotlin/com/kuvaszuptime/kuvasz/repositories/MonitorRepository.kt @@ -11,6 +11,8 @@ import com.kuvaszuptime.kuvasz.tables.SslEvent.SSL_EVENT import com.kuvaszuptime.kuvasz.tables.UptimeEvent.UPTIME_EVENT import com.kuvaszuptime.kuvasz.tables.daos.MonitorDao import com.kuvaszuptime.kuvasz.tables.pojos.MonitorPojo +import com.kuvaszuptime.kuvasz.tables.records.MonitorRecord +import com.kuvaszuptime.kuvasz.util.fetchOneIntoOrThrow import com.kuvaszuptime.kuvasz.util.getCurrentTimestamp import com.kuvaszuptime.kuvasz.util.toPersistenceError import org.jooq.Configuration @@ -62,8 +64,7 @@ class MonitorRepository(jooqConfig: Configuration) : MonitorDao(jooqConfig) { .insertInto(MONITOR) .set(dsl.newRecord(MONITOR, monitorPojo)) .returning(MONITOR.asterisk()) - .fetchOne() - .into(MonitorPojo::class.java) + .fetchOneIntoOrThrow() ) } catch (e: DataAccessException) { e.handle() @@ -83,8 +84,7 @@ class MonitorRepository(jooqConfig: Configuration) : MonitorDao(jooqConfig) { .set(MONITOR.PAGERDUTY_INTEGRATION_KEY, updatedPojo.pagerdutyIntegrationKey) .where(MONITOR.ID.eq(updatedPojo.id)) .returning(MONITOR.asterisk()) - .fetchOne() - .into(MonitorPojo::class.java) + .fetchOneIntoOrThrow() ) } catch (e: DataAccessException) { e.handle() diff --git a/src/main/kotlin/com/kuvaszuptime/kuvasz/repositories/SSLEventRepository.kt b/src/main/kotlin/com/kuvaszuptime/kuvasz/repositories/SSLEventRepository.kt index 49fb604..346cb37 100644 --- a/src/main/kotlin/com/kuvaszuptime/kuvasz/repositories/SSLEventRepository.kt +++ b/src/main/kotlin/com/kuvaszuptime/kuvasz/repositories/SSLEventRepository.kt @@ -7,14 +7,15 @@ import com.kuvaszuptime.kuvasz.tables.SslEvent.SSL_EVENT import com.kuvaszuptime.kuvasz.tables.daos.SslEventDao import com.kuvaszuptime.kuvasz.tables.pojos.SslEventPojo import org.jooq.Configuration +import org.jooq.impl.DSL import java.time.OffsetDateTime import javax.inject.Singleton @Singleton -class SSLEventRepository(jooqConfig: Configuration) : SslEventDao(jooqConfig) { +class SSLEventRepository(private val jooqConfig: Configuration) : SslEventDao(jooqConfig) { private val dsl = jooqConfig.dsl() - fun insertFromMonitorEvent(event: SSLMonitorEvent) { + fun insertFromMonitorEvent(event: SSLMonitorEvent, configuration: Configuration? = jooqConfig) { val eventToInsert = SslEventPojo() .setMonitorId(event.monitor.id) .setStatus(event.sslStatus) @@ -25,7 +26,10 @@ class SSLEventRepository(jooqConfig: Configuration) : SslEventDao(jooqConfig) { eventToInsert.error = event.error.message } - insert(eventToInsert) + DSL.using(configuration) + .insertInto(SSL_EVENT) + .set(dsl.newRecord(SSL_EVENT, eventToInsert)) + .execute() } fun getPreviousEventByMonitorId(monitorId: Int): SslEventPojo? = @@ -35,8 +39,9 @@ class SSLEventRepository(jooqConfig: Configuration) : SslEventDao(jooqConfig) { .and(SSL_EVENT.ENDED_AT.isNull) .fetchOneInto(SslEventPojo::class.java) - fun endEventById(eventId: Int, endedAt: OffsetDateTime) = - dsl.update(SSL_EVENT) + fun endEventById(eventId: Int, endedAt: OffsetDateTime, configuration: Configuration? = jooqConfig) = + DSL.using(configuration) + .update(SSL_EVENT) .set(SSL_EVENT.ENDED_AT, endedAt) .set(SSL_EVENT.UPDATED_AT, endedAt) .where(SSL_EVENT.ID.eq(eventId)) diff --git a/src/main/kotlin/com/kuvaszuptime/kuvasz/repositories/UptimeEventRepository.kt b/src/main/kotlin/com/kuvaszuptime/kuvasz/repositories/UptimeEventRepository.kt index d6fd014..0c0547d 100644 --- a/src/main/kotlin/com/kuvaszuptime/kuvasz/repositories/UptimeEventRepository.kt +++ b/src/main/kotlin/com/kuvaszuptime/kuvasz/repositories/UptimeEventRepository.kt @@ -8,14 +8,15 @@ import com.kuvaszuptime.kuvasz.tables.UptimeEvent.UPTIME_EVENT import com.kuvaszuptime.kuvasz.tables.daos.UptimeEventDao import com.kuvaszuptime.kuvasz.tables.pojos.UptimeEventPojo import org.jooq.Configuration +import org.jooq.impl.DSL import java.time.OffsetDateTime import javax.inject.Singleton @Singleton -class UptimeEventRepository(jooqConfig: Configuration) : UptimeEventDao(jooqConfig) { +class UptimeEventRepository(private val jooqConfig: Configuration) : UptimeEventDao(jooqConfig) { private val dsl = jooqConfig.dsl() - fun insertFromMonitorEvent(event: UptimeMonitorEvent) { + fun insertFromMonitorEvent(event: UptimeMonitorEvent, configuration: Configuration? = jooqConfig) { val eventToInsert = UptimeEventPojo() .setMonitorId(event.monitor.id) .setStatus(event.uptimeStatus) @@ -26,7 +27,10 @@ class UptimeEventRepository(jooqConfig: Configuration) : UptimeEventDao(jooqConf eventToInsert.error = event.error.message } - insert(eventToInsert) + DSL.using(configuration) + .insertInto(UPTIME_EVENT) + .set(dsl.newRecord(UPTIME_EVENT, eventToInsert)) + .execute() } fun getPreviousEventByMonitorId(monitorId: Int): UptimeEventPojo? = @@ -36,8 +40,9 @@ class UptimeEventRepository(jooqConfig: Configuration) : UptimeEventDao(jooqConf .and(UPTIME_EVENT.ENDED_AT.isNull) .fetchOneInto(UptimeEventPojo::class.java) - fun endEventById(eventId: Int, endedAt: OffsetDateTime) = - dsl.update(UPTIME_EVENT) + fun endEventById(eventId: Int, endedAt: OffsetDateTime, configuration: Configuration? = jooqConfig) = + DSL.using(configuration) + .update(UPTIME_EVENT) .set(UPTIME_EVENT.ENDED_AT, endedAt) .set(UPTIME_EVENT.UPDATED_AT, endedAt) .where(UPTIME_EVENT.ID.eq(eventId)) diff --git a/src/main/kotlin/com/kuvaszuptime/kuvasz/util/Jooq+.kt b/src/main/kotlin/com/kuvaszuptime/kuvasz/util/Jooq+.kt index 3c99cfc..a7e9d1c 100644 --- a/src/main/kotlin/com/kuvaszuptime/kuvasz/util/Jooq+.kt +++ b/src/main/kotlin/com/kuvaszuptime/kuvasz/util/Jooq+.kt @@ -4,17 +4,20 @@ package com.kuvaszuptime.kuvasz.util import com.kuvaszuptime.kuvasz.models.DuplicationError import com.kuvaszuptime.kuvasz.models.PersistenceError -import org.jooq.Configuration -import org.jooq.DAO +import org.jooq.InsertResultStep import org.jooq.TableRecord +import org.jooq.UpdateResultStep import org.jooq.exception.DataAccessException +import org.jooq.exception.NoDataFoundException import org.postgresql.util.PSQLException -fun , P, T> DAO.transaction(block: () -> Unit) { - configuration().dsl().transaction { _: Configuration -> block() } -} - fun DataAccessException.toPersistenceError(): PersistenceError = getCause(PSQLException::class.java)?.message?.let { message -> if (message.contains("duplicate key")) DuplicationError() else PersistenceError(message) } ?: PersistenceError(message) + +inline fun , reified O> InsertResultStep.fetchOneIntoOrThrow(): O = + fetchOne()?.into(O::class.java) ?: throw NoDataFoundException() + +inline fun , reified O> UpdateResultStep.fetchOneIntoOrThrow(): O = + fetchOne()?.into(O::class.java) ?: throw NoDataFoundException() diff --git a/src/test/kotlin/com/kuvaszuptime/kuvasz/controllers/MonitorControllerTest.kt b/src/test/kotlin/com/kuvaszuptime/kuvasz/controllers/MonitorControllerTest.kt index 4deaa73..1ce4978 100644 --- a/src/test/kotlin/com/kuvaszuptime/kuvasz/controllers/MonitorControllerTest.kt +++ b/src/test/kotlin/com/kuvaszuptime/kuvasz/controllers/MonitorControllerTest.kt @@ -438,7 +438,7 @@ class MonitorControllerTest( then("it should delete the integration key of the given monitor") { response.status shouldBe HttpStatus.NO_CONTENT - monitorInDb.pagerdutyIntegrationKey shouldBe null + monitorInDb!!.pagerdutyIntegrationKey shouldBe null } } diff --git a/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/DatabaseEventHandlerTest.kt b/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/DatabaseEventHandlerTest.kt index e4b3cb9..421ff88 100644 --- a/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/DatabaseEventHandlerTest.kt +++ b/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/DatabaseEventHandlerTest.kt @@ -30,20 +30,28 @@ import io.mockk.clearAllMocks import io.mockk.spyk import io.mockk.verify import io.mockk.verifyOrder +import org.jooq.Configuration @MicronautTest(startApplication = false) class DatabaseEventHandlerTest( uptimeEventRepository: UptimeEventRepository, latencyLogRepository: LatencyLogRepository, monitorRepository: MonitorRepository, - sslEventRepository: SSLEventRepository + sslEventRepository: SSLEventRepository, + jooqConfig: Configuration ) : DatabaseBehaviorSpec() { init { val eventDispatcher = EventDispatcher() val uptimeEventRepositorySpy = spyk(uptimeEventRepository) val latencyLogRepositorySpy = spyk(latencyLogRepository) val sslEventRepositorySpy = spyk(sslEventRepository) - DatabaseEventHandler(eventDispatcher, uptimeEventRepositorySpy, latencyLogRepositorySpy, sslEventRepositorySpy) + DatabaseEventHandler( + eventDispatcher, + uptimeEventRepositorySpy, + latencyLogRepositorySpy, + sslEventRepositorySpy, + jooqConfig + ) given("the DatabaseEventHandler - UPTIME events") { `when`("it receives a MonitorUpEvent and there is no previous event for the monitor") { @@ -69,11 +77,11 @@ class DatabaseEventHandlerTest( ) } - expectedUptimeRecord.status shouldBe UptimeStatus.UP + expectedUptimeRecord!!.status shouldBe UptimeStatus.UP expectedUptimeRecord.startedAt shouldBe event.dispatchedAt expectedUptimeRecord.endedAt shouldBe null expectedUptimeRecord.updatedAt shouldBe event.dispatchedAt - expectedLatencyRecord.latency shouldBe event.latency + expectedLatencyRecord!!.latency shouldBe event.latency } } @@ -93,7 +101,7 @@ class DatabaseEventHandlerTest( verify(exactly = 1) { uptimeEventRepositorySpy.insertFromMonitorEvent(event) } verify(exactly = 0) { uptimeEventRepositorySpy.endEventById(any(), any()) } - expectedUptimeRecord.status shouldBe UptimeStatus.DOWN + expectedUptimeRecord!!.status shouldBe UptimeStatus.DOWN expectedUptimeRecord.startedAt shouldBe event.dispatchedAt expectedUptimeRecord.endedAt shouldBe null expectedUptimeRecord.updatedAt shouldBe event.dispatchedAt @@ -123,14 +131,14 @@ class DatabaseEventHandlerTest( val expectedUptimeRecord = uptimeEventRepository.fetchOne(UPTIME_EVENT.MONITOR_ID, monitor.id) val latencyRecords = latencyLogRepository.fetchByMonitorId(monitor.id).sortedBy { it.createdAt } - verify(exactly = 1) { uptimeEventRepositorySpy.insertFromMonitorEvent(firstEvent) } - verify(exactly = 0) { uptimeEventRepositorySpy.endEventById(any(), any()) } + verify(exactly = 1) { uptimeEventRepositorySpy.insertFromMonitorEvent(firstEvent, any()) } + verify(exactly = 0) { uptimeEventRepositorySpy.endEventById(any(), any(), any()) } verifyOrder { latencyLogRepositorySpy.insertLatencyForMonitor(monitor.id, firstEvent.latency) latencyLogRepositorySpy.insertLatencyForMonitor(monitor.id, secondEvent.latency) } - expectedUptimeRecord.status shouldBe UptimeStatus.UP + expectedUptimeRecord!!.status shouldBe UptimeStatus.UP expectedUptimeRecord.endedAt shouldBe null expectedUptimeRecord.updatedAt shouldBe secondEvent.dispatchedAt latencyRecords shouldHaveSize 2 @@ -148,7 +156,7 @@ class DatabaseEventHandlerTest( error = Throwable() ) eventDispatcher.dispatch(firstEvent) - val firstUptimeRecord = uptimeEventRepository.fetchOne(UPTIME_EVENT.MONITOR_ID, monitor.id) + val firstUptimeRecord = uptimeEventRepository.fetchOne(UPTIME_EVENT.MONITOR_ID, monitor.id)!! val secondEvent = MonitorUpEvent( monitor = monitor, @@ -160,13 +168,13 @@ class DatabaseEventHandlerTest( then("it should create a new UptimeEvent and a LatencyLog record, and end the previous one") { val uptimeRecords = uptimeEventRepository.fetchByMonitorId(monitor.id).sortedBy { it.startedAt } - val latencyRecord = latencyLogRepository.fetchOne(LATENCY_LOG.MONITOR_ID, monitor.id) + val latencyRecord = latencyLogRepository.fetchOne(LATENCY_LOG.MONITOR_ID, monitor.id)!! verifyOrder { - uptimeEventRepositorySpy.insertFromMonitorEvent(firstEvent) + uptimeEventRepositorySpy.insertFromMonitorEvent(firstEvent, any()) latencyLogRepositorySpy.insertLatencyForMonitor(monitor.id, secondEvent.latency) - uptimeEventRepositorySpy.endEventById(firstUptimeRecord.id, secondEvent.dispatchedAt) - uptimeEventRepositorySpy.insertFromMonitorEvent(secondEvent) + uptimeEventRepositorySpy.endEventById(firstUptimeRecord.id, secondEvent.dispatchedAt, any()) + uptimeEventRepositorySpy.insertFromMonitorEvent(secondEvent, any()) } uptimeRecords[0].status shouldBe UptimeStatus.DOWN @@ -188,7 +196,7 @@ class DatabaseEventHandlerTest( latency = 1000 ) eventDispatcher.dispatch(firstEvent) - val firstUptimeRecord = uptimeEventRepository.fetchOne(UPTIME_EVENT.MONITOR_ID, monitor.id) + val firstUptimeRecord = uptimeEventRepository.fetchOne(UPTIME_EVENT.MONITOR_ID, monitor.id)!! val secondEvent = MonitorDownEvent( monitor = monitor, @@ -200,13 +208,13 @@ class DatabaseEventHandlerTest( then("it should create a new UptimeEvent record and end the previous one") { val uptimeRecords = uptimeEventRepository.fetchByMonitorId(monitor.id).sortedBy { it.startedAt } - val latencyRecord = latencyLogRepository.fetchOne(LATENCY_LOG.MONITOR_ID, monitor.id) + val latencyRecord = latencyLogRepository.fetchOne(LATENCY_LOG.MONITOR_ID, monitor.id)!! verifyOrder { latencyLogRepositorySpy.insertLatencyForMonitor(monitor.id, firstEvent.latency) - uptimeEventRepositorySpy.insertFromMonitorEvent(firstEvent) - uptimeEventRepositorySpy.endEventById(firstUptimeRecord.id, secondEvent.dispatchedAt) - uptimeEventRepositorySpy.insertFromMonitorEvent(secondEvent) + uptimeEventRepositorySpy.insertFromMonitorEvent(firstEvent, any()) + uptimeEventRepositorySpy.endEventById(firstUptimeRecord.id, secondEvent.dispatchedAt, any()) + uptimeEventRepositorySpy.insertFromMonitorEvent(secondEvent, any()) } uptimeRecords[0].status shouldBe UptimeStatus.UP @@ -231,7 +239,7 @@ class DatabaseEventHandlerTest( eventDispatcher.dispatch(event) then("it should insert a new SSLEvent record with status VALID") { - val expectedSSLRecord = sslEventRepository.fetchOne(SSL_EVENT.MONITOR_ID, event.monitor.id) + val expectedSSLRecord = sslEventRepository.fetchOne(SSL_EVENT.MONITOR_ID, event.monitor.id)!! verify(exactly = 1) { sslEventRepositorySpy.insertFromMonitorEvent(event) } verify(exactly = 0) { sslEventRepositorySpy.endEventById(any(), any()) } @@ -255,10 +263,10 @@ class DatabaseEventHandlerTest( then("it should insert a new SSLEvent record with status INVALID") { val expectedSSLRecord = sslEventRepository.fetchOne(SSL_EVENT.MONITOR_ID, event.monitor.id) - verify(exactly = 1) { sslEventRepositorySpy.insertFromMonitorEvent(event) } - verify(exactly = 0) { sslEventRepositorySpy.endEventById(any(), any()) } + verify(exactly = 1) { sslEventRepositorySpy.insertFromMonitorEvent(event, any()) } + verify(exactly = 0) { sslEventRepositorySpy.endEventById(any(), any(), any()) } - expectedSSLRecord.status shouldBe SslStatus.INVALID + expectedSSLRecord!!.status shouldBe SslStatus.INVALID expectedSSLRecord.startedAt shouldBe event.dispatchedAt expectedSSLRecord.endedAt shouldBe null expectedSSLRecord.updatedAt shouldBe event.dispatchedAt @@ -289,7 +297,7 @@ class DatabaseEventHandlerTest( verify(exactly = 1) { sslEventRepositorySpy.insertFromMonitorEvent(firstEvent) } verify(exactly = 0) { sslEventRepositorySpy.endEventById(any(), any()) } - expectedSSLRecord.status shouldBe SslStatus.VALID + expectedSSLRecord!!.status shouldBe SslStatus.VALID expectedSSLRecord.endedAt shouldBe null expectedSSLRecord.updatedAt shouldBe secondEvent.dispatchedAt } @@ -303,7 +311,7 @@ class DatabaseEventHandlerTest( error = SSLValidationError("ssl error") ) eventDispatcher.dispatch(firstEvent) - val firstSSLRecord = sslEventRepository.fetchOne(SSL_EVENT.MONITOR_ID, monitor.id) + val firstSSLRecord = sslEventRepository.fetchOne(SSL_EVENT.MONITOR_ID, monitor.id)!! val secondEvent = SSLValidEvent( monitor = monitor, @@ -316,9 +324,9 @@ class DatabaseEventHandlerTest( val sslRecords = sslEventRepository.fetchByMonitorId(monitor.id).sortedBy { it.startedAt } verifyOrder { - sslEventRepositorySpy.insertFromMonitorEvent(firstEvent) - sslEventRepositorySpy.endEventById(firstSSLRecord.id, secondEvent.dispatchedAt) - sslEventRepositorySpy.insertFromMonitorEvent(secondEvent) + sslEventRepositorySpy.insertFromMonitorEvent(firstEvent, any()) + sslEventRepositorySpy.endEventById(firstSSLRecord.id, secondEvent.dispatchedAt, any()) + sslEventRepositorySpy.insertFromMonitorEvent(secondEvent, any()) } sslRecords[0].status shouldBe SslStatus.INVALID @@ -338,7 +346,7 @@ class DatabaseEventHandlerTest( previousEvent = null ) eventDispatcher.dispatch(firstEvent) - val firstSSLRecord = sslEventRepository.fetchOne(SSL_EVENT.MONITOR_ID, monitor.id) + val firstSSLRecord = sslEventRepository.fetchOne(SSL_EVENT.MONITOR_ID, monitor.id)!! val secondEvent = SSLInvalidEvent( monitor = monitor, @@ -351,9 +359,9 @@ class DatabaseEventHandlerTest( val sslRecords = sslEventRepository.fetchByMonitorId(monitor.id).sortedBy { it.startedAt } verifyOrder { - sslEventRepositorySpy.insertFromMonitorEvent(firstEvent) - sslEventRepositorySpy.endEventById(firstSSLRecord.id, secondEvent.dispatchedAt) - sslEventRepositorySpy.insertFromMonitorEvent(secondEvent) + sslEventRepositorySpy.insertFromMonitorEvent(firstEvent, any()) + sslEventRepositorySpy.endEventById(firstSSLRecord.id, secondEvent.dispatchedAt, any()) + sslEventRepositorySpy.insertFromMonitorEvent(secondEvent, any()) } sslRecords[0].status shouldBe SslStatus.VALID @@ -375,7 +383,7 @@ class DatabaseEventHandlerTest( eventDispatcher.dispatch(event) then("it should insert a new SSLEvent record with status WILL_EXPIRE") { - val expectedSSLRecord = sslEventRepository.fetchOne(SSL_EVENT.MONITOR_ID, event.monitor.id) + val expectedSSLRecord = sslEventRepository.fetchOne(SSL_EVENT.MONITOR_ID, event.monitor.id)!! verify(exactly = 1) { sslEventRepositorySpy.insertFromMonitorEvent(event) } verify(exactly = 0) { sslEventRepositorySpy.endEventById(any(), any()) } @@ -405,7 +413,7 @@ class DatabaseEventHandlerTest( eventDispatcher.dispatch(secondEvent) then("it should not insert a new SSLEvent record") { - val expectedSSLRecord = sslEventRepository.fetchOne(SSL_EVENT.MONITOR_ID, monitor.id) + val expectedSSLRecord = sslEventRepository.fetchOne(SSL_EVENT.MONITOR_ID, monitor.id)!! verify(exactly = 1) { sslEventRepositorySpy.insertFromMonitorEvent(firstEvent) } verify(exactly = 0) { sslEventRepositorySpy.endEventById(any(), any()) } @@ -424,7 +432,7 @@ class DatabaseEventHandlerTest( previousEvent = null ) eventDispatcher.dispatch(firstEvent) - val firstSSLRecord = sslEventRepository.fetchOne(SSL_EVENT.MONITOR_ID, monitor.id) + val firstSSLRecord = sslEventRepository.fetchOne(SSL_EVENT.MONITOR_ID, monitor.id)!! val secondEvent = SSLWillExpireEvent( monitor = monitor, @@ -437,9 +445,9 @@ class DatabaseEventHandlerTest( val sslRecords = sslEventRepository.fetchByMonitorId(monitor.id).sortedBy { it.startedAt } verifyOrder { - sslEventRepositorySpy.insertFromMonitorEvent(firstEvent) - sslEventRepositorySpy.endEventById(firstSSLRecord.id, secondEvent.dispatchedAt) - sslEventRepositorySpy.insertFromMonitorEvent(secondEvent) + sslEventRepositorySpy.insertFromMonitorEvent(firstEvent, any()) + sslEventRepositorySpy.endEventById(firstSSLRecord.id, secondEvent.dispatchedAt, any()) + sslEventRepositorySpy.insertFromMonitorEvent(secondEvent, any()) } sslRecords[0].status shouldBe SslStatus.VALID diff --git a/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/PagerdutyEventHandlerTest.kt b/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/PagerdutyEventHandlerTest.kt index 66a2d89..c48c7a5 100644 --- a/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/PagerdutyEventHandlerTest.kt +++ b/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/PagerdutyEventHandlerTest.kt @@ -35,20 +35,28 @@ import io.mockk.mockk import io.mockk.slot import io.mockk.verify import io.reactivex.Single +import org.jooq.Configuration @MicronautTest(startApplication = false) class PagerdutyEventHandlerTest( private val monitorRepository: MonitorRepository, private val uptimeEventRepository: UptimeEventRepository, sslEventRepository: SSLEventRepository, - latencyLogRepository: LatencyLogRepository + latencyLogRepository: LatencyLogRepository, + jooqConfig: Configuration ) : DatabaseBehaviorSpec() { private val mockClient = mockk() init { val eventDispatcher = EventDispatcher() - DatabaseEventHandler(eventDispatcher, uptimeEventRepository, latencyLogRepository, sslEventRepository) + DatabaseEventHandler( + eventDispatcher, + uptimeEventRepository, + latencyLogRepository, + sslEventRepository, + jooqConfig + ) PagerdutyEventHandler(eventDispatcher, mockClient) given("the PagerdutyEventHandler - UPTIME events") { diff --git a/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/SMTPEventHandlerTest.kt b/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/SMTPEventHandlerTest.kt index 86242a9..91a92b0 100644 --- a/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/SMTPEventHandlerTest.kt +++ b/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/SMTPEventHandlerTest.kt @@ -30,9 +30,11 @@ import io.mockk.clearAllMocks import io.mockk.slot import io.mockk.spyk import io.mockk.verify +import org.jooq.Configuration import org.simplejavamail.api.email.Email import java.time.OffsetDateTime +@Suppress("LongParameterList") @MicronautTest(startApplication = false) class SMTPEventHandlerTest( private val monitorRepository: MonitorRepository, @@ -40,14 +42,21 @@ class SMTPEventHandlerTest( private val sslEventRepository: SSLEventRepository, latencyLogRepository: LatencyLogRepository, smtpEventHandlerConfig: SMTPEventHandlerConfig, - smtpMailer: SMTPMailer + smtpMailer: SMTPMailer, + jooqConfig: Configuration ) : DatabaseBehaviorSpec() { init { val eventDispatcher = EventDispatcher() val emailFactory = EmailFactory(smtpEventHandlerConfig) val mailerSpy = spyk(smtpMailer, recordPrivateCalls = true) - DatabaseEventHandler(eventDispatcher, uptimeEventRepository, latencyLogRepository, sslEventRepository) + DatabaseEventHandler( + eventDispatcher, + uptimeEventRepository, + latencyLogRepository, + sslEventRepository, + jooqConfig + ) SMTPEventHandler(smtpEventHandlerConfig, mailerSpy, eventDispatcher) given("the SMTPEventHandler - UPTIME events") { diff --git a/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/SlackEventHandlerTest.kt b/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/SlackEventHandlerTest.kt index 2922528..1b9abe9 100644 --- a/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/SlackEventHandlerTest.kt +++ b/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/SlackEventHandlerTest.kt @@ -34,6 +34,7 @@ import io.mockk.slot import io.mockk.spyk import io.mockk.verify import io.reactivex.Single +import org.jooq.Configuration import java.time.OffsetDateTime @MicronautTest(startApplication = false) @@ -41,7 +42,8 @@ class SlackEventHandlerTest( private val monitorRepository: MonitorRepository, private val uptimeEventRepository: UptimeEventRepository, private val sslEventRepository: SSLEventRepository, - latencyLogRepository: LatencyLogRepository + latencyLogRepository: LatencyLogRepository, + jooqConfig: Configuration ) : DatabaseBehaviorSpec() { private val mockClient = mockk() @@ -50,7 +52,13 @@ class SlackEventHandlerTest( val slackWebhookService = SlackWebhookService(mockClient) val webhookServiceSpy = spyk(slackWebhookService, recordPrivateCalls = true) - DatabaseEventHandler(eventDispatcher, uptimeEventRepository, latencyLogRepository, sslEventRepository) + DatabaseEventHandler( + eventDispatcher, + uptimeEventRepository, + latencyLogRepository, + sslEventRepository, + jooqConfig + ) SlackEventHandler(webhookServiceSpy, eventDispatcher) given("the SlackEventHandler - UPTIME events") { diff --git a/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/TelegramEventHandlerTest.kt b/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/TelegramEventHandlerTest.kt index 306bc97..e34c5f3 100644 --- a/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/TelegramEventHandlerTest.kt +++ b/src/test/kotlin/com/kuvaszuptime/kuvasz/handlers/TelegramEventHandlerTest.kt @@ -35,6 +35,7 @@ import io.mockk.slot import io.mockk.spyk import io.mockk.verify import io.reactivex.Single +import org.jooq.Configuration import java.time.OffsetDateTime @MicronautTest(startApplication = false) @@ -42,7 +43,8 @@ class TelegramEventHandlerTest( private val monitorRepository: MonitorRepository, private val uptimeEventRepository: UptimeEventRepository, private val sslEventRepository: SSLEventRepository, - latencyLogRepository: LatencyLogRepository + latencyLogRepository: LatencyLogRepository, + jooqConfig: Configuration ) : DatabaseBehaviorSpec() { private val mockClient = mockk() @@ -55,7 +57,13 @@ class TelegramEventHandlerTest( val telegramAPIService = TelegramAPIService(eventHandlerConfig, mockClient) val apiServiceSpy = spyk(telegramAPIService, recordPrivateCalls = true) - DatabaseEventHandler(eventDispatcher, uptimeEventRepository, latencyLogRepository, sslEventRepository) + DatabaseEventHandler( + eventDispatcher, + uptimeEventRepository, + latencyLogRepository, + sslEventRepository, + jooqConfig + ) TelegramEventHandler(apiServiceSpy, eventDispatcher) given("the TelegramEventHandler") { diff --git a/src/test/kotlin/com/kuvaszuptime/kuvasz/testutils/TestDbContainer.kt b/src/test/kotlin/com/kuvaszuptime/kuvasz/testutils/TestDbContainer.kt index ef2b560..f127587 100644 --- a/src/test/kotlin/com/kuvaszuptime/kuvasz/testutils/TestDbContainer.kt +++ b/src/test/kotlin/com/kuvaszuptime/kuvasz/testutils/TestDbContainer.kt @@ -2,7 +2,7 @@ package com.kuvaszuptime.kuvasz.testutils import org.testcontainers.containers.PostgreSQLContainer -class TestDbContainer : PostgreSQLContainer() { +class TestDbContainer : PostgreSQLContainer("postgres:12") { companion object { lateinit var instance: TestDbContainer @@ -10,7 +10,6 @@ class TestDbContainer : PostgreSQLContainer() { if (!Companion::instance.isInitialized) { instance = TestDbContainer() - instance.dockerImageName = "postgres:12" instance.start() System.setProperty("datasources.default.url", instance.jdbcUrl)