1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.bean.atom;
18
19 import static org.apache.juneau.TestUtils.*;
20 import static org.apache.juneau.bean.atom.AtomBuilder.*;
21 import static org.junit.jupiter.api.Assertions.*;
22
23 import java.net.*;
24
25 import org.apache.juneau.*;
26 import org.apache.juneau.xml.*;
27 import org.junit.jupiter.api.*;
28
29 class AtomTest extends TestBase {
30
31 public Feed createFeed() throws Exception {
32 return
33 feed("tag:foo.org", "Title", "2016-12-31T05:02:03Z")
34 .setSubtitle(text("html").setText("Subtitle"))
35 .setLinks(
36 link("alternate", "text/html", "http://foo.org/").setHreflang("en"),
37 link("self", "application/atom+xml", "http://foo.org/feed.atom")
38 )
39 .setGenerator(
40 generator("Example Toolkit").setUri("http://www.foo.org/").setVersion("1.0")
41 )
42 .setEntries(
43 entry("tag:foo.org", "Title", "2016-12-31T05:02:03Z")
44 .setLinks(
45 link("alternate", "text/html", "http://foo.org/2005/04/02/atom"),
46 link("enclosure", "audio/mpeg", "http://foo.org/audio/foobar.mp3").setLength(1337)
47 )
48 .setPublished("2016-12-31T05:02:03Z")
49 .setAuthors(
50 person("John Smith").setUri(new URI("http://foo.org/")).setEmail("foo@foo.org")
51 )
52 .setContributors(
53 person("John Smith"),
54 person("Jane Smith")
55 )
56 .setContent(
57 content("xhtml")
58 .setLang("en")
59 .setBase("http://foo.org/")
60 .setText("<div><p><i>[Sample content]</i></p></div>")
61 )
62 );
63 }
64
65 @Test void a01_normal() throws Exception {
66 var p = XmlParser.DEFAULT;
67 var f = createFeed();
68
69 var expected = """
70 <feed>
71 <entry>
72 <author>
73 <email>foo@foo.org</email>
74 <name>John Smith</name>
75 <uri>http:
76 </author>
77 <content base='http:
78 <contributor>
79 <name>John Smith</name>
80 </contributor>
81 <contributor>
82 <name>Jane Smith</name>
83 </contributor>
84 <id>tag:foo.org</id>
85 <link href='http:
86 <link href='http:
87 <published>2016-12-31T05:02:03Z</published>
88 <title>Title</title>
89 <updated>2016-12-31T05:02:03Z</updated>
90 </entry>
91 <generator uri='http:
92 <id>tag:foo.org</id>
93 <link href='http:
94 <link href='http:
95 <subtitle type='html'>Subtitle</subtitle>
96 <title>Title</title>
97 <updated>2016-12-31T05:02:03Z</updated>
98 </feed>
99 """;
100 var s = XmlSerializer.create().sq().ws().sortProperties().build();
101 var r = s.serialize(f);
102 assertEquals(expected, r);
103 var f2 = p.parse(r, Feed.class);
104 assertEquals(json(f2), json(f));
105 }
106
107 @Test void a02_withNamespaces() throws Exception {
108 var p = XmlParser.DEFAULT;
109 var f = createFeed();
110
111 var expected = """
112 <atom:feed xmlns='http:
113 <atom:entry>
114 <atom:author>
115 <atom:email>foo@foo.org</atom:email>
116 <atom:name>John Smith</atom:name>
117 <atom:uri>http:
118 </atom:author>
119 <atom:content xml:base='http:
120 <atom:contributor>
121 <atom:name>John Smith</atom:name>
122 </atom:contributor>
123 <atom:contributor>
124 <atom:name>Jane Smith</atom:name>
125 </atom:contributor>
126 <atom:id>tag:foo.org</atom:id>
127 <atom:link href='http:
128 <atom:link href='http:
129 <atom:published>2016-12-31T05:02:03Z</atom:published>
130 <atom:title>Title</atom:title>
131 <atom:updated>2016-12-31T05:02:03Z</atom:updated>
132 </atom:entry>
133 <atom:generator uri='http:
134 <atom:id>tag:foo.org</atom:id>
135 <atom:link href='http:
136 <atom:link href='http:
137 <atom:subtitle type='html'>Subtitle</atom:subtitle>
138 <atom:title>Title</atom:title>
139 <atom:updated>2016-12-31T05:02:03Z</atom:updated>
140 </atom:feed>
141 """;
142 var s = XmlSerializer.create().sq().ws().enableNamespaces().addNamespaceUrisToRoot().sortProperties().build();
143 var r = s.serialize(f);
144 assertEquals(expected, r);
145 var f2 = p.parse(r, Feed.class);
146 assertEquals(json(f2), json(f));
147 }
148
149 @Test void a03_withNamespacesWithAtomAsDefault() throws Exception {
150 var p = XmlParser.DEFAULT;
151 var f = createFeed();
152
153 var expected = """
154 <feed xmlns='http:
155 <entry>
156 <author>
157 <email>foo@foo.org</email>
158 <name>John Smith</name>
159 <uri>http:
160 </author>
161 <content xml:base='http:
162 <contributor>
163 <name>John Smith</name>
164 </contributor>
165 <contributor>
166 <name>Jane Smith</name>
167 </contributor>
168 <id>tag:foo.org</id>
169 <link href='http:
170 <link href='http:
171 <published>2016-12-31T05:02:03Z</published>
172 <title>Title</title>
173 <updated>2016-12-31T05:02:03Z</updated>
174 </entry>
175 <generator uri='http:
176 <id>tag:foo.org</id>
177 <link href='http:
178 <link href='http:
179 <subtitle type='html'>Subtitle</subtitle>
180 <title>Title</title>
181 <updated>2016-12-31T05:02:03Z</updated>
182 </feed>
183 """;
184 var s = XmlSerializer.create().sq().ws().defaultNamespace(Namespace.of("atom")).enableNamespaces().addNamespaceUrisToRoot().sortProperties().build();
185 var r = s.serialize(f);
186 assertEquals(expected, r);
187 var f2 = p.parse(r, Feed.class);
188 assertEquals(json(f2), json(f));
189 }
190
191 @Test void a04_toString() throws Exception {
192 var p = XmlParser.DEFAULT;
193 var f = createFeed();
194 var r = f.toString();
195 var f2 = p.parse(r, Feed.class);
196 assertEquals(json(f2), json(f));
197 }
198 }