Skip to content

Commit

Permalink
Refactor, and apply check inside data class.
Browse files Browse the repository at this point in the history
  • Loading branch information
MalazAlkoj committed Jun 24, 2024
1 parent 090800c commit d19552e
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 33 deletions.
5 changes: 3 additions & 2 deletions nais/nais-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ spec:
outbound:
external:
- host: "pdl-api.dev-fss-pub.nais.io"
- host: "ereg-services-q1.dev-fss-pub.nais.io"
rules:
- application: digdir-krr-proxy
namespace: team-rocket
Expand Down Expand Up @@ -105,8 +106,8 @@ spec:
value: "dev-fss.pdl.pdl-api"
- name: PDL_URL
value: "https://pdl-api.dev-fss-pub.nais.io/graphql"
- name: EREG_BASEURL
value: https://ereg-services-q1.intern.dev.nav.no
- name: EREG_URL
value: https://ereg-services-q1.dev-fss-pub.nais.io
redis:
- instance: oppfolgingsplan
access: readwrite
5 changes: 3 additions & 2 deletions nais/nais-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ spec:
outbound:
external:
- host: "pdl-api.prod-fss-pub.nais.io"
- host: "ereg-services.prod-fss-pub.nais.io"
rules:
- application: digdir-krr-proxy
namespace: team-rocket
Expand Down Expand Up @@ -109,8 +110,8 @@ spec:
value: "prod-fss.pdl.pdl-api"
- name: PDL_URL
value: "https://pdl-api.prod-fss-pub.nais.io/graphql"
- name: EREG_BASEURL
value: https://ereg-services.intern.nav.no
- name: EREG_URL
value: https://ereg-services.prod-fss-pub.nais.io
redis:
- instance: oppfolgingsplan
access: readwrite
6 changes: 0 additions & 6 deletions src/main/kotlin/no/nav/syfo/config/ApplicationConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package no.nav.syfo.config
import org.springframework.cache.annotation.EnableCaching
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
import org.springframework.scheduling.annotation.EnableScheduling
import org.springframework.transaction.annotation.EnableTransactionManagement
import org.springframework.web.client.RestTemplate
Expand All @@ -14,11 +13,6 @@ import org.springframework.web.client.RestTemplate
@EnableCaching

