Skip to content

Commit

Permalink
Deserializing observing StackStarting. Closes #425
Browse files Browse the repository at this point in the history
  • Loading branch information
Turini committed Mar 7, 2014
1 parent d4a45cd commit b9d7d5e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import br.com.caelum.vraptor.controller.ControllerMethod;
import br.com.caelum.vraptor.core.MethodInfo;
import br.com.caelum.vraptor.events.ReadyToExecuteMethod;
import br.com.caelum.vraptor.events.StackStarting;
import br.com.caelum.vraptor.ioc.Container;
import br.com.caelum.vraptor.serialization.Deserializer;
import br.com.caelum.vraptor.serialization.Deserializers;
Expand Down Expand Up @@ -66,7 +67,7 @@ public DeserializingObserver(Deserializers deserializers, Container container) {
this.container = container;
}

public void deserializes(@Observes ReadyToExecuteMethod event, HttpServletRequest request,
public void deserializes(@Observes StackStarting event, HttpServletRequest request,
MethodInfo methodInfo, Status status) throws IOException {

ControllerMethod method = event.getControllerMethod();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import br.com.caelum.vraptor.controller.ControllerMethod;
import br.com.caelum.vraptor.controller.DefaultControllerMethod;
import br.com.caelum.vraptor.core.MethodInfo;
import br.com.caelum.vraptor.events.ReadyToExecuteMethod;
import br.com.caelum.vraptor.events.StackStarting;
import br.com.caelum.vraptor.factory.Factories;
import br.com.caelum.vraptor.ioc.Container;
import br.com.caelum.vraptor.serialization.Deserializer;
Expand Down Expand Up @@ -63,15 +63,15 @@ public void doesntConsume() {}

@Test
public void shouldOnlyAcceptMethodsWithConsumesAnnotation() throws Exception {
observer.deserializes(new ReadyToExecuteMethod(doesntConsume), request, methodInfo, status);
observer.deserializes(new StackStarting(doesntConsume), request, methodInfo, status);
verifyZeroInteractions(request);
}

@Test
public void willSetHttpStatusCode415IfTheControllerMethodDoesNotSupportTheGivenMediaTypes() throws Exception {
when(request.getContentType()).thenReturn("image/jpeg");

observer.deserializes(new ReadyToExecuteMethod(consumeXml), request, methodInfo, status);
observer.deserializes(new StackStarting(consumeXml), request, methodInfo, status);

verify(status).unsupportedMediaType("Request with media type [image/jpeg]. Expecting one of [application/xml].");
}
Expand All @@ -81,7 +81,7 @@ public void willSetHttpStatusCode415IfThereIsNoDeserializerButIsAccepted() throw
when(request.getContentType()).thenReturn("application/xml");
when(deserializers.deserializerFor("application/xml", container)).thenReturn(null);

observer.deserializes(new ReadyToExecuteMethod(consumeXml), request, methodInfo, status);
observer.deserializes(new StackStarting(consumeXml), request, methodInfo, status);

verify(status).unsupportedMediaType("Unable to handle media type [application/xml]: no deserializer found.");
}
Expand All @@ -96,7 +96,7 @@ public void willSetMethodParametersWithDeserializationAndContinueStackAfterDeser
when(deserializer.deserialize(null, consumeXml)).thenReturn(new Object[] {"abc", "def"});
when(deserializers.deserializerFor("application/xml", container)).thenReturn(deserializer);

observer.deserializes(new ReadyToExecuteMethod(consumeXml), request, methodInfo, status);
observer.deserializes(new StackStarting(consumeXml), request, methodInfo, status);

assertEquals(methodInfo.getValuedParameters()[0].getValue(), "abc");
assertEquals(methodInfo.getValuedParameters()[1].getValue(), "def");
Expand All @@ -112,7 +112,7 @@ public void willSetMethodParametersWithDeserializationEvenIfTheContentTypeHasCha
when(deserializer.deserialize(null, consumeXml)).thenReturn(new Object[] {"abc", "def"});
when(deserializers.deserializerFor("application/xml", container)).thenReturn(deserializer);

observer.deserializes(new ReadyToExecuteMethod(consumeXml), request, methodInfo, status);
observer.deserializes(new StackStarting(consumeXml), request, methodInfo, status);

assertEquals(methodInfo.getValuedParameters()[0].getValue(), "abc");
assertEquals(methodInfo.getValuedParameters()[1].getValue(), "def");
Expand All @@ -131,7 +131,7 @@ public void willDeserializeForAnyContentTypeIfPossible() throws Exception {
when(deserializer.deserialize(null, consumesAnything)).thenReturn(new Object[] {"abc", "def"});

when(deserializers.deserializerFor("application/xml", container)).thenReturn(deserializer);
observer.deserializes(new ReadyToExecuteMethod(consumesAnything), request, methodInfo, status);
observer.deserializes(new StackStarting(consumesAnything), request, methodInfo, status);

assertEquals(methodInfo.getValuedParameters()[0].getValue(), "abc");
assertEquals(methodInfo.getValuedParameters()[1].getValue(), "def");
Expand All @@ -149,7 +149,7 @@ public void willSetOnlyNonNullParameters() throws Exception {
when(deserializer.deserialize(null, consumeXml)).thenReturn(new Object[] {null, "deserialized"});

when(deserializers.deserializerFor("application/xml", container)).thenReturn(deserializer);
observer.deserializes(new ReadyToExecuteMethod(consumeXml), request, methodInfo, status);
observer.deserializes(new StackStarting(consumeXml), request, methodInfo, status);

assertEquals(methodInfo.getValuedParameters()[0].getValue(), "original1");
assertEquals(methodInfo.getValuedParameters()[1].getValue(), "deserialized");
Expand All @@ -162,6 +162,6 @@ public void shouldThrowInterceptionExceptionIfAnIOExceptionOccurs() throws Excep

final Deserializer deserializer = mock(Deserializer.class);
when(deserializers.deserializerFor("application/xml", container)).thenReturn(deserializer);
observer.deserializes(new ReadyToExecuteMethod(consumeXml), request, methodInfo, status);
observer.deserializes(new StackStarting(consumeXml), request, methodInfo, status);
}
}

0 comments on commit b9d7d5e

Please sign in to comment.