Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IS-2985: Update publishedAt for Foresporsel #17

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package no.nav.syfo.application

import no.nav.syfo.domain.Foresporsel
import no.nav.syfo.domain.Personident
import java.util.UUID

interface IForesporselRepository {
fun createForesporsel(foresporsel: Foresporsel): Foresporsel

fun getForesporsler(personident: Personident): List<Foresporsel>

fun setPublishedAt(foresporselUuid: UUID)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import no.nav.syfo.domain.Veilederident
import no.nav.syfo.domain.Virksomhetsnummer
import no.nav.syfo.infrastructure.database.DatabaseInterface
import no.nav.syfo.infrastructure.database.toList
import no.nav.syfo.util.nowUTC
import java.sql.ResultSet
import java.sql.SQLException
import java.time.OffsetDateTime
import java.util.*

Expand Down Expand Up @@ -39,6 +41,20 @@ class ForesporselRepository(val database: DatabaseInterface) : IForesporselRepos
}
}

override fun setPublishedAt(foresporselUuid: UUID) {
database.connection.use { connection ->
connection.prepareStatement(SET_PUBLISHED_AT).use {
it.setObject(1, nowUTC())
it.setString(2, foresporselUuid.toString())
val updated = it.executeUpdate()
if (updated != 1) {
throw SQLException("Expected a single row to be updated, got update count $updated")
}
}
connection.commit()
}
}

companion object {
private const val CREATE_FORESPORSEL =
"""
Expand All @@ -60,6 +76,13 @@ class ForesporselRepository(val database: DatabaseInterface) : IForesporselRepos
FROM foresporsel
WHERE arbeidstaker_personident = ?
"""

private const val SET_PUBLISHED_AT =
"""
UPDATE foresporsel
SET published_at = ?
WHERE uuid = ?
"""
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/test/kotlin/no/nav/syfo/ExternalMockEnvironment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import no.nav.syfo.application.IVarselProducer
import no.nav.syfo.infrastructure.clients.azuread.AzureAdClient
import no.nav.syfo.infrastructure.clients.wellknown.WellKnown
import no.nav.syfo.infrastructure.database.TestDatabase
import no.nav.syfo.infrastructure.database.repository.ForesporselRepository
import no.nav.syfo.infrastructure.mock.mockHttpClient
import java.nio.file.Paths

Expand All @@ -29,6 +30,7 @@ class ExternalMockEnvironment private constructor() {
httpClient = mockHttpClient,
)
val varselProducer = mockk<IVarselProducer>(relaxed = true)
val foresporselRepository = ForesporselRepository(database)

companion object {
val instance: ExternalMockEnvironment = ExternalMockEnvironment()
Expand Down
3 changes: 3 additions & 0 deletions src/test/kotlin/no/nav/syfo/TestUtils.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package no.nav.syfo

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotEquals

infix fun <T> T.shouldBeEqualTo(other: T) = assertEquals(this, other)

infix fun <T> T.shouldNotBeEqualTo(other: T) = assertNotEquals(this, other)
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package no.nav.syfo.infrastructure.database

import io.zonky.test.db.postgres.embedded.EmbeddedPostgres
import no.nav.syfo.infrastructure.database.repository.PForesporsel
import no.nav.syfo.infrastructure.database.repository.toPForesporsel
import org.flywaydb.core.Flyway
import java.sql.Connection
import java.util.*

class TestDatabase : DatabaseInterface {
private val pg: EmbeddedPostgres =
Expand Down Expand Up @@ -42,6 +45,21 @@ fun TestDatabase.dropData() {
}
}

fun TestDatabase.getForesporsel(uuid: UUID): PForesporsel {
val query =
"""
SELECT *
FROM foresporsel
WHERE uuid = ?
"""
return this.connection.use { connection ->
connection.prepareStatement(query).use {
it.setString(1, uuid.toString())
it.executeQuery().toList { toPForesporsel() }.single()
}
}
}

class TestDatabaseNotResponding : DatabaseInterface {
override val connection: Connection
get() = throw Exception("Not working")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import no.nav.syfo.ExternalMockEnvironment
import no.nav.syfo.UserConstants
import no.nav.syfo.generator.generateForsporsel
import no.nav.syfo.infrastructure.database.dropData
import no.nav.syfo.infrastructure.database.getForesporsel
import no.nav.syfo.shouldBeEqualTo
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import no.nav.syfo.shouldNotBeEqualTo
import org.junit.jupiter.api.*
import java.sql.SQLException
import java.time.temporal.ChronoUnit
import java.util.*

class ForesporselRepositoryTest {
private val externalMockEnvironment = ExternalMockEnvironment.instance
private val database = externalMockEnvironment.database
private val foresporselRepository = ForesporselRepository(database)
private val foresporselRepository = externalMockEnvironment.foresporselRepository

private val foresporsel = generateForsporsel()

@BeforeEach
fun setup() {
Expand All @@ -26,8 +29,6 @@ class ForesporselRepositoryTest {
inner class CreateForesporselTests {
@Test
fun `creates a new Foresporsel`() {
val foresporsel = generateForsporsel()

val createdForesporsel = foresporselRepository.createForesporsel(foresporsel)

createdForesporsel.uuid shouldBeEqualTo foresporsel.uuid
Expand All @@ -44,7 +45,6 @@ class ForesporselRepositoryTest {
inner class GetForesporslerTests {
@Test
fun `gets Foresporsler`() {
val foresporsel = generateForsporsel()
val createdForesporsel = foresporselRepository.createForesporsel(foresporsel)

val fetchedForesporsel = foresporselRepository.getForesporsler(foresporsel.arbeidstakerPersonident)
Expand All @@ -54,7 +54,6 @@ class ForesporselRepositoryTest {

@Test
fun `gets Foresporsler only for given personident`() {
val foresporsel = generateForsporsel()
val otherForesporsel = generateForsporsel(UserConstants.ARBEIDSTAKER_PERSONIDENT_2)
foresporselRepository.createForesporsel(foresporsel)
foresporselRepository.createForesporsel(otherForesporsel)
Expand All @@ -68,7 +67,6 @@ class ForesporselRepositoryTest {

@Test
fun `gets several Foresporsler for given personident`() {
val foresporsel = generateForsporsel()
val otherForesporsel = generateForsporsel()
foresporselRepository.createForesporsel(foresporsel)
foresporselRepository.createForesporsel(otherForesporsel)
Expand All @@ -88,4 +86,26 @@ class ForesporselRepositoryTest {
fetchedForesporsel.size shouldBeEqualTo 0
}
}

@Nested
@DisplayName("Update publishedAt")
inner class PublishedAtTests {
@Test
fun `updates publishedAt for Foresporsel`() {
val createdForesporsel = foresporselRepository.createForesporsel(foresporsel)

foresporselRepository.setPublishedAt(createdForesporsel.uuid)

val pForesporsel = database.getForesporsel(createdForesporsel.uuid)

pForesporsel.publishedAt shouldNotBeEqualTo null
}

@Test
fun `failes when no Foresporsel for uuid`() {
assertThrows<SQLException> {
foresporselRepository.setPublishedAt(UUID.randomUUID())
}
}
}
}
Loading