DefaultResponseDeserializer implements ResponseDeserializer { @Override public <A extends RestAction<R>, R> R deserialize(A action, Response response) throws ActionException { if (isSuccessStatusCode(response)) { return getDeserializedResponse(action, response); } else { throw new ActionException(response.getStatusText()); } } @Inject protected  DefaultResponseDeserializer(Provider<Set<Serialization>> serializationsProvider); @Override R deserialize(A action, Response response);  }
@Test(expected = ActionException.class) public void deserialize_errorCode_throws() throws ActionException { UnsecuredRestAction action = new UnsecuredRestAction(HttpMethod.GET, ""); Response response = mock(Response.class); given(response.getStatusCode()).willReturn(404); deserializer.deserialize(action, response); }
@Test(expected = ActionException.class) public void deserialize_resultClassNotKnown_throws() throws ActionException { UnsecuredRestAction action = new UnsecuredRestAction(HttpMethod.GET, ""); Response response = mock(Response.class); given(response.getStatusCode()).willReturn(200); deserializer.deserialize(action, response); }
@Test(expected = ActionException.class) public void deserialize_noCapableSerialization_throws() throws ActionException { UnsecuredRestAction action = new UnsecuredRestAction(HttpMethod.GET, ""); action.setResultClass("MyClass<Hey<Ho>>>"); Response response = mock(Response.class); given(response.getStatusCode()).willReturn(200); given(response.getHeader(HttpHeaders.CONTENT_TYPE)).willReturn("any"); deserializer.deserialize(action, response); }
@Test public void deserialize_serialization_delegates() throws ActionException { String resultType = "MyClass<Hey<Ho>>>"; ContentType contentType = ContentType.valueOf("that-content-type"); String serializedContent = "agakrybatgfkasfh"; Object expectedResult = new Object(); UnsecuredRestAction action = new UnsecuredRestAction(HttpMethod.GET, ""); action.setResultClass(resultType); Response response = mock(Response.class); given(response.getStatusCode()).willReturn(200); given(response.getHeader(HttpHeaders.CONTENT_TYPE)).willReturn(contentType.toString()); given(response.getText()).willReturn(serializedContent); given(serialization.canDeserialize(resultType, contentType)).willReturn(true); given(serialization.deserialize(resultType, contentType, serializedContent)).willReturn(expectedResult); Object result = deserializer.deserialize(action, response); assertThat(result).isSameAs(expectedResult); }