Skip to content

Commit

Permalink
Fjernet boilerplate fra synt-sykemelding-api sin bruk av ServerProper…
Browse files Browse the repository at this point in the history
…ties.
  • Loading branch information
rfc3092 committed Oct 26, 2023
1 parent e84af9b commit b7520a4
Show file tree
Hide file tree
Showing 19 changed files with 95 additions and 129 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package no.nav.testnav.apps.syntsykemeldingapi.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}.
* <br/><br/>
* Husk at Spring Boot bruker <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.typesafe-configuration-properties.relaxed-binding">relaxed binding</a>
* mellom configuration properties og field names.
*
* @see ServerProperties
*/
@Configuration
@ConfigurationProperties(prefix = "consumers")
@NoArgsConstructor(access = PACKAGE)
@Getter
@Setter(PACKAGE)
public class Consumers {

private ServerProperties testnavArbeidsforholdService;
private ServerProperties testnavHelsepersonellService;
private ServerProperties testnavOrganisasjonService;
private ServerProperties testnavPdlProxy;
private ServerProperties sykemeldingApi;
private ServerProperties syntSykemelding;

}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import no.nav.testnav.apps.syntsykemeldingapi.config.credentials.ArbeidsforholdServiceProperties;
import no.nav.testnav.apps.syntsykemeldingapi.config.Consumers;
import no.nav.testnav.apps.syntsykemeldingapi.consumer.command.GetArbeidsforholdCommand;
import no.nav.testnav.libs.dto.oppsummeringsdokumentservice.v1.ArbeidsforholdDTO;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange;
import org.springframework.stereotype.Component;

