Skip to content

Commit

Permalink
Midlertidig lagrer
Browse files Browse the repository at this point in the history
  • Loading branch information
espenwaaga committed Apr 27, 2023
1 parent 5a8b07f commit f632d3f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ public static String contextPathFor(FpApplication application) {
throw new IllegalArgumentException("Utviklerfeil: angitt app er ikke i fp-familien");
}
var appname = application.name().toLowerCase();
// Sjekk om override for kjøring i IDE <app>.override.url=http://localhost:localport/<appname> (evt med port og annen path)
var override = contextPathProperty(application);
if (CLUSTER.isLocal() && override!= null) {
return override;

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

// Sjekk om kryss-lokasjon - da trengs ingress og litt ulike varianter
var clusterForApplication = getCluster(application);
if (!CLUSTER.equals(clusterForApplication)) {
Expand All @@ -78,11 +79,11 @@ public static String contextPathFor(FpApplication application) {
throw new IllegalStateException("Utviklerfeil: Skal ikke komme hit");
}
}

// Samme lokasjon og cluster - bruk service discovery
var prefix = "http://" + appname;
return switch (CLUSTER) {
case DEV_FSS, PROD_FSS -> prefix + "/" + appname;
case VTP -> prefix + ":8080/" + appname;
default -> throw new IllegalArgumentException("Ikke implementert for Cluster " + CLUSTER.clusterName());
};
}
Expand All @@ -94,20 +95,14 @@ public static String scopesFor(FpApplication application) {
return "api://" + getCluster(application).clusterName() + "." + FORELDREPENGER.getName() + "." + application.name().toLowerCase() + "/.default";
}

private static String contextPathProperty(FpApplication application) {
return Optional.ofNullable(ENV.getProperty(application.name().toLowerCase() + ".override.url"))
.map(s -> s.replace("localhost:localport", "localhost:" + LOCAL_PORTS.get(application)))
.orElse(null);
}

private static Cluster getCluster(FpApplication application) {
if (CLUSTER.isProd()) {
return GCP_APPS.contains(application) ? Cluster.PROD_GCP : Cluster.PROD_FSS;
} else if (CLUSTER.isDev()) {
}
if (CLUSTER.isDev()) {
return GCP_APPS.contains(application) ? Cluster.DEV_GCP : Cluster.DEV_FSS;
} else {
return Cluster.VTP;
}
throw new IllegalArgumentException("Utviklerfeil: Skal ikke kunne nå her med cluster annet enn de som er definert over");
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
package no.nav.vedtak.felles.integrasjon.rest;

import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mockStatic;

import java.net.URI;

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

import no.nav.foreldrepenger.konfig.Cluster;

public class TestRestClientConfig {

private static MockedStatic<Cluster> clusterMock;

@BeforeAll
public static void beforeAll() {
clusterMock = mockStatic(Cluster.class);
clusterMock.when(Cluster::values).thenCallRealMethod();
}

@BeforeEach
public void beforeEach() {
clusterMock.clearInvocations();
clusterMock.reset();
clusterMock.when(Cluster::values).thenCallRealMethod();
}

@AfterAll
public static void afterEach() {
clusterMock.close();
}

@Test
void lokal_profil() {
clusterMock.when(Cluster::current).thenReturn(Cluster.VTP);
var config = RestConfig.forClient(TestAbakusMedEndpointProperty.class);
assertThat(config.fpContextPath()).isEqualTo(URI.create("http://localhost:8015/fpabakus"));
}

@Test
void testCase_app_med_endpoint_property_uten_verdi() {
clusterMock.when(Cluster::current).thenReturn(Cluster.DEV_FSS);
var config = RestConfig.forClient(TestAbakusMedEndpointProperty.class);
assertThat(config.tokenConfig()).isEqualTo(TokenFlow.ADAPTIVE);
assertThat(config.scopes()).isEqualTo(FpApplication.scopesFor(FpApplication.FPABAKUS));
Expand All @@ -19,6 +54,7 @@ void testCase_app_med_endpoint_property_uten_verdi() {

@Test
void testCase_app_med_endpoint_property_som_har_verdi() {
clusterMock.when(Cluster::current).thenReturn(Cluster.DEV_FSS);
System.setProperty("non.existent", "http://fpabakus/fpabakus/ekstern/target");
var config = RestConfig.forClient(TestAbakusMedEndpointProperty.class);
assertThat(config.scopes()).isEqualTo(FpApplication.scopesFor(FpApplication.FPABAKUS));
Expand All @@ -29,6 +65,7 @@ void testCase_app_med_endpoint_property_som_har_verdi() {

@Test
void testCase_kun_application_uten_properties() {
clusterMock.when(Cluster::current).thenReturn(Cluster.DEV_FSS);
var config = RestConfig.forClient(TestRiskUtenEndpoint.class);
assertThat(config.tokenConfig()).isEqualTo(TokenFlow.STS_CC);
assertThat(config.scopes()).isEqualTo(FpApplication.scopesFor(FpApplication.FPRISK));
Expand All @@ -38,6 +75,7 @@ void testCase_kun_application_uten_properties() {

@Test
void testCase_app_med_scope_property_uten_verdi() {
clusterMock.when(Cluster::current).thenReturn(Cluster.DEV_FSS);
var config = RestConfig.forClient(TestFormidlingAdaptiveMedScopeProperty.class);
assertThat(config.tokenConfig()).isEqualTo(TokenFlow.ADAPTIVE);
assertThat(config.scopes()).isEqualTo(FpApplication.scopesFor(FpApplication.FPFORMIDLING));
Expand All @@ -47,6 +85,7 @@ void testCase_app_med_scope_property_uten_verdi() {

@Test
void testCase_app_med_scope_property_med_verdi() {
clusterMock.when(Cluster::current).thenReturn(Cluster.DEV_FSS);
System.setProperty("non.existent", "api://local.default.fpformidling-local/.default");
var config = RestConfig.forClient(TestFormidlingAdaptiveMedScopeProperty.class);
assertThat(config.scopes()).isEqualTo("api://local.default.fpformidling-local/.default");
Expand All @@ -57,6 +96,7 @@ void testCase_app_med_scope_property_med_verdi() {

@Test
void testCase_ikke_app_med_endpoint_scope_property_uten_verdi() {
clusterMock.when(Cluster::current).thenReturn(Cluster.DEV_FSS);
var config = RestConfig.forClient(TestFormidlingMedEndpointScopes.class);
assertThat(config.scopes()).isEqualTo("api://local.teamforeldrepenger.fp-formidling/.default");
assertThat(config.getContextPath()).isEmpty();
Expand All @@ -65,6 +105,7 @@ void testCase_ikke_app_med_endpoint_scope_property_uten_verdi() {

@Test
void testCase_ikke_app_med_endpoint_scope_property_med_verdi() {
clusterMock.when(Cluster::current).thenReturn(Cluster.DEV_FSS);
System.setProperty("non.existent", "http://fpformidling/fpformidling/ekstern/target");
System.setProperty("non.existent2", "api://local.default.fpformidling/.default");
var config = RestConfig.forClient(TestFormidlingMedEndpointScopes.class);
Expand Down

0 comments on commit f632d3f

Please sign in to comment.