Skip to content

Commit

Permalink
added kotest, improved tests, small code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
JayJabowski committed Jun 21, 2024
1 parent 9668927 commit 3298ac1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 65 deletions.
8 changes: 8 additions & 0 deletions src/fibonacci-worker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<kotlin.version>1.9.24</kotlin.version>
<camunda.version>8.5.3</camunda.version>
<mockk.version>1.12.0</mockk.version>
<kotest.version>5.9.1</kotest.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -61,6 +62,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.kotest</groupId>
<artifactId>kotest-assertions-core-jvm</artifactId>
<version>${kotest.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import io.micrometer.core.instrument.MeterRegistry
import org.springframework.stereotype.Service
import java.util.concurrent.atomic.AtomicInteger

private const val BASE_NAME = "fibonacciworker_jobs"
private const val JOB_KEY_TAG = "jobKey"
internal const val BASE_NAME = "fibonacciworker_jobs"
internal const val JOB_KEY_TAG = "jobKey"

@Service
class JobExporterService(private val registry: MeterRegistry) {

private val jobsInExecution = AtomicInteger(0)

fun getJobsInExecution() = jobsInExecution.get()

init {
Gauge.builder("${BASE_NAME}_in_execution") {
jobsInExecution.toDouble()
Expand All @@ -29,6 +27,7 @@ class JobExporterService(private val registry: MeterRegistry) {
.tag(JOB_KEY_TAG, jobId)
.register(registry)
.increment()

jobsInExecution.incrementAndGet()
}

Expand All @@ -38,6 +37,7 @@ class JobExporterService(private val registry: MeterRegistry) {
.tag(JOB_KEY_TAG, jobId)
.register(registry)
.increment()

jobsInExecution.decrementAndGet()
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package de.envite.greenbpm.camunda_process_carbon_pricing.fibonacci_worker

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import io.kotest.matchers.shouldBe
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource

class FibonacciKtTest{
@ParameterizedTest(name= "For {0}, the Fibonacci is {1}")
class FibonacciKtTest {
@ParameterizedTest(name = "For {0}, the Fibonacci is {1}")
@MethodSource("getData")
fun `should return correct fibonacci values`(input: Int, expected: Int){
assertEquals(fibonacciRec(input), expected)
}
fun `should return correct fibonacci values`(input: Int, expected: Int) = fibonacciRec(input) shouldBe expected

companion object{
@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
@@ -1,75 +1,37 @@
package de.envite.greenbpm.camunda_process_carbon_pricing.fibonacci_worker

import io.micrometer.core.instrument.Counter
import io.micrometer.core.instrument.MeterRegistry
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import io.kotest.assertions.assertSoftly
import io.kotest.matchers.shouldBe
import io.micrometer.core.instrument.simple.SimpleMeterRegistry
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
class JobExporterServiceTest {
private lateinit var meterRegistry: MeterRegistry
private lateinit var classUnderTest: JobExporterService
private lateinit var jobsStartedCounter: Counter
private lateinit var jobsFinishedCounter: Counter

@BeforeEach
fun setup(){
meterRegistry = mockk(relaxed = true)
jobsStartedCounter = mockk(relaxed = true)
jobsFinishedCounter = mockk(relaxed = true)

every { Counter.builder("fibonacciworker_jobs_started")
.tag("jobKey","testId")
.register(meterRegistry)
} returns jobsStartedCounter

every { Counter.builder("fibonacciworker_jobs_finished")
.tag("jobKey","testId")
.register(meterRegistry)
} returns jobsFinishedCounter
class JobExporterServiceTest {

classUnderTest = JobExporterService(meterRegistry)
}
private val meterRegistry: SimpleMeterRegistry = SimpleMeterRegistry()
private val classUnderTest: JobExporterService = JobExporterService(meterRegistry)

@Test
fun `reportJobStarted should increase jobsStartedCounter`() {
fun `reportJobStarted should increase jobsStartedCounter and update gauge`() {
val id = "testId"

classUnderTest.reportJobStarted(id)

verify { jobsStartedCounter.increment() }

assertEquals(1, classUnderTest.getJobsInExecution())
assertSoftly {
meterRegistry.get("${BASE_NAME}_started").counter().count() shouldBe 1.0
meterRegistry.get("${BASE_NAME}_in_execution").gauge().value() shouldBe 1.0
}
}

@Test
fun `reportJobFinished should increase jobsFinishedCounter`() {
fun `reportJobStarted should decrease jobsStartedCounter and update gauge`() {
val id = "testId"

classUnderTest.reportJobFinished(id)

verify { jobsFinishedCounter.increment() }

assertEquals(-1, classUnderTest.getJobsInExecution())

}

@Test
fun `increase and decrease jobsInExecution`() {
val id = "testId"

classUnderTest.reportJobStarted(id)

assertEquals(1, classUnderTest.getJobsInExecution())

classUnderTest.reportJobFinished(id)

assertEquals(0, classUnderTest.getJobsInExecution())

assertSoftly {
meterRegistry.get("${BASE_NAME}_finished").counter().count() shouldBe 1.0
meterRegistry.get("${BASE_NAME}_in_execution").gauge().value() shouldBe -1.0
}
}
}

0 comments on commit 3298ac1

Please sign in to comment.