@Test(expected = CRestException.class) public void shouldShouleThrowLastExceptionWhenAllDeserializerHaveFailed() throws Exception { Exception e1 = new Exception(); Exception e2 = new Exception(); Deserializer mockDeserializer1 = mock(Deserializer.class); Deserializer mockDeserializer2 = mock(Deserializer.class); when(mockMethodConfig.getDeserializers()).thenReturn(new Deserializer[]{mockDeserializer1, mockDeserializer2}); when(mockDeserializer1.deserialize(String.class, String.class, mockDumpStream, UTF8)).thenThrow(e1); when(mockDeserializer2.deserialize(String.class, String.class, mockDumpStream, UTF8)).thenThrow(e2); mockStatic(IOs.class); when(IOs.toByteArray(mockStream, true)).thenReturn(SOME_STRING_UTF8_BYTES); try { toTest.deserialize(mockResponse); fail(); } catch (Exception e) { verify(mockDumpStream, times(2)).mark(0); verify(mockDumpStream, times(2)).reset(); assertSame(e2, e.getCause()); throw e; } }
public <T> T deserialize(Response response) throws Exception { MethodConfig mc = response.getRequest().getMethodConfig(); Deserializer[] deserializers = mc.getDeserializers(); isTrue(deserializers.length > 0, NO_DESERIALIZERS_ERROR, mc); InputStream pStream = response.asStream(); boolean isDumped; if (deserializers.length > 1) { pStream = new ByteArrayInputStream(toByteArray(pStream, true)); isDumped = true; }else{ isDumped = false; } Exception deserializationException = null; for (Deserializer deserializer : deserializers) { try { LOG.debug("Trying to deserialize response with user specified deserializer: %s.", deserializer); return deserializer.<T>deserialize( (Class<T>) response.getExpectedType(), response.getExpectedGenericType(), pStream, response.getCharset()); } catch (Exception e) { LOG.warn(e, "Failed to deserialize response with user specified deserializer: %s. Trying next.", deserializer); deserializationException = e; if(isDumped) { pStream.mark(0); pStream.reset(); } } } throw new CRestException("Could not deserialize response with given deserializers " + Arrays.toString(deserializers), deserializationException); }
ResponseDeserializerByDeserializers implements ResponseDeserializer { public <T> T deserialize(Response response) throws Exception { MethodConfig mc = response.getRequest().getMethodConfig(); Deserializer[] deserializers = mc.getDeserializers(); isTrue(deserializers.length > 0, NO_DESERIALIZERS_ERROR, mc); InputStream pStream = response.asStream(); boolean isDumped; if (deserializers.length > 1) { pStream = new ByteArrayInputStream(toByteArray(pStream, true)); isDumped = true; }else{ isDumped = false; } Exception deserializationException = null; for (Deserializer deserializer : deserializers) { try { LOG.debug("Trying to deserialize response with user specified deserializer: %s.", deserializer); return deserializer.<T>deserialize( (Class<T>) response.getExpectedType(), response.getExpectedGenericType(), pStream, response.getCharset()); } catch (Exception e) { LOG.warn(e, "Failed to deserialize response with user specified deserializer: %s. Trying next.", deserializer); deserializationException = e; if(isDumped) { pStream.mark(0); pStream.reset(); } } } throw new CRestException("Could not deserialize response with given deserializers " + Arrays.toString(deserializers), deserializationException); } }
ResponseDeserializerByDeserializers implements ResponseDeserializer { public <T> T deserialize(Response response) throws Exception { MethodConfig mc = response.getRequest().getMethodConfig(); Deserializer[] deserializers = mc.getDeserializers(); isTrue(deserializers.length > 0, NO_DESERIALIZERS_ERROR, mc); InputStream pStream = response.asStream(); boolean isDumped; if (deserializers.length > 1) { pStream = new ByteArrayInputStream(toByteArray(pStream, true)); isDumped = true; }else{ isDumped = false; } Exception deserializationException = null; for (Deserializer deserializer : deserializers) { try { LOG.debug("Trying to deserialize response with user specified deserializer: %s.", deserializer); return deserializer.<T>deserialize( (Class<T>) response.getExpectedType(), response.getExpectedGenericType(), pStream, response.getCharset()); } catch (Exception e) { LOG.warn(e, "Failed to deserialize response with user specified deserializer: %s. Trying next.", deserializer); deserializationException = e; if(isDumped) { pStream.mark(0); pStream.reset(); } } } throw new CRestException("Could not deserialize response with given deserializers " + Arrays.toString(deserializers), deserializationException); }  }
ResponseDeserializerByDeserializers implements ResponseDeserializer { public <T> T deserialize(Response response) throws Exception { MethodConfig mc = response.getRequest().getMethodConfig(); Deserializer[] deserializers = mc.getDeserializers(); isTrue(deserializers.length > 0, NO_DESERIALIZERS_ERROR, mc); InputStream pStream = response.asStream(); boolean isDumped; if (deserializers.length > 1) { pStream = new ByteArrayInputStream(toByteArray(pStream, true)); isDumped = true; }else{ isDumped = false; } Exception deserializationException = null; for (Deserializer deserializer : deserializers) { try { LOG.debug("Trying to deserialize response with user specified deserializer: %s.", deserializer); return deserializer.<T>deserialize( (Class<T>) response.getExpectedType(), response.getExpectedGenericType(), pStream, response.getCharset()); } catch (Exception e) { LOG.warn(e, "Failed to deserialize response with user specified deserializer: %s. Trying next.", deserializer); deserializationException = e; if(isDumped) { pStream.mark(0); pStream.reset(); } } } throw new CRestException("Could not deserialize response with given deserializers " + Arrays.toString(deserializers), deserializationException); }  T deserialize(Response response); }
ResponseDeserializerByDeserializers implements ResponseDeserializer { public <T> T deserialize(Response response) throws Exception { MethodConfig mc = response.getRequest().getMethodConfig(); Deserializer[] deserializers = mc.getDeserializers(); isTrue(deserializers.length > 0, NO_DESERIALIZERS_ERROR, mc); InputStream pStream = response.asStream(); boolean isDumped; if (deserializers.length > 1) { pStream = new ByteArrayInputStream(toByteArray(pStream, true)); isDumped = true; }else{ isDumped = false; } Exception deserializationException = null; for (Deserializer deserializer : deserializers) { try { LOG.debug("Trying to deserialize response with user specified deserializer: %s.", deserializer); return deserializer.<T>deserialize( (Class<T>) response.getExpectedType(), response.getExpectedGenericType(), pStream, response.getCharset()); } catch (Exception e) { LOG.warn(e, "Failed to deserialize response with user specified deserializer: %s. Trying next.", deserializer); deserializationException = e; if(isDumped) { pStream.mark(0); pStream.reset(); } } } throw new CRestException("Could not deserialize response with given deserializers " + Arrays.toString(deserializers), deserializationException); }  T deserialize(Response response);  }