forked from spring-projects/spring-framework
/
AtomFeedHttpMessageConverterTests.java
152 lines (123 loc) · 4.89 KB
/
AtomFeedHttpMessageConverterTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.converter.feed;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import com.rometools.rome.feed.atom.Entry;
import com.rometools.rome.feed.atom.Feed;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.xmlunit.diff.DefaultNodeMatcher;
import org.xmlunit.diff.ElementSelectors;
import org.xmlunit.diff.NodeMatcher;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.tests.XmlContent;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Arjen Poutsma
*/
public class AtomFeedHttpMessageConverterTests {
private static final MediaType ATOM_XML_UTF8 =
new MediaType(MediaType.APPLICATION_ATOM_XML, StandardCharsets.UTF_8);
private AtomFeedHttpMessageConverter converter;
@BeforeEach
public void setUp() {
converter = new AtomFeedHttpMessageConverter();
}
@Test
public void canRead() {
assertThat(converter.canRead(Feed.class, MediaType.APPLICATION_ATOM_XML)).isTrue();
assertThat(converter.canRead(Feed.class, ATOM_XML_UTF8)).isTrue();
}
@Test
public void canWrite() {
assertThat(converter.canWrite(Feed.class, MediaType.APPLICATION_ATOM_XML)).isTrue();
assertThat(converter.canWrite(Feed.class, ATOM_XML_UTF8)).isTrue();
}
@Test
public void read() throws IOException {
InputStream is = getClass().getResourceAsStream("atom.xml");
MockHttpInputMessage inputMessage = new MockHttpInputMessage(is);
inputMessage.getHeaders().setContentType(ATOM_XML_UTF8);
Feed result = converter.read(Feed.class, inputMessage);
assertThat(result.getTitle()).isEqualTo("title");
assertThat(result.getSubtitle().getValue()).isEqualTo("subtitle");
List<?> entries = result.getEntries();
assertThat(entries.size()).isEqualTo(2);
Entry entry1 = (Entry) entries.get(0);
assertThat(entry1.getId()).isEqualTo("id1");
assertThat(entry1.getTitle()).isEqualTo("title1");
Entry entry2 = (Entry) entries.get(1);
assertThat(entry2.getId()).isEqualTo("id2");
assertThat(entry2.getTitle()).isEqualTo("title2");
}
@Test
public void write() throws IOException {
Feed feed = new Feed("atom_1.0");
feed.setTitle("title");
Entry entry1 = new Entry();
entry1.setId("id1");
entry1.setTitle("title1");
Entry entry2 = new Entry();
entry2.setId("id2");
entry2.setTitle("title2");
List<Entry> entries = new ArrayList<>(2);
entries.add(entry1);
entries.add(entry2);
feed.setEntries(entries);
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(feed, null, outputMessage);
assertThat(outputMessage.getHeaders().getContentType())
.as("Invalid content-type")
.isEqualTo(ATOM_XML_UTF8);
String expected = "<feed xmlns=\"http://www.w3.org/2005/Atom\">" + "<title>title</title>" +
"<entry><id>id1</id><title>title1</title></entry>" +
"<entry><id>id2</id><title>title2</title></entry></feed>";
NodeMatcher nm = new DefaultNodeMatcher(ElementSelectors.byName);
assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
.isSimilarToIgnoringWhitespace(expected, nm);
}
@Test
public void writeOtherCharset() throws IOException {
Feed feed = new Feed("atom_1.0");
feed.setTitle("title");
String encoding = "ISO-8859-1";
feed.setEncoding(encoding);
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(feed, null, outputMessage);
assertThat(outputMessage.getHeaders().getContentType())
.as("Invalid content-type")
.isEqualTo(new MediaType("application", "atom+xml", Charset.forName(encoding)));
}
@Test
public void writeOtherContentTypeParameters() throws IOException {
MockHttpOutputMessage message = new MockHttpOutputMessage();
MediaType contentType = new MediaType("application", "atom+xml", singletonMap("type", "feed"));
converter.write(new Feed("atom_1.0"), contentType, message);
assertThat(message.getHeaders().getContentType().getParameters())
.as("Invalid content-type")
.hasSize(2)
.containsEntry("type", "feed")
.containsEntry("charset", "UTF-8");
}
}