Skip to content

Commit

Permalink
Støtte for RestKlient send som gir List/Map (#1242)
Browse files Browse the repository at this point in the history
  • Loading branch information
jolarsen authored Jan 31, 2023
1 parent 03e6206 commit 235cd31
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down Expand Up @@ -74,6 +75,22 @@ public static <T> T fromJson(URL json, Class<T> clazz) {
}
}

public static <T> List<T> listFromJson(String json, Class<T> clazz) {
try {
return MAPPER.readerForListOf(clazz).readValue(json);
} catch (IOException e) {
throw deserializationException(e);
}
}

public static <T> Map<String, T> mapFromJson(String json, Class<T> clazz) {
try {
return MAPPER.readerForMapOf(clazz).readValue(json);
} catch (IOException e) {
throw deserializationException(e);
}
}

public static JsonNode treeFromJson(String json) {
try {
return MAPPER.readTree(json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -45,10 +44,8 @@ public List<Grunnlag> hentGrunnlag(String fnr, LocalDate fom, LocalDate tom) {
.queryParam("fnr", fnr)
.queryParam("fom", konverter(fom))
.queryParam("tom", konverter(tom)).build();
var grunnlag = restClient.send(RestRequest.newGET(path, restConfig), Grunnlag[].class);
return Arrays.asList(grunnlag);
return restClient.sendReturnList(RestRequest.newGET(path, restConfig), Grunnlag.class);
} catch (Exception e) {
LOG.warn("Feil ved oppslag mot {}, returnerer ingen grunnlag", restConfig.endpoint(), e);
throw new TekniskException( "FP-180125",
String.format("Tjeneste %s gir feil, meld til #infotrygd_replikering hvis dette skjer gjennom lengre tidsperiode.", restConfig.endpoint()), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.time.LocalDate;
import java.util.Arrays;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -98,7 +97,7 @@ class DeserializationTest {

@Test
void test_utbetalt() {
var grunnlagene = Arrays.asList(DefaultJsonMapper.fromJson(SAMPLE, Grunnlag[].class));
var grunnlagene = DefaultJsonMapper.listFromJson(SAMPLE, Grunnlag.class);
var grunnlag = grunnlagene.get(0);
assertThat(grunnlag.status().kode()).isEqualTo(StatusKode.L);
assertThat(grunnlag.arbeidsforhold().get(0).orgnr().orgnr()).isEqualTo("888888888");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.net.HttpURLConnection;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
Expand Down Expand Up @@ -36,6 +38,16 @@ public <T> Optional<T> sendReturnOptional(RestRequest request, Class<T> clazz) {
return Optional.ofNullable(mapResponse(response, String::isEmpty, clazz));
}

public <T> List<T> sendReturnList(RestRequest request, Class<T> clazz) {
var response= httpklient.send(request);
return DefaultJsonMapper.listFromJson(response, clazz);
}

public <T> Map<String, T> sendReturnMap(RestRequest request, Class<T> clazz) {
var response= httpklient.send(request);
return DefaultJsonMapper.mapFromJson(response, clazz);
}

public byte[] sendReturnByteArray(RestRequest request) {
return httpklient.sendReturnByteArray(request);
}
Expand Down

0 comments on commit 235cd31

Please sign in to comment.