Skip to content

Commit

Permalink
Added a copy of the common GenererNavnCommand, for testing of new cod…
Browse files Browse the repository at this point in the history
…e in app.
  • Loading branch information
rfc3092 committed Feb 28, 2025
1 parent a9786d9 commit e339014
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package no.nav.dolly.budpro.navn;

import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import no.nav.dolly.libs.command.Command;
import no.nav.testnav.libs.dto.generernavnservice.v1.NavnDTO;
import no.nav.testnav.libs.securitycore.domain.AccessToken;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.Optional;
import java.util.concurrent.Callable;

@RequiredArgsConstructor
@AllArgsConstructor
public class CustomGenererNavnCommand implements Callable<NavnDTO[]> {

private final WebClient webClient;
private final Mono<AccessToken> accessToken;
private Long seed;
private final Integer antall;

@Override
public NavnDTO[] call() {
return Command.fetch(
webClient,
accessToken,
builder -> builder
.path("/api/v1/navn")
.queryParamIfPresent("seed", Optional.ofNullable(seed))
.queryParam("antall", antall)
.build(),
NavnDTO[].class);
}

}
Original file line number Diff line number Diff line change
@@ -1,46 +1,39 @@
package no.nav.dolly.budpro.navn;

import no.nav.dolly.budpro.Consumers;
import no.nav.testnav.libs.commands.generernavnservice.v1.GenererNavnCommand;
import no.nav.testnav.libs.reactivecore.config.WebClientConfig;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import no.nav.testnav.libs.securitycore.domain.AccessToken;
import no.nav.testnav.libs.standalone.servletsecurity.exchange.TokenExchange;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.Arrays;

@Service
@Import(WebClientConfig.class)
public class GeneratedNameService {

private final ServerProperties serverProperties;
private final TokenExchange tokenExchange;
private final Mono<AccessToken> accessToken;
private final WebClient webClient;

GeneratedNameService(
Consumers consumers,
TokenExchange tokenExchange,
WebClient.Builder webClientBuilder
) {
serverProperties = consumers.getGenererNavnService();
this.tokenExchange = tokenExchange;
this.webClient = webClientBuilder
.baseUrl(serverProperties.getUrl())
var consumer = consumers.getGenererNavnService();
accessToken = tokenExchange.exchange(consumer);
webClient = webClientBuilder
.baseUrl(consumer.getUrl())
.build();
}

public String[] getNames(Long seed, int number) {
var accessToken = tokenExchange
.exchange(serverProperties)
.blockOptional()
.orElseThrow(() -> new IllegalStateException("Failed to get token for %s".formatted(serverProperties.getName())))
.getTokenValue();
var arrayOfDTOs = new GenererNavnCommand(webClient, accessToken, seed, number)
.call();
var names = new CustomGenererNavnCommand(webClient, accessToken, seed, number).call();
return Arrays
.stream(arrayOfDTOs)
.stream(names)
.map(name -> name.getAdjektiv() + " " + name.getSubstantiv())
.toList()
.toArray(new String[0]);
Expand Down

0 comments on commit e339014

Please sign in to comment.