Skip to content

Commit

Permalink
Refactor TpsServicerutineM201Response and update IdentService #deploy…
Browse files Browse the repository at this point in the history
…-tps-messaging-service

The TpsServicerutineM201Response class has been simplified by removing the TpsPersonData inner class and moving its variables up a level. The XmlType, XmlRootElement, and JacksonXmlProperty annotations have been added for XML conversion. Meanwhile, the IdentService class now uses an XmlMapper instead of an ObjectMapper for parsing XML responses.
  • Loading branch information
krharum committed Mar 7, 2024
1 parent 43f318d commit 18af015
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlCData;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -18,26 +21,19 @@
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@XmlRootElement(name = "tpsPersonData")
@XmlType(propOrder = {"tpsServiceRutine", "tpsSvar"})
public class TpsServicerutineM201Response {

private TpsPersonData tpsPersonData;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public static class TpsPersonData {

private TpsServiceRutineType tpsServiceRutine;
private TpsSvar tpsSvar;
}
private TpsServiceRutineType tpsServiceRutine;
private TpsSvar tpsSvar;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@XmlType(propOrder = {"svarStatus", "personDataM201"})
public static class TpsSvar {

private TpsMeldingResponse svarStatus;
Expand All @@ -49,28 +45,31 @@ public static class TpsSvar {
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@XmlType(propOrder = {"antallFM201", "AFnr"})
public static class PersondataFraTpsM201 {

private int antallFM201;
private AFnr AFnr;
private AFnr aFnr;
}

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@XmlType(propOrder = "EFnr")
public static class AFnr {

@JacksonXmlCData
@JacksonXmlElementWrapper(useWrapping = false)
private List<EFnr> EFnr;
@JacksonXmlProperty(localName = "eFnr")
private List<EFnr> eFnr;

public List<EFnr> getEFnr() {
if (Objects.isNull(EFnr)) {
EFnr = new ArrayList<>();
if (Objects.isNull(eFnr)) {
eFnr = new ArrayList<>();
}
return EFnr;
return eFnr;
}
}

Expand All @@ -79,6 +78,7 @@ public List<EFnr> getEFnr() {
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@XmlType(propOrder = {"fnr", "sp", "kn", "fn", "mn", "en", "svarStatus"})
public static class EFnr {

private String fnr;
Expand All @@ -89,4 +89,4 @@ public static class EFnr {
private String en;
private TpsMeldingResponse svarStatus;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package no.nav.testnav.apps.tpsmessagingservice.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import no.nav.testnav.apps.tpsmessagingservice.consumer.ServicerutineConsumer;
Expand All @@ -10,7 +13,6 @@
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 org.json.XML;
import org.springframework.stereotype.Service;

import java.util.HashMap;
Expand All @@ -33,15 +35,22 @@ public class IdentService {

private final ServicerutineConsumer servicerutineConsumer;
private final TestmiljoerServiceConsumer testmiljoerServiceConsumer;
private final ObjectMapper objectMapper;
private final XmlMapper xmlMapper;

public IdentService(ServicerutineConsumer servicerutineConsumer,
TestmiljoerServiceConsumer testmiljoerServiceConsumer,
ObjectMapper objectMapper) {
TestmiljoerServiceConsumer testmiljoerServiceConsumer) {

this.servicerutineConsumer = servicerutineConsumer;
this.testmiljoerServiceConsumer = testmiljoerServiceConsumer;
this.objectMapper = objectMapper;
this.xmlMapper = XmlMapper
.builder()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true)
.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true)
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.build();
}

public List<TpsIdentStatusDTO> getIdenter(List<String> identer, List<String> miljoer, Boolean includeProd) {
Expand Down Expand Up @@ -74,10 +83,10 @@ public List<TpsIdentStatusDTO> getIdenter(List<String> identer, List<String> mil

private boolean exists(String ident, TpsServicerutineM201Response response) {

return nonNull(response.getTpsPersonData()) && nonNull(response.getTpsPersonData().getTpsSvar()) &&
nonNull(response.getTpsPersonData().getTpsSvar().getPersonDataM201()) &&
nonNull(response.getTpsPersonData().getTpsSvar().getPersonDataM201().getAFnr()) &&
response.getTpsPersonData().getTpsSvar().getPersonDataM201().getAFnr().getEFnr().stream()
return nonNull(response.getTpsSvar()) &&
nonNull(response.getTpsSvar().getPersonDataM201()) &&
nonNull(response.getTpsSvar().getPersonDataM201().getAFnr()) &&
response.getTpsSvar().getPersonDataM201().getAFnr().getEFnr().stream()
.anyMatch(eFnr -> ident.equals(eFnr.getFnr()) && isNull(eFnr.getSvarStatus()));
}

Expand All @@ -102,16 +111,13 @@ private TpsServicerutineM201Response unmarshallFromXml(String endringsmeldingRes
if (TpsMeldingCommand.NO_RESPONSE.equals(endringsmeldingResponse)) {

return TpsServicerutineM201Response.builder()
.tpsPersonData(TpsServicerutineM201Response.TpsPersonData.builder()
.tpsSvar(TpsServicerutineM201Response.TpsSvar.builder()
.svarStatus(EndringsmeldingUtil.getNoAnswerStatus())
.build())
.tpsSvar(TpsServicerutineM201Response.TpsSvar.builder()
.svarStatus(EndringsmeldingUtil.getNoAnswerStatus())
.build())
.build();
} else {

var jsonRoot = XML.toJSONObject(endringsmeldingResponse);
return objectMapper.readValue(jsonRoot.toString(), TpsServicerutineM201Response.class);
return xmlMapper.readValue(endringsmeldingResponse, TpsServicerutineM201Response.class);
}
}

Expand Down

0 comments on commit 18af015

Please sign in to comment.