use of com.helger.phive.api.result.ValidationResultList in project phive by phax.
the class PhiveJsonHelper method getAsValidationResultList.
/**
* Try to parse a JSON structure and convert it back to a
* {@link ValidationResultList}.
*
* @param aJson
* The JSON to be read. May be <code>null</code>.
* @param aValidationTypeResolver
* The validation type resolver to be used. May not be
* <code>null</code>.
* @return <code>null</code> in case reverse operation fails.
*/
@Nullable
public static ValidationResultList getAsValidationResultList(@Nullable final IJsonObject aJson, @Nonnull final Function<String, IValidationType> aValidationTypeResolver) {
ValueEnforcer.notNull(aValidationTypeResolver, "ValidationTypeResolver");
if (aJson == null)
return null;
final IJsonArray aResults = aJson.getAsArray(JSON_RESULTS);
if (aResults == null)
return null;
final ValidationResultList ret = new ValidationResultList();
for (final IJson aResult : aResults) {
final IJsonObject aResultObj = aResult.getAsObject();
if (aResultObj != null) {
final String sSuccess = aResultObj.getAsString(JSON_SUCCESS);
final ETriState eSuccess = getAsTriState(sSuccess);
if (eSuccess == null) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("Failed to resolve TriState '" + sSuccess + "'");
continue;
}
final String sValidationType = aResultObj.getAsString(JSON_ARTIFACT_TYPE);
final IValidationType aValidationType = aValidationTypeResolver.apply(sValidationType);
if (aValidationType == null) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("Failed to resolve ValidationType '" + sValidationType + "'");
continue;
}
final String sArtefactPathType = aResultObj.getAsString(JSON_ARTIFACT_PATH_TYPE);
final String sArtefactPath = aResultObj.getAsString(JSON_ARTIFACT_PATH);
final IReadableResource aRes = getAsValidationResource(sArtefactPathType, sArtefactPath);
if (aRes == null) {
if (LOGGER.isDebugEnabled())
LOGGER.debug("Failed to resolve ValidationArtefact '" + sArtefactPathType + "' with path '" + sArtefactPath + "'");
continue;
}
final ValidationArtefact aVA = new ValidationArtefact(aValidationType, aRes);
if (eSuccess.isUndefined()) {
// Ignored level
ret.add(ValidationResult.createIgnoredResult(aVA));
} else {
// We have results
final IJsonArray aItems = aResultObj.getAsArray(JSON_ITEMS);
final ErrorList aErrorList = new ErrorList();
for (final IJson aItem : aItems) {
final IJsonObject aItemObj = aItem.getAsObject();
if (aItemObj != null) {
final IError aError = getAsIError(aItemObj);
aErrorList.add(aError);
}
}
final ValidationResult aVR = new ValidationResult(aVA, aErrorList);
ret.add(aVR);
}
}
}
return ret;
}
use of com.helger.phive.api.result.ValidationResultList in project phive by phax.
the class PhiveJsonHelperTest method testValidationResultsBackAndForth.
@Test
public void testValidationResultsBackAndForth() {
// Register
final ValidationExecutorSetRegistry<IValidationSourceXML> aRegistry = new ValidationExecutorSetRegistry<>();
final VESID aVESID = new VESID("group", "art", "1.0");
final ValidationExecutorSet<IValidationSourceXML> aVES = new ValidationExecutorSet<>(aVESID, "name", false);
aVES.addExecutor(ValidationExecutorXSD.create(new ClassPathResource("test/schema1.xsd")));
aRegistry.registerValidationExecutorSet(aVES);
// Validate
final ValidationResultList aVRL = ValidationExecutionManager.executeValidation(aVES, ValidationSourceXML.create(new ClassPathResource("test/schema1.xml")));
assertTrue(aVRL.containsAtLeastOneError());
// To JSON
final Locale aDisplayLocale = Locale.US;
final IJsonObject aObj = new JsonObject();
PhiveJsonHelper.applyValidationResultList(aObj, aVES, aVRL, aDisplayLocale, 123, null, null);
// And back
final IValidationExecutorSet<IValidationSourceXML> aVES2 = PhiveJsonHelper.getAsVES(aRegistry, aObj);
assertNotNull(aVES2);
assertSame(aVES, aVES2);
final ValidationResultList aVRL2 = PhiveJsonHelper.getAsValidationResultList(aObj);
assertNotNull(aVRL2);
// direct equals doesn't work, because of the restored exception
assertEquals(aVRL.size(), aVRL2.size());
assertEquals(1, aVRL.size());
assertEquals(aVRL.get(0).getErrorList().size(), aVRL2.get(0).getErrorList().size());
// and forth
final IJsonObject aObj2 = new JsonObject();
PhiveJsonHelper.applyValidationResultList(aObj2, aVES2, aVRL2, aDisplayLocale, 123, null, null);
CommonsTestHelper.testDefaultImplementationWithEqualContentObject(aObj, aObj2);
}
use of com.helger.phive.api.result.ValidationResultList in project phive by phax.
the class IValidationExecutionManager method executeValidation.
/**
* Perform a validation with all the contained executors.
*
* @param aSource
* The source artefact to be validated. May not be <code>null</code>.
* @param aLocale
* Custom locale to use e.g. for error messages. May be
* <code>null</code> to use the system default locale.
* @return The validation result list. Never <code>null</code>. For each
* contained executor a result is added to the result list.
* @see #executeValidation(IValidationSource, ValidationResultList, Locale)
*/
@Nonnull
default ValidationResultList executeValidation(@Nonnull final SOURCETYPE aSource, @Nullable final Locale aLocale) {
final ValidationResultList ret = new ValidationResultList();
executeValidation(aSource, ret, aLocale);
return ret;
}
use of com.helger.phive.api.result.ValidationResultList in project en16931-cii2ubl by phax.
the class CIIToUBL21ConverterTest method testConvertAndValidateAll.
@Test
public void testConvertAndValidateAll() {
final String sBasePath = MockSettings.getBaseDir().getAbsolutePath();
for (final File aFile : MockSettings.getAllTestFiles()) {
LOGGER.info("Converting " + aFile.toString() + " to UBL 2.1");
// Main conversion
final ErrorList aErrorList = new ErrorList();
final Serializable aInvoice = new CIIToUBL21Converter().convertCIItoUBL(aFile, aErrorList);
assertTrue("Errors: " + aErrorList.toString(), aErrorList.isEmpty());
assertNotNull(aInvoice);
final File aDestFile = new File("toubl21/" + aFile.getParentFile().getAbsolutePath().substring(sBasePath.length()), FilenameHelper.getBaseName(aFile.getName()) + "-ubl.xml");
final ValidationResultList aResultList;
if (aInvoice instanceof InvoiceType) {
final InvoiceType aUBLInvoice = (InvoiceType) aInvoice;
// Check UBL XSD scheme
final UBL21WriterBuilder<InvoiceType> aWriter = UBL21Writer.invoice().setFormattedOutput(true);
aWriter.write(aUBLInvoice, aDestFile);
// Validate against EN16931 validation rules
aResultList = ValidationExecutionManager.executeValidation(MockSettings.VES_REGISTRY.getOfID(EN16931Validation.VID_UBL_INVOICE_137), ValidationSourceXML.create(new FileSystemResource(aDestFile)));
} else {
final CreditNoteType aUBLInvoice = (CreditNoteType) aInvoice;
// Check UBL XSD scheme
final UBL21WriterBuilder<CreditNoteType> aWriter = UBL21Writer.creditNote().setFormattedOutput(true);
aWriter.write(aUBLInvoice, aDestFile);
// Validate against EN16931 validation rules
aResultList = ValidationExecutionManager.executeValidation(MockSettings.VES_REGISTRY.getOfID(EN16931Validation.VID_UBL_CREDIT_NOTE_137), ValidationSourceXML.create(new FileSystemResource(aDestFile)));
}
assertNotNull(aResultList);
// Check that no errors (but maybe warnings) are contained
for (final ValidationResult aResult : aResultList) {
assertTrue("Errors: " + aResult.getErrorList().toString(), aResult.getErrorList().isEmpty());
}
}
}
use of com.helger.phive.api.result.ValidationResultList in project en16931-cii2ubl by phax.
the class CIIToUBL23ConverterTest method testConvertAndValidateAll.
@Test
public void testConvertAndValidateAll() {
final String sBasePath = MockSettings.getBaseDir().getAbsolutePath();
for (final File aFile : MockSettings.getAllTestFiles()) {
LOGGER.info("Converting " + aFile.toString() + " to UBL 2.3");
// Main conversion
final ErrorList aErrorList = new ErrorList();
final Serializable aInvoice = new CIIToUBL23Converter().convertCIItoUBL(aFile, aErrorList);
assertTrue("Errors: " + aErrorList.toString(), aErrorList.isEmpty());
assertNotNull(aInvoice);
final File aDestFile = new File("toubl23/" + aFile.getParentFile().getAbsolutePath().substring(sBasePath.length()), FilenameHelper.getBaseName(aFile.getName()) + "-ubl.xml");
final ValidationResultList aResultList;
if (aInvoice instanceof InvoiceType) {
final InvoiceType aUBLInvoice = (InvoiceType) aInvoice;
// Check UBL XSD scheme
final UBL23WriterBuilder<InvoiceType> aWriter = UBL23Writer.invoice().setFormattedOutput(true);
aWriter.write(aUBLInvoice, aDestFile);
// Validate against EN16931 validation rules
aResultList = ValidationExecutionManager.executeValidation(MockSettings.VES_REGISTRY.getOfID(EN16931Validation.VID_UBL_INVOICE_137), ValidationSourceXML.create(new FileSystemResource(aDestFile)));
} else {
final CreditNoteType aUBLInvoice = (CreditNoteType) aInvoice;
// Check UBL XSD scheme
final UBL23WriterBuilder<CreditNoteType> aWriter = UBL23Writer.creditNote().setFormattedOutput(true);
aWriter.write(aUBLInvoice, aDestFile);
// Validate against EN16931 validation rules
aResultList = ValidationExecutionManager.executeValidation(MockSettings.VES_REGISTRY.getOfID(EN16931Validation.VID_UBL_CREDIT_NOTE_137), ValidationSourceXML.create(new FileSystemResource(aDestFile)));
}
assertNotNull(aResultList);
// Check that no errors (but maybe warnings) are contained
for (final ValidationResult aResult : aResultList) {
assertTrue("Errors: " + aResult.getErrorList().toString(), aResult.getErrorList().isEmpty());
}
}
}
Aggregations