Skip to content

Commit

Permalink
fix: added repositoryUniqueId validation
Browse files Browse the repository at this point in the history
  • Loading branch information
SimoneNuts committed Dec 27, 2023
1 parent 7ecf493 commit e234187
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import it.finanze.sanita.fse2.ms.gtw.dispatcher.exceptions.ConnectionRefusedException;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.IDocumentReferenceSRV;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.StringUtility;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.ValidationUtility;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
Expand Down Expand Up @@ -109,6 +110,7 @@ private DocumentReferenceDTO buildDocumentReferenceDTO(final String encodedCDA,
}
documentReferenceDTO.setPracticeSettingCode(requestBody.getAssettoOrganizzativo().getDescription());
documentReferenceDTO.setTipoDocumentoLivAlto(requestBody.getTipoDocumentoLivAlto().getCode());
ValidationUtility.repositoryUniqueIdValidation(requestBody.getIdentificativoRep());
documentReferenceDTO.setRepositoryUniqueID(requestBody.getIdentificativoRep());
documentReferenceDTO.setServiceStartTime(requestBody.getDataInizioPrestazione());
documentReferenceDTO.setServiceStopTime(requestBody.getDataFinePrestazione());
Expand Down Expand Up @@ -232,6 +234,7 @@ private DocumentEntryDTO createDocumentEntry(final org.jsoup.nodes.Document docC
de.setAuthor(authorElement.attr(EXTENSION_ATTRIBUTE));
}

ValidationUtility.repositoryUniqueIdValidation(requestBody.getIdentificativoRep());
de.setRepositoryUniqueId(requestBody.getIdentificativoRep());

if(requestBody.getDescriptions() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,23 @@
*/
package it.finanze.sanita.fse2.ms.gtw.dispatcher.utility;

import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.RestExecutionResultEnum.INVALID_ID_DOC;
import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.RestExecutionResultEnum.OLDER_DAY;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.response.ErrorResponseDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ErrorInstanceEnum;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.SubjectOrganizationEnum;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.exceptions.ValidationException;


public final class ValidationUtility {

private static final String REPOSITORY_UNIQUE_ID_REGEX = "2\\.16\\.840\\.1\\.113883\\.2\\.9\\.2\\.(.*)\\.4\\.5\\..*";

public static void checkDayAfterValidation(Date insertionDate, Integer dayAllowToPublishAfterValidation) {
if(DateUtility.getDifferenceDays(insertionDate, new Date()) > dayAllowToPublishAfterValidation) {
final ErrorResponseDTO error = ErrorResponseDTO.builder()
Expand All @@ -32,4 +38,29 @@ public static void checkDayAfterValidation(Date insertionDate, Integer dayAllowT
throw new ValidationException(error);
}
}

public static void repositoryUniqueIdValidation(String repositoryUniqueId) {
Pattern pattern = Pattern.compile(REPOSITORY_UNIQUE_ID_REGEX);
Matcher m = pattern.matcher(repositoryUniqueId);

boolean output;
if(m.matches()) {
output = true;
if(m.groupCount()>0) {
output = SubjectOrganizationEnum.getCode(m.group(1)) != null;
}
} else {
output = false;
}

if (!output) {
final ErrorResponseDTO error = ErrorResponseDTO.builder()
.type(INVALID_ID_DOC.getType())
.title(INVALID_ID_DOC.getTitle())
.instance(ErrorInstanceEnum.INVALID_ID_ERROR.getInstance())
.detail("Error: Invalid format for repositoryUniqueId").build();
throw new ValidationException(error);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private PublicationCreationReqDTO buildPublicationReqDTO(String workflowInstance
output.setDataFinePrestazione(""+new Date().getTime());
output.setHealthDataFormat(HealthDataFormatEnum.CDA);
output.setIdentificativoDoc("Identificativo doc");
output.setIdentificativoRep("Identificativo rep");
output.setIdentificativoRep("2.16.840.1.113883.2.9.2.080.4.5.1234");
output.setIdentificativoSottomissione("identificativo sottomissione");
output.setMode(InjectionModeEnum.ATTACHMENT);
output.setAttiCliniciRegoleAccesso(Arrays.asList(EventCodeEnum._94503_0.getCode()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.request.ValidationCDAReqDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.response.ValidationResDTO;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.*;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.exceptions.ValidationException;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.repository.entity.ValidatedDocumentsETY;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.ICdaSRV;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.service.IConfigSRV;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.FileUtility;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.StringUtility;
import it.finanze.sanita.fse2.ms.gtw.dispatcher.utility.ValidationUtility;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
Expand Down Expand Up @@ -434,4 +436,14 @@ void validatorClientErrorTest() {
assertEquals(RestExecutionResultEnum.SYNTAX_ERROR, result.values().iterator().next());
}

@Test
@DisplayName("Validator String repositoryUniqueId Test")
void validatorRepositoryUniqueIdFromStringTest() {
String rightId = "2.16.840.1.113883.2.9.2.080.4.5.1234";
String wrongRegionId = "2.16.840.1.113883.2.9.2.123.4.5.1234";

assertDoesNotThrow(() -> ValidationUtility.repositoryUniqueIdValidation(rightId));
assertThrows(ValidationException.class, () -> ValidationUtility.repositoryUniqueIdValidation(wrongRegionId));
}

}

0 comments on commit e234187

Please sign in to comment.