Skip to content

Commit

Permalink
Add TestConfig and Oppdragsdata utility classes
Browse files Browse the repository at this point in the history
Added TestConfig.java to provide Spring test configuration, including beans for OppdragService, OppdragConsumer, and MapperFacade. Created Oppdragsdata.java in utility package to facilitate the construction of OppdragRequest and SendInnOppdragResponse objects, enhancing the testing structure.
  • Loading branch information
krharum committed Nov 1, 2024
1 parent 2099800 commit 7d1b54d
Show file tree
Hide file tree
Showing 15 changed files with 1,108 additions and 599 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;

@EnableWs
@Configuration
public class OppdragWsConfiguration {
public class OppdragWsConfiguration extends WsConfigurerAdapter {

@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
// this package must match the package in the <generatePackage> specified in
// pom.xml
// build.gradle
marshaller.setContextPath("no.nav.testnav.oppdragservice.wsdl");
return marshaller;
}

@Bean
public OppdragClient countryClient(Jaxb2Marshaller marshaller) {
public OppdragClient oppdragClient(Jaxb2Marshaller marshaller) {

var client = new OppdragClient();
client.setDefaultUri("http://localhost:8080/ws");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package no.nav.testnav.oppdragservice.consumer;

import lombok.extern.slf4j.Slf4j;
import no.nav.testnav.oppdragservice.wsdl.SendInnOppdragRequest;
import no.nav.testnav.oppdragservice.wsdl.SendInnOppdragResponse;
import org.springframework.stereotype.Service;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.client.core.SoapActionCallback;

@Slf4j
@Service
public class OppdragConsumer {
public class OppdragConsumer extends WebServiceGatewaySupport {

public void sendOppdrag(String xmlPayload) {
public SendInnOppdragResponse sendOppdrag(SendInnOppdragRequest melding) {

return (SendInnOppdragResponse) getWebServiceTemplate().marshalSendAndReceive("", melding,
new SoapActionCallback("no.nav.testnav.oppdragservice.wsdl.SendInnOppdragResponse"));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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.converter.BidirectionalConverter;
import ma.glasnost.orika.metadata.Type;
import org.springframework.stereotype.Component;

Expand All @@ -11,13 +11,19 @@

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

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

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

return FORMATTER.format(localDate);
}

@Override
public LocalDate convertFrom(String s, Type<LocalDate> type, MappingContext mappingContext) {

return LocalDate.from(FORMATTER.parse(s));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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.converter.BidirectionalConverter;
import ma.glasnost.orika.metadata.Type;
import org.springframework.stereotype.Component;

Expand All @@ -11,13 +11,19 @@

@Slf4j
@Component
public class LocalDateTimeCustomMapping extends CustomConverter<LocalDateTime, String> {
public class LocalDateTimeCustomMapping extends BidirectionalConverter<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) {
public String convertTo(LocalDateTime localDateTime, Type<String> type, MappingContext mappingContext) {

return FORMATTER.format(localDateTime);
}

@Override
public LocalDateTime convertFrom(String s, Type<LocalDateTime> type, MappingContext mappingContext) {

return LocalDateTime.from(FORMATTER.parse(s));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import ma.glasnost.orika.CustomMapper;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.MappingContext;
import no.nav.testnav.libs.dto.oppdragservice.v1.Oppdrag;
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;
Expand All @@ -26,17 +26,17 @@ public void mapAtoB(OppdragRequest source,
MappingContext context) {

var oppdragRequest = new SendInnOppdragRequest2();
oppdragRequest.setOppdrag(mapperFacade.map(source, Oppdrag.class, context));
oppdragRequest.setOppdrag(mapperFacade.map(source.getOppdrag(), no.nav.testnav.oppdragservice.wsdl.Oppdrag.class, context));
destination.setRequest(oppdragRequest);
}
})
.register();

factory.classMap(OppdragRequest.class, Oppdrag.class)
factory.classMap(Oppdrag.class, no.nav.testnav.oppdragservice.wsdl.Oppdrag.class)
.customize(new CustomMapper<>() {
@Override
public void mapAtoB(OppdragRequest source,
Oppdrag destination,
public void mapAtoB(Oppdrag source,
no.nav.testnav.oppdragservice.wsdl.Oppdrag destination,
MappingContext context) {

if (nonNull(source.getUtbetFrekvens())) {
Expand All @@ -48,10 +48,10 @@ public void mapAtoB(OppdragRequest source,
.byDefault()
.register();

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package no.nav.testnav.oppdragservice.mapper;

import ma.glasnost.orika.CustomMapper;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.MappingContext;
import no.nav.testnav.libs.dto.oppdragservice.v1.Oppdrag;
import no.nav.testnav.libs.dto.oppdragservice.v1.OppdragResponse;
import no.nav.testnav.oppdragservice.wsdl.SendInnOppdragResponse;
import org.springframework.stereotype.Component;

import static java.util.Objects.nonNull;

@Component
public class OppdragResponseMappingStrategy implements MappingStrategy{

@Override
public void register(MapperFactory factory) {

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

var oppdrag = source.getResponse().getOppdrag();
if (nonNull(oppdrag.getUtbetFrekvens())) {
oppdrag.setUtbetFrekvens(oppdrag.getUtbetFrekvens()
.replace("14DG", "_14DG"));
}

oppdrag.getOppdragslinje().stream()
.filter(oppdragslinje -> nonNull(oppdragslinje.getTypeSats()))
.forEach(oppdragslinje ->
oppdragslinje.setTypeSats(oppdragslinje.getTypeSats()
.replace("14DB","_14DB")));

destination.setOppdrag(mapperFacade.map(oppdrag, Oppdrag.class, context));
}
})
.register();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public SendInnOppdragResponse sendInnOppdrag(OppdragRequest oppdragRequest) {
var request = mapperFacade.map(oppdragRequest, SendInnOppdragRequest.class);
var xmlRequest = marshallToXml(request);

oppdragConsumer.sendOppdrag(xmlRequest);
var response = oppdragConsumer.sendOppdrag(request);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package no.nav.testnav.oppdragservice.config;

import jakarta.xml.bind.JAXBException;
import ma.glasnost.orika.MapperFacade;
import no.nav.testnav.oppdragservice.consumer.OppdragConsumer;
import no.nav.testnav.oppdragservice.mapper.LocalDateCustomMapping;
import no.nav.testnav.oppdragservice.mapper.LocalDateTimeCustomMapping;
import no.nav.testnav.oppdragservice.mapper.MapperTestUtils;
import no.nav.testnav.oppdragservice.mapper.OppdragRequestMappingStrategy;
import no.nav.testnav.oppdragservice.service.OppdragService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class TestConfig {

@Bean
public OppdragService oppdragService() throws JAXBException {
return new OppdragService(oppdragConsumer(), mapperFacade());
}

@Bean
public OppdragConsumer oppdragConsumer() {
return new OppdragConsumer();
}

@Bean
public MapperFacade mapperFacade() {

return MapperTestUtils.createMapperFacadeForMappingStrategy(
List.of(new LocalDateCustomMapping(),
new LocalDateTimeCustomMapping()),
new OppdragRequestMappingStrategy());
}
}
Loading

0 comments on commit 7d1b54d

Please sign in to comment.