-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lagt inn caching i DB for valutakurser fra ECB (#4058)
Co-authored-by: marius-nav <[email protected]>
- Loading branch information
1 parent
f57e02d
commit 1dfeeea
Showing
7 changed files
with
220 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/no/nav/familie/ba/sak/integrasjoner/ecb/domene/ECBValutakursCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package no.nav.familie.ba.sak.integrasjoner.ecb.domene | ||
|
||
import jakarta.persistence.Column | ||
import jakarta.persistence.Entity | ||
import jakarta.persistence.EntityListeners | ||
import jakarta.persistence.GeneratedValue | ||
import jakarta.persistence.GenerationType | ||
import jakarta.persistence.Id | ||
import jakarta.persistence.SequenceGenerator | ||
import jakarta.persistence.Table | ||
import no.nav.familie.ba.sak.common.BaseEntitet | ||
import no.nav.familie.ba.sak.sikkerhet.RollestyringMotDatabase | ||
import java.math.BigDecimal | ||
import java.time.LocalDate | ||
|
||
@EntityListeners(RollestyringMotDatabase::class) | ||
@Entity(name = "EcbValutakursCache") | ||
@Table(name = "ECBVALUTAKURSCACHE") | ||
data class ECBValutakursCache( | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ecbvalutakurscache_seq_generator") | ||
@SequenceGenerator(name = "ecbvalutakurscache_seq_generator", sequenceName = "ecbvalutakurscache_seq", allocationSize = 50) | ||
val id: Long = 0, | ||
|
||
@Column(name = "valutakursdato", columnDefinition = "DATE") | ||
val valutakursdato: LocalDate? = null, | ||
|
||
@Column(name = "valutakode") | ||
val valutakode: String? = null, | ||
|
||
@Column(name = "kurs", nullable = false) | ||
val kurs: BigDecimal, | ||
) : BaseEntitet() |
8 changes: 8 additions & 0 deletions
8
...ain/kotlin/no/nav/familie/ba/sak/integrasjoner/ecb/domene/ECBValutakursCacheRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package no.nav.familie.ba.sak.integrasjoner.ecb.domene | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
import java.time.LocalDate | ||
|
||
interface ECBValutakursCacheRepository : JpaRepository<ECBValutakursCache, Long> { | ||
fun findByValutakodeAndValutakursdato(valutakode: String, valutakursdato: LocalDate): ECBValutakursCache? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/resources/db/migration/V253__ecb_valutakurs_cache.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
CREATE TABLE ecbvalutakurscache | ||
( | ||
ID BIGINT NOT NULL PRIMARY KEY, | ||
VALUTAKURSDATO TIMESTAMP(3) DEFAULT null, | ||
VALUTAKODE VARCHAR DEFAULT null, | ||
KURS DECIMAL DEFAULT null, | ||
VERSJON BIGINT DEFAULT 0 NOT NULL, | ||
OPPRETTET_AV VARCHAR DEFAULT 'VL' NOT NULL, | ||
OPPRETTET_TID TIMESTAMP(3) DEFAULT localtimestamp NOT NULL, | ||
ENDRET_AV VARCHAR, | ||
ENDRET_TID TIMESTAMP(3) | ||
); | ||
|
||
CREATE SEQUENCE ecbvalutakurscache_seq INCREMENT BY 50 START WITH 1 NO CYCLE; | ||
CREATE INDEX valutakode_valutadato_idx ON ecbvalutakurscache (VALUTAKURSDATO, VALUTAKODE); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...t/integrasjonstester/kotlin/no/nav/familie/ba/sak/integrasjoner/ecb/ECBIntegrationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package no.nav.familie.ba.sak.integrasjoner.ecb | ||
|
||
import io.mockk.every | ||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.junit5.MockKExtension | ||
import io.mockk.verify | ||
import no.nav.familie.ba.sak.config.AbstractSpringIntegrationTest | ||
import no.nav.familie.ba.sak.config.DatabaseCleanupService | ||
import no.nav.familie.ba.sak.integrasjoner.ecb.domene.ECBValutakursCacheRepository | ||
import no.nav.familie.valutakurs.Frequency | ||
import no.nav.familie.valutakurs.ValutakursRestClient | ||
import no.nav.familie.valutakurs.domene.ECBExchangeRate | ||
import no.nav.familie.valutakurs.domene.ECBExchangeRateDate | ||
import no.nav.familie.valutakurs.domene.ECBExchangeRateKey | ||
import no.nav.familie.valutakurs.domene.ECBExchangeRateValue | ||
import no.nav.familie.valutakurs.domene.ECBExchangeRatesData | ||
import no.nav.familie.valutakurs.domene.ECBExchangeRatesDataSet | ||
import no.nav.familie.valutakurs.domene.ECBExchangeRatesForCurrency | ||
import no.nav.familie.valutakurs.domene.toExchangeRates | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.TestInstance | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import java.math.BigDecimal | ||
import java.time.LocalDate | ||
|
||
@ExtendWith(MockKExtension::class) | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class ECBIntegrationTest : AbstractSpringIntegrationTest() { | ||
|
||
@MockK | ||
private lateinit var ecbClient: ValutakursRestClient | ||
|
||
@Autowired | ||
private lateinit var ecbService: ECBService | ||
|
||
@Autowired | ||
private lateinit var ecbValutakursCacheRepository: ECBValutakursCacheRepository | ||
|
||
@Autowired | ||
private lateinit var databaseCleanupService: DatabaseCleanupService | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
ecbService = ECBService( | ||
ecbClient = ecbClient, | ||
ecbValutakursCacheRepository = ecbValutakursCacheRepository, | ||
) | ||
databaseCleanupService.truncate() | ||
} | ||
|
||
@Test | ||
fun `Skal teste at valutakurs hentes fra cache dersom valutakursen allerede er hentet fra ECB`() { | ||
val valutakursDato = LocalDate.of(2022, 7, 20) | ||
val ecbExchangeRatesData = createECBResponse( | ||
Frequency.Daily, | ||
listOf(Pair("NOK", BigDecimal.valueOf(9.4567))), | ||
valutakursDato.toString(), | ||
) | ||
every { | ||
ecbClient.hentValutakurs( | ||
any(), | ||
any(), | ||
any(), | ||
) | ||
} returns ecbExchangeRatesData.toExchangeRates() | ||
|
||
ecbService.hentValutakurs("EUR", valutakursDato) | ||
val valutakurs = ecbValutakursCacheRepository.findByValutakodeAndValutakursdato("EUR", valutakursDato) | ||
assertEquals(valutakurs!!.kurs, BigDecimal.valueOf(9.4567)) | ||
ecbService.hentValutakurs("EUR", valutakursDato) | ||
verify(exactly = 1) { | ||
ecbClient.hentValutakurs( | ||
any(), | ||
any(), | ||
any(), | ||
) | ||
} | ||
} | ||
|
||
private fun createECBResponse( | ||
frequency: Frequency, | ||
exchangeRates: List<Pair<String, BigDecimal>>, | ||
exchangeRateDate: String, | ||
): ECBExchangeRatesData { | ||
return ECBExchangeRatesData( | ||
ECBExchangeRatesDataSet( | ||
exchangeRates.map { | ||
ECBExchangeRatesForCurrency( | ||
listOf( | ||
ECBExchangeRateKey("CURRENCY", it.first), | ||
ECBExchangeRateKey("FREQ", frequency.toFrequencyParam()), | ||
), | ||
listOf( | ||
ECBExchangeRate( | ||
ECBExchangeRateDate(exchangeRateDate), | ||
ECBExchangeRateValue((it.second)), | ||
), | ||
), | ||
) | ||
}, | ||
), | ||
) | ||
} | ||
} |