Skip to content

Commit

Permalink
TFP-3391 la til roundtrip test i kontrakter (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
jolarsen authored May 24, 2020
1 parent 199c1db commit 36814de
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
25 changes: 23 additions & 2 deletions vl-kontrakt-abonnent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,29 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<scope>provided</scope>
</dependency>

</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<!-- for Bean validation -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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 = 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();


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();
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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 36814de

Please sign in to comment.