HttpMessageConverterViewTests.java 5.9 KB
Newer Older
R
Rossen Stoyanchev 已提交
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
/*
 * Copyright 2002-2016 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
 *
 *      http://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.web.reactive.result.view;


import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
26
import java.util.LinkedHashMap;
R
Rossen Stoyanchev 已提交
27 28 29 30 31 32 33 34
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import reactor.core.test.TestSubscriber;

import org.springframework.core.ResolvableType;
35 36
import org.springframework.http.codec.json.JacksonJsonEncoder;
import org.springframework.http.codec.xml.Jaxb2Encoder;
37
import org.springframework.core.codec.StringEncoder;
R
Rossen Stoyanchev 已提交
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
import org.springframework.core.io.buffer.support.DataBufferTestUtils;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.MockServerHttpRequest;
import org.springframework.http.server.reactive.MockServerHttpResponse;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.ModelMap;
import org.springframework.util.MimeType;
import org.springframework.web.reactive.HandlerResult;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.adapter.DefaultServerWebExchange;
import org.springframework.web.server.session.DefaultWebSessionManager;
import org.springframework.web.server.session.WebSessionManager;

import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;


/**
 * Unit tests for {@link HttpMessageConverterView}.
 * @author Rossen Stoyanchev
 */
public class HttpMessageConverterViewTests {

	private HttpMessageConverterView view;

	private HandlerResult result;

	private ModelMap model;


	@Before
	public void setup() throws Exception {
74
		this.view = new HttpMessageConverterView(new JacksonJsonEncoder());
R
Rossen Stoyanchev 已提交
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
		this.model = new ExtendedModelMap();
		this.result = new HandlerResult(new Object(), null, ResolvableType.NONE, model);
	}


	@Test
	public void supportedMediaTypes() throws Exception {
		List<MimeType> mimeTypes = Arrays.asList(
				new MimeType("application", "json", StandardCharsets.UTF_8),
				new MimeType("application", "*+json", StandardCharsets.UTF_8));

		assertEquals(mimeTypes, this.view.getSupportedMediaTypes());
	}

	@Test
	public void extractObject() throws Exception {
		this.view.setModelKeys(Collections.singleton("foo2"));
		this.model.addAttribute("foo1", "bar1");
		this.model.addAttribute("foo2", "bar2");
		this.model.addAttribute("foo3", "bar3");

		assertEquals("bar2", this.view.extractObjectToRender(this.result));
	}

	@Test
	public void extractObjectNoMatch() throws Exception {
		this.view.setModelKeys(Collections.singleton("foo2"));
		this.model.addAttribute("foo1", "bar1");

		assertNull(this.view.extractObjectToRender(this.result));
	}

	@Test
	public void extractObjectMultipleMatches() throws Exception {
		this.view.setModelKeys(new HashSet<>(Arrays.asList("foo1", "foo2")));
		this.model.addAttribute("foo1", "bar1");
		this.model.addAttribute("foo2", "bar2");
		this.model.addAttribute("foo3", "bar3");

		Object value = this.view.extractObjectToRender(this.result);
		assertNotNull(value);
		assertEquals(HashMap.class, value.getClass());

		Map<?, ?> map = (Map<?, ?>) value;
		assertEquals(2, map.size());
		assertEquals("bar1", map.get("foo1"));
		assertEquals("bar2", map.get("foo2"));
	}

	@Test
	public void extractObjectMultipleMatchesNotSupported() throws Exception {
126
		HttpMessageConverterView view = new HttpMessageConverterView(new StringEncoder());
R
Rossen Stoyanchev 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
		view.setModelKeys(new HashSet<>(Arrays.asList("foo1", "foo2")));
		this.model.addAttribute("foo1", "bar1");
		this.model.addAttribute("foo2", "bar2");

		try {
			view.extractObjectToRender(this.result);
			fail();
		}
		catch (IllegalStateException ex) {
			String message = ex.getMessage();
			assertTrue(message, message.contains("Map rendering is not supported"));
		}
	}

	@Test
	public void extractObjectNotSupported() throws Exception {
143
		HttpMessageConverterView view = new HttpMessageConverterView(new Jaxb2Encoder());
R
Rossen Stoyanchev 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
		view.setModelKeys(new HashSet<>(Collections.singletonList("foo1")));
		this.model.addAttribute("foo1", "bar1");

		try {
			view.extractObjectToRender(this.result);
			fail();
		}
		catch (IllegalStateException ex) {
			String message = ex.getMessage();
			assertTrue(message, message.contains("[foo1] is not supported"));
		}
	}

	@Test
	public void render() throws Exception {
159 160 161 162 163
		Map<String, String> pojoData = new LinkedHashMap<>();
		pojoData.put("foo", "f");
		pojoData.put("bar", "b");
		this.model.addAttribute("pojoData", pojoData);
		this.view.setModelKeys(Collections.singleton("pojoData"));
R
Rossen Stoyanchev 已提交
164 165 166 167 168 169 170 171

		MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, new URI("/path"));
		MockServerHttpResponse response = new MockServerHttpResponse();
		WebSessionManager manager = new DefaultWebSessionManager();
		ServerWebExchange exchange = new DefaultServerWebExchange(request, response, manager);

		this.view.render(result, MediaType.APPLICATION_JSON, exchange);

172 173
		TestSubscriber
				.subscribe(response.getBody())
174
				.assertValuesWith(buf -> assertEquals("{\"foo\":\"f\",\"bar\":\"b\"}",
R
Rossen Stoyanchev 已提交
175 176 177
						DataBufferTestUtils.dumpString(buf, Charset.forName("UTF-8"))));
	}

178

R
Rossen Stoyanchev 已提交
179
}