AtomFeedHttpMessageConverterTests.java 4.9 KB
Newer Older
A
Arjen Poutsma 已提交
1
/*
P
Phillip Webb 已提交
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.atom.Entry;
import com.rometools.rome.feed.atom.Feed;
28 29
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
30 31 32
import org.xmlunit.diff.DefaultNodeMatcher;
import org.xmlunit.diff.ElementSelectors;
import org.xmlunit.diff.NodeMatcher;
A
Arjen Poutsma 已提交
33 34 35 36

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

39
import static java.util.Collections.singletonMap;
40
import static org.assertj.core.api.Assertions.assertThat;
41

42 43 44
/**
 * @author Arjen Poutsma
 */
A
Arjen Poutsma 已提交
45 46
public class AtomFeedHttpMessageConverterTests {

R
Rossen Stoyanchev 已提交
47 48 49 50
	private static final MediaType ATOM_XML_UTF8 =
			new MediaType(MediaType.APPLICATION_ATOM_XML, StandardCharsets.UTF_8);


A
Arjen Poutsma 已提交
51 52 53
	private AtomFeedHttpMessageConverter converter;


54
	@BeforeEach
A
Arjen Poutsma 已提交
55 56 57 58
	public void setUp() {
		converter = new AtomFeedHttpMessageConverter();
	}

59

A
Arjen Poutsma 已提交
60 61
	@Test
	public void canRead() {
R
Rossen Stoyanchev 已提交
62 63
		assertThat(converter.canRead(Feed.class, MediaType.APPLICATION_ATOM_XML)).isTrue();
		assertThat(converter.canRead(Feed.class, ATOM_XML_UTF8)).isTrue();
A
Arjen Poutsma 已提交
64 65 66 67
	}

	@Test
	public void canWrite() {
R
Rossen Stoyanchev 已提交
68 69
		assertThat(converter.canWrite(Feed.class, MediaType.APPLICATION_ATOM_XML)).isTrue();
		assertThat(converter.canWrite(Feed.class, ATOM_XML_UTF8)).isTrue();
A
Arjen Poutsma 已提交
70 71 72 73 74 75
	}

	@Test
	public void read() throws IOException {
		InputStream is = getClass().getResourceAsStream("atom.xml");
		MockHttpInputMessage inputMessage = new MockHttpInputMessage(is);
R
Rossen Stoyanchev 已提交
76
		inputMessage.getHeaders().setContentType(ATOM_XML_UTF8);
A
Arjen Poutsma 已提交
77
		Feed result = converter.read(Feed.class, inputMessage);
78 79
		assertThat(result.getTitle()).isEqualTo("title");
		assertThat(result.getSubtitle().getValue()).isEqualTo("subtitle");
80
		List<?> entries = result.getEntries();
81
		assertThat(entries.size()).isEqualTo(2);
A
Arjen Poutsma 已提交
82 83

		Entry entry1 = (Entry) entries.get(0);
84 85
		assertThat(entry1.getId()).isEqualTo("id1");
		assertThat(entry1.getTitle()).isEqualTo("title1");
A
Arjen Poutsma 已提交
86 87

		Entry entry2 = (Entry) entries.get(1);
88 89
		assertThat(entry2.getId()).isEqualTo("id2");
		assertThat(entry2.getTitle()).isEqualTo("title2");
A
Arjen Poutsma 已提交
90 91 92
	}

	@Test
R
Rossen Stoyanchev 已提交
93
	public void write() throws IOException {
A
Arjen Poutsma 已提交
94 95 96 97 98 99 100 101 102 103 104
		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");

105
		List<Entry> entries = new ArrayList<>(2);
A
Arjen Poutsma 已提交
106 107 108 109 110 111 112
		entries.add(entry1);
		entries.add(entry2);
		feed.setEntries(entries);

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

113 114
		assertThat(outputMessage.getHeaders().getContentType())
				.as("Invalid content-type")
R
Rossen Stoyanchev 已提交
115
				.isEqualTo(ATOM_XML_UTF8);
A
Arjen Poutsma 已提交
116 117 118
		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>";
119
		NodeMatcher nm = new DefaultNodeMatcher(ElementSelectors.byName);
120 121
		assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
				.isSimilarToIgnoringWhitespace(expected, nm);
A
Arjen Poutsma 已提交
122 123 124
	}

	@Test
R
Rossen Stoyanchev 已提交
125
	public void writeOtherCharset() throws IOException {
A
Arjen Poutsma 已提交
126 127 128 129 130 131 132 133
		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);

134 135 136 137 138 139 140
		assertThat(outputMessage.getHeaders().getContentType())
				.as("Invalid content-type")
				.isEqualTo(new MediaType("application", "atom+xml", Charset.forName(encoding)));
	}

	@Test
	public void writeOtherContentTypeParameters() throws IOException {
R
Rossen Stoyanchev 已提交
141 142 143
		MockHttpOutputMessage message = new MockHttpOutputMessage();
		MediaType contentType = new MediaType("application", "atom+xml", singletonMap("type", "feed"));
		converter.write(new Feed("atom_1.0"), contentType, message);
144

R
Rossen Stoyanchev 已提交
145
		assertThat(message.getHeaders().getContentType().getParameters())
146
				.as("Invalid content-type")
R
Rossen Stoyanchev 已提交
147 148 149
				.hasSize(2)
				.containsEntry("type", "feed")
				.containsEntry("charset", "UTF-8");
A
Arjen Poutsma 已提交
150 151 152
	}

}