From 36814de2408460dfbd83f475a3591cfb94061b18 Mon Sep 17 00:00:00 2001 From: Jens-Otto Larsen <46576810+jolarsen@users.noreply.github.com> Date: Sun, 24 May 2020 18:44:56 +0200 Subject: [PATCH] TFP-3391 la til roundtrip test i kontrakter (#205) --- vl-kontrakt-abonnent/pom.xml | 25 ++++++++- .../abonnent/F\303\270dselsHendelseTest.java" | 54 +++++++++++++++++++ .../kontrakter/abonnent/TestJsonMapper.java | 38 +++++++++++++ 3 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 "vl-kontrakt-abonnent/src/test/java/no/nav/foreldrepenger/kontrakter/abonnent/F\303\270dselsHendelseTest.java" create mode 100644 vl-kontrakt-abonnent/src/test/java/no/nav/foreldrepenger/kontrakter/abonnent/TestJsonMapper.java diff --git a/vl-kontrakt-abonnent/pom.xml b/vl-kontrakt-abonnent/pom.xml index 52576e1c..43e8e2ac 100644 --- a/vl-kontrakt-abonnent/pom.xml +++ b/vl-kontrakt-abonnent/pom.xml @@ -24,8 +24,29 @@ com.fasterxml.jackson.core jackson-annotations provided - - + + + com.fasterxml.jackson.core + jackson-databind + + + com.fasterxml.jackson.datatype + jackson-datatype-jdk8 + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + + + + org.hibernate.validator + hibernate-validator + + + org.glassfish + javax.el + test + diff --git "a/vl-kontrakt-abonnent/src/test/java/no/nav/foreldrepenger/kontrakter/abonnent/F\303\270dselsHendelseTest.java" "b/vl-kontrakt-abonnent/src/test/java/no/nav/foreldrepenger/kontrakter/abonnent/F\303\270dselsHendelseTest.java" new file mode 100644 index 00000000..1923688e --- /dev/null +++ "b/vl-kontrakt-abonnent/src/test/java/no/nav/foreldrepenger/kontrakter/abonnent/F\303\270dselsHendelseTest.java" @@ -0,0 +1,54 @@ +package no.nav.foreldrepenger.kontrakter.abonnent; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.LocalDate; +import java.util.Collections; + +import javax.validation.Validation; + +import org.junit.Test; + +import com.fasterxml.jackson.databind.ObjectReader; +import com.fasterxml.jackson.databind.ObjectWriter; + +import no.nav.foreldrepenger.kontrakter.abonnent.tps.FødselHendelseDto; + +public class FødselsHendelseTest { + + private static final ObjectWriter WRITER = TestJsonMapper.getMapper().writerWithDefaultPrettyPrinter(); + private static final ObjectReader READER = TestJsonMapper.getMapper().reader(); + + private static final String AKTØR_ID = "10000000001"; + private static final LocalDate NÅ = LocalDate.now(); + + @Test + public void test_fødselshendelse() throws Exception { + var fødselsHendelse = new FødselHendelseDto(); + fødselsHendelse.setId("id_1"); + fødselsHendelse.setAktørIdForeldre(Collections.singletonList(AKTØR_ID)); + fødselsHendelse.setFødselsdato(NÅ); + + + String json = WRITER.writeValueAsString(fødselsHendelse); + System.out.println(json); + + FødselHendelseDto roundTripped = READER.forType(FødselHendelseDto.class).readValue(json); + + assertThat(roundTripped).isNotNull(); + assertThat(roundTripped.getId()).isEqualTo("id_1"); + assertThat(roundTripped.getAktørIdForeldre().get(0)).isEqualTo(AKTØR_ID); + assertThat(roundTripped.getFødselsdato()).isEqualTo(NÅ); + assertThat(roundTripped.getHendelsetype()).isEqualTo("FØDSEL"); + validateResult(roundTripped); + } + + private void validateResult(Object roundTripped) { + assertThat(roundTripped).isNotNull(); + try (var factory = Validation.buildDefaultValidatorFactory()) { + var validator = factory.getValidator(); + var violations = validator.validate(roundTripped); + assertThat(violations).isEmpty(); + } + } +} \ No newline at end of file diff --git a/vl-kontrakt-abonnent/src/test/java/no/nav/foreldrepenger/kontrakter/abonnent/TestJsonMapper.java b/vl-kontrakt-abonnent/src/test/java/no/nav/foreldrepenger/kontrakter/abonnent/TestJsonMapper.java new file mode 100644 index 00000000..3b93a77f --- /dev/null +++ b/vl-kontrakt-abonnent/src/test/java/no/nav/foreldrepenger/kontrakter/abonnent/TestJsonMapper.java @@ -0,0 +1,38 @@ +package no.nav.foreldrepenger.kontrakter.abonnent; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.InjectableValues; +import com.fasterxml.jackson.databind.InjectableValues.Std; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; + +public class TestJsonMapper { + + private static final ObjectMapper OM; + + static { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.registerModule(new Jdk8Module()); + objectMapper.registerModule(new JavaTimeModule()); + objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + objectMapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE); + objectMapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE); + objectMapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE); + objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); + objectMapper.setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.ANY); + + Std std = new InjectableValues.Std(); + objectMapper.setInjectableValues(std); + OM = objectMapper; + } + + public static ObjectMapper getMapper() { + return OM; + } + +}