1 | package eu.clarin.cmdi.virtualcollectionregistry.service.impl; |
---|
2 | |
---|
3 | import eu.clarin.cmdi.virtualcollectionregistry.model.Creator; |
---|
4 | import eu.clarin.cmdi.virtualcollectionregistry.model.GeneratedBy; |
---|
5 | import eu.clarin.cmdi.virtualcollectionregistry.model.GeneratedByQuery; |
---|
6 | import eu.clarin.cmdi.virtualcollectionregistry.model.Resource; |
---|
7 | import eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollection; |
---|
8 | import eu.clarin.cmdi.virtualcollectionregistry.service.VirtualCollectionMarshaller.Format; |
---|
9 | import java.io.ByteArrayInputStream; |
---|
10 | import java.io.ByteArrayOutputStream; |
---|
11 | import java.io.InputStream; |
---|
12 | import java.io.InputStreamReader; |
---|
13 | import java.io.Reader; |
---|
14 | import java.io.StringReader; |
---|
15 | import java.text.DateFormat; |
---|
16 | import java.text.SimpleDateFormat; |
---|
17 | import org.custommonkey.xmlunit.Diff; |
---|
18 | import org.custommonkey.xmlunit.XMLUnit; |
---|
19 | import static org.junit.Assert.assertEquals; |
---|
20 | import static org.junit.Assert.assertEquals; |
---|
21 | import static org.junit.Assert.assertEquals; |
---|
22 | import static org.junit.Assert.assertNotNull; |
---|
23 | import static org.junit.Assert.assertNotNull; |
---|
24 | import static org.junit.Assert.assertNotNull; |
---|
25 | import static org.junit.Assert.assertNull; |
---|
26 | import static org.junit.Assert.assertNull; |
---|
27 | import static org.junit.Assert.assertNull; |
---|
28 | |
---|
29 | import static org.junit.Assert.assertTrue; |
---|
30 | import org.junit.Test; |
---|
31 | |
---|
32 | public class VirtualCollectionMarshallerTest { |
---|
33 | |
---|
34 | @Test |
---|
35 | public void testMinmalExtensional() throws Exception { |
---|
36 | VirtualCollectionMarshallerImpl m = |
---|
37 | new VirtualCollectionMarshallerImpl(); |
---|
38 | VirtualCollection vc = new VirtualCollection(); |
---|
39 | vc.setType(VirtualCollection.Type.EXTENSIONAL); |
---|
40 | vc.setName("Test 12"); |
---|
41 | vc.getResources().add(new Resource(Resource.Type.RESOURCE, "a/ref/")); |
---|
42 | |
---|
43 | ByteArrayOutputStream out = new ByteArrayOutputStream(65536); |
---|
44 | m.marshal(out, Format.XML, vc); |
---|
45 | VirtualCollection vc2 = |
---|
46 | m.unmarshal(new ByteArrayInputStream(out.toByteArray()), |
---|
47 | Format.XML, "utf-8"); |
---|
48 | |
---|
49 | assertEquals(vc, vc2); |
---|
50 | } |
---|
51 | |
---|
52 | @Test |
---|
53 | public void testUnmarshallExtensionalCollection() throws Exception { |
---|
54 | VirtualCollectionMarshallerImpl m = |
---|
55 | new VirtualCollectionMarshallerImpl(); |
---|
56 | InputStream in = this.getClass().getClassLoader() |
---|
57 | .getResourceAsStream("VirtualCollection-extensional.xml"); |
---|
58 | VirtualCollection vc = m.unmarshal(in, Format.XML, "utf-8"); |
---|
59 | |
---|
60 | final DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
---|
61 | assertNotNull(vc); |
---|
62 | assertEquals("Test Collection 1", vc.getName()); |
---|
63 | assertEquals(VirtualCollection.Type.EXTENSIONAL, vc.getType()); |
---|
64 | assertEquals("Test Description", vc.getDescription()); |
---|
65 | assertEquals(sdf.parse("2009-12-31"), vc.getCreationDate()); |
---|
66 | assertEquals(VirtualCollection.Purpose.SAMPLE, vc.getPurpose()); |
---|
67 | assertEquals(VirtualCollection.Reproducibility.UNTENDED, |
---|
68 | vc.getReproducibility()); |
---|
69 | assertEquals("Unmaintaned test collection.", |
---|
70 | vc.getReproducibilityNotice()); |
---|
71 | |
---|
72 | assertNotNull(vc.getKeywords()); |
---|
73 | assertEquals(2, vc.getKeywords().size()); |
---|
74 | assertNotNull(vc.getKeywords()); |
---|
75 | assertEquals("test", vc.getKeywords().get(0)); |
---|
76 | assertEquals("unmaintained", vc.getKeywords().get(1)); |
---|
77 | |
---|
78 | assertNotNull(vc.getCreators()); |
---|
79 | assertEquals(2, vc.getCreators().size()); |
---|
80 | Creator c = vc.getCreators().get(0); |
---|
81 | assertNotNull(c); |
---|
82 | assertEquals("Joe User", c.getPerson()); |
---|
83 | assertEquals("Example Address Joe", c.getAddress()); |
---|
84 | assertEquals("joe.user@example.com", c.getEMail()); |
---|
85 | assertEquals("Example Organization", c.getOrganisation()); |
---|
86 | assertEquals("+1-800-555-0101", c.getTelephone()); |
---|
87 | assertEquals("http://www.example.org/~joe/", c.getWebsite()); |
---|
88 | assertEquals("Test Role Joe", c.getRole()); |
---|
89 | c = vc.getCreators().get(1); |
---|
90 | assertNotNull(c); |
---|
91 | assertEquals("Jane User", c.getPerson()); |
---|
92 | assertEquals("Example Address Jane", c.getAddress()); |
---|
93 | assertEquals("jane.user@example.com", c.getEMail()); |
---|
94 | assertEquals("Example Organization", c.getOrganisation()); |
---|
95 | assertEquals("+1-800-555-0102", c.getTelephone()); |
---|
96 | assertEquals("http://www.example.org/~jane/", c.getWebsite()); |
---|
97 | assertEquals("Test Role Jane", c.getRole()); |
---|
98 | |
---|
99 | assertNotNull(vc.getResources()); |
---|
100 | assertEquals(3, vc.getResources().size()); |
---|
101 | |
---|
102 | Resource r = vc.getResources().get(0); |
---|
103 | assertNotNull(r); |
---|
104 | assertEquals(Resource.Type.METADATA, r.getType()); |
---|
105 | assertEquals("http://hdl.handle.net/1839/00-0000-0000-0005-671C-C", |
---|
106 | r.getRef()); |
---|
107 | assertNull(r.getLabel()); |
---|
108 | assertNull(r.getDescription()); |
---|
109 | |
---|
110 | r = vc.getResources().get(1); |
---|
111 | assertNotNull(r); |
---|
112 | assertEquals(Resource.Type.METADATA, r.getType()); |
---|
113 | assertEquals("http://hdl.handle.net/1839/00-0000-0000-0006-671C-C", |
---|
114 | r.getRef()); |
---|
115 | assertEquals("Second metadata resource", r.getLabel()); |
---|
116 | assertNull(r.getDescription()); |
---|
117 | |
---|
118 | r = vc.getResources().get(2); |
---|
119 | assertNotNull(r); |
---|
120 | assertEquals(Resource.Type.METADATA, r.getType()); |
---|
121 | assertEquals("http://hdl.handle.net/1839/00-0000-0000-0007-671C-C", |
---|
122 | r.getRef()); |
---|
123 | assertNull(r.getLabel()); |
---|
124 | assertEquals("Third metadata resource", r.getDescription()); |
---|
125 | |
---|
126 | assertNull(vc.getGeneratedBy()); |
---|
127 | |
---|
128 | // round-tripping |
---|
129 | ByteArrayOutputStream out = new ByteArrayOutputStream(65536); |
---|
130 | m.marshal(out, Format.XML, vc); |
---|
131 | VirtualCollection vc2 = |
---|
132 | m.unmarshal(new ByteArrayInputStream(out.toByteArray()), |
---|
133 | Format.XML, "utf-8"); |
---|
134 | assertEquals(vc.getOwner(), vc2.getOwner()); |
---|
135 | assertEquals(vc.getPersistentIdentifier(), |
---|
136 | vc2.getPersistentIdentifier()); |
---|
137 | assertEquals(vc.getState(), vc2.getState()); |
---|
138 | assertEquals(vc.getType(), vc2.getType()); |
---|
139 | assertEquals(vc.getName(), vc2.getName()); |
---|
140 | assertEquals(vc.getDescription(), vc2.getDescription()); |
---|
141 | assertEquals(vc.getCreationDate(), vc2.getCreationDate()); |
---|
142 | assertEquals(vc.getCreators(), vc2.getCreators()); |
---|
143 | assertEquals(vc.getPurpose(), vc2.getPurpose()); |
---|
144 | assertEquals(vc.getReproducibility(), vc2.getReproducibility()); |
---|
145 | assertEquals(vc.getReproducibilityNotice(), |
---|
146 | vc2.getReproducibilityNotice()); |
---|
147 | assertEquals(vc.getKeywords(), vc2.getKeywords()); |
---|
148 | assertEquals(vc.getResources(), vc2.getResources()); |
---|
149 | assertEquals(vc.getGeneratedBy(), vc2.getGeneratedBy()); |
---|
150 | assertEquals(vc, vc2); |
---|
151 | } |
---|
152 | |
---|
153 | @Test |
---|
154 | public void testUnmarshallIntensionalCollection() throws Exception { |
---|
155 | VirtualCollectionMarshallerImpl m = |
---|
156 | new VirtualCollectionMarshallerImpl(); |
---|
157 | InputStream in = this.getClass().getClassLoader() |
---|
158 | .getResourceAsStream("VirtualCollection-intensional.xml"); |
---|
159 | VirtualCollection vc = m.unmarshal(in, Format.XML, "utf-8"); |
---|
160 | |
---|
161 | final DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
---|
162 | assertNotNull(vc); |
---|
163 | assertEquals("Test Collection 2", vc.getName()); |
---|
164 | assertEquals(VirtualCollection.Type.INTENSIONAL, vc.getType()); |
---|
165 | assertEquals("Test Description", vc.getDescription()); |
---|
166 | assertEquals(sdf.parse("2009-12-31"), vc.getCreationDate()); |
---|
167 | assertEquals(VirtualCollection.Purpose.SAMPLE, vc.getPurpose()); |
---|
168 | assertEquals(VirtualCollection.Reproducibility.UNTENDED, |
---|
169 | vc.getReproducibility()); |
---|
170 | assertEquals("Unmaintaned test collection.", |
---|
171 | vc.getReproducibilityNotice()); |
---|
172 | |
---|
173 | assertNotNull(vc.getKeywords()); |
---|
174 | assertEquals(2, vc.getKeywords().size()); |
---|
175 | assertNotNull(vc.getKeywords()); |
---|
176 | assertEquals("test", vc.getKeywords().get(0)); |
---|
177 | assertEquals("unmaintained", vc.getKeywords().get(1)); |
---|
178 | |
---|
179 | assertNotNull(vc.getCreators()); |
---|
180 | assertEquals(1, vc.getCreators().size()); |
---|
181 | Creator c = vc.getCreators().get(0); |
---|
182 | assertNotNull(c); |
---|
183 | assertEquals("Joe User", c.getPerson()); |
---|
184 | assertEquals("joe.user@example.com", c.getEMail()); |
---|
185 | assertEquals("Example Organization", c.getOrganisation()); |
---|
186 | |
---|
187 | |
---|
188 | assertEquals(0, vc.getResources().size()); |
---|
189 | |
---|
190 | GeneratedBy generatedBy = vc.getGeneratedBy(); |
---|
191 | assertNotNull(generatedBy); |
---|
192 | assertEquals("GeneratedBy description", generatedBy.getDescription()); |
---|
193 | assertEquals("urn:x-vcr:test-uri", generatedBy.getURI()); |
---|
194 | GeneratedByQuery query = generatedBy.getQuery(); |
---|
195 | assertNotNull(query); |
---|
196 | assertEquals("sql", query.getProfile()); |
---|
197 | assertEquals("<SQL>Test-Query</SQL>", query.getValue()); |
---|
198 | |
---|
199 | // round-tripping |
---|
200 | ByteArrayOutputStream out = new ByteArrayOutputStream(65536); |
---|
201 | m.marshal(out, Format.XML, vc); |
---|
202 | VirtualCollection vc2 = |
---|
203 | m.unmarshal(new ByteArrayInputStream(out.toByteArray()), |
---|
204 | Format.XML, "utf-8"); |
---|
205 | assertEquals(vc.getOwner(), vc2.getOwner()); |
---|
206 | assertEquals(vc.getPersistentIdentifier(), |
---|
207 | vc2.getPersistentIdentifier()); |
---|
208 | assertEquals(vc.getState(), vc2.getState()); |
---|
209 | assertEquals(vc.getType(), vc2.getType()); |
---|
210 | assertEquals(vc.getName(), vc2.getName()); |
---|
211 | assertEquals(vc.getDescription(), vc2.getDescription()); |
---|
212 | assertEquals(vc.getCreationDate(), vc2.getCreationDate()); |
---|
213 | assertEquals(vc.getCreators(), vc2.getCreators()); |
---|
214 | assertEquals(vc.getPurpose(), vc2.getPurpose()); |
---|
215 | assertEquals(vc.getReproducibility(), vc2.getReproducibility()); |
---|
216 | assertEquals(vc.getReproducibilityNotice(), |
---|
217 | vc2.getReproducibilityNotice()); |
---|
218 | assertEquals(vc.getKeywords(), vc2.getKeywords()); |
---|
219 | assertEquals(vc.getResources(), vc2.getResources()); |
---|
220 | assertEquals(vc.getGeneratedBy(), vc2.getGeneratedBy()); |
---|
221 | assertEquals(vc, vc2); |
---|
222 | } |
---|
223 | |
---|
224 | } // class VirtualCollectionMarshallerTest |
---|