Search in sources :

Example 1 with VESID

use of com.helger.phive.api.executorset.VESID 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 2 with VESID

use of com.helger.phive.api.executorset.VESID in project phive by phax.

the class VOM1Converter method _createExecutorSchematron.

@Nonnull
private ValidationExecutorSchematron _createExecutorSchematron(@Nonnull final VOMSchematronType aSchematron) {
    final IReadableResource aRes;
    {
        final String sBuiltIn = aSchematron.getBuiltIn();
        if (StringHelper.hasText(sBuiltIn)) {
            LOGGER.info("Trying to resolve built-in Schematron artifact '" + sBuiltIn + "'");
            aRes = m_aResourceResolver.getResourceOfID(sBuiltIn);
            if (aRes == null)
                throw new IllegalStateException("Failed to resolve built-in Schematron artifact '" + sBuiltIn + "'");
        } else {
            // External resource
            final VESID aVESID = _createVESID(aSchematron.getResource());
            LOGGER.info("Trying to resolve Schematron artifact with ID '" + aVESID.getAsSingleID() + "'");
            aRes = m_aArtifactResolver.getArtifactOfID(aVESID);
            if (aRes == null)
                throw new IllegalStateException("Failed to resolve Schematron artifact with ID '" + aVESID.getAsSingleID() + "'");
        }
    }
    final EValidationType eValidationType;
    if (StringHelper.hasNoText(aSchematron.getType()))
        eValidationType = EValidationType.SCHEMATRON_SCH;
    else
        switch(aSchematron.getType()) {
            case "pure":
                eValidationType = EValidationType.SCHEMATRON_PURE;
                break;
            case "sch":
                eValidationType = EValidationType.SCHEMATRON_SCH;
                break;
            case "xslt":
                eValidationType = EValidationType.SCHEMATRON_XSLT;
                break;
            case "schxslt":
                eValidationType = EValidationType.SCHEMATRON_SCHXSLT;
                break;
            case "oioubl":
                eValidationType = EValidationType.SCHEMATRON_OIOUBL;
                break;
            default:
                throw new IllegalStateException("The Schematron type '" + aSchematron.getType() + "' is unsupported.");
        }
    if (aSchematron.getPrerequisiteCount() > 1)
        throw new IllegalStateException("Currently only 1 prerequsite path is supported");
    final MapBasedNamespaceContext aNamespaceContext = new MapBasedNamespaceContext();
    final VOMNamespacesType aNamespaces = aSchematron.getNamespaces();
    if (aNamespaces != null) {
        final String sBuiltIn = aNamespaces.getBuiltIn();
        if (sBuiltIn != null) {
            // Built-in
            LOGGER.info("Trying to resolve built-in namespace context '" + sBuiltIn + "'");
            final MapBasedNamespaceContext ret = m_aNamespaceContextResolver.getNamespaceContextOfID(sBuiltIn);
            if (ret == null)
                throw new IllegalStateException("Failed to resolve built-in namespace context with ID '" + sBuiltIn + "'");
            aNamespaceContext.addMappings(ret);
        } else {
        // Start empty
        }
        for (final VOMNamespaceMappingType aMapping : aNamespaces.getMapping()) {
            // Default to the default namespace prefix
            final String sPrefix = StringHelper.getNotNull(aMapping.getPrefix(), "");
            aNamespaceContext.setMapping(sPrefix, aMapping.getNamespace());
        }
    }
    final ValidationExecutorSchematron ret = new ValidationExecutorSchematron(new ValidationArtefact(eValidationType, aRes), aSchematron.hasPrerequisiteEntries() ? aSchematron.getPrerequisiteAtIndex(0) : null, aNamespaceContext.hasAnyMapping() ? aNamespaceContext : null);
    // Custom errors afterwards (optional)
    for (final VOMCustomError aCE : aSchematron.getCustomError()) ret.addCustomErrorLevel(aCE.getId(), getAsErrorLevel(aCE.getLevel()));
    if (aSchematron.hasOptionEntries())
        LOGGER.warn("Ignoring all Schematron options");
    return ret;
}
Also used : MapBasedNamespaceContext(com.helger.xml.namespace.MapBasedNamespaceContext) VESID(com.helger.phive.api.executorset.VESID) VOMCustomError(com.helger.phive.engine.vom.v10.VOMCustomError) EValidationType(com.helger.phive.api.EValidationType) IReadableResource(com.helger.commons.io.resource.IReadableResource) VOMNamespaceMappingType(com.helger.phive.engine.vom.v10.VOMNamespaceMappingType) ValidationArtefact(com.helger.phive.api.artefact.ValidationArtefact) ValidationExecutorSchematron(com.helger.phive.engine.schematron.ValidationExecutorSchematron) VOMNamespacesType(com.helger.phive.engine.vom.v10.VOMNamespacesType) Nonnull(javax.annotation.Nonnull)

Example 3 with VESID

use of com.helger.phive.api.executorset.VESID in project phive by phax.

the class VOM1Converter method _createExecutorXSD.

