Commit 5e2266a5 authored by R.W.Majeed's avatar R.W.Majeed

XML test documents and test cases for validation

parent c1a17f27
Pipeline #1262 skipped
package org.aktin.exchange;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.xml.bind.JAXB;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
public class TestUnmarshallDocuments {
XMLReader reader;
Validator validator;
@Before
public void initializeXIncludeReader() throws SAXException{
SAXParser parser;
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setXIncludeAware(true);
factory.setNamespaceAware(true);
// do not insert xml:base attributes for XIncludes
factory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false);
parser = factory.newSAXParser();
} catch (ParserConfigurationException e) {
throw new SAXException(e);
}
reader = parser.getXMLReader();
}
@Before
public void initializeValidator() throws IOException, SAXException{
URL xsd = getClass().getResource("/schemagen/schema1.xsd");
SchemaFactory factory =
SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema;
try( InputStream in = xsd.openStream() ){
schema = factory.newSchema(new StreamSource(in));
}
validator = schema.newValidator();
}
private Source getResource(URL url){
SAXSource xml = new SAXSource(reader, new InputSource(url.toString()));
//xml.setSystemId(doc.toString());
return xml;
}
private Source getResource(String name){
return getResource(getClass().getResource(name));
}
@Test
public void validateQuery() throws IOException, SAXException{
validator.validate(getResource("/query.xml"));
}
@Test
public void unmarshallQuery() throws IOException, SAXException{
Source xml = getResource("/query.xml");
Query query = JAXB.unmarshal(xml, Query.class);
Assert.assertEquals(query.schedule.getClass(), SingleExecution.class);
SingleExecution se = (SingleExecution)query.schedule;
Assert.assertNotNull(se.duration);
Assert.assertNotNull(se.reference);
//System.out.println("Duration:"+se.duration);
//System.out.println("Reference:"+se.reference);
// print XML output
//se.duration = Period.ofMonths(1);
//se.reference = Instant.now();
//JAXB.marshal(query, System.out);
}
@Test
public void validateQueryRequest() throws IOException, SAXException, TransformerException{
validator.validate(getResource("/request.xml"));
}
}
<query xmlns="http://aktin.org/ns/exchange"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<id>123</id>
<description>Lala</description>
<principal>
<name>Prince Ipal</name>
<organisation>AKTIN</organisation>
<email>it-support@aktin.org</email>
<phone>+49 (441) 798 - 2772</phone>
</principal>
<schedule xsi:type="singleExecution">
<duration>-P1M</duration>
<reference>2016-04-01T00:00:00Z</reference>
</schedule>
<concepts>
<concept id="birthdate" xsi:type="raw"/>
</concepts>
</query>
<queryRequest xmlns="http://aktin.org/ns/exchange"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xml="http://www.w3.org/XML/1998/namespace">
<id>123</id>
<xi:include href="query.xml"/>
</queryRequest>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment