View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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://foo.org/</uri>
76  				</author>
77  				<content base='http://foo.org/' lang='en' type='xhtml'><div><p><i>[Sample content]</i></p></div></content>
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://foo.org/2005/04/02/atom' rel='alternate' type='text/html'/>
86  				<link href='http://foo.org/audio/foobar.mp3' length='1337' rel='enclosure' type='audio/mpeg'/>
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://www.foo.org/' version='1.0'>Example Toolkit</generator>
92  			<id>tag:foo.org</id>
93  			<link href='http://foo.org/' hreflang='en' rel='alternate' type='text/html'/>
94  			<link href='http://foo.org/feed.atom' rel='self' type='application/atom+xml'/>
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://www.apache.org/2013/Juneau' xmlns:atom='http://www.w3.org/2005/Atom/' xmlns:xml='http://www.w3.org/XML/1998/namespace'>
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://foo.org/</atom:uri>
118 				</atom:author>
119 				<atom:content xml:base='http://foo.org/' xml:lang='en' type='xhtml'><div><p><i>[Sample content]</i></p></div></atom:content>
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://foo.org/2005/04/02/atom' rel='alternate' type='text/html'/>
128 				<atom:link href='http://foo.org/audio/foobar.mp3' length='1337' rel='enclosure' type='audio/mpeg'/>
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://www.foo.org/' version='1.0'>Example Toolkit</atom:generator>
134 			<atom:id>tag:foo.org</atom:id>
135 			<atom:link href='http://foo.org/' hreflang='en' rel='alternate' type='text/html'/>
136 			<atom:link href='http://foo.org/feed.atom' rel='self' type='application/atom+xml'/>
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://www.w3.org/2005/Atom/' xmlns:xml='http://www.w3.org/XML/1998/namespace'>
155 			<entry>
156 				<author>
157 					<email>foo@foo.org</email>
158 					<name>John Smith</name>
159 					<uri>http://foo.org/</uri>
160 				</author>
161 				<content xml:base='http://foo.org/' xml:lang='en' type='xhtml'><div><p><i>[Sample content]</i></p></div></content>
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://foo.org/2005/04/02/atom' rel='alternate' type='text/html'/>
170 				<link href='http://foo.org/audio/foobar.mp3' length='1337' rel='enclosure' type='audio/mpeg'/>
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://www.foo.org/' version='1.0'>Example Toolkit</generator>
176 			<id>tag:foo.org</id>
177 			<link href='http://foo.org/' hreflang='en' rel='alternate' type='text/html'/>
178 			<link href='http://foo.org/feed.atom' rel='self' type='application/atom+xml'/>
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 }