From 066112c425f0781037027994d682b6ff39cabbf2 Mon Sep 17 00:00:00 2001 From: Cato Olsen Date: Thu, 26 Oct 2023 13:58:45 +0200 Subject: [PATCH] Fjernet boilerplate fra person-export-api sin bruk av ServerProperties. --- .../personexportapi/config/Consumers.java | 30 +++++++++++++++++++ .../consumer/KodeverkConsumer.java | 16 +++++----- .../consumer/TpsfConsumer.java | 22 +++++++------- .../credential/KodeverkProperties.java | 10 ------- .../consumer/credential/TpsfProperties.java | 10 ------- 5 files changed, 48 insertions(+), 40 deletions(-) create mode 100644 apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/config/Consumers.java delete mode 100644 apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/consumer/credential/KodeverkProperties.java delete mode 100644 apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/consumer/credential/TpsfProperties.java diff --git a/apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/config/Consumers.java b/apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/config/Consumers.java new file mode 100644 index 00000000000..e9b4af4af4f --- /dev/null +++ b/apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/config/Consumers.java @@ -0,0 +1,30 @@ +package no.nav.testnav.apps.personexportapi.config; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import no.nav.testnav.libs.securitycore.domain.ServerProperties; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +import static lombok.AccessLevel.PACKAGE; + +/** + * Samler alle placeholders for ulike {@code consumers.*}-konfigurasjon her, dvs. subklasser av {@code ServerProperties}. + *

+ * Husk at Spring Boot bruker relaxed binding + * mellom configuration properties og field names. + * + * @see ServerProperties + */ +@Configuration +@ConfigurationProperties(prefix = "consumers") +@NoArgsConstructor(access = PACKAGE) +@Getter +@Setter(PACKAGE) +public class Consumers { + + private ServerProperties kodeverk; + private ServerProperties tpsForvalter; + +} diff --git a/apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/consumer/KodeverkConsumer.java b/apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/consumer/KodeverkConsumer.java index 6b5d44256d1..e426ddacbaa 100644 --- a/apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/consumer/KodeverkConsumer.java +++ b/apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/consumer/KodeverkConsumer.java @@ -6,8 +6,8 @@ import java.util.List; import java.util.Map; +import no.nav.testnav.apps.personexportapi.config.Consumers; import no.nav.testnav.apps.personexportapi.consumer.command.GetKodeverkCommand; -import no.nav.testnav.apps.personexportapi.consumer.credential.KodeverkProperties; import no.nav.testnav.apps.personexportapi.consumer.response.KodeverkBetydningerResponse; import no.nav.testnav.libs.securitycore.domain.ServerProperties; import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange; @@ -25,16 +25,16 @@ public class KodeverkConsumer { private final WebClient webClient; private final TokenExchange tokenExchange; - private final ServerProperties properties; + private final ServerProperties serverProperties; public KodeverkConsumer( TokenExchange tokenExchange, - KodeverkProperties serviceProperties) { - + Consumers consumers) { this.tokenExchange = tokenExchange; - this.properties = serviceProperties; - this.webClient = WebClient.builder() - .baseUrl(serviceProperties.getUrl()) + serverProperties = consumers.getKodeverk(); + this.webClient = WebClient + .builder() + .baseUrl(serverProperties.getUrl()) .codecs(configurer -> configurer .defaultCodecs() .maxInMemorySize(16 * 1024 * 1024)) @@ -55,7 +55,7 @@ public String getKodeverkOppslag(String kodeverk, String verdi) { @Cacheable(sync = true) public Map> getKodeverkByName(String kodeverk) { - var kodeverkResponse = tokenExchange.exchange(properties) + var kodeverkResponse = tokenExchange.exchange(serverProperties) .flatMap(accessToken -> new GetKodeverkCommand(webClient, accessToken.getTokenValue(), kodeverk).call()) .block(); diff --git a/apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/consumer/TpsfConsumer.java b/apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/consumer/TpsfConsumer.java index 6217a70b446..e7d9d485d13 100644 --- a/apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/consumer/TpsfConsumer.java +++ b/apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/consumer/TpsfConsumer.java @@ -1,14 +1,13 @@ package no.nav.testnav.apps.personexportapi.consumer; import lombok.extern.slf4j.Slf4j; +import no.nav.testnav.apps.personexportapi.config.Consumers; import no.nav.testnav.apps.personexportapi.consumer.command.GetTpsfGrupperCommand; import no.nav.testnav.apps.personexportapi.consumer.command.GetTpsfMeldingerFromPageCommand; -import no.nav.testnav.apps.personexportapi.consumer.credential.TpsfProperties; import no.nav.testnav.apps.personexportapi.consumer.dto.GruppeDTO; import no.nav.testnav.apps.personexportapi.domain.Person; import no.nav.testnav.libs.securitycore.domain.ServerProperties; import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange; -import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import org.springframework.web.client.HttpClientErrorException; @@ -32,26 +31,25 @@ public class TpsfConsumer { private final WebClient webClient; private final TokenExchange tokenExchange; - private final ServerProperties properties; + private final ServerProperties serverProperties; private final ExecutorService executorService; public TpsfConsumer( TokenExchange tokenExchange, - TpsfProperties serviceProperties, - @Value("${consumers.tps-forvalter.threads}") Integer threads) { - + Consumers consumers) { this.tokenExchange = tokenExchange; - this.properties = serviceProperties; - this.webClient = WebClient.builder() - .baseUrl(properties.getUrl()) + serverProperties = consumers.getTpsForvalter(); + this.webClient = WebClient + .builder() + .baseUrl(serverProperties.getUrl()) .build(); - this.executorService = Executors.newFixedThreadPool(threads); + this.executorService = Executors.newFixedThreadPool(serverProperties.getThreads()); } private GruppeDTO getGruppe(String avspillingsgruppe) { log.info("Henter avspillingsgruppe med id {}...", avspillingsgruppe); - var list = tokenExchange.exchange(properties) + var list = tokenExchange.exchange(serverProperties) .flatMap(accessToken -> new GetTpsfGrupperCommand(webClient, accessToken.getTokenValue()).call()) .block(); @@ -71,7 +69,7 @@ private int getNumberOfPages(String avspillingsgruppe) { private CompletableFuture> getPersonFromPage(String avspillingsgruppe, int page, int numberOfPages) { return CompletableFuture.supplyAsync( - () -> tokenExchange.exchange(properties) + () -> tokenExchange.exchange(serverProperties) .flatMap(accessToken -> new GetTpsfMeldingerFromPageCommand( webClient, accessToken.getTokenValue(), avspillingsgruppe, page).call()) .block(), diff --git a/apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/consumer/credential/KodeverkProperties.java b/apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/consumer/credential/KodeverkProperties.java deleted file mode 100644 index dd2dd7d21c0..00000000000 --- a/apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/consumer/credential/KodeverkProperties.java +++ /dev/null @@ -1,10 +0,0 @@ -package no.nav.testnav.apps.personexportapi.consumer.credential; - -import no.nav.testnav.libs.securitycore.domain.ServerProperties; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; - -@Configuration -@ConfigurationProperties(prefix = "consumers.kodeverk") -public class KodeverkProperties extends ServerProperties { -} diff --git a/apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/consumer/credential/TpsfProperties.java b/apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/consumer/credential/TpsfProperties.java deleted file mode 100644 index f69fa3e48a4..00000000000 --- a/apps/person-export-api/src/main/java/no/nav/testnav/apps/personexportapi/consumer/credential/TpsfProperties.java +++ /dev/null @@ -1,10 +0,0 @@ -package no.nav.testnav.apps.personexportapi.consumer.credential; - -import no.nav.testnav.libs.securitycore.domain.ServerProperties; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; - -@Configuration -@ConfigurationProperties(prefix = "consumers.tps-forvalter") -public class TpsfProperties extends ServerProperties { -}