|
| 1 | +/* |
| 2 | + * Copyright 2015 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.hateoas; |
| 17 | + |
| 18 | +import static org.hamcrest.CoreMatchers.*; |
| 19 | +import static org.junit.Assert.*; |
| 20 | +import static org.mockito.Mockito.*; |
| 21 | + |
| 22 | +import java.io.StringWriter; |
| 23 | +import java.lang.reflect.Method; |
| 24 | +import java.lang.reflect.Type; |
| 25 | +import java.util.Collections; |
| 26 | + |
| 27 | +import org.apache.commons.io.output.WriterOutputStream; |
| 28 | +import org.junit.Assume; |
| 29 | +import org.junit.Test; |
| 30 | +import org.springframework.hateoas.PagedResources.PageMetadata; |
| 31 | +import org.springframework.http.HttpHeaders; |
| 32 | +import org.springframework.http.HttpOutputMessage; |
| 33 | +import org.springframework.http.MediaType; |
| 34 | +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; |
| 35 | +import org.springframework.util.ReflectionUtils; |
| 36 | + |
| 37 | +/** |
| 38 | + * Integration tests for serialization of {@link PagedResources}. |
| 39 | + * |
| 40 | + * @author Oliver Gierke |
| 41 | + */ |
| 42 | +public class Jackson2PagedResourcesIntegrationTest { |
| 43 | + |
| 44 | + private static String REFERENCE = "{\"links\":[],\"content\":[{\"firstname\":\"Dave\",\"lastname\":\"Matthews\"}],\"page\":{\"size\":1,\"totalElements\":2,\"totalPages\":2,\"number\":0}}"; |
| 45 | + |
| 46 | + private static Method SPRING_4_2_WRITE_METHOD; |
| 47 | + |
| 48 | + static { |
| 49 | + |
| 50 | + try { |
| 51 | + SPRING_4_2_WRITE_METHOD = MappingJackson2HttpMessageConverter.class.getMethod("write", Object.class, Type.class, |
| 52 | + MediaType.class, HttpOutputMessage.class); |
| 53 | + } catch (Exception e) {} |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @see SPR-13318 |
| 58 | + */ |
| 59 | + @Test |
| 60 | + public void serializesPagedResourcesCorrectly() throws Exception { |
| 61 | + |
| 62 | + Assume.assumeThat(SPRING_4_2_WRITE_METHOD, is(notNullValue())); |
| 63 | + |
| 64 | + User user = new User(); |
| 65 | + user.firstname = "Dave"; |
| 66 | + user.lastname = "Matthews"; |
| 67 | + |
| 68 | + PageMetadata metadata = new PagedResources.PageMetadata(1, 0, 2); |
| 69 | + PagedResources<User> resources = new PagedResources<User>(Collections.singleton(user), metadata); |
| 70 | + |
| 71 | + Method method = Sample.class.getMethod("someMethod"); |
| 72 | + StringWriter writer = new StringWriter(); |
| 73 | + |
| 74 | + HttpOutputMessage outputMessage = mock(HttpOutputMessage.class); |
| 75 | + when(outputMessage.getBody()).thenReturn(new WriterOutputStream(writer)); |
| 76 | + when(outputMessage.getHeaders()).thenReturn(new HttpHeaders()); |
| 77 | + |
| 78 | + MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); |
| 79 | + |
| 80 | + ReflectionUtils.invokeMethod(SPRING_4_2_WRITE_METHOD, converter, resources, method.getGenericReturnType(), |
| 81 | + MediaType.APPLICATION_JSON, outputMessage); |
| 82 | + |
| 83 | + assertThat(writer.toString(), is(REFERENCE)); |
| 84 | + } |
| 85 | + |
| 86 | + interface Sample { |
| 87 | + Resources<?> someMethod(); |
| 88 | + } |
| 89 | + |
| 90 | + static class User { |
| 91 | + public String firstname, lastname; |
| 92 | + } |
| 93 | +} |
0 commit comments