From fabb023a763ac5647f37c80eb464ea17e7ecb50a Mon Sep 17 00:00:00 2001 From: rpk Date: Thu, 14 Mar 2024 10:38:31 +0100 Subject: [PATCH 1/2] Fixed check for null object returned from lookup service. Added changes to support new map-sid --- .../service/pseudo/RecordMapProcessorFactory.java | 9 +++++---- .../ssb/dlp/pseudo/service/sid/MultiSidResponse.java | 8 ++++++-- .../pseudo/service/sid/ExternalSidServiceTest.java | 11 ++++++----- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/main/java/no/ssb/dlp/pseudo/service/pseudo/RecordMapProcessorFactory.java b/src/main/java/no/ssb/dlp/pseudo/service/pseudo/RecordMapProcessorFactory.java index e3b6f3a..877b535 100644 --- a/src/main/java/no/ssb/dlp/pseudo/service/pseudo/RecordMapProcessorFactory.java +++ b/src/main/java/no/ssb/dlp/pseudo/service/pseudo/RecordMapProcessorFactory.java @@ -4,6 +4,7 @@ import lombok.RequiredArgsConstructor; import no.ssb.dapla.dlp.pseudo.func.PseudoFuncInput; import no.ssb.dapla.dlp.pseudo.func.PseudoFuncOutput; +import no.ssb.dapla.dlp.pseudo.func.TransformDirection; import no.ssb.dapla.dlp.pseudo.func.fpe.FpeFunc; import no.ssb.dapla.dlp.pseudo.func.map.MapFunc; import no.ssb.dapla.dlp.pseudo.func.map.MapFuncConfig; @@ -44,7 +45,7 @@ public RecordMapProcessor newPseudonymizeRecordProcesso for (PseudoConfig config : pseudoConfigs) { final PseudoFuncs fieldPseudonymizer = newPseudoFuncs(config.getRules(), pseudoKeysetsOf(config.getKeysets())); - chain.preprocessor((f, v) -> init(fieldPseudonymizer, f, v)); + chain.preprocessor((f, v) -> init(fieldPseudonymizer, f, v, TransformDirection.APPLY)); chain.register((f, v) -> process(PSEUDONYMIZE, fieldPseudonymizer, f, v, metadataProcessor)); } return new RecordMapProcessor<>(chain, metadataProcessor); @@ -57,7 +58,7 @@ public RecordMapProcessor newDepseudonymizeRecordProces for (PseudoConfig config : pseudoConfigs) { final PseudoFuncs fieldDepseudonymizer = newPseudoFuncs(config.getRules(), pseudoKeysetsOf(config.getKeysets())); - chain.preprocessor((f, v) -> init(fieldDepseudonymizer, f, v)); + chain.preprocessor((f, v) -> init(fieldDepseudonymizer, f, v, TransformDirection.RESTORE)); chain.register((f, v) -> process(DEPSEUDONYMIZE, fieldDepseudonymizer, f, v, metadataProcessor)); } @@ -83,10 +84,10 @@ protected PseudoFuncs newPseudoFuncs(Collection rules, return new PseudoFuncs(rules, pseudoSecrets.resolve(), keysets); } - private String init(PseudoFuncs pseudoFuncs, FieldDescriptor field, String varValue) { + private String init(PseudoFuncs pseudoFuncs, FieldDescriptor field, String varValue, TransformDirection direction) { if (varValue != null) { pseudoFuncs.findPseudoFunc(field).ifPresent(pseudoFunc -> - pseudoFunc.getFunc().init(PseudoFuncInput.of(varValue))); + pseudoFunc.getFunc().init(PseudoFuncInput.of(varValue), direction)); } return varValue; } diff --git a/src/main/java/no/ssb/dlp/pseudo/service/sid/MultiSidResponse.java b/src/main/java/no/ssb/dlp/pseudo/service/sid/MultiSidResponse.java index 0a1f1ce..f269f02 100644 --- a/src/main/java/no/ssb/dlp/pseudo/service/sid/MultiSidResponse.java +++ b/src/main/java/no/ssb/dlp/pseudo/service/sid/MultiSidResponse.java @@ -26,11 +26,15 @@ public class MultiSidResponse { @Jacksonized @Introspected @Serdeable - public record Mapping (List fnrList, List snr, List fnr) { } + public record Mapping (List fnrList, List snrList, List snr, List fnr) { } public Map toMap() { Map result = new HashMap<>(); - for (ListIterator it = getMapping().fnrList().listIterator(); it.hasNext(); ) { + + ListIterator identifierIterator = getMapping().fnrList != null ? + getMapping().fnrList().listIterator() : getMapping().snrList().listIterator(); + + for (ListIterator it = identifierIterator; it.hasNext(); ) { int index = it.nextIndex(); result.put(it.next(), new SidInfo.SidInfoBuilder() .snr(getMapping().snr().get(index)) diff --git a/src/test/java/no/ssb/dlp/pseudo/service/sid/ExternalSidServiceTest.java b/src/test/java/no/ssb/dlp/pseudo/service/sid/ExternalSidServiceTest.java index 1a5e414..e7b5f6c 100644 --- a/src/test/java/no/ssb/dlp/pseudo/service/sid/ExternalSidServiceTest.java +++ b/src/test/java/no/ssb/dlp/pseudo/service/sid/ExternalSidServiceTest.java @@ -37,9 +37,9 @@ public void testInvokeMultiFnr() { // sidService should call our sidClient mock when(sidClient.lookup(any(MultiSidRequest.class))).thenReturn(Publishers.just( MultiSidResponse.builder().mapping(MultiSidResponse.Mapping.builder() - .fnrList(Arrays.asList("11854898347")) - .fnr(Arrays.asList("11854898347")) - .snr(Arrays.asList("0001ha3")) + .fnrList(Arrays.asList("01839899544","11854898347")) + .fnr(Arrays.asList("01839899544","11854898347")) + .snr(Arrays.asList("0006kh2","0001ha3")) .build()).build()) ); sidService.lookupFnr(List.of("11854898347"), Optional.empty()); @@ -62,8 +62,9 @@ public void testInvokeMultiSnr() { // sidService should call our sidClient mock when(sidClient.lookup(any(MultiSidRequest.class))).thenReturn(Publishers.just( MultiSidResponse.builder().mapping(MultiSidResponse.Mapping.builder() - .snr(List.of("0001ha3", "0006kh2")) - .fnr(List.of("11854898347")) + .snrList(Arrays.asList("0006kh2", "0001ha3")) + .snr(List.of("0006kh2", "0001ha3")) + .fnr(List.of("01839899544","11854898347")) .build()).build()) ); sidService.lookupSnr(List.of("0001ha3"),Optional.empty()); From 650acbe87a98d459f34b97bb13d78af06055a44a Mon Sep 17 00:00:00 2001 From: rpk Date: Thu, 14 Mar 2024 10:50:52 +0100 Subject: [PATCH 2/2] Corrected passed parameters --- .../pseudo/service/pseudo/RecordMapProcessorFactory.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/no/ssb/dlp/pseudo/service/pseudo/RecordMapProcessorFactory.java b/src/main/java/no/ssb/dlp/pseudo/service/pseudo/RecordMapProcessorFactory.java index 877b535..b5cb74d 100644 --- a/src/main/java/no/ssb/dlp/pseudo/service/pseudo/RecordMapProcessorFactory.java +++ b/src/main/java/no/ssb/dlp/pseudo/service/pseudo/RecordMapProcessorFactory.java @@ -45,7 +45,7 @@ public RecordMapProcessor newPseudonymizeRecordProcesso for (PseudoConfig config : pseudoConfigs) { final PseudoFuncs fieldPseudonymizer = newPseudoFuncs(config.getRules(), pseudoKeysetsOf(config.getKeysets())); - chain.preprocessor((f, v) -> init(fieldPseudonymizer, f, v, TransformDirection.APPLY)); + chain.preprocessor((f, v) -> init(fieldPseudonymizer,TransformDirection.APPLY, f, v)); chain.register((f, v) -> process(PSEUDONYMIZE, fieldPseudonymizer, f, v, metadataProcessor)); } return new RecordMapProcessor<>(chain, metadataProcessor); @@ -58,7 +58,7 @@ public RecordMapProcessor newDepseudonymizeRecordProces for (PseudoConfig config : pseudoConfigs) { final PseudoFuncs fieldDepseudonymizer = newPseudoFuncs(config.getRules(), pseudoKeysetsOf(config.getKeysets())); - chain.preprocessor((f, v) -> init(fieldDepseudonymizer, f, v, TransformDirection.RESTORE)); + chain.preprocessor((f, v) -> init(fieldDepseudonymizer, TransformDirection.RESTORE, f, v)); chain.register((f, v) -> process(DEPSEUDONYMIZE, fieldDepseudonymizer, f, v, metadataProcessor)); } @@ -84,7 +84,7 @@ protected PseudoFuncs newPseudoFuncs(Collection rules, return new PseudoFuncs(rules, pseudoSecrets.resolve(), keysets); } - private String init(PseudoFuncs pseudoFuncs, FieldDescriptor field, String varValue, TransformDirection direction) { + private String init(PseudoFuncs pseudoFuncs, TransformDirection direction, FieldDescriptor field, String varValue) { if (varValue != null) { pseudoFuncs.findPseudoFunc(field).ifPresent(pseudoFunc -> pseudoFunc.getFunc().init(PseudoFuncInput.of(varValue), direction));