class ApplicationConfig {
@Bean
@Primary
fun restTemplate(): RestTemplate {
return RestTemplate()
}

@Bean(name = ["scheduler"])
fun restTemplateScheduler(): RestTemplate {
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/no/nav/syfo/domain/Virksomhetsnummer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package no.nav.syfo.domain

data class Virksomhetsnummer(val value: String) {
private val nineDigits = Regex("^d{9}\$")

init {
require(!nineDigits.matches(value)) {
"Value is not a valid Virksomhetsnummer"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import org.springframework.web.client.RestClientResponseException
import org.springframework.web.client.RestTemplate

@Service
class EregConsumer @Inject constructor(
@Value("\${ereg.baseurl}") private val baseUrl: String,
class EregClient @Inject constructor(
@Value("\${ereg.url}") private val baseUrl: String,
private val metric: Metrikk,
@param:Qualifier("scheduler") private val restTemplate: RestTemplate,
) {
Expand Down Expand Up @@ -57,7 +57,7 @@ class EregConsumer @Inject constructor(
}

private fun getEregUrl(): String {
return "$baseUrl/ereg/api/v1/organisasjon/{virksomhetsnummer}"
return "$baseUrl/ereg/api/v2/organisasjon/{virksomhetsnummer}"
}

private fun entity(): HttpEntity<String> {
Expand All @@ -69,7 +69,7 @@ class EregConsumer @Inject constructor(
}

companion object {
private val LOG = LoggerFactory.getLogger(EregConsumer::class.java)
private val LOG = LoggerFactory.getLogger(EregClient::class.java)

private const val METRIC_CALL_EREG_SUCCESS = "call_ereg_success"
private const val METRIC_CALL_EREG_FAIL = "call_ereg_fail"
Expand Down
33 changes: 20 additions & 13 deletions src/main/kotlin/no/nav/syfo/virksomhet/VirksomhetController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package no.nav.syfo.virksomhet
import no.nav.security.token.support.core.api.ProtectedWithClaims
import no.nav.security.token.support.core.context.TokenValidationContextHolder
import no.nav.syfo.auth.tokenx.TokenXUtil.TokenXIssuer.TOKENX
import no.nav.syfo.ereg.EregConsumer
import no.nav.syfo.ereg.EregClient
import no.nav.syfo.domain.Virksomhet
import no.nav.syfo.auth.tokenx.TokenXUtil
import no.nav.syfo.domain.Virksomhetsnummer
import no.nav.syfo.util.virksomhetsnummerInvalid
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.HttpStatus
Expand All @@ -23,7 +24,7 @@ import javax.inject.Inject
@RequestMapping(value = ["/api/v1/virksomhet/{virksomhetsnummer}"])
class VirksomhetController @Inject constructor(
private val contextHolder: TokenValidationContextHolder,
private val eregConsumer: EregConsumer,
private val eregClient: EregClient,
@Value("\${oppfolgingsplan.frontend.client.id}")
private val oppfolgingsplanClientId: String,
) {
Expand All @@ -33,17 +34,23 @@ class VirksomhetController @Inject constructor(
): ResponseEntity<Virksomhet> {
TokenXUtil.validateTokenXClaims(contextHolder, oppfolgingsplanClientId)

if (virksomhetsnummerInvalid(virksomhetsnummer)) {
return ResponseEntity.status(HttpStatus.I_AM_A_TEAPOT).build()
}
val vikrsomhetsnummerAsObject = Virksomhetsnummer(virksomhetsnummer)

return when {
virksomhetsnummerInvalid(vikrsomhetsnummerAsObject.value) -> {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build()
}

return ResponseEntity
.status(HttpStatus.OK)
.body(
Virksomhet(
virksomhetsnummer = virksomhetsnummer,
navn = eregConsumer.virksomhetsnavn(virksomhetsnummer),
),
)
else -> {
ResponseEntity
.status(HttpStatus.OK)
.body(
Virksomhet(
virksomhetsnummer = vikrsomhetsnummerAsObject.value,
navn = eregClient.virksomhetsnavn(virksomhetsnummer),
),
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import no.nav.security.token.support.core.context.TokenValidationContext
import no.nav.security.token.support.core.context.TokenValidationContextHolder
import no.nav.security.token.support.core.jwt.JwtTokenClaims
import no.nav.syfo.domain.Virksomhet
import no.nav.syfo.ereg.EregConsumer
import no.nav.syfo.ereg.EregClient
import no.nav.syfo.testhelper.UserConstants.VIRKSOMHETSNUMMER
import org.springframework.http.ResponseEntity
import org.springframework.http.HttpStatus
Expand All @@ -19,21 +19,21 @@ class VirksomhetControllerTest : FunSpec({
val mockTokenValidationContext = mockk<TokenValidationContext>()
val mockJwtTokenClaims = mockk<JwtTokenClaims>()

val eregConsumer = mockk<EregConsumer>()
val eregClient = mockk<EregClient>()
val virksomhetsNavn = "Tull og fanteri AS"
val virksomhet = Virksomhet(VIRKSOMHETSNUMMER, virksomhetsNavn)

val virksomhetController = VirksomhetController(
contextHolder = contextHolder,
eregConsumer = eregConsumer,
eregClient = eregClient,
oppfolgingsplanClientId = "123456789"
)

beforeTest {
every { contextHolder.getTokenValidationContext() } returns mockTokenValidationContext
every { mockTokenValidationContext.getClaims("tokenx") } returns mockJwtTokenClaims
every { mockJwtTokenClaims.getStringClaim("client_id") } returns "123456789"
every { eregConsumer.virksomhetsnavn(VIRKSOMHETSNUMMER) } returns virksomhetsNavn
every { eregClient.virksomhetsnavn(VIRKSOMHETSNUMMER) } returns virksomhetsNavn
}

test("should return virksomhet with valid virksomhetsnummer") {
Expand All @@ -48,7 +48,7 @@ class VirksomhetControllerTest : FunSpec({
test("should return 418 status code with invalid virksomhetsnummer") {
val res: ResponseEntity<Virksomhet> = virksomhetController.getVirksomhet("12345678")

res.statusCode shouldBe HttpStatus.I_AM_A_TEAPOT
res.statusCode shouldBe HttpStatus.BAD_REQUEST
res.body shouldBe null
}
})
2 changes: 1 addition & 1 deletion src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ azure.openid.config.token.endpoint: "http://azure"
azure.app.client.id: 'client.id'
azure.app.client.secret: 'client.secret'
security.token.service.rest.url: "http://security-token-service.url"
ereg.baseurl: "http://ereg"
ereg.url: "http://ereg"

krr.url: "http://krr.url"
krr.scope: "krr.scope"

0 comments on commit d19552e

Please sign in to comment.