forked from cloudevents/sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: JSON format to assume JSON content-type where possible (cloudev…
…ents#604) Signed-off-by: Matej Vašek <[email protected]>
- Loading branch information
1 parent
7b9d020
commit 55fddb3
Showing
4 changed files
with
98 additions
and
8 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
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
69 changes: 69 additions & 0 deletions
69
formats/json-jackson/src/test/java/io/cloudevents/jackson/CloudEventDeserializerTest.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,69 @@ | ||
package io.cloudevents.jackson; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import io.cloudevents.CloudEvent; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.StringReader; | ||
|
||
import static io.cloudevents.jackson.JsonFormat.getCloudEventJacksonModule; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class CloudEventDeserializerTest { | ||
|
||
private static final String nonBinaryPayload = "{\n" + | ||
" \"specversion\" : \"1.0\",\n" + | ||
" \"type\" : \"com.example.someevent\",\n" + | ||
" \"source\" : \"/mycontext\",\n" + | ||
" \"subject\": null,\n" + | ||
" \"id\" : \"D234-1234-1234\",\n" + | ||
" \"time\" : \"2018-04-05T17:31:00Z\",\n" + | ||
" \"comexampleextension1\" : \"value\",\n" + | ||
" \"comexampleothervalue\" : 5,\n" + | ||
" \"data\" : \"I'm just a string\"\n" + | ||
"}"; | ||
|
||
private static final String binaryPayload = "{\n" + | ||
" \"specversion\" : \"1.0\",\n" + | ||
" \"type\" : \"com.example.someevent\",\n" + | ||
" \"source\" : \"/mycontext\",\n" + | ||
" \"id\" : \"D234-1234-1234\",\n" + | ||
" \"data_base64\" : \"eyAieHl6IjogMTIzIH0=\"\n" + | ||
"}"; | ||
|
||
@Test | ||
void impliedDataContentTypeNonBinaryData() throws IOException { | ||
ObjectMapper mapper = getObjectMapper(false); | ||
StringReader reader = new StringReader(nonBinaryPayload); | ||
CloudEvent ce = mapper.readValue(reader, CloudEvent.class); | ||
assertThat(ce.getDataContentType()).isEqualTo("application/json"); | ||
|
||
mapper = getObjectMapper(true); | ||
reader = new StringReader(nonBinaryPayload); | ||
ce = mapper.readValue(reader, CloudEvent.class); | ||
assertThat(ce.getDataContentType()).isNull(); | ||
} | ||
|
||
@Test | ||
void impliedDataContentTypeBinaryData() throws IOException { | ||
final ObjectMapper mapper = getObjectMapper(false); | ||
StringReader reader = new StringReader(binaryPayload); | ||
CloudEvent ce = mapper.readValue(reader, CloudEvent.class); | ||
assertThat(ce.getDataContentType()).isNull(); | ||
} | ||
|
||
private static ObjectMapper getObjectMapper(boolean disableDataContentTypeDefaulting) { | ||
final ObjectMapper mapper = new ObjectMapper(); | ||
final SimpleModule module = getCloudEventJacksonModule( | ||
JsonFormatOptions | ||
.builder() | ||
.disableDataContentTypeDefaulting(disableDataContentTypeDefaulting) | ||
.build() | ||
); | ||
mapper.registerModule(module); | ||
return mapper; | ||
} | ||
|
||
} |