IntegratedNetworkResponseParser implements NetworkResponseParser { @Override public <T> Response<T> parseNetworkResponse(NetworkResponse response, Class<T> clazz) { Assert.notNull(response, "NetworkResponse"); Assert.notNull(clazz, "Target class token"); if (clazz == Void.class) { return Response.success(null, null); } if (clazz == String.class) { return STRING_NETWORK_RESPONSE_PARSER.parseNetworkResponse(response, clazz); } if (clazz == NetworkResponse.class) { @SuppressWarnings("unchecked") Response<T> successResponse = (Response<T>) Response.success(response, HttpHeaderParser.parseCacheHeaders(response)); return successResponse; } String contentTypeString = getResponseHeader(response, CONTENT_TYPE_HEADER_KEY); if (contentTypeString == null) { return Response.error(new ParseError(new UnsupportedContentTypeException("It cannot find any response parser, " + "because the response content type is null."))); } ContentType contentType = ContentType.createContentType(contentTypeString); NetworkResponseParser responseParser = parsers.get(contentType); if (responseParser == null) { return Response.error(new ParseError(new UnsupportedContentTypeException("It cannot find any response parser " + "for the response content type."))); } return responseParser.parseNetworkResponse(response, clazz); } IntegratedNetworkResponseParser(Map<ContentType, NetworkResponseParser> parsers); @Override Response<T> parseNetworkResponse(NetworkResponse response, Class<T> clazz);  }
@Test public void resultShouldBeStringWhenTargetClassIsString() { networkResponse = createJsonNetworkResponse(); parsers = createDefaultParsersMap(); Class<String> clazz = String.class; integratedResponseParser = createIntegratedNetworkResponseParser(parsers); Response<String> response = integratedResponseParser.parseNetworkResponse(networkResponse, clazz); assertThat(jsonContent, is(response.result)); }
@Test public void resultShouldBeNullWhenTargetClassIsVoid() { networkResponse = new NetworkResponse(jsonContent.getBytes()); parsers = createDefaultParsersMap(); integratedResponseParser = createIntegratedNetworkResponseParser(parsers); Class<Void> clazz = Void.class; Response<Void> response = integratedResponseParser.parseNetworkResponse(networkResponse, clazz); assertNull(response.result); assertNull(response.cacheEntry); }
@Test public void parserShouldSelectJsonMappedResponseParserWhenContentTypeIsApplicationJson() { networkResponse = createJsonNetworkResponse(); NetworkResponseParser responseParser = mock(NetworkResponseParser.class); parsers.put(ContentType.CONTENT_TYPE_APPLICATION_JSON, responseParser); integratedResponseParser = createIntegratedNetworkResponseParser(parsers); Class<News> clazz = News.class; Response<News> response = integratedResponseParser.parseNetworkResponse(networkResponse, clazz); verify(responseParser).parseNetworkResponse(networkResponse, clazz); }
@Test public void parserShouldReturnVolleyErrorWhenContentTypeIsNotMappedIntoResponseParser() { networkResponse = createUnknownNetworkResponse(); NetworkResponseParser responseParser = mock(NetworkResponseParser.class); parsers.put(ContentType.CONTENT_TYPE_APPLICATION_JSON, responseParser); integratedResponseParser = createIntegratedNetworkResponseParser(parsers); Class<News> clazz = News.class; Response<News> response = integratedResponseParser.parseNetworkResponse(networkResponse, clazz); assertNotNull(response.error); }
@Test public void parserShouldReturnVolleyErrorWhenContentTypeIsNotSpecified() { networkResponse = new NetworkResponse(jsonContent.getBytes()); NetworkResponseParser responseParser = mock(NetworkResponseParser.class); parsers.put(ContentType.CONTENT_TYPE_APPLICATION_JSON, responseParser); integratedResponseParser = createIntegratedNetworkResponseParser(parsers); Class<News> clazz = News.class; Response<News> response = integratedResponseParser.parseNetworkResponse(networkResponse, clazz); assertNotNull(response.error); }