1 | package eu.clarin.cmdi.virtualcollectionregistry.service.impl; |
---|
2 | |
---|
3 | import eu.clarin.cmdi.virtualcollectionregistry.model.Creator; |
---|
4 | import eu.clarin.cmdi.virtualcollectionregistry.model.Resource; |
---|
5 | import eu.clarin.cmdi.virtualcollectionregistry.model.User; |
---|
6 | import eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollection; |
---|
7 | import eu.clarin.cmdi.virtualcollectionregistry.pid.PersistentIdentifier; |
---|
8 | import java.io.IOException; |
---|
9 | import java.io.InputStreamReader; |
---|
10 | import java.io.StringReader; |
---|
11 | import java.io.StringWriter; |
---|
12 | import java.text.DateFormat; |
---|
13 | import static java.text.DateFormat.SHORT; |
---|
14 | import java.text.ParseException; |
---|
15 | import java.util.List; |
---|
16 | import java.util.Locale; |
---|
17 | import java.util.concurrent.CopyOnWriteArrayList; |
---|
18 | import java.util.regex.Pattern; |
---|
19 | import javax.xml.stream.XMLOutputFactory; |
---|
20 | import javax.xml.stream.XMLStreamWriter; |
---|
21 | import org.custommonkey.xmlunit.Diff; |
---|
22 | import org.custommonkey.xmlunit.Difference; |
---|
23 | import org.custommonkey.xmlunit.DifferenceListener; |
---|
24 | import org.custommonkey.xmlunit.XMLTestCase; |
---|
25 | import org.custommonkey.xmlunit.XMLUnit; |
---|
26 | import org.junit.Test; |
---|
27 | import org.w3c.dom.Node; |
---|
28 | import org.xml.sax.SAXException; |
---|
29 | |
---|
30 | /** |
---|
31 | * |
---|
32 | * @author twagoo |
---|
33 | */ |
---|
34 | public class VirtualCollectionCMDIWriterImplTest extends XMLTestCase { |
---|
35 | |
---|
36 | private static final String CONTROL_INSTANCE = "/vc-instance1.xml"; |
---|
37 | |
---|
38 | /** |
---|
39 | * Patterns for paths to be ignored in XML comparison |
---|
40 | */ |
---|
41 | private final List<Pattern> IGNORE_PATHS = new CopyOnWriteArrayList<>(new Pattern[]{ |
---|
42 | //ignore creation date |
---|
43 | Pattern.compile("\\/CMD\\[1\\]\\/Header\\[1\\]\\/MdCreationDate\\[1\\]\\/text\\(\\).*"), |
---|
44 | //ignore resource proxy id's |
---|
45 | Pattern.compile("\\/CMD\\[1\\]\\/Resources\\[1\\]\\/ResourceProxyList\\[1\\]\\/ResourceProxy\\[.*\\]\\/@id"), |
---|
46 | //ignore resource proxy references from Resource component instances |
---|
47 | Pattern.compile("\\/CMD\\[1\\]\\/Components\\[1\\]\\/VirtualCollection\\[1\\]\\/Resource\\[.*\\]\\/@ref") |
---|
48 | }); |
---|
49 | |
---|
50 | /** |
---|
51 | * Test of writeCMDI method, of class VirtualCollectionCMDIWriterImpl. |
---|
52 | * |
---|
53 | * @throws java.lang.Exception |
---|
54 | */ |
---|
55 | @Test |
---|
56 | public void testWriteCMDIExtensional() throws Exception { |
---|
57 | final VirtualCollectionCMDIWriterImpl instance = new VirtualCollectionCMDIWriterImpl(new VirtualCollectionCMDICreatorImpl()); |
---|
58 | |
---|
59 | // create a collection to serialize |
---|
60 | final VirtualCollection vc = createTestVC(); |
---|
61 | |
---|
62 | // prepare an output writer |
---|
63 | final XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory(); |
---|
64 | final StringWriter stringWriter = new StringWriter(); |
---|
65 | final XMLStreamWriter xmlWriter = xmlOutputFactory.createXMLStreamWriter(stringWriter); |
---|
66 | |
---|
67 | // write CMDI for instance to the writer |
---|
68 | xmlWriter.writeStartDocument(); |
---|
69 | instance.writeCMDI(xmlWriter, vc); |
---|
70 | xmlWriter.writeEndDocument(); |
---|
71 | xmlWriter.close(); |
---|
72 | |
---|
73 | // compare output to control document |
---|
74 | final StringReader testOutputReader = new StringReader(stringWriter.toString()); |
---|
75 | final InputStreamReader controlReader = new InputStreamReader(getClass().getResourceAsStream(CONTROL_INSTANCE)); |
---|
76 | |
---|
77 | assertCMDIEqual(controlReader, testOutputReader); |
---|
78 | } |
---|
79 | |
---|
80 | private VirtualCollection createTestVC() throws ParseException { |
---|
81 | final VirtualCollection vc = new VirtualCollection(); |
---|
82 | vc.setName("Virtual Collection Name"); |
---|
83 | vc.setDescription("Test collection description"); |
---|
84 | vc.setType(VirtualCollection.Type.EXTENSIONAL); |
---|
85 | vc.setReproducibility(VirtualCollection.Reproducibility.INTENDED); |
---|
86 | vc.setPurpose(VirtualCollection.Purpose.SAMPLE); |
---|
87 | |
---|
88 | vc.setOwner(new User("Test user")); |
---|
89 | vc.setCreationDate(DateFormat.getDateInstance(SHORT, Locale.US).parse("01/01/14")); |
---|
90 | |
---|
91 | vc.setState(VirtualCollection.State.PUBLIC_PENDING); |
---|
92 | //setting the pid requires 'public pending' state and will set the state to public |
---|
93 | vc.setPersistentIdentifier(new PersistentIdentifier(vc, PersistentIdentifier.Type.HANDLE, "9999/1234-5678")); |
---|
94 | |
---|
95 | final Creator creator = new Creator("Test creator"); |
---|
96 | creator.setEMail("test@creator.org"); |
---|
97 | creator.setOrganisation("Test Inc."); |
---|
98 | vc.getCreators().add(creator); |
---|
99 | |
---|
100 | final Resource resource1 = new Resource(Resource.Type.METADATA, "http://my/metadata.cmdi"); |
---|
101 | resource1.setLabel("Label of first"); |
---|
102 | vc.getResources().add(resource1); |
---|
103 | |
---|
104 | final Resource resource2 = new Resource(Resource.Type.RESOURCE, "http://my/resource.mpg"); |
---|
105 | resource2.setDescription("Description of second"); |
---|
106 | vc.getResources().add(resource2); |
---|
107 | |
---|
108 | // add one without label or description |
---|
109 | vc.getResources().add(new Resource(Resource.Type.RESOURCE, "http://other/resource.mpg")); |
---|
110 | return vc; |
---|
111 | } |
---|
112 | |
---|
113 | private void assertCMDIEqual(final InputStreamReader controlReader, final StringReader testOutputReader) throws IOException, SAXException { |
---|
114 | XMLUnit.setIgnoreWhitespace(true); |
---|
115 | XMLUnit.setIgnoreAttributeOrder(true); |
---|
116 | |
---|
117 | final Diff diff = XMLUnit.compareXML(controlReader, testOutputReader); |
---|
118 | setIgnorePaths(diff); |
---|
119 | |
---|
120 | assertTrue(diff.toString(), diff.similar()); |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * approach based on |
---|
125 | * http://ayazanwar.wordpress.com/2012/09/23/xmlunit-ignoring-certain-pieces-of-xml-during-xml-comparison-using-regular-expression/ |
---|
126 | * |
---|
127 | * @param diff |
---|
128 | * @param ignorePaths |
---|
129 | */ |
---|
130 | private void setIgnorePaths(final Diff diff) { |
---|
131 | diff.overrideDifferenceListener(new DifferenceListener() { |
---|
132 | |
---|
133 | @Override |
---|
134 | public int differenceFound(Difference difference) { |
---|
135 | final String testXPath = difference.getTestNodeDetail().getXpathLocation(); |
---|
136 | for (Pattern ignorePath : IGNORE_PATHS) { |
---|
137 | if (ignorePath.matcher(testXPath).find()) { |
---|
138 | return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR; |
---|
139 | } |
---|
140 | } |
---|
141 | return DifferenceListener.RETURN_ACCEPT_DIFFERENCE; |
---|
142 | } |
---|
143 | |
---|
144 | @Override |
---|
145 | public void skippedComparison(Node control, Node test) { |
---|
146 | } |
---|
147 | }); |
---|
148 | } |
---|
149 | |
---|
150 | } |
---|