diff --git a/src/main/java/de/samply/share/client/util/connector/CtsConnector.java b/src/main/java/de/samply/share/client/util/connector/CtsConnector.java index e6fb83c7..ab3e7508 100644 --- a/src/main/java/de/samply/share/client/util/connector/CtsConnector.java +++ b/src/main/java/de/samply/share/client/util/connector/CtsConnector.java @@ -4,7 +4,6 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.parser.DataFormatException; import ca.uhn.fhir.util.ResourceReferenceInfo; -import com.google.common.io.CharSource; import com.mchange.rmi.NotAuthorizedException; import de.samply.share.client.control.ApplicationBean; import de.samply.share.client.crypt.Crypt; @@ -19,6 +18,7 @@ import de.samply.share.client.util.xml.XmlUtils; import de.samply.share.common.utils.SamplyShareUtils; import jakarta.ws.rs.NotFoundException; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; @@ -87,11 +87,11 @@ public CtsConnector() throws MalformedURLException { * upload endpoint. * * @param bundleString the patient bundle as String. - * @throws IOException IOException - * @throws NotFoundException NotFoundException - * @throws NotAuthorizedException NotAuthorizedException + * @throws IOException IOException + * @throws NotFoundException NotFoundException + * @throws NotAuthorizedException NotAuthorizedException * @throws MainzellisteConnectorException MainzellisteConnectorException - * @throws CtsConnectorException CtsConnectorException + * @throws CtsConnectorException CtsConnectorException */ public Response postFhirToCts(String bundleString, MediaType mediaType) throws IOException, @@ -159,7 +159,7 @@ private Bundle pseudonymiseBundle(String bundleString, MediaType mediaType) * Pseudonymize any patient data in the bundle. * * @param xmlString the patient bundle which should be pseudonimised - * @return the pseudonimised bundle + * @return the pseudonimised xml * @throws IOException IOException * @throws ConfigurationException ConfigurationException * @throws DataFormatException DataFormatException @@ -167,8 +167,9 @@ private Bundle pseudonymiseBundle(String bundleString, MediaType mediaType) private Document pseudonymiseXml(String xmlString) throws IOException, NotFoundException, NotAuthorizedException, MainzellisteConnectorException, XmlPareException { - InputStream xmlInputStream = CharSource.wrap(xmlString) - .asByteSource(StandardCharsets.UTF_8).openStream(); + byte[] bytes = xmlString.getBytes(StandardCharsets.UTF_8); + InputStream xmlInputStream = new ByteArrayInputStream(bytes); + Document xmlDocument; xmlDocument = xmlUtils.domBuilder(xmlInputStream); MainzellisteConnector mainzellisteConnector = ApplicationBean.getMainzellisteConnector(); @@ -235,7 +236,7 @@ private String cryptIds(Bundle bundle, boolean encrypt) throws CtsConnectorExcep * @throws GeneralSecurityException GeneralSecurityException */ public static IIdType getEncryptedId(IIdType idType, boolean encrypt, Crypt crypt) - throws GeneralSecurityException { + throws GeneralSecurityException { String id = idType.getIdPart(); if (id == null || "".equals(id)) { logger.error("Reference or URL does not contain an ID of a resource " + idType.getValue()); @@ -287,21 +288,20 @@ public void closeResponse(CloseableHttpResponse response) { /** * Takes a stringified XML file, assumed to be containing identifying patient data (IDAT), - * replaces the IDAT with a pseudonym, and then sends the pseudonymized xml to the CTS data - * upload endpoint. + * replaces the IDAT with a pseudonym, and then sends the pseudonymized xml to the CTS data upload + * endpoint. * * @param xmlString the patient bundle as String. - * @throws IOException IOException - * @throws NotFoundException NotFoundException - * @throws NotAuthorizedException NotAuthorizedException + * @throws IOException IOException + * @throws NotFoundException NotFoundException + * @throws NotAuthorizedException NotAuthorizedException * @throws MainzellisteConnectorException MainzellisteConnectorException - * @throws CtsConnectorException CtsConnectorException + * @throws CtsConnectorException CtsConnectorException */ public Response postXmlToCts(String xmlString) - throws IOException, - NotFoundException, NotAuthorizedException, XmlPareException, - MainzellisteConnectorException, CtsConnectorException { - + throws IOException, + NotFoundException, NotAuthorizedException, XmlPareException, + MainzellisteConnectorException, CtsConnectorException { Document pseudonymXmlDocument = pseudonymiseXml(xmlString); String pseudonymXmlAsString = xmlUtils.xmlDocToString(pseudonymXmlDocument); HttpEntity entity = new StringEntity(pseudonymXmlAsString, Consts.UTF_8); @@ -314,7 +314,7 @@ public Response postXmlToCts(String xmlString) response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); String message = - "CTS server response: statusCode:" + statusCode + "; response: " + response; + "CTS server response: statusCode:" + statusCode + "; response: " + response; String responseBody = EntityUtils.toString(response.getEntity(), Consts.UTF_8); if (responseBody != null && !responseBody.isEmpty()) { message += ";body: " + responseBody; diff --git a/src/main/java/de/samply/share/client/util/connector/MainzellisteConnector.java b/src/main/java/de/samply/share/client/util/connector/MainzellisteConnector.java index 6aca2e59..84ff9465 100644 --- a/src/main/java/de/samply/share/client/util/connector/MainzellisteConnector.java +++ b/src/main/java/de/samply/share/client/util/connector/MainzellisteConnector.java @@ -188,7 +188,7 @@ public Bundle getPatientFhirPseudonym(Bundle bundle) /** * prepare xml transaction to be sent to the edc system. * - * @param xmlDoc xml tree. + * @param xmlDoc xml tree * @return xmlDoc * @throws NotFoundException Not Found Exception * @throws NotAuthorizedException Not Authorized Exception @@ -226,14 +226,26 @@ public Document getPatientXmlPseudonym(Document xmlDoc) tokenNode.setTextContent(encryptedId.get(MAINZELLISTE_IDTYPE_ENC_ID).getAsString()); identifyingDataNode.appendChild(tokenNode); } catch (ConflictException | MandatoryAttributeException - | IllegalArgumentException | XmlPareException e) { + | IllegalArgumentException | XmlPareException e) { throw new MainzellisteConnectorException(e); } return xmlDoc; } - private String concatenateDate(String year, String month, String day) { - return year + "-" + month + "-" + day; + protected String concatenateDate(String year, String month, String day) { + StringBuilder stringBuilder = new StringBuilder(10); + stringBuilder.append(year); + stringBuilder.append('-'); + if (month.length() == 1) { + stringBuilder.append('0'); + } + stringBuilder.append(month); + stringBuilder.append('-'); + if (day.length() == 1) { + stringBuilder.append('0'); + } + stringBuilder.append(day); + return stringBuilder.toString(); } @@ -859,7 +871,7 @@ HumanName getPatientName(List nameList, HumanName.NameUse nameUse) { /** * validate a date. * - * @param date date as String + * @param date date as String * @param dateFormatter Date Time Formatter * @return boolean */ diff --git a/src/main/java/de/samply/share/client/util/xml/XmlUtils.java b/src/main/java/de/samply/share/client/util/xml/XmlUtils.java index c2567238..f98d1af4 100644 --- a/src/main/java/de/samply/share/client/util/xml/XmlUtils.java +++ b/src/main/java/de/samply/share/client/util/xml/XmlUtils.java @@ -35,9 +35,9 @@ public class XmlUtils { public static final String GEBURTSMONAT_ELEMENT = "geburtsmonat"; public static final String GEBURTSJAHR_ELEMENT = "geburtsjahr"; public static final String VERSICHERUNGSNUMMER_ELEMENT = "versicherungsnummer"; - public static final String ADRESSEPLZ_ELEMENT = "adresseplz"; - public static final String ADRESSESTADT_ELEMENT = "adressestadt"; - public static final String ADRESSESTRASSE_ELEMENT = "adressestrasse"; + public static final String ADRESSEPLZ_ELEMENT = "adresse.plz"; + public static final String ADRESSESTADT_ELEMENT = "adresse.stadt"; + public static final String ADRESSESTRASSE_ELEMENT = "adresse.strasse"; public static final String MEDICAL_DATA_ELEMENT = "medical-data"; public static final String TOKEN_ELEMENT = "token"; public static final String BIRTHDATE_ELEMENT = "birthdate"; diff --git a/src/test/java/de/samply/share/client/util/connector/MainzellisteConnectorTest.java b/src/test/java/de/samply/share/client/util/connector/MainzellisteConnectorTest.java new file mode 100644 index 00000000..f624ae5e --- /dev/null +++ b/src/test/java/de/samply/share/client/util/connector/MainzellisteConnectorTest.java @@ -0,0 +1,26 @@ +package de.samply.share.client.util.connector; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import de.samply.share.client.util.connector.MainzellisteConnector; +import org.junit.jupiter.api.Test; + +class MainzellisteConnectorTest { + + @Test + void testConcatenateDate() { + MainzellisteConnector connectorMock = mock( + de.samply.share.client.util.connector.MainzellisteConnector.class); + String year = "2023"; + String month = "4"; + String day = "20"; + + String expectedDate = "2023-04-20"; + when(connectorMock.concatenateDate(year, month, day)).thenCallRealMethod(); + String actualDate = connectorMock.concatenateDate(year, month, day); + + assertEquals(expectedDate, actualDate); + + } +} \ No newline at end of file