From f61843431a114a4b1866ecdbe60d6f0a1a5445e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Skaar=2C=20Bj=C3=B8rn-Andre?= Date: Mon, 11 Mar 2024 08:24:29 +0100 Subject: [PATCH] Changed PseudoFuncInput and PseudoFuncOutput to only carry a single value. --- pom.xml | 2 +- .../dlp/pseudo/core/func/DaeadFuncTest.java | 19 ++++++------ .../dlp/pseudo/core/func/Ff31FuncTest.java | 28 ++++++++--------- .../pseudo/core/func/LegacyFpeFuncTest.java | 30 +++++++++---------- .../core/func/PseudoFuncConfigPresetTest.java | 4 +-- .../dlp/pseudo/core/func/RedactFuncTest.java | 2 +- 6 files changed, 43 insertions(+), 42 deletions(-) diff --git a/pom.xml b/pom.xml index fbeb4c7..9880f85 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ statisticsnorway/dapla-dlp-pseudo-core - 1.3.0 + 1.3.1-SNAPSHOT 32.0.0-jre 1.5.1 1.4.6 diff --git a/src/test/java/no/ssb/dlp/pseudo/core/func/DaeadFuncTest.java b/src/test/java/no/ssb/dlp/pseudo/core/func/DaeadFuncTest.java index 919ee86..435a953 100644 --- a/src/test/java/no/ssb/dlp/pseudo/core/func/DaeadFuncTest.java +++ b/src/test/java/no/ssb/dlp/pseudo/core/func/DaeadFuncTest.java @@ -1,12 +1,15 @@ package no.ssb.dlp.pseudo.core.func; -import com.google.common.collect.ImmutableList; import com.google.crypto.tink.CleartextKeysetHandle; import com.google.crypto.tink.DeterministicAead; import com.google.crypto.tink.JsonKeysetReader; import com.google.crypto.tink.KeysetHandle; import com.google.crypto.tink.daead.DeterministicAeadConfig; -import no.ssb.dapla.dlp.pseudo.func.*; +import no.ssb.dapla.dlp.pseudo.func.PseudoFunc; +import no.ssb.dapla.dlp.pseudo.func.PseudoFuncConfig; +import no.ssb.dapla.dlp.pseudo.func.PseudoFuncFactory; +import no.ssb.dapla.dlp.pseudo.func.PseudoFuncInput; +import no.ssb.dapla.dlp.pseudo.func.PseudoFuncOutput; import no.ssb.dapla.dlp.pseudo.func.tink.daead.TinkDaeadFuncConfig; import no.ssb.dapla.dlp.pseudo.func.tink.fpe.TinkFpeFuncConfig; import org.junit.jupiter.api.BeforeAll; @@ -14,7 +17,7 @@ import java.util.Map; -import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.*; public class DaeadFuncTest { @@ -45,13 +48,11 @@ private PseudoFunc f(String funcDecl) throws Exception { return PseudoFuncFactory.create(config); } - private void transformAndRestore(Object originalVal, Object expectedVal, PseudoFunc func) { - Iterable expectedElements = (expectedVal instanceof Iterable) ? (Iterable) expectedVal : ImmutableList.of(expectedVal); - Iterable originalElements = (originalVal instanceof Iterable) ? (Iterable) originalVal : ImmutableList.of(originalVal); + private void transformAndRestore(String originalVal, String expectedVal, PseudoFunc func) { PseudoFuncOutput pseudonymized = func.apply(PseudoFuncInput.of(originalVal)); - assertThat(pseudonymized.getValues()).containsExactlyElementsOf(expectedElements); - PseudoFuncOutput depseudonymized = func.restore(PseudoFuncInput.of(pseudonymized.getValues())); - assertThat(depseudonymized.getValues()).containsExactlyElementsOf(originalElements); + assertThat(pseudonymized.getValue()).isEqualTo(expectedVal); + PseudoFuncOutput depseudonymized = func.restore(PseudoFuncInput.of(pseudonymized.getValue())); + assertThat(depseudonymized.getValue()).isEqualTo(originalVal); } @Test diff --git a/src/test/java/no/ssb/dlp/pseudo/core/func/Ff31FuncTest.java b/src/test/java/no/ssb/dlp/pseudo/core/func/Ff31FuncTest.java index fcc0599..6e3286e 100644 --- a/src/test/java/no/ssb/dlp/pseudo/core/func/Ff31FuncTest.java +++ b/src/test/java/no/ssb/dlp/pseudo/core/func/Ff31FuncTest.java @@ -1,21 +1,23 @@ package no.ssb.dlp.pseudo.core.func; -import com.google.common.collect.ImmutableList; import com.google.crypto.tink.CleartextKeysetHandle; import com.google.crypto.tink.JsonKeysetReader; import com.google.crypto.tink.KeysetHandle; import no.ssb.crypto.tink.fpe.Fpe; import no.ssb.crypto.tink.fpe.FpeConfig; import no.ssb.crypto.tink.fpe.IncompatiblePlaintextException; -import no.ssb.dapla.dlp.pseudo.func.*; +import no.ssb.dapla.dlp.pseudo.func.PseudoFunc; +import no.ssb.dapla.dlp.pseudo.func.PseudoFuncConfig; +import no.ssb.dapla.dlp.pseudo.func.PseudoFuncFactory; +import no.ssb.dapla.dlp.pseudo.func.PseudoFuncInput; +import no.ssb.dapla.dlp.pseudo.func.PseudoFuncOutput; import no.ssb.dapla.dlp.pseudo.func.tink.fpe.TinkFpeFuncConfig; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import java.util.Map; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.*; public class Ff31FuncTest { @@ -46,13 +48,11 @@ private PseudoFunc f(String funcDecl) throws Exception { return PseudoFuncFactory.create(config); } - private void transformAndRestore(Object originalVal, Object expectedVal, PseudoFunc func) { - Iterable expectedElements = (expectedVal instanceof Iterable) ? (Iterable) expectedVal : ImmutableList.of(expectedVal); - Iterable originalElements = (originalVal instanceof Iterable) ? (Iterable) originalVal : ImmutableList.of(originalVal); + private void transformAndRestore(String originalVal, String expectedVal, PseudoFunc func) { PseudoFuncOutput pseudonymized = func.apply(PseudoFuncInput.of(originalVal)); - assertThat(pseudonymized.getValues()).containsExactlyElementsOf(expectedElements); - PseudoFuncOutput depseudonymized = func.restore(PseudoFuncInput.of(pseudonymized.getValues())); - assertThat(depseudonymized.getValues()).containsExactlyElementsOf(originalElements); + assertThat(pseudonymized.getValue()).isEqualTo(expectedVal); + PseudoFuncOutput depseudonymized = func.restore(PseudoFuncInput.of(pseudonymized.getValue())); + assertThat(depseudonymized.getValue()).isEqualTo(originalVal); } @Test @@ -82,10 +82,10 @@ void givenText_ff31Delete_shouldEncryptAndDecrypt() throws Exception { PseudoFunc func = f(funcDeclStr); PseudoFuncOutput pseudonymized = func.apply(PseudoFuncInput.of("Ken sent me...")); - assertThat(pseudonymized.getFirstValue()).isEqualTo("6DyNHKvig"); + assertThat(pseudonymized.getValue()).isEqualTo("6DyNHKvig"); PseudoFuncOutput depseudonymized = func.restore(PseudoFuncInput.of("6DyNHKvig")); - assertThat(depseudonymized.getFirstValue()).isEqualTo("Kensentme"); + assertThat(depseudonymized.getValue()).isEqualTo("Kensentme"); } @Test @@ -94,10 +94,10 @@ void givenText_ff31Redact_shouldEncryptAndDecrypt() throws Exception { PseudoFunc func = f(funcDeclStr); PseudoFuncOutput pseudonymized = func.apply(PseudoFuncInput.of("Ken sent me...")); - assertThat(pseudonymized.getFirstValue()).isEqualTo("3WD8UlZRDER1z5"); + assertThat(pseudonymized.getValue()).isEqualTo("3WD8UlZRDER1z5"); PseudoFuncOutput depseudonymized = func.restore(PseudoFuncInput.of("3WD8UlZRDER1z5")); - assertThat(depseudonymized.getFirstValue()).isEqualTo("KenZsentZmeZZZ"); + assertThat(depseudonymized.getValue()).isEqualTo("KenZsentZmeZZZ"); } diff --git a/src/test/java/no/ssb/dlp/pseudo/core/func/LegacyFpeFuncTest.java b/src/test/java/no/ssb/dlp/pseudo/core/func/LegacyFpeFuncTest.java index 5035ba3..e6f9618 100644 --- a/src/test/java/no/ssb/dlp/pseudo/core/func/LegacyFpeFuncTest.java +++ b/src/test/java/no/ssb/dlp/pseudo/core/func/LegacyFpeFuncTest.java @@ -1,15 +1,17 @@ package no.ssb.dlp.pseudo.core.func; -import com.google.common.collect.ImmutableList; -import no.ssb.dapla.dlp.pseudo.func.*; +import no.ssb.dapla.dlp.pseudo.func.PseudoFunc; +import no.ssb.dapla.dlp.pseudo.func.PseudoFuncConfig; +import no.ssb.dapla.dlp.pseudo.func.PseudoFuncFactory; +import no.ssb.dapla.dlp.pseudo.func.PseudoFuncInput; +import no.ssb.dapla.dlp.pseudo.func.PseudoFuncOutput; import no.ssb.dapla.dlp.pseudo.func.fpe.FpeFunc; import no.ssb.dapla.dlp.pseudo.func.fpe.FpeFuncConfig; import org.junit.jupiter.api.Test; import java.util.Map; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.*; public class LegacyFpeFuncTest { @@ -17,36 +19,34 @@ public class LegacyFpeFuncTest { "secret1", "C5sn7B4YtwcilAwuVx6NuAsMWLusOSA/ldia40ZugDI=" ); - private PseudoFunc f(String funcDecl) throws Exception { + private PseudoFunc f(String funcDecl) { PseudoFuncConfig config = PseudoFuncConfigFactory.get(funcDecl); String keyId = config.getRequired(FpeFuncConfig.Param.KEY_ID, String.class); config.add(FpeFuncConfig.Param.KEY_DATA, KEYSETS.get(keyId)); return PseudoFuncFactory.create(config); } - private void transformAndRestore(Object originalVal, Object expectedVal, PseudoFunc func) { - Iterable expectedElements = (expectedVal instanceof Iterable) ? (Iterable) expectedVal : ImmutableList.of(expectedVal); - Iterable originalElements = (originalVal instanceof Iterable) ? (Iterable) originalVal : ImmutableList.of(originalVal); + private void transformAndRestore(String originalVal, String expectedVal, PseudoFunc func) { PseudoFuncOutput pseudonymized = func.apply(PseudoFuncInput.of(originalVal)); - assertThat(pseudonymized.getValues()).containsExactlyElementsOf(expectedElements); - PseudoFuncOutput depseudonymized = func.restore(PseudoFuncInput.of(pseudonymized.getValues())); - assertThat(depseudonymized.getValues()).containsExactlyElementsOf(originalElements); + assertThat(pseudonymized.getValue()).isEqualTo(expectedVal); + PseudoFuncOutput depseudonymized = func.restore(PseudoFuncInput.of(pseudonymized.getValue())); + assertThat(depseudonymized.getValue()).isEqualTo(originalVal); } @Test - void givenText_fpeAnychar_shouldEncryptAndDecrypt() throws Exception { + void givenText_fpeAnychar_shouldEncryptAndDecrypt() { String funcDeclStr = "fpe-anychar(keyId=secret1)"; transformAndRestore("Something", "-Æ'GÕT@«L", f(funcDeclStr)); } @Test - void givenText_fpeCustomAlphabet_shouldEncryptAndDecrypt() throws Exception { + void givenText_fpeCustomAlphabet_shouldEncryptAndDecrypt() { String funcDeclStr = "fpe-abcdefghij(keyId=secret1)"; transformAndRestore("abcdef", "djcjbf", f(funcDeclStr)); } @Test - void givenNonAlphabetText_fpeCustomAlphabet_shouldFail() throws Exception { + void givenNonAlphabetText_fpeCustomAlphabet_shouldFail() { String funcDeclStr = "fpe-abcdefghij(keyId=secret1, replaceIllegalChars=false, replaceIllegalCharsWith=X)"; assertThatThrownBy(() -> { f(funcDeclStr).apply(PseudoFuncInput.of("abcHELLO")); @@ -56,7 +56,7 @@ void givenNonAlphabetText_fpeCustomAlphabet_shouldFail() throws Exception { } @Test - void givenDigits_fpeDigits_shouldEncryptAndDecrypt() throws Exception { + void givenDigits_fpeDigits_shouldEncryptAndDecrypt() { String funcDeclStr = "fpe-digits(keyId=secret1)"; transformAndRestore("1234567890", "7830880047", f(funcDeclStr)); } diff --git a/src/test/java/no/ssb/dlp/pseudo/core/func/PseudoFuncConfigPresetTest.java b/src/test/java/no/ssb/dlp/pseudo/core/func/PseudoFuncConfigPresetTest.java index 3db2465..44c20f0 100644 --- a/src/test/java/no/ssb/dlp/pseudo/core/func/PseudoFuncConfigPresetTest.java +++ b/src/test/java/no/ssb/dlp/pseudo/core/func/PseudoFuncConfigPresetTest.java @@ -29,12 +29,12 @@ public String getAlgorithm() { @Override public PseudoFuncOutput apply(PseudoFuncInput input) { - return new PseudoFuncOutput(); + return new PseudoFuncOutput(null); } @Override public PseudoFuncOutput restore(PseudoFuncInput input) { - return new PseudoFuncOutput(); + return new PseudoFuncOutput(null); } } diff --git a/src/test/java/no/ssb/dlp/pseudo/core/func/RedactFuncTest.java b/src/test/java/no/ssb/dlp/pseudo/core/func/RedactFuncTest.java index d7e6a7d..781f2c5 100644 --- a/src/test/java/no/ssb/dlp/pseudo/core/func/RedactFuncTest.java +++ b/src/test/java/no/ssb/dlp/pseudo/core/func/RedactFuncTest.java @@ -13,7 +13,7 @@ private PseudoFunc f(String funcDecl) { } static void assertEqual(PseudoFuncOutput out, Object expected) { - assertThat(out.getFirstValue()).isEqualTo(expected); + assertThat(out.getValue()).isEqualTo(expected); } @Test