diff options
2 files changed, 956 insertions, 3 deletions
diff --git a/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/test/clients/ErnpRestClientTest.java b/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/test/clients/ErnpRestClientTest.java index 9eb574fd..ab1a502c 100644 --- a/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/test/clients/ErnpRestClientTest.java +++ b/eidas_modules/authmodule-eIDAS-v2/src/test/java/at/asitplus/eidas/specific/modules/auth/eidas/v2/test/clients/ErnpRestClientTest.java @@ -3,10 +3,20 @@ package at.asitplus.eidas.specific.modules.auth.eidas.v2.test.clients; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +import java.net.HttpURLConnection; +import java.util.Arrays; +import java.util.Iterator; +import java.util.UUID; +import java.util.concurrent.TimeUnit; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.RandomStringUtils; import org.junit.AfterClass; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; @@ -16,16 +26,22 @@ import org.springframework.test.annotation.DirtiesContext.ClassMode; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + import at.asitplus.eidas.specific.connector.test.config.dummy.MsConnectorDummyConfigMap; import at.asitplus.eidas.specific.modules.auth.eidas.v2.clients.ernp.ErnpRestClient.ErnpRegisterResult; import at.asitplus.eidas.specific.modules.auth.eidas.v2.clients.ernp.IErnpClient; import at.asitplus.eidas.specific.modules.auth.eidas.v2.dao.RegisterResult; import at.asitplus.eidas.specific.modules.auth.eidas.v2.dao.SimpleEidasData; import at.asitplus.eidas.specific.modules.auth.eidas.v2.exception.EidasSAuthenticationException; +import at.asitplus.eidas.specific.modules.auth.eidas.v2.handler.DeSpecificDetailSearchProcessor; +import at.gv.egiz.eaaf.core.impl.utils.TransactionIdUtils; import lombok.SneakyThrows; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import okhttp3.mockwebserver.RecordedRequest; +import okhttp3.mockwebserver.SocketPolicy; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { @@ -36,7 +52,8 @@ public class ErnpRestClientTest { @Autowired MsConnectorDummyConfigMap basicConfig; @Autowired IErnpClient client; - + + private static ObjectMapper mapper = new ObjectMapper(); private static MockWebServer mockWebServer; /** @@ -59,6 +76,19 @@ public class ErnpRestClientTest { } + /** + * jUnit test initializer. + * + * @throws InterruptedException in case of an error + */ + @Before + public void initialize() throws InterruptedException { + mockWebServer.takeRequest(2, TimeUnit.MILLISECONDS); + TransactionIdUtils.setTransactionId(UUID.randomUUID().toString()); + + } + + @Test @SneakyThrows public void searchWithPersonalIdentifierServerError() { @@ -82,6 +112,7 @@ public class ErnpRestClientTest { () -> client.searchWithPersonIdentifier( eidasDataFirst.getPseudonym(), eidasDataFirst.getCitizenCountryCode())); + mockWebServer.takeRequest(); assertEquals("wrong errorCode", "module.eidasauth.matching.11", error.getErrorId()); } @@ -122,9 +153,931 @@ public class ErnpRestClientTest { assertNotNull("no ERnP response", resp); assertEquals("wrong resp size", 1, resp.getPersonResult().size()); + + } + + @Test + @SneakyThrows + public void searchResidence() { + // execute operation + ErnpRegisterResult resp = client.searchWithResidenceData(null, null, null, null, null, null); + assertNotNull("no ERnP response", resp); + assertEquals("wrong resp size", 0, resp.getPersonResult().size()); + + } + + @Test + @SneakyThrows + public void searchWithMdsNoResponse() { + final String cc = "CZ"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc); + + mockWebServer.enqueue(new MockResponse() + .setSocketPolicy(SocketPolicy.NO_RESPONSE) + .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); + + // execute operation + EidasSAuthenticationException error = assertThrows("wrong Exception", EidasSAuthenticationException.class, + () -> client.searchWithMds(eidasDataFirst.getGivenName(), eidasDataFirst.getFamilyName(), eidasDataFirst.getDateOfBirth(), cc)); + assertEquals("wrong errorCode", "module.eidasauth.matching.11", error.getErrorId()); + mockWebServer.takeRequest(); + + + } + + @Test + @SneakyThrows + public void searchWithMdsErrorResponse() { + final String cc = "CZ"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(400) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/error_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + // execute operation + EidasSAuthenticationException error = assertThrows("wrong Exception", EidasSAuthenticationException.class, + () -> client.searchWithMds(eidasDataFirst.getGivenName(), eidasDataFirst.getFamilyName(), eidasDataFirst.getDateOfBirth(), cc)); + assertEquals("wrong errorCode", "module.eidasauth.matching.11", error.getErrorId()); + mockWebServer.takeRequest(); + + } + + @Test + @SneakyThrows + public void searchWithMdsNoResult() { + final String cc = "CZ"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/ernp_empty_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + // execute operation + ErnpRegisterResult resp = client.searchWithMds(eidasDataFirst.getGivenName(), eidasDataFirst.getFamilyName(), eidasDataFirst.getDateOfBirth(), cc); + + // validate request + final RecordedRequest request = mockWebServer.takeRequest(); + String reqBody = request.getBody().readUtf8(); + assertFalse("no request body", reqBody.isEmpty()); + JsonNode reqJson = mapper.readTree(reqBody); + checkSearchOptions(reqJson, "Searching with MDS only"); + JsonNode person = getJsonObject(reqJson, "suchdaten"); + checkJsonElement(person, "familienname", eidasDataFirst.getFamilyName()); + checkJsonElement(person, "vorname", eidasDataFirst.getGivenName()); + checkPersonDateOfBirth(person, eidasDataFirst.getDateOfBirth()); + + // validate state + assertNotNull("no ERnP response", resp); + assertEquals("wrong resp size", 0, resp.getPersonResult().size()); + + } + + @Test + @SneakyThrows + public void searchWithMdsSingleResult() { + final String cc = "CZ"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/3_search_with_mds_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + // execute operation + ErnpRegisterResult resp = client.searchWithMds(eidasDataFirst.getGivenName(), eidasDataFirst.getFamilyName(), eidasDataFirst.getDateOfBirth(), cc); + + // validate state + mockWebServer.takeRequest(); + assertNotNull("no ERnP response", resp); + assertEquals("wrong resp size", 1, resp.getPersonResult().size()); RegisterResult persInfo = resp.getPersonResult().get(0); - assertEquals("wrong familyname", "XXXSZR", persInfo.getFamilyName()); + assertEquals("wrong familyname", "DOPISNÍ", persInfo.getFamilyName()); + assertEquals("wrong givenName", "DANA", persInfo.getGivenName()); + assertEquals("wrong dateOfBirth", "1996-01-01", persInfo.getDateOfBirth()); + assertEquals("wrong bpk", "vypyCkyczK7i+cgPWlJasuJphIA=", persInfo.getBpk()); + assertEquals("wrong pseudonym", "7cEYWithDEElementsasdfsafsaf4CDVzNT4E7cjkU4VqForjUnit", persInfo.getPseudonym().get(0)); + assertNull("placeOfBirth", persInfo.getPlaceOfBirth()); + assertNull("birthName", persInfo.getBirthName()); + } + + @Test + @SneakyThrows + public void searchWithMdsMultiResult() { + final String cc = "CZ"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/4_search_with_mds_multi_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + // execute operation + ErnpRegisterResult resp = client.searchWithMds(eidasDataFirst.getGivenName(), eidasDataFirst.getFamilyName(), eidasDataFirst.getDateOfBirth(), cc); + + // validate state + mockWebServer.takeRequest(); + assertNotNull("no ERnP response", resp); + assertEquals("wrong resp size", 2, resp.getPersonResult().size()); + + } + + + @Test + @SneakyThrows + public void searchWithPersonalIdNoResponse() { + final String cc = "CZ"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc); + + mockWebServer.enqueue(new MockResponse() + .setSocketPolicy(SocketPolicy.NO_RESPONSE) + .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); + + // execute operation + EidasSAuthenticationException error = assertThrows("wrong Exception", EidasSAuthenticationException.class, + () -> client.searchWithPersonIdentifier(eidasDataFirst.getPseudonym(), cc)); + assertEquals("wrong errorCode", "module.eidasauth.matching.11", error.getErrorId()); + mockWebServer.takeRequest(); + + } + + @Test + @SneakyThrows + public void searchWithPersonalIdErrorResponse() { + final String cc = "CZ"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(400) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/error_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + // execute operation + EidasSAuthenticationException error = assertThrows("wrong Exception", EidasSAuthenticationException.class, + () -> client.searchWithPersonIdentifier(eidasDataFirst.getPseudonym(), cc)); + assertEquals("wrong errorCode", "module.eidasauth.matching.11", error.getErrorId()); + mockWebServer.takeRequest(); + + } + + @Test + @SneakyThrows + public void searchWithPersonalIdNoResult() { + final String cc = "CZ"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/ernp_empty_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + // execute operation + ErnpRegisterResult resp = client.searchWithPersonIdentifier(eidasDataFirst.getPseudonym(), cc); + + // validate request + final RecordedRequest request = mockWebServer.takeRequest(); + String reqBody = request.getBody().readUtf8(); + assertFalse("no request body", reqBody.isEmpty()); + JsonNode reqJson = mapper.readTree(reqBody); + checkSearchOptions(reqJson, "Searching PersonIdentifier"); + JsonNode person = getJsonObject(reqJson, "suchdaten"); + checkEidasDocument(person, "http://eidas.europa.eu/attributes/naturalperson/PersonIdentifier", cc, eidasDataFirst.getPseudonym()); + + // validate state + assertNotNull("no ERnP response", resp); + assertEquals("wrong resp size", 0, resp.getPersonResult().size()); + + } + + @Test + @SneakyThrows + public void searchWithPersonalIdSingleResult() { + final String cc = "DE"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/1_search_with_personalId_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + // execute operation + ErnpRegisterResult resp = client.searchWithPersonIdentifier(eidasDataFirst.getPseudonym(), cc); + + // validate state + mockWebServer.takeRequest(); + assertNotNull("no ERnP response", resp); + assertEquals("wrong resp size", 1, resp.getPersonResult().size()); + RegisterResult persInfo = resp.getPersonResult().get(0); + assertEquals("wrong familyname", "CtKKrtUe", persInfo.getFamilyName()); + assertEquals("wrong givenName", "dUeYzUFg", persInfo.getGivenName()); + assertEquals("wrong dateOfBirth", "1985-05-05", persInfo.getDateOfBirth()); + assertEquals("wrong bpk", "+OQnljn0Son1W2rkM73nP/VMsvc=", persInfo.getBpk()); + assertEquals("wrong pseudonym", "Y8ADWaeh0h", persInfo.getPseudonym().get(0)); + assertEquals("wrong placeOfBirth", "hrFevCfP", persInfo.getPlaceOfBirth()); + assertEquals("wrong birthName", "sNUEAhEr", persInfo.getBirthName()); + + } + + @Test + @SneakyThrows + public void searchWithPersonalIdSingleResultCountryNoMatch() { + final String cc = "CZ"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/1_search_with_personalId_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + // execute operation + ErnpRegisterResult resp = client.searchWithPersonIdentifier(eidasDataFirst.getPseudonym(), cc); + + // validate state + mockWebServer.takeRequest(); + assertNotNull("no ERnP response", resp); + assertEquals("wrong resp size", 1, resp.getPersonResult().size()); + RegisterResult persInfo = resp.getPersonResult().get(0); + assertEquals("wrong familyname", "CtKKrtUe", persInfo.getFamilyName()); + assertEquals("wrong givenName", "dUeYzUFg", persInfo.getGivenName()); + assertEquals("wrong dateOfBirth", "1985-05-05", persInfo.getDateOfBirth()); + assertEquals("wrong bpk", "+OQnljn0Son1W2rkM73nP/VMsvc=", persInfo.getBpk()); + assertTrue("pseudonym", persInfo.getPseudonym().isEmpty()); + assertNull("placeOfBirth", persInfo.getPlaceOfBirth()); + assertNull("birthName", persInfo.getBirthName()); + + } + + + @Test + @SneakyThrows + public void searchWithPersonalIdMultiResult() { + final String cc = "CZ"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/4_search_with_mds_multi_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + // execute operation + EidasSAuthenticationException error = assertThrows("wrong Exception", EidasSAuthenticationException.class, + () -> client.searchWithPersonIdentifier(eidasDataFirst.getPseudonym(), cc)); + assertEquals("wrong errorCode", "module.eidasauth.matching.03", error.getErrorId()); + mockWebServer.takeRequest(); + + } + + + @Test + @SneakyThrows + public void searchWithCcspecificsNoResponse() { + final String cc = "CZ"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc); + + mockWebServer.enqueue(new MockResponse() + .setSocketPolicy(SocketPolicy.NO_RESPONSE) + .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); + + // execute operation + EidasSAuthenticationException error = assertThrows("wrong Exception", EidasSAuthenticationException.class, + () -> client.searchCountrySpecific(new DeSpecificDetailSearchProcessor().generateSearchRequest(eidasDataFirst), cc)); + assertEquals("wrong errorCode", "module.eidasauth.matching.11", error.getErrorId()); + mockWebServer.takeRequest(); + + } + + @Test + @SneakyThrows + public void searchWithCcspecificsErrorResponse() { + final String cc = "CZ"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(400) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/error_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + // execute operation + EidasSAuthenticationException error = assertThrows("wrong Exception", EidasSAuthenticationException.class, + () -> client.searchCountrySpecific(new DeSpecificDetailSearchProcessor().generateSearchRequest(eidasDataFirst), cc)); + assertEquals("wrong errorCode", "module.eidasauth.matching.11", error.getErrorId()); + mockWebServer.takeRequest(); + + } + + @Test + @SneakyThrows + public void searchWithCcspecificsNoResult() { + final String cc = "DE"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc).toBuilder() + .birthName(RandomStringUtils.randomAlphabetic(5)) + .placeOfBirth(RandomStringUtils.randomAlphabetic(5)) + .build(); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/ernp_empty_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + // execute operation + ErnpRegisterResult resp = client.searchCountrySpecific( + new DeSpecificDetailSearchProcessor().generateSearchRequest(eidasDataFirst), cc); + + // validate request + final RecordedRequest request = mockWebServer.takeRequest(); + String reqBody = request.getBody().readUtf8(); + assertFalse("no request body", reqBody.isEmpty()); + JsonNode reqJson = mapper.readTree(reqBody); + checkSearchOptions(reqJson, "Searching DE specific"); + JsonNode person = getJsonObject(reqJson, "suchdaten"); + checkJsonElement(person, "familienname", eidasDataFirst.getFamilyName()); + checkJsonElement(person, "vorname", eidasDataFirst.getGivenName()); + checkPersonDateOfBirth(person, eidasDataFirst.getDateOfBirth()); + checkEidasDocument(person, "http://eidas.europa.eu/attributes/naturalperson/PlaceOfBirth", cc, eidasDataFirst.getPlaceOfBirth()); + checkEidasDocument(person, "http://eidas.europa.eu/attributes/naturalperson/BirthName", cc, eidasDataFirst.getBirthName()); + + // validate state + assertNotNull("no ERnP response", resp); + assertEquals("wrong resp size", 0, resp.getPersonResult().size()); + + } + + @Test + @SneakyThrows + public void searchWithCcspecificsSingleResult() { + final String cc = "DE"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/1_search_with_personalId_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + // execute operation + ErnpRegisterResult resp = client.searchCountrySpecific( + new DeSpecificDetailSearchProcessor().generateSearchRequest(eidasDataFirst), cc); + + // validate state + mockWebServer.takeRequest(); + assertNotNull("no ERnP response", resp); + assertEquals("wrong resp size", 1, resp.getPersonResult().size()); + RegisterResult persInfo = resp.getPersonResult().get(0); + assertEquals("wrong familyname", "CtKKrtUe", persInfo.getFamilyName()); + assertEquals("wrong givenName", "dUeYzUFg", persInfo.getGivenName()); + assertEquals("wrong dateOfBirth", "1985-05-05", persInfo.getDateOfBirth()); + assertEquals("wrong bpk", "+OQnljn0Son1W2rkM73nP/VMsvc=", persInfo.getBpk()); + assertEquals("wrong pseudonym", "Y8ADWaeh0h", persInfo.getPseudonym().get(0)); + assertEquals("wrong placeOfBirth", "hrFevCfP", persInfo.getPlaceOfBirth()); + assertEquals("wrong birthName", "sNUEAhEr", persInfo.getBirthName()); + + } + + @Test + @SneakyThrows + public void searchWithCcspecificsSingleResultCountryNoMatch() { + final String cc = "CZ"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/1_search_with_personalId_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + // execute operation + ErnpRegisterResult resp = client.searchCountrySpecific( + new DeSpecificDetailSearchProcessor().generateSearchRequest(eidasDataFirst), cc); + + // validate state + mockWebServer.takeRequest(); + assertNotNull("no ERnP response", resp); + assertEquals("wrong resp size", 1, resp.getPersonResult().size()); + RegisterResult persInfo = resp.getPersonResult().get(0); + assertEquals("wrong familyname", "CtKKrtUe", persInfo.getFamilyName()); + assertEquals("wrong givenName", "dUeYzUFg", persInfo.getGivenName()); + assertEquals("wrong dateOfBirth", "1985-05-05", persInfo.getDateOfBirth()); + assertEquals("wrong bpk", "+OQnljn0Son1W2rkM73nP/VMsvc=", persInfo.getBpk()); + assertTrue("pseudonym", persInfo.getPseudonym().isEmpty()); + assertNull("placeOfBirth", persInfo.getPlaceOfBirth()); + assertNull("birthName", persInfo.getBirthName()); + + } + + + @Test + @SneakyThrows + public void searchWithCcspecificsMultiResult() { + final String cc = "CZ"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/4_search_with_mds_multi_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + // execute operation + EidasSAuthenticationException error = assertThrows("wrong Exception", EidasSAuthenticationException.class, + () -> client.searchCountrySpecific(new DeSpecificDetailSearchProcessor().generateSearchRequest(eidasDataFirst), cc)); + assertEquals("wrong errorCode", "module.eidasauth.matching.03", error.getErrorId()); + mockWebServer.takeRequest(); + + } + + + @Test + @SneakyThrows + public void addPersonNoResponse() { + final String cc = "CZ"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc); + + mockWebServer.enqueue(new MockResponse() + .setSocketPolicy(SocketPolicy.NO_RESPONSE) + .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); + + // execute operation + EidasSAuthenticationException error = assertThrows("wrong Exception", EidasSAuthenticationException.class, + () -> client.add(eidasDataFirst)); + assertEquals("wrong errorCode", "module.eidasauth.matching.11", error.getErrorId()); + mockWebServer.takeRequest(); + + } + + @Test + @SneakyThrows + public void addPersonSimpleSuccess() { + final String cc = "CZ"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/2_add_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + // execute operation + // execute operation + ErnpRegisterResult resp = client.add(eidasDataFirst); + + // validate request + final RecordedRequest request = mockWebServer.takeRequest(); + String reqBody = request.getBody().readUtf8(); + assertFalse("no request body", reqBody.isEmpty()); + JsonNode reqJson = mapper.readTree(reqBody); + checkJsonElement(reqJson, "begruendung", "Add new person"); + JsonNode person = getJsonObject(reqJson, "personendaten"); + checkJsonElement(person, "familienname", eidasDataFirst.getFamilyName()); + checkJsonElement(person, "vorname", eidasDataFirst.getGivenName()); + checkPersonDateOfBirth(person, eidasDataFirst.getDateOfBirth()); + checkEidasDocument(reqJson, "http://eidas.europa.eu/attributes/naturalperson/PersonIdentifier", cc, eidasDataFirst.getPseudonym()); + checkEidasDocument(reqJson, "http://eidas.europa.eu/attributes/naturalperson/PlaceOfBirth", cc); + checkEidasDocument(reqJson, "http://eidas.europa.eu/attributes/naturalperson/BirthName", cc); + + + // validate state + assertNotNull("no ERnP response", resp); + assertEquals("wrong resp size", 1, resp.getPersonResult().size()); + RegisterResult persInfo = resp.getPersonResult().get(0); + assertEquals("wrong familyname", "mRjMKAQc", persInfo.getFamilyName()); + assertEquals("wrong givenName", "vdqZZIaA", persInfo.getGivenName()); + assertEquals("wrong dateOfBirth", "1996-01-01", persInfo.getDateOfBirth()); + assertEquals("wrong bpk", "TBGoMlirU881e2jMGETa9WLx1+A=", persInfo.getBpk()); + + } + + @Test + @SneakyThrows + public void addPersonSimpleComplexe() { + final String cc = "DE"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc).toBuilder() + .birthName(RandomStringUtils.randomAlphabetic(5)) + .placeOfBirth(RandomStringUtils.randomAlphabetic(5)) + .build(); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/2_add_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + // execute operation + // execute operation + ErnpRegisterResult resp = client.add(eidasDataFirst); + + // validate request + final RecordedRequest request = mockWebServer.takeRequest(); + String reqBody = request.getBody().readUtf8(); + assertFalse("no request body", reqBody.isEmpty()); + JsonNode reqJson = mapper.readTree(reqBody); + checkJsonElement(reqJson, "begruendung", "Add new person"); + JsonNode person = getJsonObject(reqJson, "personendaten"); + checkJsonElement(person, "familienname", eidasDataFirst.getFamilyName()); + checkJsonElement(person, "vorname", eidasDataFirst.getGivenName()); + checkPersonDateOfBirth(person, eidasDataFirst.getDateOfBirth()); + checkEidasDocument(reqJson, "http://eidas.europa.eu/attributes/naturalperson/PersonIdentifier", cc, eidasDataFirst.getPseudonym()); + checkEidasDocument(reqJson, "http://eidas.europa.eu/attributes/naturalperson/PlaceOfBirth", cc, eidasDataFirst.getPlaceOfBirth()); + checkEidasDocument(reqJson, "http://eidas.europa.eu/attributes/naturalperson/BirthName", cc, eidasDataFirst.getBirthName()); + + // validate state + assertNotNull("no ERnP response", resp); + assertEquals("wrong resp size", 1, resp.getPersonResult().size()); + RegisterResult persInfo = resp.getPersonResult().get(0); + assertEquals("wrong familyname", "mRjMKAQc", persInfo.getFamilyName()); + assertEquals("wrong givenName", "vdqZZIaA", persInfo.getGivenName()); + assertEquals("wrong dateOfBirth", "1996-01-01", persInfo.getDateOfBirth()); + assertEquals("wrong bpk", "TBGoMlirU881e2jMGETa9WLx1+A=", persInfo.getBpk()); + assertEquals("wrong pseudonym", "88hvWzUaIX", persInfo.getPseudonym().get(0)); + assertEquals("wrong placeOfBirth", "VRNCAylF", persInfo.getPlaceOfBirth()); + assertEquals("wrong birthName", "miEklFHC", persInfo.getBirthName()); + + } + + @Test + @SneakyThrows + public void updateNoLatestVersion() { + final String cc = "DE"; + final SimpleEidasData eidasDataFirst = generateRandomEidasData(cc).toBuilder() + .birthName(RandomStringUtils.randomAlphabetic(5)) + .placeOfBirth(RandomStringUtils.randomAlphabetic(5)) + .build(); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/ernp_empty_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + RegisterResult ernpResult = RegisterResult.builder() + .familyName(eidasDataFirst.getFamilyName()) + .givenName(eidasDataFirst.getGivenName()) + .dateOfBirth(eidasDataFirst.getDateOfBirth()) + .bpk("") + .pseudonym(Arrays.asList(eidasDataFirst.getPseudonym())) + .build(); + + // execute operation + EidasSAuthenticationException error = assertThrows("wrong Exception", EidasSAuthenticationException.class, + () -> client.update(ernpResult, eidasDataFirst)); + assertEquals("wrong errorCode", "module.eidasauth.matching.03", error.getErrorId()); + mockWebServer.takeRequest(); + + + } + + + @Test + @SneakyThrows + public void updateNoUpdateRequired() { + final String cc = "DE"; + final String personalIdentifierFirst = "Y8ADWaeh0h"; + final SimpleEidasData eidasDataFirst = SimpleEidasData.builder() + .citizenCountryCode(cc) + .familyName("CtKKrtUe") + .givenName("dUeYzUFg") + .dateOfBirth("1985-05-05") + .personalIdentifier(cc + "/AT/" + personalIdentifierFirst) + .pseudonym(personalIdentifierFirst) + .build(); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/1_kitt_search_latest_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + RegisterResult ernpResult = RegisterResult.builder() + .familyName(eidasDataFirst.getFamilyName()) + .givenName(eidasDataFirst.getGivenName()) + .dateOfBirth(eidasDataFirst.getDateOfBirth()) + .bpk("+OQnljn0Son1W2rkM73nP/VMsvc=") + .pseudonym(Arrays.asList(eidasDataFirst.getPseudonym())) + .birthName("sNUEAhEr") + .placeOfBirth("hrFevCfP") + .build(); + + // execute operation + ErnpRegisterResult resp = client.update(ernpResult, eidasDataFirst); + + // validate request + final RecordedRequest request = mockWebServer.takeRequest(); + String reqBody = request.getBody().readUtf8(); + assertFalse("no request body", reqBody.isEmpty()); + JsonNode reqJson = mapper.readTree(reqBody); + checkSearchOptions(reqJson, "KITT get-latest-version"); + JsonNode person = getJsonObject(reqJson, "suchdaten"); + checkJsonElement(person, "familienname", ernpResult.getFamilyName()); + checkJsonElement(person, "vorname", ernpResult.getGivenName()); + checkJsonElement(person, "bpkZp", ernpResult.getBpk()); + checkPersonDateOfBirth(person, ernpResult.getDateOfBirth()); + + //validate state + assertNotNull("no ERnP response", resp); + assertEquals("wrong resp size", 1, resp.getPersonResult().size()); + RegisterResult persInfo = resp.getPersonResult().get(0); + assertEquals("wrong familyname", ernpResult.getFamilyName(), persInfo.getFamilyName()); + assertEquals("wrong givenName", ernpResult.getGivenName(), persInfo.getGivenName()); + assertEquals("wrong dateOfBirth", ernpResult.getDateOfBirth(), persInfo.getDateOfBirth()); + assertEquals("wrong bpk", ernpResult.getBpk(), persInfo.getBpk()); + assertEquals("wrong pseudonym", ernpResult.getPseudonym().get(0), persInfo.getPseudonym().get(0)); + assertEquals("wrong placeOfBirth", "hrFevCfP", persInfo.getPlaceOfBirth()); + assertEquals("wrong birthName", "sNUEAhEr", persInfo.getBirthName()); + + } + + @Test + @SneakyThrows + public void updateUpdateRequiredMds() { + final String cc = "DE"; + final String personalIdentifierFirst = "Y8ADWaeh0h"; + final SimpleEidasData eidasDataFirst = SimpleEidasData.builder() + .citizenCountryCode(cc) + .familyName("mVzTMpig6r") + .givenName("Jb2vj1Xpql") + .dateOfBirth("1985-05-05") + .personalIdentifier(cc + "/AT/" + personalIdentifierFirst) + .pseudonym(personalIdentifierFirst) + .placeOfBirth("hrFevCfP") + .birthName("sNUEAhEr") + .build(); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/1_kitt_search_latest_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/1_kitt_update_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + RegisterResult ernpResult = RegisterResult.builder() + .familyName("CtKKrtUe") + .givenName("dUeYzUFg") + .dateOfBirth("1985-05-05") + .bpk("+OQnljn0Son1W2rkM73nP/VMsvc=") + .pseudonym(Arrays.asList("Y8ADWaeh0h")) + .birthName("sNUEAhEr") + .placeOfBirth("hrFevCfP") + .build(); + + // execute operation + ErnpRegisterResult resp = client.update(ernpResult, eidasDataFirst); + + // validate request + // check get-latest-version request + final RecordedRequest request = mockWebServer.takeRequest(); + String reqBody = request.getBody().readUtf8(); + assertFalse("no request body", reqBody.isEmpty()); + JsonNode reqJson = mapper.readTree(reqBody); + checkSearchOptions(reqJson, "KITT get-latest-version"); + JsonNode person = getJsonObject(reqJson, "suchdaten"); + checkJsonElement(person, "familienname", ernpResult.getFamilyName()); + checkJsonElement(person, "vorname", ernpResult.getGivenName()); + checkJsonElement(person, "bpkZp", ernpResult.getBpk()); + checkPersonDateOfBirth(person, ernpResult.getDateOfBirth()); + + // check update request + final RecordedRequest requestKitt = mockWebServer.takeRequest(); + String reqBodyKitt = requestKitt.getBody().readUtf8(); + assertFalse("no request body", reqBodyKitt.isEmpty()); + JsonNode reqJsonKitt = mapper.readTree(reqBodyKitt); + checkJsonElement(reqJsonKitt, "begruendung", "KITT update dataset"); + checkJsonElement(reqJsonKitt, "entityId", "1933000000000475"); + checkJsonElement(reqJsonKitt, "version", "2022-03-03T10:07:28.885Z"); + JsonNode personChange = getJsonObject(reqJsonKitt, "aendern"); + JsonNode personKitt = getJsonObject(personChange, "personendaten"); + checkJsonElement(personKitt, "familienname", eidasDataFirst.getFamilyName()); + checkJsonElement(personKitt, "vorname", eidasDataFirst.getGivenName()); + checkPersonDateOfBirth(personKitt, eidasDataFirst.getDateOfBirth()); + + assertFalse("find 'aendern' element", reqJsonKitt.has("anlegen")); + assertFalse("find 'aendern' element", personChange.has("eidas")); + + + //validate state + assertNotNull("no ERnP response", resp); + assertEquals("wrong resp size", 1, resp.getPersonResult().size()); + RegisterResult persInfo = resp.getPersonResult().get(0); + assertEquals("wrong familyname", eidasDataFirst.getFamilyName(), persInfo.getFamilyName()); + assertEquals("wrong givenName", eidasDataFirst.getGivenName(), persInfo.getGivenName()); + assertEquals("wrong dateOfBirth", ernpResult.getDateOfBirth(), persInfo.getDateOfBirth()); + assertEquals("wrong bpk", ernpResult.getBpk(), persInfo.getBpk()); + assertEquals("wrong pseudonym", ernpResult.getPseudonym().get(0), persInfo.getPseudonym().get(0)); + assertEquals("wrong placeOfBirth", "hrFevCfP", persInfo.getPlaceOfBirth()); + assertEquals("wrong birthName", "sNUEAhEr", persInfo.getBirthName()); + + } + + @Test + @SneakyThrows + public void updateUpdateRequiredEidasDocs() { + final String cc = "DE"; + final String personalIdentifierFirst = "nj1m79jm9z"; + final SimpleEidasData eidasDataFirst = SimpleEidasData.builder() + .citizenCountryCode(cc) + .familyName("mRjMKAQc") + .givenName("vdqZZIaA") + .dateOfBirth("1996-01-01") + .personalIdentifier(cc + "/AT/" + personalIdentifierFirst) + .pseudonym(personalIdentifierFirst) + .birthName(RandomStringUtils.randomAlphabetic(10)) + .build(); + + // set ERnP response + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/2_kitt_search_latest_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(IOUtils.toString( + ErnpRestClientTest.class.getResourceAsStream("/data/ernp/2_kitt_update_resp.json"), + "UTF-8")) + .setHeader("Content-Type", "application/json;charset=utf-8")); + + RegisterResult ernpResult = RegisterResult.builder() + .familyName("mRjMKAQc") + .givenName("vdqZZIaA") + .dateOfBirth("1996-01-01") + .bpk("TBGoMlirU881e2jMGETa9WLx1+A=") + .pseudonym(Arrays.asList("88hvWzUaIX")) + .birthName("VRNCAylF") + .placeOfBirth("miEklFHC") + .build(); + + // execute operation + ErnpRegisterResult resp = client.update(ernpResult, eidasDataFirst); + + // validate request + // check get-latest-version request + final RecordedRequest request = mockWebServer.takeRequest(); + String reqBody = request.getBody().readUtf8(); + assertFalse("no request body", reqBody.isEmpty()); + JsonNode reqJson = mapper.readTree(reqBody); + checkSearchOptions(reqJson, "KITT get-latest-version"); + JsonNode person = getJsonObject(reqJson, "suchdaten"); + checkJsonElement(person, "familienname", ernpResult.getFamilyName()); + checkJsonElement(person, "vorname", ernpResult.getGivenName()); + checkJsonElement(person, "bpkZp", ernpResult.getBpk()); + checkPersonDateOfBirth(person, ernpResult.getDateOfBirth()); + + // check update request + final RecordedRequest requestKitt = mockWebServer.takeRequest(); + String reqBodyKitt = requestKitt.getBody().readUtf8(); + assertFalse("no request body", reqBodyKitt.isEmpty()); + JsonNode reqJsonKitt = mapper.readTree(reqBodyKitt); + checkJsonElement(reqJsonKitt, "begruendung", "KITT update dataset"); + checkJsonElement(reqJsonKitt, "entityId", "1933000000000498"); + checkJsonElement(reqJsonKitt, "version", "2022-03-03T10:14:59.712Z"); + JsonNode personChange = getJsonObject(reqJsonKitt, "anlegen"); + checkEidasDocument(personChange, "http://eidas.europa.eu/attributes/naturalperson/PersonIdentifier", cc, eidasDataFirst.getPseudonym()); + assertFalse("find 'aendern' element", reqJsonKitt.has("aendern")); + + //validate state + assertNotNull("no ERnP response", resp); + assertEquals("wrong resp size", 1, resp.getPersonResult().size()); + RegisterResult persInfo = resp.getPersonResult().get(0); + assertEquals("wrong familyname", eidasDataFirst.getFamilyName(), persInfo.getFamilyName()); + assertEquals("wrong givenName", eidasDataFirst.getGivenName(), persInfo.getGivenName()); + assertEquals("wrong dateOfBirth", ernpResult.getDateOfBirth(), persInfo.getDateOfBirth()); + assertEquals("wrong bpk", ernpResult.getBpk(), persInfo.getBpk()); + assertEquals("wrong pseudonym", ernpResult.getPseudonym().get(0), persInfo.getPseudonym().get(0)); + assertEquals("wrong pseudonym", eidasDataFirst.getPseudonym(), persInfo.getPseudonym().get(1)); + assertEquals("wrong placeOfBirth", "VRNCAylF", persInfo.getPlaceOfBirth()); + assertEquals("wrong birthName", "miEklFHC", persInfo.getBirthName()); + + } + + + private SimpleEidasData generateRandomEidasData(String cc) { + final String personalIdentifierFirst = RandomStringUtils.randomAlphanumeric(10); + return SimpleEidasData.builder() + .citizenCountryCode(cc) + .familyName(RandomStringUtils.randomAlphanumeric(10)) + .givenName(RandomStringUtils.randomAlphanumeric(10)) + .dateOfBirth("1996-10-15") + .personalIdentifier(cc + "/AT/" + personalIdentifierFirst) + .pseudonym(personalIdentifierFirst) + .build(); + + } + + private void checkEidasDocument(JsonNode person, String art, String cc, String expected) { + assertTrue("no element: eidas", person.has("eidas")); + assertTrue("wrong type element: eidas", person.get("eidas").isArray()); + + boolean found = false; + Iterator<JsonNode> docs = person.get("eidas").elements(); + while (docs.hasNext() && !found) { + JsonNode el = docs.next(); + assertTrue("art", el.has("art")); + assertTrue("wert", el.has("wert")); + assertTrue("cc", el.has("staatscode2")); + found = art.equals(el.get("art").asText()) && cc.equals(el.get("staatscode2").asText()) + && expected.equals(el.get("wert").asText()); + + } + assertTrue("Missing eidas document", found); + + } + + private void checkEidasDocument(JsonNode person, String art, String cc) { + assertTrue("no element: eidas", person.has("eidas")); + assertTrue("wrong type element: eidas", person.get("eidas").isArray()); + + boolean found = false; + Iterator<JsonNode> docs = person.get("eidas").elements(); + while (docs.hasNext() && !found) { + JsonNode el = docs.next(); + assertTrue("art", el.has("art")); + assertTrue("wert", el.has("wert")); + assertTrue("cc", el.has("staatscode2")); + found = art.equals(el.get("art").asText()) && cc.equals(el.get("staatscode2").asText()); + + } + assertFalse("Missing eidas document", found); + + } + + private void checkPersonDateOfBirth(JsonNode person, String dateOfBirth) { + JsonNode birthDay = getJsonObject(person, "geburtsdatum"); + String[] el = dateOfBirth.split("-"); + checkJsonElement(birthDay, "jahr", Integer.parseInt(el[0])); + checkJsonElement(birthDay, "monat", Integer.parseInt(el[1])); + checkJsonElement(birthDay, "tag", Integer.parseInt(el[2])); + + } + + private void checkSearchOptions(JsonNode json, String reason) { + checkJsonElement(json, "begruendung", reason); + JsonNode options = getJsonObject(json, "suchoptionen"); + checkJsonElement(options, "historisch", "AktuellUndHistorisch"); + checkJsonElement(options, "sucheMitNamensteilen", false); + checkJsonElement(options, "suchwizard", false); + checkJsonElement(options, "zmr", false); + + } + + private JsonNode getJsonObject(JsonNode json, String key) { + assertTrue("no element: " + key, json.has(key)); + assertTrue("wrong type element: " + key, json.get(key).isObject()); + return json.get(key); + + } + + private void checkJsonElement(JsonNode json, String key, int expected) { + assertTrue("no element: " + key, json.has(key)); + assertTrue("wong element-type: " + key, json.get(key).isInt()); + assertEquals("wong element-value: " + key, expected, json.get(key).asInt()); + + } + + private void checkJsonElement(JsonNode json, String key, String expected) { + assertTrue("no element: " + key, json.has(key)); + assertTrue("wong element-type: " + key, json.get(key).isTextual()); + assertEquals("wong element-value: " + key, expected, json.get(key).asText()); + + } + + private void checkJsonElement(JsonNode json, String key, boolean expected) { + assertTrue("no element: " + key, json.has(key)); + assertTrue("wong element-type: " + key, json.get(key).isBoolean()); + assertEquals("wong element-value: " + key, expected, json.get(key).asBoolean()); } diff --git a/eidas_modules/authmodule-eIDAS-v2/src/test/resources/data/ernp/3_search_with_mds_resp.json b/eidas_modules/authmodule-eIDAS-v2/src/test/resources/data/ernp/3_search_with_mds_resp.json index 50735f23..87be362d 100644 --- a/eidas_modules/authmodule-eIDAS-v2/src/test/resources/data/ernp/3_search_with_mds_resp.json +++ b/eidas_modules/authmodule-eIDAS-v2/src/test/resources/data/ernp/3_search_with_mds_resp.json @@ -28,7 +28,7 @@ "basiszahl": "000501189333", "bpkZp": "vypyCkyczK7i+cgPWlJasuJphIA=", "entityId": "1933000000000047", - "familienname": "DOPISN[0xc3][0x8d]", + "familienname": "DOPISNÍ", "geburtsdatum": { "jahr": 1996, "monat": 1, |