Skip to content

Commit

Permalink
Bruker heller mocktio til å mocke ut Environment
Browse files Browse the repository at this point in the history
  • Loading branch information
espenwaaga committed Apr 27, 2023
1 parent 94c5d6d commit cc8d9f6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,32 @@ public boolean specified() {


public static String contextPathFor(FpApplication application) {
return contextPathFor(application, ENV.getCluster());
return contextPathFor(application, ENV);
}

public static String scopesFor(FpApplication application) {
return scopesFor(application, ENV.getCluster());
return scopesFor(application, ENV);
}

static String contextPathFor(FpApplication application, Cluster clusten) {
static String contextPathFor(FpApplication application, Environment currentEnvironment) {
if (application == null || NONFP.equals(application)) {
throw new IllegalArgumentException("Utviklerfeil: angitt app er ikke i fp-familien");
}
var appname = application.name().toLowerCase();
var cluster = currentEnvironment.getCluster();

if (clusten.isLocal()) {
return Optional.ofNullable(ENV.getProperty(application.name().toLowerCase() + ".override.url"))
if (cluster.isLocal()) {
return Optional.ofNullable(currentEnvironment.getProperty(application.name().toLowerCase() + ".override.url"))
.orElseGet(() -> String.format("http://localhost:%s/%s", LOCAL_PORTS.get(application), appname));
}

var clusterForApplication = getClusterTilFPApplikasjonenSomSkalKalles(application, clusten);
if (clusten.isCoLocated(clusterForApplication)) {
var clusterForApplication = getClusterTilFPApplikasjonenSomSkalKalles(application, currentEnvironment);
if (cluster.isCoLocated(clusterForApplication)) {
return String.format("http://%s/%s", appname, appname);
} else {
var prefix = "https://" + appname;
if (clusten.isFss()) { // Kaller fra FSS til GCP
return prefix + ".intern" + (clusten.isProd() ? "" : ".dev") + ".nav.no/" + appname;
if (currentEnvironment.isFss()) { // Kaller fra FSS til GCP
return prefix + ".intern" + (cluster.isProd() ? "" : ".dev") + ".nav.no/" + appname;
} else { // Kaller fra GCP til FSS
if (FPSAK.equals(application)) {
prefix += "-api";
Expand All @@ -89,18 +90,18 @@ static String contextPathFor(FpApplication application, Cluster clusten) {
}
}

static String scopesFor(FpApplication application, Cluster cluster) {
if (cluster.isLocal()) {
static String scopesFor(FpApplication application, Environment currentEnvironment) {
if (currentEnvironment.isLocal()) {
return "api://" + Cluster.VTP.clusterName() + "." + FORELDREPENGER.getName() + "." + Cluster.VTP.clusterName() + "/.default";
}
return "api://" + getClusterTilFPApplikasjonenSomSkalKalles(application, cluster).clusterName() + "." + FORELDREPENGER.getName() + "." + application.name().toLowerCase() + "/.default";
return "api://" + getClusterTilFPApplikasjonenSomSkalKalles(application, currentEnvironment).clusterName() + "." + FORELDREPENGER.getName() + "." + application.name().toLowerCase() + "/.default";
}

private static Cluster getClusterTilFPApplikasjonenSomSkalKalles(FpApplication application, Cluster cluster) {
if (cluster.isProd()) {
private static Cluster getClusterTilFPApplikasjonenSomSkalKalles(FpApplication application, Environment currentEnvironment) {
if (currentEnvironment.isProd()) {
return GCP_APPS.contains(application) ? Cluster.PROD_GCP : Cluster.PROD_FSS;
}
if (cluster.isDev()) {
if (currentEnvironment.isDev()) {
return GCP_APPS.contains(application) ? Cluster.DEV_GCP : Cluster.DEV_FSS;
}
throw new IllegalArgumentException("Utviklerfeil: Skal ikke kunne nå her med cluster annet enn de som er definert over");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,76 @@
package no.nav.vedtak.felles.integrasjon.rest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import no.nav.foreldrepenger.konfig.Cluster;
import no.nav.foreldrepenger.konfig.Environment;

class FpApplicationTest {

private Environment environment = Mockito.mock(Environment.class);

@Test
void test_at_service_discovery_brukes_mellom_FPAppp_i_samme_cluster() {
var contextPath = FpApplication.contextPathFor(FpApplication.FPSAK, Cluster.DEV_FSS);
when(environment.getCluster()).thenReturn(Cluster.DEV_FSS);
when(environment.isDev()).thenReturn(true);
var contextPath = FpApplication.contextPathFor(FpApplication.FPSAK, environment);
assertThat(contextPath).isEqualTo("http://fpsak/fpsak");
}

@Test
void test_at_fss_til_gcp_bruker_full_ingress() {
var contextPath = FpApplication.contextPathFor(FpApplication.FPOVERSIKT, Cluster.DEV_FSS);
when(environment.getCluster()).thenReturn(Cluster.DEV_FSS);
when(environment.isFss()).thenReturn(true);
when(environment.isDev()).thenReturn(true);
var contextPath = FpApplication.contextPathFor(FpApplication.FPOVERSIKT, environment);
assertThat(contextPath).isEqualTo("https://fpoversikt.intern.dev.nav.no/fpoversikt");
}

@Test
void test_at_gcp_til_annen_app_enn_fpsak_i_fss_bruker_ingress_av_type_pub() {
var contextPath = FpApplication.contextPathFor(FpApplication.FPTILBAKE, Cluster.DEV_GCP);
when(environment.getCluster()).thenReturn(Cluster.DEV_GCP);
when(environment.isDev()).thenReturn(true);
var contextPath = FpApplication.contextPathFor(FpApplication.FPTILBAKE, environment);
assertThat(contextPath).isEqualTo("https://fptilbake.dev-fss-pub.nais.io/fptilbake");
}

@Test
void test_at_gcp_til_fpsak_i_fss_bruker_ingress_av_type_pub() {
var contextPath = FpApplication.contextPathFor(FpApplication.FPSAK, Cluster.DEV_GCP);
assertThat(contextPath).isEqualTo("https://fpsak-api.dev-fss-pub.nais.io/fpsak");
when(environment.getCluster()).thenReturn(Cluster.PROD_GCP);
when(environment.isProd()).thenReturn(true);
var contextPath = FpApplication.contextPathFor(FpApplication.FPSAK, environment);
assertThat(contextPath).isEqualTo("https://fpsak-api.prod-fss-pub.nais.io/fpsak");
}

@Test
void test_at_vi_bruker_default_lokalhost_ved_lokal_kjøring_uten_override_url_satt() {
var contextPath = FpApplication.contextPathFor(FpApplication.FPABAKUS, Cluster.VTP);
when(environment.getCluster()).thenReturn(Cluster.VTP);
when(environment.isDev()).thenReturn(true);
when(environment.getProperty("fpabakus.override.url")).thenReturn(null);
var contextPath = FpApplication.contextPathFor(FpApplication.FPABAKUS, environment);
assertThat(contextPath).isEqualTo("http://localhost:8015/fpabakus");
}

@Test
void bruk_override_url_hvis_oppgitt_og_cluster_er_vtp() {
var overrideUrl = "http://override.url/";
System.setProperty("fpabakus.override.url", overrideUrl);
var contextPath = FpApplication.contextPathFor(FpApplication.FPABAKUS, Cluster.VTP);
when(environment.getCluster()).thenReturn(Cluster.VTP);
when(environment.getProperty("fpabakus.override.url")).thenReturn(overrideUrl);
var contextPath = FpApplication.contextPathFor(FpApplication.FPABAKUS, environment);
assertThat(contextPath).isEqualTo(overrideUrl);
}

@Test
void ikke_bruk_override_url_hvis_oppgitt_og_cluster_dev_eller_prod() {
var overrideUrl = "http://override.url/";
System.setProperty("fpabakus.override.url", overrideUrl);
var contextPath = FpApplication.contextPathFor(FpApplication.FPABAKUS, Cluster.DEV_FSS);
when(environment.getCluster()).thenReturn(Cluster.DEV_FSS);
when(environment.isDev()).thenReturn(true);
when(environment.getProperty("fpabakus.override.url")).thenReturn(overrideUrl);
var contextPath = FpApplication.contextPathFor(FpApplication.FPABAKUS, environment);
assertThat(contextPath).isEqualTo("http://fpabakus/fpabakus");
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>no.nav.foreldrepenger.felles</groupId>
<artifactId>fp-bom</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>0.5.0</version>
</parent>

<artifactId>felles-root</artifactId>
Expand Down

0 comments on commit cc8d9f6

Please sign in to comment.