@Nonnull
private ValidationExecutorXSD _createExecutorXSD(@Nonnull final VOMXSDType aXsd) {
    final ValidationExecutorXSD ret;
    final String sBuiltIn = aXsd.getBuiltIn();
    if (StringHelper.hasText(sBuiltIn)) {
        LOGGER.info("Trying to resolve built-in XSD artifact '" + sBuiltIn + "'");
        final Schema aSchema = m_aXmlSchemaResolver.getXmlSchemaOfID(sBuiltIn);
        if (aSchema == null)
            throw new IllegalStateException("Failed to resolve built-in XSD artifact '" + sBuiltIn + "'");
        ret = new ValidationExecutorXSD(new ValidationArtefact(EValidationType.XSD, new ReadableResourceByteArray("built-in-" + sBuiltIn, ArrayHelper.EMPTY_BYTE_ARRAY)), () -> aSchema);
    } else {
        // External resource
        final VESID aVESID = _createVESID(aXsd.getResource());
        LOGGER.info("Trying to resolve XSD artifact with ID '" + aVESID.getAsSingleID() + "'");
        final IReadableResource aRes = m_aArtifactResolver.getArtifactOfID(aVESID);
        if (aRes == null)
            throw new IllegalStateException("Failed to resolve XSD artifact with ID '" + aVESID.getAsSingleID() + "'");
        ret = ValidationExecutorXSD.create(aRes);
    }
    if (aXsd.hasOptionEntries())
        LOGGER.warn("Ignoring all XSD options");
    return ret;
}
Also used : VESID(com.helger.phive.api.executorset.VESID) Schema(javax.xml.validation.Schema) ValidationArtefact(com.helger.phive.api.artefact.ValidationArtefact) IReadableResource(com.helger.commons.io.resource.IReadableResource) ValidationExecutorXSD(com.helger.phive.engine.xsd.ValidationExecutorXSD) ReadableResourceByteArray(com.helger.commons.io.resource.inmemory.ReadableResourceByteArray) Nonnull(javax.annotation.Nonnull)

Example 4 with VESID

use of com.helger.phive.api.executorset.VESID in project phive by phax.

the class PhiveJsonHelper method getAsVES.

@Nullable
public static <T extends IValidationSource> IValidationExecutorSet<T> getAsVES(@Nonnull final ValidationExecutorSetRegistry<T> aRegistry, @Nullable final IJsonObject aJson) {
    ValueEnforcer.notNull(aRegistry, "Registry");
    if (aJson != null) {
        IJsonObject aObj = aJson.getAsObject(JSON_VES);
        if (aObj == null)
            aObj = aJson;
        final String sVESID = aObj.getAsString(JSON_VESID);
        if (StringHelper.hasText(sVESID)) {
            final VESID aVESID = VESID.parseIDOrNull(sVESID);
            if (aVESID != null)
                return aRegistry.getOfID(aVESID);
        }
    }
    return null;
}
Also used : VESID(com.helger.phive.api.executorset.VESID) IJsonObject(com.helger.json.IJsonObject) Nullable(javax.annotation.Nullable)

Example 5 with VESID

use of com.helger.phive.api.executorset.VESID in project phive by phax.

the class PhiveJsonHelperTest method testValidationResults.

@Test
public void testValidationResults() {
    final IJsonObject aObj = new JsonObject();
    final Locale aDisplayLocale = Locale.US;
    final VESID aVESID = new VESID("group", "art", "1.0");
    final IValidationExecutorSet<?> aVES = new ValidationExecutorSet<>(aVESID, "name", false);
    PhiveJsonHelper.applyValidationResultList(aObj, aVES, new CommonsArrayList<>(), aDisplayLocale, 123, null, null);
    final String sJson = aObj.getAsJsonString();
    assertEquals("{\"ves\":{\"vesid\":\"group:art:1.0\",\"name\":\"name\",\"deprecated\":false}," + "\"success\":true," + "\"interrupted\":false," + "\"mostSevereErrorLevel\":\"SUCCESS\"," + "\"results\":[]," + "\"durationMS\":123}", sJson);
}
Also used : Locale(java.util.Locale) ValidationExecutorSet(com.helger.phive.api.executorset.ValidationExecutorSet) IValidationExecutorSet(com.helger.phive.api.executorset.IValidationExecutorSet) VESID(com.helger.phive.api.executorset.VESID) IJsonObject(com.helger.json.IJsonObject) IJsonObject(com.helger.json.IJsonObject) JsonObject(com.helger.json.JsonObject) Test(org.junit.Test)

Aggregations

VESID (com.helger.phive.api.executorset.VESID)5 IJsonObject (com.helger.json.IJsonObject)3 IReadableResource (com.helger.commons.io.resource.IReadableResource)2 JsonObject (com.helger.json.JsonObject)2 ValidationArtefact (com.helger.phive.api.artefact.ValidationArtefact)2 IValidationExecutorSet (com.helger.phive.api.executorset.IValidationExecutorSet)2 ValidationExecutorSet (com.helger.phive.api.executorset.ValidationExecutorSet)2 Locale (java.util.Locale)2 Nonnull (javax.annotation.Nonnull)2 Test (org.junit.Test)2 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)1 ReadableResourceByteArray (com.helger.commons.io.resource.inmemory.ReadableResourceByteArray)1 EValidationType (com.helger.phive.api.EValidationType)1 ValidationExecutorSetRegistry (com.helger.phive.api.executorset.ValidationExecutorSetRegistry)1 ValidationResultList (com.helger.phive.api.result.ValidationResultList)1 ValidationExecutorSchematron (com.helger.phive.engine.schematron.ValidationExecutorSchematron)1 IValidationSourceXML (com.helger.phive.engine.source.IValidationSourceXML)1 VOMCustomError (com.helger.phive.engine.vom.v10.VOMCustomError)1 VOMNamespaceMappingType (com.helger.phive.engine.vom.v10.VOMNamespaceMappingType)1 VOMNamespacesType (com.helger.phive.engine.vom.v10.VOMNamespacesType)1