Skip to content

Commit

Permalink
Add custom mappings for LocalDate and LocalDateTime
Browse files Browse the repository at this point in the history
Introduced new classes LocalDateCustomMapping and LocalDateTimeCustomMapping to handle custom conversion formats. Updated OppdragRequest and related mapping strategies to use these custom converters. Added unit tests to verify the new mapping logic.
  • Loading branch information
krharum committed Oct 31, 2024
1 parent 83ee526 commit 2099800
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package no.nav.testnav.oppdragservice.mapper;

import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.CustomConverter;
import ma.glasnost.orika.MappingContext;
import ma.glasnost.orika.metadata.Type;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Slf4j
@Component
public class LocalDateCustomMapping extends CustomConverter<LocalDate, String> {

private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd-MM-yyyy");

@Override
public String convert(LocalDate localDate, Type<? extends String> type, MappingContext mappingContext) {

return FORMATTER.format(localDate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package no.nav.testnav.oppdragservice.mapper;

import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.CustomConverter;
import ma.glasnost.orika.MappingContext;
import ma.glasnost.orika.metadata.Type;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@Component
public class LocalDateTimeCustomMapping extends CustomConverter<LocalDateTime, String> {

private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH.mm.ss.SSSSSS");

@Override
public String convert(LocalDateTime localDateTime, Type<? extends String> type, MappingContext mappingContext) {

return FORMATTER.format(localDateTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import ma.glasnost.orika.MappingContext;
import no.nav.testnav.libs.dto.oppdragservice.v1.OppdragRequest;
import no.nav.testnav.oppdragservice.wsdl.Oppdrag;
import no.nav.testnav.oppdragservice.wsdl.Oppdragslinje;
import no.nav.testnav.oppdragservice.wsdl.SendInnOppdragRequest;
import no.nav.testnav.oppdragservice.wsdl.SendInnOppdragRequest2;
import org.springframework.stereotype.Component;

import static java.util.Objects.nonNull;

@Component
public class OppdragRequestMappingStrategy implements MappingStrategy{

Expand All @@ -22,7 +25,9 @@ public void mapAtoB(OppdragRequest source,
SendInnOppdragRequest destination,
MappingContext context) {

destination.setRequest(mapperFacade.map(source, SendInnOppdragRequest2.class, context));
var oppdragRequest = new SendInnOppdragRequest2();
oppdragRequest.setOppdrag(mapperFacade.map(source, Oppdrag.class, context));
destination.setRequest(oppdragRequest);
}
})
.register();
Expand All @@ -34,7 +39,26 @@ public void mapAtoB(OppdragRequest source,
Oppdrag destination,
MappingContext context) {

if (nonNull(source.getUtbetFrekvens())) {
destination.setUtbetFrekvens(destination.getUtbetFrekvens()
.replace("_",""));
}
}
})
.byDefault()
.register();

factory.classMap(OppdragRequest.Oppdragslinje.class, Oppdragslinje.class)
.customize(new CustomMapper<>() {
@Override
public void mapAtoB(OppdragRequest.Oppdragslinje source,
Oppdragslinje destination,
MappingContext context) {

if (nonNull(source.getTypeSats())) {
destination.setTypeSats(destination.getTypeSats()
.replace("_",""));
}
}
})
.byDefault()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.impl.DefaultMapperFactory;

import java.util.List;

import static java.util.Objects.nonNull;

public class MapperTestUtils {
Expand All @@ -12,15 +14,16 @@ public static MapperFacade createMapperFacadeForMappingStrategy(MappingStrategy.
return createMapperFacadeForMappingStrategy(null, strategies);
}

public static MapperFacade createMapperFacadeForMappingStrategy(CustomConverter<Object, Object> converter, MappingStrategy... strategies) {
public static MapperFacade createMapperFacadeForMappingStrategy(List<CustomConverter> converters, MappingStrategy... strategies) {
DefaultMapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

for (MappingStrategy strategy : strategies) {
strategy.register(mapperFactory);
}

if (nonNull(converter)) {
mapperFactory.getConverterFactory().registerConverter(converter);
if (nonNull(converters)) {
converters
.forEach(converter -> mapperFactory.getConverterFactory().registerConverter(converter));
}
return mapperFactory.getMapperFacade();
}
Expand Down
Loading

0 comments on commit 2099800

Please sign in to comment.