Skip to content

Commit

Permalink
F!! Legger til lagring av rinasaker for gjenny i bucket
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel.skarpas@nav.no
  • Loading branch information
MariamPervez committed Dec 4, 2023
1 parent c545cbe commit ebdaaa6
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 10 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ dependencies {
implementation ("no.nav.common:token-client:3.2023.09.13_04.55-a8ff452fbd94")
implementation("org.springframework.kafka:spring-kafka:${springKafkaVersion}")

//GCP
implementation("com.google.cloud:google-cloud-storage:2.29.1")

implementation("no.nav.eessi.pensjon:ep-metrics:2.0.44")
implementation("no.nav.eessi.pensjon:ep-logging:2.1.38")
implementation("no.nav.eessi.pensjon:ep-eux:2.1.82")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ class GjennyController (
}

@PostMapping("/sed/add")
fun leggTilInstitusjon(@RequestBody request: ApiRequest): DocumentsItem? = prefillController.addInstutionAndDocument(request.copy(gjenny = true))
fun leggTilInstitusjon(@RequestBody request: ApiRequest): DocumentsItem? {
return prefillController.addInstutionAndDocument(request.copy(gjenny = true))
}

@PostMapping("/sed/replysed/{parentid}")
fun prefillSed(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import no.nav.eessi.pensjon.fagmodul.eux.BucUtils
import no.nav.eessi.pensjon.fagmodul.eux.EuxInnhentingService
import no.nav.eessi.pensjon.fagmodul.eux.EuxPrefillService
import no.nav.eessi.pensjon.fagmodul.prefill.InnhentingService
import no.nav.eessi.pensjon.gcp.GcpStorageService
import no.nav.eessi.pensjon.logging.AuditLogger
import no.nav.eessi.pensjon.metrics.MetricsHelper
import no.nav.eessi.pensjon.shared.api.ApiRequest
Expand All @@ -35,6 +36,7 @@ class PrefillController(
private val euxPrefillService: EuxPrefillService,
private val euxInnhentingService: EuxInnhentingService,
private val innhentingService: InnhentingService,
private val gcpStorageService: GcpStorageService,
private val auditlogger: AuditLogger,
@Autowired(required = false) private val metricsHelper: MetricsHelper = MetricsHelper.ForTest()
) {
Expand Down Expand Up @@ -85,14 +87,15 @@ class PrefillController(
""".trimIndent())

if (x005docs.isEmpty()) {
if (request.gjenny){
request.euxCaseId?.let { gcpStorageService.lagre(it) }
}
euxPrefillService.checkAndAddInstitution(dataModel, bucUtil, emptyList(), nyeInstitusjoner)
} else if (x005docs.firstOrNull { it.status == "empty"} != null ) {
//hvis finnes som draft.. kaste bad request til sb..

val x005Liste = nyeInstitusjoner.map {
logger.debug("Prefiller X005, legger til Institusjon på X005 ${it.institution}")
val x005Liste = nyeInstitusjoner.map { nyeInstitusjonerMap ->
logger.debug("Prefiller X005, legger til Institusjon på X005 ${nyeInstitusjonerMap.institution}")
// ID og Navn på X005 er påkrevd må hente innn navn fra UI.
val x005request = request.copy(avdodfnr = null, sed = SedType.X005, institutions = listOf(it))
val x005request = request.copy(avdodfnr = null, sed = SedType.X005, institutions = listOf(nyeInstitusjonerMap))
mapJsonToAny<X005>(innhentingService.hentPreutyltSed(x005request))
}
euxPrefillService.checkAndAddInstitution(dataModel, bucUtil, x005Liste, nyeInstitusjoner)
Expand Down Expand Up @@ -196,6 +199,9 @@ class PrefillController(
request.vedtakId,
dataModel.sedType
)
if (request.gjenny) {
gcpStorageService.lagre(request.euxCaseId!!)
}

val parent = bucUtil.findDocument(parentId)
val result = bucUtil.findDocument(docresult.documentId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ class EuxPrefillService (private val euxKlient: EuxKlientLib,
return euxCaseId
}

//sjekk for ekisternede deltakere og nye fra UI. samt legge til deltakere på vanlig måte dersom buc er ny
//legger til evt. x005 dersom det finens.
/**
* Ved opprettelse av ny Buc, kan det legges til deltakere uten at man trenger å opprette en X005.
* Dersom Bucen allerede eksisterer må det opprettes en X005 for å legge til ny institusjon.
*/
//ny/nye deltakere så må det opprettes en X005 for den nye institusjonen.
fun checkAndAddInstitution(dataModel: PrefillDataModel, bucUtil: BucUtils, x005Liste: List<X005>, nyeInstitusjoner: List<InstitusjonItem>) {
val navCaseOwner = bucUtil.getCaseOwner()?.country == "NO"
logger.debug(
Expand Down
17 changes: 17 additions & 0 deletions src/main/kotlin/no/nav/eessi/pensjon/gcp/GcpConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package no.nav.eessi.pensjon.gcp

import com.google.cloud.storage.Storage
import com.google.cloud.storage.StorageOptions
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile

@Profile("test", "prod")
@Configuration
class GcpConfig {

@Bean
fun gcpStorage() : Storage{
return StorageOptions.getDefaultInstance().service
}
}
55 changes: 55 additions & 0 deletions src/main/kotlin/no/nav/eessi/pensjon/gcp/GcpStorageService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package no.nav.eessi.pensjon.gcp

import com.google.cloud.storage.BlobId
import com.google.cloud.storage.BlobInfo
import com.google.cloud.storage.Storage
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import java.nio.ByteBuffer

@Component
class GcpStorageService(
@param:Value("\${GCP_BUCKET_NAME}") var bucketname: String,
private val gcpStorage: Storage) {
private val logger = LoggerFactory.getLogger(GcpStorageService::class.java)

init {
ensureBucketExists()
}

private fun ensureBucketExists() {
when (gcpStorage.get(bucketname) != null) {
false -> throw IllegalStateException("Fant ikke bucket med navn $bucketname. Må provisjoneres")
true -> logger.info("Bucket $bucketname funnet.")
}
}

fun lagre(euxCaseId: String) {
if (eksisterer(euxCaseId)) return
val blobInfo = BlobInfo.newBuilder(BlobId.of(bucketname, euxCaseId)).setContentType("application/json").build()
kotlin.runCatching {
gcpStorage.writer(blobInfo).use {
it.write(ByteBuffer.wrap(euxCaseId.toByteArray()))
}
}.onFailure { e ->
logger.error("Feilet med å lagre dokument med id: ${blobInfo.blobId.name}", e)
}.onSuccess {
logger.info("Lagret fil med blobid: ${blobInfo.blobId.name} og bytes: $it")
}
}

fun eksisterer(storageKey: String): Boolean{
logger.debug("sjekker om $storageKey finnes i bucket: $bucketname")
val obj = gcpStorage.get(BlobId.of(bucketname, storageKey))

kotlin.runCatching {
obj.exists()
}.onFailure {
}.onSuccess {
return true
}
return false
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class ArchitectureTest {
.haveNameMatching("set[A-Z]+.*")
.and().doNotHaveRawParameterTypes(MetricsHelper.Metric::class.java)
.and().areDeclaredInClassesThat().areNotAnnotatedWith(Scope::class.java) // If scope is not singleton it might be ok
.and().areDeclaredInClassesThat().haveNameNotMatching(".*(STSService|Template|Config)") // these use setter injection
.and().areDeclaredInClassesThat().haveNameNotMatching(".*(STSService|Template|Config|GcpStorageService)") // these use setter injection
.should().beDeclaredInClassesThat().areAnnotatedWith(springStereotype)
.because("Spring-components (usually singletons) must not have mutable instance fields " +
"as they can easily be misused and create 'race conditions'")
Expand All @@ -275,7 +275,7 @@ class ArchitectureTest {
.areNotFinal()
.and().doNotHaveRawType(MetricsHelper.Metric::class.java)
.and().areDeclaredInClassesThat().areNotAnnotatedWith(Scope::class.java)// If scope is not singleton it might be ok
.and().areDeclaredInClassesThat().haveNameNotMatching(".*(STSService|Template|Config)") // these use setter injection
.and().areDeclaredInClassesThat().haveNameNotMatching(".*(STSService|Template|Config|GcpStorageService)") // these use setter injection
.should().beDeclaredInClassesThat().areAnnotatedWith(springStereotype)
.because("Spring-components (usually singletons) must not have mutable instance fields " +
"as they can easily be misused and create 'race conditions'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ internal class PrefillControllerTest {
mockEuxPrefillService,
mockEuxInnhentingService,
innhentingService,
mockk(relaxed = true),
auditLogger
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.ninjasquad.springmockk.MockkBeans
import no.nav.eessi.pensjon.UnsecuredWebMvcTestLauncher
import no.nav.eessi.pensjon.eux.klient.EuxKlientAsSystemUser
import no.nav.eessi.pensjon.fagmodul.config.RestTemplateConfig
import no.nav.eessi.pensjon.gcp.GcpStorageService
import no.nav.eessi.pensjon.pensjonsinformasjon.clients.PensjonsinformasjonClient
import no.nav.eessi.pensjon.personoppslag.pdl.PersonService
import no.nav.security.token.support.spring.test.EnableMockOAuth2Server
Expand Down Expand Up @@ -36,6 +37,7 @@ import org.springframework.web.client.RestTemplate
MockkBean(name = "safGraphQlOidcRestTemplate", classes = [RestTemplate::class]),
MockkBean(name = "pensjonsinformasjonClient", classes = [PensjonsinformasjonClient::class]),
MockkBean(name = "personService", classes = [PersonService::class]),
MockkBean(name = "gcpStorageService", classes = [GcpStorageService::class]),
MockkBean(name = "euxKlient", classes = [EuxKlientAsSystemUser::class])
)
class EessiTokenValdationTest {
Expand Down

0 comments on commit ebdaaa6

Please sign in to comment.