DefaultRepresentationResult implements RepresentationResult { public <T> Serializer from(T object) { return from(object, null); } DefaultRepresentationResult(FormatResolver formatResolver, Result result, List<Serialization> serializations, RestHeadersHandler headersHandler); Serializer from(T object); Serializer from(T object, String alias);  }
@Test public void whenThereIsNoFormatGivenShouldForwardToDefaultPage() throws Exception { when(formatResolver.getAcceptFormat()).thenReturn(null); Serializer serializer = representation.from(new Object()); assertThat(serializer, is(instanceOf(IgnoringSerializer.class))); verify(status).notAcceptable(); }
@Test public void shouldSend404IfNothingIsRendered() throws Exception { when(formatResolver.getAcceptFormat()).thenReturn(null); Serializer serializer = representation.from(null); assertThat(serializer, is(instanceOf(IgnoringSerializer.class))); verify(status).notFound(); }
@Test public void whenThereIsNoFormatGivenShouldForwardToDefaultPageWithAlias() throws Exception { when(formatResolver.getAcceptFormat()).thenReturn(null); Object object = new Object(); Serializer serializer = representation.from(object, "Alias!"); assertThat(serializer, is(instanceOf(IgnoringSerializer.class))); verify(status).notAcceptable(); }
@Test public void whenThereIsAFormatGivenShouldUseCorrectSerializer() throws Exception { when(formatResolver.getAcceptFormat()).thenReturn("xml"); when(serialization.accepts("xml")).thenReturn(true); Object object = new Object(); representation.from(object); verify(serialization).from(object); }
@Test public void whenThereIsAFormatGivenShouldUseCorrectSerializerWithAlias() throws Exception { when(formatResolver.getAcceptFormat()).thenReturn("xml"); when(serialization.accepts("xml")).thenReturn(true); Object object = new Object(); representation.from(object, "Alias!"); verify(serialization).from(object, "Alias!"); }
@Test public void whenSerializationDontAcceptsFormatItShouldntBeUsed() throws Exception { when(formatResolver.getAcceptFormat()).thenReturn("xml"); when(serialization.accepts("xml")).thenReturn(false); Object object = new Object(); representation.from(object); verify(serialization, never()).from(object); }
@Test public void whenTheResourceIsHypermediaAddRestHeaders() throws Exception { when(formatResolver.getAcceptFormat()).thenReturn("xml"); when(serialization.accepts("xml")).thenReturn(true); HypermediaResource object = mock(HypermediaResource.class); representation.from(object); verify(serialization).from(object); verify(headerHandler).handle(object); }