Skip to content

Commit

Permalink
Simplify XML request preparation in IdentService
Browse files Browse the repository at this point in the history
Reworked the IdentService to simplify the preparation of XML requests, removing several static constants and the marshallToXML() method altogether. The new approach formats the XML string directly in the prepareRequest() method, streamlining the process and making the code easier to understand.
  • Loading branch information
krharum committed Feb 28, 2024
1 parent 701ac0a commit 7e070c9
Showing 1 changed file with 19 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.JAXBException;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import no.nav.testnav.apps.tpsmessagingservice.consumer.ServicerutineConsumer;
Expand All @@ -16,14 +13,9 @@
import no.nav.testnav.apps.tpsmessagingservice.exception.BadRequestException;
import no.nav.testnav.apps.tpsmessagingservice.utils.EndringsmeldingUtil;
import no.nav.testnav.libs.data.tpsmessagingservice.v1.TpsIdentStatusDTO;
import no.nav.tps.ctg.m201.domain.SRnavn;
import no.nav.tps.ctg.m201.domain.TpsPersonData;
import no.nav.tps.ctg.m201.domain.TpsServiceRutineType;
import org.slf4j.event.Level;
import org.springframework.stereotype.Service;

import javax.xml.namespace.QName;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -37,26 +29,20 @@
@Service
public class IdentService {

private static final String SERVICE_NAME_OLD = "FS_03_FDLISTER_DISKNAVN_M";
private static final String SERVICE_NAME_NEW = "FS03-FDLISTER-DISKNAVN-M";
private static final String EMPTY_FNR = "<NFnr/>";
private static final String ZERO_COUNT = "<antallFnr>0</antallFnr>";
private static final int MAX_LIMIT = 80;
private static final String BAD_REQUEST = "Antall identer kan ikke være større enn " + MAX_LIMIT;
private static final String PROD = "p";
private static final String PROD_LIKE_ENV = "q2";

private final ServicerutineConsumer servicerutineConsumer;
private final JAXBContext requestContext;
private final TestmiljoerServiceConsumer testmiljoerServiceConsumer;
private final XmlMapper xmlMapper;

public IdentService(ServicerutineConsumer servicerutineConsumer,
TestmiljoerServiceConsumer testmiljoerServiceConsumer)
throws JAXBException {
TestmiljoerServiceConsumer testmiljoerServiceConsumer) {

this.servicerutineConsumer = servicerutineConsumer;
this.testmiljoerServiceConsumer = testmiljoerServiceConsumer;
this.requestContext = JAXBContext.newInstance(TpsPersonData.class);
this.xmlMapper = XmlMapper
.builder()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
Expand Down Expand Up @@ -96,18 +82,6 @@ public List<TpsIdentStatusDTO> getIdenter(List<String> identer, List<String> mil
.toList();
}

@SneakyThrows
public static String marshallToXML(JAXBContext requestContext, TpsPersonData endringsmelding) {

var marshaller = requestContext.createMarshaller();

var writer = new StringWriter();
JAXBElement<TpsPersonData> element = new JAXBElement<>(new QName("", "tpsPersonData"), TpsPersonData.class, endringsmelding);
marshaller.marshal(element, writer);

return writer.toString();
}

private boolean exists(String ident, TpsServicerutineM201Response response) {

return nonNull(response.getTpsSvar()) &&
Expand All @@ -119,16 +93,7 @@ private boolean exists(String ident, TpsServicerutineM201Response response) {

private Map<String, TpsServicerutineM201Response> readFromTps(List<String> identer, List<String> miljoer, boolean isProd) {

var request = prepareRequest(identer, isProd);
var xmlRequest = marshallToXML(requestContext, request)
.replace(SERVICE_NAME_OLD, SERVICE_NAME_NEW);

if (xmlRequest.contains(EMPTY_FNR) && !xmlRequest.contains(ZERO_COUNT)) {
xmlRequest = xmlRequest
.replace(EMPTY_FNR, "<nFnr>%s</nFnr>".formatted(identer.stream()
.map("<fnr>%s</fnr>"::formatted)
.collect(Collectors.joining(""))));
}
var xmlRequest = prepareRequest(identer, isProd);

log.info("M201 request: {}", xmlRequest);

Expand Down Expand Up @@ -162,17 +127,21 @@ private TpsServicerutineM201Response unmarshallFromXml(String endringsmeldingRes
}
}

private TpsPersonData prepareRequest(List<String> identer, boolean isProd) {

var request = new TpsPersonData();
request.setTpsServiceRutine(new TpsServiceRutineType());
request.getTpsServiceRutine().setServiceRutinenavn(SRnavn.FS_03_FDLISTER_DISKNAVN_M);
request.getTpsServiceRutine().setAksjonsKode("A");
request.getTpsServiceRutine().setAksjonsKode2(isProd ? "2" : "0");
request.getTpsServiceRutine().setAntallFnr(Integer.toString(identer.size()));
request.getTpsServiceRutine().setNFnr(new TpsServiceRutineType.NFnr());
request.getTpsServiceRutine().getNFnr().getFnr().addAll(identer);

return request;
private String prepareRequest(List<String> identer, boolean isProd) {

return "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<tpsPersonData>" +
"<tpsServiceRutine>" +
"<serviceRutinenavn>FS03-FDLISTER-DISKNAVN-M</serviceRutinenavn>" +
"<aksjonsKode>A</aksjonsKode>" +
"<aksjonsKode2>%s</aksjonsKode2>" +
"<antallFnr>%s</antallFnr>" +
"<nFnr>%s</nFnr>" +
"</tpsServiceRutine>" +
"</tpsPersonData>".formatted(isProd ? "2" : "0",
Integer.toString(identer.size()),
identer.stream()
.map("<fnr>%s</fnr>"::formatted)
.collect(Collectors.joining()));
}
}

0 comments on commit 7e070c9

Please sign in to comment.