RssChannelHttpMessageConverterTests.java 5.0 KB
Newer Older
A
Arjen Poutsma 已提交
1
/*
S
Sam Brannen 已提交
2
 * Copyright 2002-2019 the original author or authors.
A
Arjen Poutsma 已提交
3 4 5 6 7
 *
 * 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
 *
S
Spring Operator 已提交
8
 *      https://www.apache.org/licenses/LICENSE-2.0
A
Arjen Poutsma 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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;
22
import java.nio.charset.StandardCharsets;
A
Arjen Poutsma 已提交
23 24 25
import java.util.ArrayList;
import java.util.List;

26 27
import com.rometools.rome.feed.rss.Channel;
import com.rometools.rome.feed.rss.Item;
28 29
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
A
Arjen Poutsma 已提交
30 31 32 33

import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
34
import org.springframework.tests.XmlContent;
A
Arjen Poutsma 已提交
35

36
import static java.util.Collections.singletonMap;
37
import static org.assertj.core.api.Assertions.assertThat;
38

39 40 41
/**
 * @author Arjen Poutsma
 */
A
Arjen Poutsma 已提交
42 43
public class RssChannelHttpMessageConverterTests {

R
Rossen Stoyanchev 已提交
44 45 46 47
	private static final MediaType RSS_XML_UTF8 =
			new MediaType(MediaType.APPLICATION_RSS_XML, StandardCharsets.UTF_8);


A
Arjen Poutsma 已提交
48 49 50
	private RssChannelHttpMessageConverter converter;


51
	@BeforeEach
A
Arjen Poutsma 已提交
52 53 54 55
	public void setUp() {
		converter = new RssChannelHttpMessageConverter();
	}

56

A
Arjen Poutsma 已提交
57
	@Test
R
Rossen Stoyanchev 已提交
58 59 60
	public void canReadAndWrite() {
		assertThat(converter.canRead(Channel.class, MediaType.APPLICATION_RSS_XML)).isTrue();
		assertThat(converter.canRead(Channel.class, RSS_XML_UTF8)).isTrue();
A
Arjen Poutsma 已提交
61

R
Rossen Stoyanchev 已提交
62 63
		assertThat(converter.canWrite(Channel.class, MediaType.APPLICATION_RSS_XML)).isTrue();
		assertThat(converter.canWrite(Channel.class, RSS_XML_UTF8)).isTrue();
A
Arjen Poutsma 已提交
64 65 66 67 68 69
	}

	@Test
	public void read() throws IOException {
		InputStream is = getClass().getResourceAsStream("rss.xml");
		MockHttpInputMessage inputMessage = new MockHttpInputMessage(is);
R
Rossen Stoyanchev 已提交
70
		inputMessage.getHeaders().setContentType(RSS_XML_UTF8);
A
Arjen Poutsma 已提交
71
		Channel result = converter.read(Channel.class, inputMessage);
72 73 74
		assertThat(result.getTitle()).isEqualTo("title");
		assertThat(result.getLink()).isEqualTo("https://example.com");
		assertThat(result.getDescription()).isEqualTo("description");
A
Arjen Poutsma 已提交
75

76
		List<?> items = result.getItems();
77
		assertThat(items.size()).isEqualTo(2);
A
Arjen Poutsma 已提交
78 79

		Item item1 = (Item) items.get(0);
80
		assertThat(item1.getTitle()).isEqualTo("title1");
A
Arjen Poutsma 已提交
81 82

		Item item2 = (Item) items.get(1);
83
		assertThat(item2.getTitle()).isEqualTo("title2");
A
Arjen Poutsma 已提交
84 85 86
	}

	@Test
R
Rossen Stoyanchev 已提交
87
	public void write() throws IOException {
A
Arjen Poutsma 已提交
88 89
		Channel channel = new Channel("rss_2.0");
		channel.setTitle("title");
S
Spring Operator 已提交
90
		channel.setLink("https://example.com");
A
Arjen Poutsma 已提交
91 92 93 94 95 96 97 98
		channel.setDescription("description");

		Item item1 = new Item();
		item1.setTitle("title1");

		Item item2 = new Item();
		item2.setTitle("title2");

99
		List<Item> items = new ArrayList<>(2);
A
Arjen Poutsma 已提交
100 101 102 103 104 105 106
		items.add(item1);
		items.add(item2);
		channel.setItems(items);

		MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
		converter.write(channel, null, outputMessage);

107 108
		assertThat(outputMessage.getHeaders().getContentType())
				.as("Invalid content-type")
R
Rossen Stoyanchev 已提交
109
				.isEqualTo(RSS_XML_UTF8);
A
Arjen Poutsma 已提交
110
		String expected = "<rss version=\"2.0\">" +
S
Spring Operator 已提交
111
				"<channel><title>title</title><link>https://example.com</link><description>description</description>" +
A
Arjen Poutsma 已提交
112 113 114
				"<item><title>title1</title></item>" +
				"<item><title>title2</title></item>" +
				"</channel></rss>";
115 116
		assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
				.isSimilarToIgnoringWhitespace(expected);
A
Arjen Poutsma 已提交
117 118 119
	}

	@Test
R
Rossen Stoyanchev 已提交
120
	public void writeOtherCharset() throws IOException {
A
Arjen Poutsma 已提交
121 122
		Channel channel = new Channel("rss_2.0");
		channel.setTitle("title");
S
Spring Operator 已提交
123
		channel.setLink("https://example.com");
A
Arjen Poutsma 已提交
124 125 126 127 128 129 130 131 132 133 134
		channel.setDescription("description");

		String encoding = "ISO-8859-1";
		channel.setEncoding(encoding);

		Item item1 = new Item();
		item1.setTitle("title1");

		MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
		converter.write(channel, null, outputMessage);

135 136 137 138 139 140 141 142 143 144 145 146
		assertThat(outputMessage.getHeaders().getContentType())
				.as("Invalid content-type")
				.isEqualTo(new MediaType("application", "rss+xml", Charset.forName(encoding)));
	}

	@Test
	public void writeOtherContentTypeParameters() throws IOException {
		Channel channel = new Channel("rss_2.0");
		channel.setTitle("title");
		channel.setLink("http://example.com");
		channel.setDescription("description");

R
Rossen Stoyanchev 已提交
147 148
		MockHttpOutputMessage message = new MockHttpOutputMessage();
		converter.write(channel, new MediaType("application", "rss+xml", singletonMap("x", "y")), message);
149

R
Rossen Stoyanchev 已提交
150
		assertThat(message.getHeaders().getContentType().getParameters())
151
				.as("Invalid content-type")
R
Rossen Stoyanchev 已提交
152 153 154
				.hasSize(2)
				.containsEntry("x", "y")
				.containsEntry("charset", "UTF-8");
A
Arjen Poutsma 已提交
155 156
	}

157
}