Search in sources :

Example 51 with Validator

use of javax.xml.validation.Validator in project ORCID-Source by ORCID.

the class ValidateV2IdentifiersTest method testPerson.

/**
     * <external-identifier:external-identifiers>
                <external-identifier:external-identifier visibility="public" put-code="1">
                        <common:external-id-type>type-1</common:external-id-type>
                        <common:external-id-value>value-1</common:external-id-value>
                        <common:external-id-url>http://url.com/1</common:external-id-url>
                        <common:created-date>2001-12-31T12:00:00</common:created-date>
                        <common:last-modified-date>2001-12-31T12:00:00</common:last-modified-date>
                        <common:source>
                                <common:source-orcid>
                                        <common:uri>http://orcid.org/8888-8888-8888-8880</common:uri>
                                        <common:path>8888-8888-8888-8880</common:path>
                                        <common:host>orcid.org</common:host>
                                </common:source-orcid>
                                <common:source-name />
                        </common:source>
                </external-identifier:external-identifier>
        </external-identifier:external-identifiers>
        
     * @throws SAXException
     * @throws IOException
     * @throws JAXBException
     * @throws ParserConfigurationException
     */
@Test
public void testPerson() throws SAXException, IOException, JAXBException, ParserConfigurationException {
    Person person = unmarshallFromPath("/record_2.0/samples/read_samples/person-2.0.xml", Person.class);
    assertEquals("credit-name", person.getName().getCreditName().getContent());
    assertEquals(1, person.getExternalIdentifiers().getExternalIdentifiers().size());
    PersonExternalIdentifier id = person.getExternalIdentifiers().getExternalIdentifiers().get(0);
    assertEquals("type-1", id.getType());
    assertEquals("value-1", id.getValue());
    assertEquals(new Url("http://url.com/1"), id.getUrl());
    assertNull(id.getRelationship());
    assertNotNull(id.getCreatedDate().getValue());
    assertNotNull(id.getLastModifiedDate().getValue());
    assertEquals(new Long(1), id.getPutCode());
    assertEquals(Visibility.PUBLIC, id.getVisibility());
    Validator validator = getValidator("person");
    validator.validate(marshall(Person.class, person));
    validator.validate(marshallToDOM(Person.class, person));
}
Also used : PersonExternalIdentifier(org.orcid.jaxb.model.record_v2.PersonExternalIdentifier) Person(org.orcid.jaxb.model.record_v2.Person) Url(org.orcid.jaxb.model.common_v2.Url) Validator(javax.xml.validation.Validator) MarshallingTest(org.orcid.jaxb.model.notification.custom.MarshallingTest) Test(org.junit.Test)

Example 52 with Validator

use of javax.xml.validation.Validator in project ORCID-Source by ORCID.

the class ValidateV2RC1SamplesTest method validateGroupIdValue.

@Test
public void validateGroupIdValue() throws SAXException, IOException, JAXBException {
    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    Schema schema = factory.newSchema(getClass().getResource("/group-id-2.0_rc1/group-id-2.0_rc1.xsd"));
    Validator validator = schema.newValidator();
    char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    char[] upperAlphabet = "abcdefghijklmnopqrstuvwxyz".toUpperCase().toCharArray();
    char[] numbers = "0123456789".toCharArray();
    char[] validCharacters = "^._~:/?#[]@!$&'()*+,;=-".toCharArray();
    //All valid characters
    char[] allValid = ArrayUtils.addAll(alphabet, upperAlphabet);
    allValid = ArrayUtils.addAll(allValid, numbers);
    allValid = ArrayUtils.addAll(allValid, validCharacters);
    String invalidCharactersString = "{}\"<>\\";
    char[] invalidCharacters = invalidCharactersString.toCharArray();
    //All valid and non valid characters
    char[] allWithInvalids = ArrayUtils.addAll(allValid, invalidCharacters);
    GroupIdRecord g1 = new GroupIdRecord();
    g1.setDescription("description");
    g1.setType("newsletter");
    System.out.println("Validating group_id agains a list of 3000 valid values");
    for (int i = 0; i < 3000; i++) {
        String randomValue = "orcid-generated:" + RandomStringUtils.random(200, allValid);
        g1.setName(randomValue);
        g1.setGroupId(randomValue);
        JAXBContext context;
        context = JAXBContext.newInstance(GroupIdRecord.class);
        Source source = new JAXBSource(context, g1);
        try {
            validator.validate(source);
        } catch (Exception e) {
            fail("fail validating: " + randomValue + " on iteration " + i);
        }
    }
    System.out.println("Validating group_id agains a list of 3000 invalid values");
    for (int i = 0; i < 3000; i++) {
        String randomValue = "orcid-generated:" + RandomStringUtils.random(200, allWithInvalids);
        boolean regenerateString = true;
        do {
            for (int j = 0; j < randomValue.length(); j++) {
                if (invalidCharactersString.contains(String.valueOf(randomValue.charAt(j)))) {
                    regenerateString = false;
                    break;
                }
            }
            if (regenerateString) {
                randomValue += RandomStringUtils.random(3, invalidCharacters);
                regenerateString = false;
            }
        } while (regenerateString);
        g1.setName(randomValue);
        g1.setGroupId(randomValue);
        JAXBContext context;
        context = JAXBContext.newInstance(GroupIdRecord.class);
        Source source = new JAXBSource(context, g1);
        try {
            validator.validate(source);
            fail(randomValue + " should not be vaild according to the XSD on iteration " + i);
        } catch (Exception e) {
        }
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) GroupIdRecord(org.orcid.jaxb.model.groupid_rc1.GroupIdRecord) Schema(javax.xml.validation.Schema) JAXBContext(javax.xml.bind.JAXBContext) Validator(javax.xml.validation.Validator) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JAXBSource(javax.xml.bind.util.JAXBSource) JAXBSource(javax.xml.bind.util.JAXBSource) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) MarshallingTest(org.orcid.jaxb.model.notification.custom.MarshallingTest) Test(org.junit.Test)