Expand All @@ -15,24 +16,23 @@
public class ArbeidsforholdConsumer {
private final TokenExchange tokenExchange;
private final WebClient webClient;
private final ArbeidsforholdServiceProperties serviceProperties;
private final ServerProperties serverProperties;

public ArbeidsforholdConsumer(
TokenExchange tokenExchange,
ArbeidsforholdServiceProperties serviceProperties) {

Consumers consumers) {
this.tokenExchange = tokenExchange;
this.serviceProperties = serviceProperties;
serverProperties = consumers.getTestnavArbeidsforholdService();
this.webClient = WebClient
.builder()
.baseUrl(serviceProperties.getUrl())
.baseUrl(serverProperties.getUrl())
.build();
}

@SneakyThrows
public ArbeidsforholdDTO getArbeidsforhold(String ident, String orgnummer, String arbeidsforholdId) {
log.info("Henter arbeidsforhold for {} i org {} med id {}", ident, orgnummer, arbeidsforholdId);
var response = tokenExchange.exchange(serviceProperties)
var response = tokenExchange.exchange(serverProperties)
.flatMap(accessToken -> new GetArbeidsforholdCommand(
webClient,
accessToken.getTokenValue(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import no.nav.testnav.apps.syntsykemeldingapi.config.credentials.HelsepersonellServiceProperties;
import no.nav.testnav.apps.syntsykemeldingapi.config.Consumers;
import no.nav.testnav.apps.syntsykemeldingapi.consumer.command.GetHelsepersonellCommand;
import no.nav.testnav.apps.syntsykemeldingapi.domain.HelsepersonellListe;
import no.nav.testnav.apps.syntsykemeldingapi.exception.HelsepersonellNotFoundException;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange;
import org.springframework.stereotype.Component;

Expand All @@ -19,24 +20,23 @@
public class HelsepersonellConsumer {
private final TokenExchange tokenExchange;
private final WebClient webClient;
private final HelsepersonellServiceProperties serviceProperties;
private final ServerProperties serverProperties;

public HelsepersonellConsumer(
TokenExchange tokenExchange,
HelsepersonellServiceProperties serviceProperties) {

Consumers consumers) {
this.tokenExchange = tokenExchange;
this.serviceProperties = serviceProperties;
serverProperties = consumers.getTestnavHelsepersonellService();
this.webClient = WebClient
.builder()
.baseUrl(serviceProperties.getUrl())
.baseUrl(serverProperties.getUrl())
.build();
}

@SneakyThrows
public HelsepersonellListe hentHelsepersonell() {
log.info("Henter helsepersonell...");
var response = tokenExchange.exchange(serviceProperties)
var response = tokenExchange.exchange(serverProperties)
.flatMap(accessToken -> new GetHelsepersonellCommand(webClient, accessToken.getTokenValue()).call())
.block();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
package no.nav.testnav.apps.syntsykemeldingapi.consumer;

import no.nav.testnav.apps.syntsykemeldingapi.config.credentials.OrganisasjonServiceProperties;
import no.nav.testnav.apps.syntsykemeldingapi.config.Consumers;
import no.nav.testnav.apps.syntsykemeldingapi.consumer.command.GetOrganisasjonCommand;
import no.nav.testnav.libs.dto.organisasjon.v1.OrganisasjonDTO;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

@Component
public class OrganisasjonConsumer {
private final OrganisasjonServiceProperties serviceProperties;
private final ServerProperties serverProperties;
private final TokenExchange tokenExchange;
private final WebClient webClient;

private static final String MILJOE = "q1";

public OrganisasjonConsumer(
OrganisasjonServiceProperties serviceProperties,
Consumers consumers,
TokenExchange tokenExchange) {

this.serviceProperties = serviceProperties;
serverProperties = consumers.getTestnavOrganisasjonService();
this.tokenExchange = tokenExchange;
this.webClient = WebClient
.builder()
.baseUrl(serviceProperties.getUrl())
.baseUrl(serverProperties.getUrl())
.build();
}

public OrganisasjonDTO getOrganisasjon(String orgnummer) {
return tokenExchange.exchange(serviceProperties).flatMap(accessToken ->
return tokenExchange.exchange(serverProperties).flatMap(accessToken ->
new GetOrganisasjonCommand(webClient, accessToken.getTokenValue(), orgnummer, MILJOE).call())
.block();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package no.nav.testnav.apps.syntsykemeldingapi.consumer;

import lombok.extern.slf4j.Slf4j;
import no.nav.testnav.apps.syntsykemeldingapi.config.credentials.PdlProxyProperties;
import no.nav.testnav.apps.syntsykemeldingapi.config.Consumers;
import no.nav.testnav.apps.syntsykemeldingapi.consumer.command.GetPdlPersonCommand;
import no.nav.testnav.apps.syntsykemeldingapi.domain.pdl.PdlPerson;
import no.nav.testnav.apps.syntsykemeldingapi.exception.PdlPersonException;
Expand All @@ -26,24 +26,25 @@
@Component
public class PdlProxyConsumer {
private final TokenExchange tokenExchange;
private final ServerProperties serviceProperties;
private final ServerProperties serverProperties;
private final WebClient webClient;

private static final String SINGLE_PERSON_QUERY = "pdlperson/pdlquery.graphql";

public PdlProxyConsumer(
PdlProxyProperties pdlProxyProperties,
Consumers consumers,
TokenExchange tokenExchange) {

this.serviceProperties = pdlProxyProperties;
serverProperties = consumers.getTestnavPdlProxy();
this.tokenExchange = tokenExchange;
this.webClient = WebClient.builder()
.exchangeStrategies(ExchangeStrategies.builder()
this.webClient = WebClient
.builder()
.exchangeStrategies(ExchangeStrategies
.builder()
.codecs(configurer -> configurer
.defaultCodecs()
.maxInMemorySize(16 * 1024 * 1024))
.build())
.baseUrl(pdlProxyProperties.getUrl())
.baseUrl(serverProperties.getUrl())
.build();
}

Expand All @@ -53,7 +54,7 @@ public PdlPerson getPdlPerson(String ident) {
}
try {
var query = getQueryFromFile(SINGLE_PERSON_QUERY);
var response = tokenExchange.exchange(serviceProperties)
var response = tokenExchange.exchange(serverProperties)
.flatMap(accessToken -> new GetPdlPersonCommand(ident, query, accessToken.getTokenValue(), webClient).call())
.block();
if (nonNull(response) && !response.getErrors().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package no.nav.testnav.apps.syntsykemeldingapi.consumer;

import lombok.extern.slf4j.Slf4j;
import no.nav.testnav.apps.syntsykemeldingapi.config.credentials.SykemeldingProperties;
import no.nav.testnav.apps.syntsykemeldingapi.config.Consumers;
import no.nav.testnav.apps.syntsykemeldingapi.consumer.command.PostSykemeldingCommand;
import no.nav.testnav.libs.dto.sykemelding.v1.SykemeldingDTO;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
Expand All @@ -15,22 +15,22 @@
public class SykemeldingConsumer {

private final TokenExchange tokenExchange;
private final ServerProperties serviceProperties;
private final ServerProperties serverProperties;
private final WebClient webClient;

public SykemeldingConsumer(
SykemeldingProperties serviceProperties,
Consumers consumers,
TokenExchange tokenExchange) {

this.serviceProperties = serviceProperties;
serverProperties = consumers.getSykemeldingApi();
this.tokenExchange = tokenExchange;
this.webClient = WebClient.builder()
.baseUrl(serviceProperties.getUrl())
this.webClient = WebClient
.builder()
.baseUrl(serverProperties.getUrl())
.build();
}

public void opprettSykemelding(SykemeldingDTO sykemelding) {
tokenExchange.exchange(serviceProperties).flatMap(accessToken ->
tokenExchange.exchange(serverProperties).flatMap(accessToken ->
new PostSykemeldingCommand(webClient, accessToken.getTokenValue(), sykemelding).call())
.block();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import no.nav.testnav.apps.syntsykemeldingapi.config.credentials.SyntSykemeldingProperties;
import no.nav.testnav.apps.syntsykemeldingapi.config.Consumers;
import no.nav.testnav.apps.syntsykemeldingapi.consumer.command.PostSyntSykemeldingCommand;
import no.nav.testnav.apps.syntsykemeldingapi.consumer.dto.SyntSykemeldingHistorikkDTO;
import no.nav.testnav.apps.syntsykemeldingapi.exception.GenererSykemeldingerException;
Expand All @@ -22,22 +22,23 @@
public class SyntElsamConsumer {

private final TokenExchange tokenExchange;
private final ServerProperties serviceProperties;
private final ServerProperties serverProperties;
private final WebClient webClient;

public SyntElsamConsumer(
SyntSykemeldingProperties syntProperties,
Consumers consumers,
TokenExchange tokenExchange) {

this.serviceProperties = syntProperties;
serverProperties = consumers.getSyntSykemelding();
this.tokenExchange = tokenExchange;
this.webClient = WebClient.builder()
.exchangeStrategies(ExchangeStrategies.builder()
this.webClient = WebClient
.builder()
.exchangeStrategies(ExchangeStrategies
.builder()
.codecs(configurer -> configurer
.defaultCodecs()
.maxInMemorySize(16 * 1024 * 1024))
.build())
.baseUrl(syntProperties.getUrl())
.baseUrl(serverProperties.getUrl())
.build();
}

Expand All @@ -47,7 +48,7 @@ public SyntSykemeldingHistorikkDTO genererSykemeldinger(String ident, LocalDate

var request = Map.of(ident, startDato.toString());

var response = tokenExchange.exchange(serviceProperties).flatMap(accessToken ->
var response = tokenExchange.exchange(serverProperties).flatMap(accessToken ->
new PostSyntSykemeldingCommand(request, accessToken.getTokenValue(), webClient).call())
.block();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.client.WireMock;
import no.nav.testnav.apps.syntsykemeldingapi.config.credentials.ArbeidsforholdServiceProperties;
import no.nav.testnav.libs.dto.oppsummeringsdokumentservice.v1.ArbeidsforholdDTO;
import no.nav.testnav.libs.securitycore.domain.AccessToken;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -58,7 +58,7 @@ public class ArbeidsforholdConsumerTest {
@Before
public void before() {
WireMock.reset();
when(tokenService.exchange(ArgumentMatchers.any(ArbeidsforholdServiceProperties.class))).thenReturn(Mono.just(new AccessToken("token")));
when(tokenService.exchange(ArgumentMatchers.any(ServerProperties.class))).thenReturn(Mono.just(new AccessToken("token")));
arbeidsforholdResponse = getTestArbeidsforholdDTO(arbeidsforholdId, orgnr);
}

Expand Down
Loading

0 comments on commit b7520a4

Please sign in to comment.