Skip to content

Commit

Permalink
Fikser manglende annotering ved flere ctoer. (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsladek authored Oct 31, 2024
1 parent 7be4e28 commit 6ae1ebc
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -25,9 +26,8 @@ public class OpptjeningAktiviteterDto {
@Size()
private List<OpptjeningPeriodeDto> perioder;

@JsonProperty(value = "midlertidigInaktivType", required = false)
@JsonProperty(value = "midlertidigInaktivType")
@Valid

private MidlertidigInaktivType midlertidigInaktivType;

protected OpptjeningAktiviteterDto() {
Expand All @@ -38,8 +38,9 @@ public OpptjeningAktiviteterDto(@JsonProperty(value = "perioder",required = true
this.perioder = perioder;
}

public OpptjeningAktiviteterDto(@JsonProperty(value = "perioder",required = true) @Valid @NotEmpty List<OpptjeningPeriodeDto> perioder,
@JsonProperty(value = "midlertidigInaktivType",required = true) @Valid MidlertidigInaktivType midlertidigInaktivType) {
@JsonCreator
public OpptjeningAktiviteterDto(@JsonProperty(value = "perioder", required = true) @Valid @NotEmpty List<OpptjeningPeriodeDto> perioder,
@JsonProperty(value = "midlertidigInaktivType") @Valid MidlertidigInaktivType midlertidigInaktivType) {

this.perioder = perioder;
this.midlertidigInaktivType = midlertidigInaktivType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package no.nav.folketrygdloven.kalkulus.opptjening.v1;

import java.time.LocalDate;
import java.util.List;
import java.util.TimeZone;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
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;

import no.nav.folketrygdloven.kalkulus.kodeverk.MidlertidigInaktivType;

import org.junit.jupiter.api.Test;

import no.nav.folketrygdloven.kalkulus.felles.v1.Periode;
import no.nav.folketrygdloven.kalkulus.kodeverk.OpptjeningAktivitetType;

import static org.assertj.core.api.Assertions.assertThat;

class OpptjeningAktiviteterDtoTest {

@Test
void serialisering() throws JsonProcessingException {
var opptjeningDto = new OpptjeningAktiviteterDto(List.of(new OpptjeningPeriodeDto(OpptjeningAktivitetType.ARBEID, new Periode(LocalDate.now(), LocalDate.now().plusDays(10)))));
var om = createObjectMapper();
var string = om.writeValueAsString(opptjeningDto);
assertThat(string).isNotBlank();
}

@Test
void serialiseringSecondCtor() throws JsonProcessingException {
var opptjeningDto = new OpptjeningAktiviteterDto(List.of(new OpptjeningPeriodeDto(OpptjeningAktivitetType.ARBEID, new Periode(LocalDate.now(), LocalDate.now().plusDays(10)))), MidlertidigInaktivType.A);
var om = createObjectMapper();
var string = om.writeValueAsString(opptjeningDto);
assertThat(string).isNotBlank();
}

@Test
void deserialisering() throws JsonProcessingException {
var json = "{\"perioder\":[{\"opptjeningAktivitetType\":\"ARBEID\",\"periode\":{\"fom\":\"2024-10-31\",\"tom\":\"2024-11-10\"}}]}";
var mapper = createObjectMapper();
var opptjeningAktivitet = mapper.readValue(json, OpptjeningAktiviteterDto.class);
assertThat(opptjeningAktivitet).isNotNull();
assertThat(opptjeningAktivitet.getPerioder()).isNotNull().hasSize(1);
assertThat(opptjeningAktivitet.getMidlertidigInaktivType()).isNull();
}

@Test
void deserialiseringSecondCtor() throws JsonProcessingException {
var json = "{\"perioder\":[{\"opptjeningAktivitetType\":\"ARBEID\",\"periode\":{\"fom\":\"2024-10-31\",\"tom\":\"2024-11-10\"}}],\"midlertidigInaktivType\":\"8-47 A\"}";

var mapper = createObjectMapper();
var opptjeningAktivitet = mapper.readValue(json, OpptjeningAktiviteterDto.class);
assertThat(opptjeningAktivitet).isNotNull();
assertThat(opptjeningAktivitet.getPerioder()).isNotNull().hasSize(1);
assertThat(opptjeningAktivitet.getMidlertidigInaktivType()).isNotNull();
}

private static ObjectMapper createObjectMapper() {
return new ObjectMapper().registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule())
.setTimeZone(TimeZone.getTimeZone("Europe/Oslo"))
.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE);
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>2.17.2</version>
<version>2.18.1</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Expand Down

0 comments on commit 6ae1ebc

Please sign in to comment.