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 | |
---|
7 | import java.io.ByteArrayInputStream; |
---|
8 | import java.io.ByteArrayOutputStream; |
---|
9 | import java.util.Date; |
---|
10 | |
---|
11 | import org.junit.Test; |
---|
12 | |
---|
13 | import clarin.cmdi.componentregistry.BaseUnitTest; |
---|
14 | import clarin.cmdi.componentregistry.DatesHelper; |
---|
15 | import clarin.cmdi.componentregistry.MDMarshaller; |
---|
16 | |
---|
17 | import javax.xml.transform.TransformerException; |
---|
18 | |
---|
19 | import org.junit.Before; |
---|
20 | |
---|
21 | public class RegisterResponseTest extends BaseUnitTest { |
---|
22 | |
---|
23 | private Date testDate = new Date(); |
---|
24 | |
---|
25 | @Test |
---|
26 | public void testRegisterError() throws Exception { |
---|
27 | RegisterResponse resp = new RegisterResponse(); |
---|
28 | resp.setRegistered(false); |
---|
29 | resp.setIsProfile(true); |
---|
30 | resp.setIsInUserSpace(true); |
---|
31 | resp.addError("Error 1"); |
---|
32 | resp.addError("Error 2, <!-- to be escaped -->"); |
---|
33 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
34 | marshaller.marshal(resp, out); |
---|
35 | String expected = ""; |
---|
36 | expected += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; |
---|
37 | expected += "<registerResponse isProfile=\"true\" registered=\"false\" isInUserSpace=\"true\" xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; |
---|
38 | expected += " <errors>\n"; |
---|
39 | expected += " <error>Error 1</error>\n"; |
---|
40 | expected += " <error>Error 2, <!-- to be escaped --></error>\n"; |
---|
41 | expected += " </errors>\n"; |
---|
42 | expected += "</registerResponse>\n"; |
---|
43 | assertXMLEqual(expected, out.toString()); |
---|
44 | RegisterResponse rr = marshaller.unmarshal(RegisterResponse.class, |
---|
45 | new ByteArrayInputStream(expected.getBytes()), null); |
---|
46 | assertFalse(rr.isRegistered()); |
---|
47 | assertTrue(rr.isProfile()); |
---|
48 | assertEquals(2, rr.getErrors().size()); |
---|
49 | } |
---|
50 | |
---|
51 | @Test |
---|
52 | public void testRegisterSucces() throws Exception { |
---|
53 | RegisterResponse resp = new RegisterResponse(); |
---|
54 | resp.setRegistered(true); |
---|
55 | resp.setIsProfile(true); |
---|
56 | resp.setIsInUserSpace(false); |
---|
57 | resp.setDescription(getProfileDescription()); |
---|
58 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
59 | marshaller.marshal(resp, out); |
---|
60 | String expected = ""; |
---|
61 | expected += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; |
---|
62 | expected += "<registerResponse isProfile=\"true\" registered=\"true\" isInUserSpace=\"false\" xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; |
---|
63 | expected += " <errors/>\n"; |
---|
64 | expected += " <description xsi:type=\"profileDescription\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"; |
---|
65 | expected += " <id>clarin.eu:cr1:p_myId</id>\n"; |
---|
66 | expected += " <description>myD</description>\n"; |
---|
67 | expected += " <name>Name</name>\n"; |
---|
68 | expected += " <registrationDate>" + DatesHelper.formatXmlDateTime(testDate) + "</registrationDate>\n"; |
---|
69 | expected += " <creatorName>myC</creatorName>\n"; |
---|
70 | expected += " <ns2:href>linkToMyProfile</ns2:href>\n"; |
---|
71 | expected += " <commentsCount>2</commentsCount>\n"; |
---|
72 | expected += " <showInEditor>true</showInEditor>\n"; |
---|
73 | expected += " </description>\n"; |
---|
74 | expected += "</registerResponse>\n"; |
---|
75 | assertXMLEqual(expected, out.toString()); |
---|
76 | |
---|
77 | RegisterResponse rr = marshaller.unmarshal(RegisterResponse.class, |
---|
78 | new ByteArrayInputStream(expected.getBytes()), null); |
---|
79 | assertTrue(rr.isRegistered()); |
---|
80 | assertTrue(rr.isProfile()); |
---|
81 | assertEquals("clarin.eu:cr1:p_myId", rr.getDescription().getId()); |
---|
82 | } |
---|
83 | |
---|
84 | @Test |
---|
85 | public void testRegisterSuccesComponent() throws Exception { |
---|
86 | RegisterResponse resp = new RegisterResponse(); |
---|
87 | resp.setRegistered(true); |
---|
88 | resp.setIsProfile(false); |
---|
89 | resp.setIsInUserSpace(true); |
---|
90 | resp.setDescription(getComponentDescription()); |
---|
91 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
92 | marshaller.marshal(resp, out); |
---|
93 | String expected = ""; |
---|
94 | expected += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; |
---|
95 | expected += "<registerResponse isProfile=\"false\" registered=\"true\" isInUserSpace=\"true\" xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; |
---|
96 | expected += " <errors/>\n"; |
---|
97 | expected += " <description xsi:type=\"componentDescription\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"; |
---|
98 | expected += " <id>clarin.eu:cr1:c_myId</id>\n"; |
---|
99 | expected += " <description>myD</description>\n"; |
---|
100 | expected += " <name>Name</name>\n"; |
---|
101 | expected += " <registrationDate>" + DatesHelper.formatXmlDateTime(testDate) + "</registrationDate>\n"; |
---|
102 | expected += " <creatorName>myC</creatorName>\n"; |
---|
103 | expected += " <ns2:href>linkToMyProfile</ns2:href>\n"; |
---|
104 | expected += " <groupName>imdi</groupName>\n"; |
---|
105 | expected += " <commentsCount>2</commentsCount>\n"; |
---|
106 | expected += " </description>\n"; |
---|
107 | expected += "</registerResponse>\n"; |
---|
108 | assertXMLEqual(expected, out.toString()); |
---|
109 | |
---|
110 | RegisterResponse rr = marshaller.unmarshal(RegisterResponse.class, |
---|
111 | new ByteArrayInputStream(expected.getBytes()), null); |
---|
112 | assertTrue(rr.isRegistered()); |
---|
113 | assertFalse(rr.isProfile()); |
---|
114 | assertEquals("clarin.eu:cr1:c_myId", rr.getDescription().getId()); |
---|
115 | } |
---|
116 | |
---|
117 | private ComponentDescription getComponentDescription() { |
---|
118 | ComponentDescription desc = new ComponentDescription(); |
---|
119 | desc.setName("Name"); |
---|
120 | desc.setId(ComponentDescription.COMPONENT_PREFIX + "myId"); |
---|
121 | desc.setGroupName("imdi"); |
---|
122 | desc.setCreatorName("myC"); |
---|
123 | desc.setDescription("myD"); |
---|
124 | desc.setRegistrationDate(testDate); |
---|
125 | desc.setHref("linkToMyProfile"); |
---|
126 | desc.setCommentsCount(2); |
---|
127 | return desc; |
---|
128 | } |
---|
129 | |
---|
130 | private ProfileDescription getProfileDescription() { |
---|
131 | ProfileDescription desc = new ProfileDescription(); |
---|
132 | desc.setName("Name"); |
---|
133 | desc.setId(ProfileDescription.PROFILE_PREFIX + "myId"); |
---|
134 | desc.setCreatorName("myC"); |
---|
135 | desc.setDescription("myD"); |
---|
136 | desc.setRegistrationDate(testDate); |
---|
137 | desc.setHref("linkToMyProfile"); |
---|
138 | desc.setCommentsCount(2); |
---|
139 | return desc; |
---|
140 | } |
---|
141 | } |
---|