1 | package clarin.cmdi.componentregistry.model; |
---|
2 | |
---|
3 | import static org.junit.Assert.assertEquals; |
---|
4 | import static org.junit.Assert.assertFalse; |
---|
5 | import static org.junit.Assert.assertTrue; |
---|
6 | import clarin.cmdi.componentregistry.BaseUnitTest; |
---|
7 | import clarin.cmdi.componentregistry.DatesHelper; |
---|
8 | import clarin.cmdi.componentregistry.MDMarshaller; |
---|
9 | |
---|
10 | import java.io.ByteArrayInputStream; |
---|
11 | import java.io.ByteArrayOutputStream; |
---|
12 | import java.util.Date; |
---|
13 | |
---|
14 | import javax.xml.transform.TransformerException; |
---|
15 | |
---|
16 | import org.custommonkey.xmlunit.XMLUnit; |
---|
17 | import org.junit.Before; |
---|
18 | import org.junit.Test; |
---|
19 | /** |
---|
20 | * |
---|
21 | * @author jean-charles FerriÚres <jean-charles.ferrieres@mpi.nl> |
---|
22 | * @author george.georgovassilis@mpi.nl |
---|
23 | */ |
---|
24 | public class CommentResponseTest extends BaseUnitTest{ |
---|
25 | |
---|
26 | private Date testDate = new Date(); |
---|
27 | |
---|
28 | /** |
---|
29 | * Test with no validate attribute should return errors |
---|
30 | * |
---|
31 | * @throws Exception |
---|
32 | */ |
---|
33 | @Test |
---|
34 | public void testRegisterError() throws Exception { |
---|
35 | CommentResponse resp = new CommentResponse(); |
---|
36 | resp.setRegistered(false); |
---|
37 | resp.setIsInUserSpace(true); |
---|
38 | resp.addError("Error 1"); |
---|
39 | resp.addError("Error 2, <!-- to be escaped -->"); |
---|
40 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
41 | marshaller.marshal(resp, out); |
---|
42 | String expected = ""; |
---|
43 | expected += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; |
---|
44 | expected += "<commentResponse registered=\"false\" isInUserSpace=\"true\" xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; |
---|
45 | expected += " <errors>\n"; |
---|
46 | expected += " <error>Error 1</error>\n"; |
---|
47 | expected += " <error>Error 2, <!-- to be escaped --></error>\n"; |
---|
48 | expected += " </errors>\n"; |
---|
49 | expected += "</commentResponse>\n"; |
---|
50 | assertXMLEqual(expected, out.toString()); |
---|
51 | |
---|
52 | CommentResponse rr = marshaller.unmarshal(CommentResponse.class, new ByteArrayInputStream(expected.getBytes()), null); |
---|
53 | assertFalse(rr.isRegistered()); |
---|
54 | assertEquals(2, rr.getErrors().size()); |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | * Test successfully processed |
---|
59 | * |
---|
60 | * @throws Exception |
---|
61 | */ |
---|
62 | @Test |
---|
63 | public void testRegisterSucces() throws Exception { |
---|
64 | CommentResponse resp = new CommentResponse(); |
---|
65 | resp.setRegistered(true); |
---|
66 | resp.setIsInUserSpace(false); |
---|
67 | resp.setComment(getComment()); |
---|
68 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
69 | marshaller.marshal(resp, out); |
---|
70 | String expected = ""; |
---|
71 | expected += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; |
---|
72 | expected += "<commentResponse registered=\"true\" isInUserSpace=\"false\" xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; |
---|
73 | expected += " <errors/>\n"; |
---|
74 | expected += " <comment>\n"; |
---|
75 | expected += " <comments>Name</comments>\n"; |
---|
76 | expected += " <commentDate>"+DatesHelper.formatXmlDateTime(testDate)+"</commentDate>\n"; |
---|
77 | expected += " <componentId>myD</componentId>\n"; |
---|
78 | expected += " <id>myId</id>\n"; |
---|
79 | expected += " <userName>J. Unit</userName>\n"; |
---|
80 | expected += " <canDelete>false</canDelete>\n"; |
---|
81 | expected += " </comment>\n"; |
---|
82 | expected += "</commentResponse>\n"; |
---|
83 | assertXMLEqual(expected, out.toString()); |
---|
84 | |
---|
85 | CommentResponse rr = marshaller.unmarshal(CommentResponse.class, new ByteArrayInputStream(expected.getBytes()), null); |
---|
86 | assertTrue(rr.isRegistered()); |
---|
87 | assertEquals("myId", rr.getComment().getId()); |
---|
88 | } |
---|
89 | |
---|
90 | private Comment getComment() { |
---|
91 | Comment com = new Comment(); |
---|
92 | com.setComment("Name"); |
---|
93 | com.setId("myId"); |
---|
94 | com.setUserId(123); |
---|
95 | com.setComponentId("myD"); |
---|
96 | com.setCommentDate(testDate); |
---|
97 | com.setUserName("J. Unit"); |
---|
98 | return com; |
---|
99 | } |
---|
100 | } |
---|