Example 53 with Validator

use of javax.xml.validation.Validator in project ORCID-Source by ORCID.

the class ValidateV2RC3Identifiers method getValidator.

public Validator getValidator(String name) throws SAXException {
    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    Schema schema = factory.newSchema(getClass().getResource("/record_2.0_rc3/" + name + "-2.0_rc3.xsd"));
    Validator validator = schema.newValidator();
    return validator;
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) Validator(javax.xml.validation.Validator)

Example 54 with Validator

use of javax.xml.validation.Validator in project ORCID-Source by ORCID.

the class ValidateV2_1IdentifiersTest method validateSampleXML.

public void validateSampleXML(String name) throws SAXException, IOException {
    Source source = getInputStream("/record_2.1/samples/read_samples/" + name + "-2.1.xml");
    Validator validator = getValidator(name);
    validator.validate(source);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JAXBSource(javax.xml.bind.util.JAXBSource) Validator(javax.xml.validation.Validator)

Example 55 with Validator

use of javax.xml.validation.Validator in project ORCID-Source by ORCID.

the class ValidateV2_1IdentifiersTest method testPerson.

/**
     * <external-identifier:external-identifiers>
                <external-identifier:external-identifier visibility="public" put-code="1">
                        <common:external-id-type>type-1</common:external-id-type>
                        <common:external-id-value>value-1</common:external-id-value>
                        <common:external-id-url>http://url.com/1</common:external-id-url>
                        <common:created-date>2001-12-31T12:00:00</common:created-date>
                        <common:last-modified-date>2001-12-31T12:00:00</common:last-modified-date>
                        <common:source>
                                <common:source-orcid>
                                        <common:uri>https://orcid.org/8888-8888-8888-8880</common:uri>
                                        <common:path>8888-8888-8888-8880</common:path>
                                        <common:host>orcid.org</common:host>
                                </common:source-orcid>
                                <common:source-name />
                        </common:source>
                </external-identifier:external-identifier>
        </external-identifier:external-identifiers>
        
     * @throws SAXException
     * @throws IOException
     * @throws JAXBException
     * @throws ParserConfigurationException
     */
@Test
public void testPerson() throws SAXException, IOException, JAXBException, ParserConfigurationException {
    Person person = unmarshallFromPath("/record_2.1/samples/read_samples/person-2.1.xml", Person.class);
    assertEquals("credit-name", person.getName().getCreditName().getContent());
    assertEquals(1, person.getExternalIdentifiers().getExternalIdentifiers().size());
    PersonExternalIdentifier id = person.getExternalIdentifiers().getExternalIdentifiers().get(0);
    assertEquals("type-1", id.getType());
    assertEquals("value-1", id.getValue());
    assertEquals(new Url("http://url.com/1"), id.getUrl());
    assertNull(id.getRelationship());
    assertNotNull(id.getCreatedDate().getValue());
    assertNotNull(id.getLastModifiedDate().getValue());
    assertEquals(new Long(1), id.getPutCode());
    assertEquals(Visibility.PUBLIC, id.getVisibility());
    Validator validator = getValidator("person");
    validator.validate(marshall(Person.class, person));
    validator.validate(marshallToDOM(Person.class, person));
}
Also used : PersonExternalIdentifier(org.orcid.jaxb.model.record_v2.PersonExternalIdentifier) Person(org.orcid.jaxb.model.record_v2.Person) Url(org.orcid.jaxb.model.common_v2.Url) Validator(javax.xml.validation.Validator) MarshallingTest(org.orcid.jaxb.model.notification.custom.MarshallingTest) Test(org.junit.Test)

Aggregations

Validator (javax.xml.validation.Validator)80 Schema (javax.xml.validation.Schema)51 SchemaFactory (javax.xml.validation.SchemaFactory)39 StreamSource (javax.xml.transform.stream.StreamSource)38 DOMSource (javax.xml.transform.dom.DOMSource)30 Source (javax.xml.transform.Source)29 Test (org.junit.Test)21 SAXException (org.xml.sax.SAXException)21 IOException (java.io.IOException)17 MarshallingTest (org.orcid.jaxb.model.notification.custom.MarshallingTest)17 Document (org.w3c.dom.Document)13 InputStream (java.io.InputStream)9 URL (java.net.URL)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9 Url (org.orcid.jaxb.model.common_v2.Url)8 InputSource (org.xml.sax.InputSource)8 StringReader (java.io.StringReader)7 JAXBSource (javax.xml.bind.util.JAXBSource)7 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)7 SAXParseException (org.xml.sax.SAXParseException)7