Search in sources :

Example 1 with ValidationResultList

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;
}
Also used : IValidationType(com.helger.phive.api.IValidationType) ETriState(com.helger.commons.state.ETriState) ErrorList(com.helger.commons.error.list.ErrorList) IJsonObject(com.helger.json.IJsonObject) ValidationResultList(com.helger.phive.api.result.ValidationResultList) IReadableResource(com.helger.commons.io.resource.IReadableResource) ValidationArtefact(com.helger.phive.api.artefact.ValidationArtefact) IJson(com.helger.json.IJson) IJsonArray(com.helger.json.IJsonArray) IError(com.helger.commons.error.IError) ValidationResult(com.helger.phive.api.result.ValidationResult) Nullable(javax.annotation.Nullable)

Example 2 with ValidationResultList

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);
}
Also used : ValidationExecutorSet(com.helger.phive.api.executorset.ValidationExecutorSet) IValidationExecutorSet(com.helger.phive.api.executorset.IValidationExecutorSet) Locale(java.util.Locale) VESID(com.helger.phive.api.executorset.VESID) IJsonObject(com.helger.json.IJsonObject) ValidationResultList(com.helger.phive.api.result.ValidationResultList) IJsonObject(com.helger.json.IJsonObject) JsonObject(com.helger.json.JsonObject) IValidationSourceXML(com.helger.phive.engine.source.IValidationSourceXML) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) ValidationExecutorSetRegistry(com.helger.phive.api.executorset.ValidationExecutorSetRegistry) Test(org.junit.Test)

Example 3 with ValidationResultList

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;
}
Also used : ValidationResultList(com.helger.phive.api.result.ValidationResultList) Nonnull(javax.annotation.Nonnull)

Example 4 with ValidationResultList

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());
        }
    }
}
Also used : Serializable(java.io.Serializable) ErrorList(com.helger.commons.error.list.ErrorList) ValidationResultList(com.helger.phive.api.result.ValidationResultList) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) ValidationResult(com.helger.phive.api.result.ValidationResult) File(java.io.File) CreditNoteType(oasis.names.specification.ubl.schema.xsd.creditnote_21.CreditNoteType) CrossIndustryInvoiceType(un.unece.uncefact.data.standard.crossindustryinvoice._100.CrossIndustryInvoiceType) InvoiceType(oasis.names.specification.ubl.schema.xsd.invoice_21.InvoiceType) Test(org.junit.Test)

Example 5 with ValidationResultList

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());
        }
    }
}
Also used : Serializable(java.io.Serializable) ErrorList(com.helger.commons.error.list.ErrorList) ValidationResultList(com.helger.phive.api.result.ValidationResultList) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) ValidationResult(com.helger.phive.api.result.ValidationResult) File(java.io.File) CreditNoteType(oasis.names.specification.ubl.schema.xsd.creditnote_23.CreditNoteType) CrossIndustryInvoiceType(un.unece.uncefact.data.standard.crossindustryinvoice._100.CrossIndustryInvoiceType) InvoiceType(oasis.names.specification.ubl.schema.xsd.invoice_23.InvoiceType) Test(org.junit.Test)

Aggregations

ValidationResultList (com.helger.phive.api.result.ValidationResultList)8 ErrorList (com.helger.commons.error.list.ErrorList)4 ValidationResult (com.helger.phive.api.result.ValidationResult)4 File (java.io.File)4 Test (org.junit.Test)4 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)3 Serializable (java.io.Serializable)3 CrossIndustryInvoiceType (un.unece.uncefact.data.standard.crossindustryinvoice._100.CrossIndustryInvoiceType)3 IJsonObject (com.helger.json.IJsonObject)2 IValidationSourceXML (com.helger.phive.engine.source.IValidationSourceXML)2 IError (com.helger.commons.error.IError)1 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)1 IReadableResource (com.helger.commons.io.resource.IReadableResource)1 ETriState (com.helger.commons.state.ETriState)1 IJson (com.helger.json.IJson)1 IJsonArray (com.helger.json.IJsonArray)1 JsonObject (com.helger.json.JsonObject)1 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)1 AS4IncomingDumperFileBased (com.helger.phase4.dump.AS4IncomingDumperFileBased)1 AS4OutgoingDumperFileBased (com.helger.phase4.dump.AS4OutgoingDumperFileBased)1