This repository was archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from tmuehl/Issue-34-fix-content-type-detection
Issue 34 fix content type detection
- Loading branch information
Showing
7 changed files
with
155 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/org/zalando/fahrschein/IOProblemDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.zalando.fahrschein; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.Optional; | ||
|
||
import static java.util.Optional.empty; | ||
import static java.util.Optional.ofNullable; | ||
|
||
public class IOProblemDeserializer extends JsonDeserializer<IOProblem>{ | ||
|
||
@Override | ||
public IOProblem deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { | ||
final JsonNode rootNode = jp.getCodec().readTree(jp); | ||
final String type = rootNode.get("type").asText(); | ||
final String title = rootNode.get("title").asText(); | ||
final int status = rootNode.get("status").asInt(); | ||
|
||
final JsonNode detailNode = rootNode.get("detail"); | ||
final Optional<String> detail = ofNullable(detailNode != null ? detailNode.asText(null) : null); | ||
|
||
final JsonNode instanceNode = rootNode.get("instance"); | ||
final String instance = instanceNode != null ? instanceNode.asText(null) : null; | ||
|
||
try { | ||
Optional<URI> maybeInstance = instance != null ? Optional.of(new URI(instance)) : empty(); | ||
|
||
return new IOProblem(new URI(type), title, status, detail, maybeInstance); | ||
} catch (URISyntaxException e) { | ||
throw new IOException("Cannot deserialize IOProblem json", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/test/java/org/zalando/fahrschein/ProblemHandlingClientHttpRequestTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package org.zalando.fahrschein; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.client.ClientHttpRequest; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.Charset; | ||
|
||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.hamcrest.core.IsEqual.equalTo; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class ProblemHandlingClientHttpRequestTest { | ||
|
||
public static final Charset UTF8 = Charset.forName("UTF-8"); | ||
|
||
@Test | ||
public void recognisesJsonAndProblemBodies() throws IOException { | ||
final ClientHttpRequest clientHttpRequest = mock(ClientHttpRequest.class); | ||
|
||
when(clientHttpRequest.execute()).thenReturn(createResponse()); | ||
|
||
final ProblemHandlingClientHttpRequest problemHandlingClientHttpRequest = | ||
new ProblemHandlingClientHttpRequest(clientHttpRequest); | ||
|
||
Exception actualException = null; | ||
try { | ||
problemHandlingClientHttpRequest.execute(); | ||
Assert.fail("No exception was thrown."); | ||
} catch (Exception e) { | ||
actualException = e; | ||
} | ||
|
||
assertThat("Expected different exception type.", actualException, instanceOf(IOProblem.class)); | ||
|
||
final IOProblem ioProblem = (IOProblem) actualException; | ||
|
||
assertThat(ioProblem.getTitle(), equalTo("Not Found")); | ||
assertThat(ioProblem.getDetail().get(), equalTo("EventType does not exist.")); | ||
} | ||
|
||
private ClientHttpResponse createResponse() { | ||
return new ClientHttpResponse() { | ||
@Override | ||
public HttpStatus getStatusCode() throws IOException { | ||
return HttpStatus.BAD_REQUEST; | ||
} | ||
|
||
@Override | ||
public int getRawStatusCode() throws IOException { | ||
return 400; | ||
} | ||
|
||
@Override | ||
public String getStatusText() throws IOException { | ||
return "Bad Request"; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
// do nothing | ||
} | ||
|
||
@Override | ||
public InputStream getBody() throws IOException { | ||
return new ByteArrayInputStream("{\"type\":\"http://httpstatus.es/404\",\"title\":\"Not Found\",\"status\":404,\"detail\":\"EventType does not exist.\"}".getBytes(UTF8)); | ||
} | ||
|
||
@Override | ||
public HttpHeaders getHeaders() { | ||
final HttpHeaders httpHeaders = new HttpHeaders(); | ||
httpHeaders.add("Content-type", "application/problem+json;charset=UTF-8"); | ||
return httpHeaders; | ||
} | ||
}; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters