|
| 1 | +package org.reactivecommons.async.rabbit.converters.json; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.JsonNode; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import io.cloudevents.CloudEvent; |
| 7 | +import io.cloudevents.core.builder.CloudEventBuilder; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.reactivecommons.api.domain.Command; |
| 10 | +import org.reactivecommons.api.domain.DomainEvent; |
| 11 | +import org.reactivecommons.async.api.AsyncQuery; |
| 12 | +import org.reactivecommons.async.commons.communications.Message; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.net.URI; |
| 16 | +import java.nio.charset.StandardCharsets; |
| 17 | +import java.time.OffsetDateTime; |
| 18 | +import java.util.Date; |
| 19 | +import java.util.UUID; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | + |
| 23 | +public class JacksonCloudEventMessageConverterTest { |
| 24 | + |
| 25 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 26 | + private final JacksonCloudEventMessageConverter converter = new JacksonCloudEventMessageConverter(objectMapper); |
| 27 | + |
| 28 | + @Test |
| 29 | + void readAsyncQuery() throws JsonProcessingException { |
| 30 | + Date date = new Date(); |
| 31 | + CloudEvent query = CloudEventBuilder.v1() // |
| 32 | + .withId(UUID.randomUUID().toString()) // |
| 33 | + .withSource(URI.create("https://spring.io/foos"))// |
| 34 | + .withType("query") |
| 35 | + .withData("application/json", CloudEventBuilderExt.asBytes(new SampleClass("35", "name1", date))) |
| 36 | + .build(); |
| 37 | + final Message message = converter.toMessage(new AsyncQuery<>(query.getType(), query)); |
| 38 | + final AsyncQuery<CloudEvent> value = converter.readAsyncQuery(message, CloudEvent.class); |
| 39 | + byte[] bytes = value.getQueryData().getData().toBytes(); |
| 40 | + String stringData = new String(bytes, StandardCharsets.UTF_8); |
| 41 | + SampleClass result = objectMapper.readValue(stringData, SampleClass.class); |
| 42 | + assertThat(result) |
| 43 | + .extracting(SampleClass::getId, SampleClass::getName, SampleClass::getDate) |
| 44 | + .containsExactly("35", "name1", date); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void readDomainEvent() throws JsonProcessingException { |
| 50 | + Date date = new Date(); |
| 51 | + CloudEvent event = CloudEventBuilder.v1() // |
| 52 | + .withId(UUID.randomUUID().toString()) // |
| 53 | + .withSource(URI.create("https://spring.io/foos"))// |
| 54 | + .withType("event") |
| 55 | + .withData("application/json", CloudEventBuilderExt.asBytes(new SampleClass("35", "name1", date))) |
| 56 | + .build(); |
| 57 | + final Message message = converter.toMessage(new DomainEvent<>(event.getType(), event.getId(), event)); |
| 58 | + final DomainEvent<CloudEvent> value = converter.readDomainEvent(message, CloudEvent.class); |
| 59 | + byte[] bytes = value.getData().getData().toBytes(); |
| 60 | + String stringData = new String(bytes, StandardCharsets.UTF_8); |
| 61 | + SampleClass result = objectMapper.readValue(stringData, SampleClass.class); |
| 62 | + assertThat(result) |
| 63 | + .extracting(SampleClass::getId, SampleClass::getName, SampleClass::getDate) |
| 64 | + .containsExactly("35", "name1", date); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void readCommand() throws JsonProcessingException { |
| 69 | + Date date = new Date(); |
| 70 | + CloudEvent command = CloudEventBuilder.v1() // |
| 71 | + .withId(UUID.randomUUID().toString()) // |
| 72 | + .withSource(URI.create("https://spring.io/foos"))// |
| 73 | + .withType("command") |
| 74 | + .withData("application/json", CloudEventBuilderExt.asBytes(new SampleClass("35", "name1", date))) |
| 75 | + .build(); |
| 76 | + final Message message = converter.toMessage(new Command<>(command.getType(), command.getId(), command)); |
| 77 | + final Command<CloudEvent> value = converter.readCommand(message, CloudEvent.class); |
| 78 | + byte[] bytes = value.getData().getData().toBytes(); |
| 79 | + String stringData = new String(bytes, StandardCharsets.UTF_8); |
| 80 | + SampleClass result = objectMapper.readValue(stringData, SampleClass.class); |
| 81 | + assertThat(result) |
| 82 | + .extracting(SampleClass::getId, SampleClass::getName, SampleClass::getDate) |
| 83 | + .containsExactly("35", "name1", date); |
| 84 | + } |
| 85 | + @Test |
| 86 | + void toMessage() { |
| 87 | + final Message message = converter.toMessage(new SampleClass("42", "Daniel", new Date())); |
| 88 | + assertThat(new String(message.getBody())).contains("42").contains("Daniel"); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void toMessageWhenDataIsNull() throws IOException { |
| 93 | + final Message message = converter.toMessage(null); |
| 94 | + |
| 95 | + final JsonNode jsonNode = objectMapper.readTree(message.getBody()); |
| 96 | + assertThat(jsonNode.isNull()).isTrue(); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void toMessageWhenDataIsEmpty() throws IOException { |
| 101 | + final Message message = converter.toMessage(""); |
| 102 | + |
| 103 | + final JsonNode jsonNode = objectMapper.readTree(message.getBody()); |
| 104 | + assertThat(jsonNode.asText()).isEmpty(); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void readValue() { |
| 109 | + Date date = new Date(); |
| 110 | + final Message message = converter.toMessage(new SampleClass("35", "name1", date)); |
| 111 | + final SampleClass value = converter.readValue(message, SampleClass.class); |
| 112 | + assertThat(value).extracting(SampleClass::getId, SampleClass::getName, SampleClass::getDate) |
| 113 | + .containsExactly("35", "name1", date); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void readValueString() { |
| 118 | + final Message message = converter.toMessage("Hi!"); |
| 119 | + final String value = converter.readValue(message, String.class); |
| 120 | + assertThat(value).isEqualTo("Hi!"); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void shouldConvertToCommandStructure() { |
| 125 | + final SampleClass data = new SampleClass("35", "name1", new Date()); |
| 126 | + final Message message = converter.toMessage(new Command<>("cmd.name", "42", data)); |
| 127 | + final Command<Object> command = converter.readCommandStructure(message); |
| 128 | + |
| 129 | + assertThat(command.getData()).isInstanceOf(JsonNode.class); |
| 130 | + assertThat(command.getName()).isEqualTo("cmd.name"); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void shouldConvertToDomainEventStructure() { |
| 135 | + final SampleClass data = new SampleClass("35", "name1", new Date()); |
| 136 | + final Message message = converter.toMessage(new DomainEvent<>("event.name", "42", data)); |
| 137 | + final DomainEvent<Object> event = converter.readDomainEventStructure(message); |
| 138 | + |
| 139 | + assertThat(event.getData()).isInstanceOf(JsonNode.class); |
| 140 | + assertThat(event.getName()).isEqualTo("event.name"); |
| 141 | + final JsonNode jsonNode = (JsonNode) event.getData(); |
| 142 | + assertThat(jsonNode.findValue("name").asText()).isEqualTo("name1"); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + void shouldConvertToQueryStructure() { |
| 147 | + final SampleClass data = new SampleClass("35", "sample1", new Date()); |
| 148 | + final Message message = converter.toMessage(new AsyncQuery<>("query.name", data)); |
| 149 | + final AsyncQuery<Object> query = converter.readAsyncQueryStructure(message); |
| 150 | + |
| 151 | + assertThat(query.getQueryData()).isInstanceOf(JsonNode.class); |
| 152 | + assertThat(query.getResource()).isEqualTo("query.name"); |
| 153 | + final JsonNode jsonNode = (JsonNode) query.getQueryData(); |
| 154 | + assertThat(jsonNode.findValue("name").asText()).isEqualTo("sample1"); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + void shouldNotFailWithTilde() { |
| 159 | + // Arrange |
| 160 | + final String name = "example with word containing tilde áéíóúñ"; |
| 161 | + final SampleClass data = new SampleClass("35", name, new Date()); |
| 162 | + final Message message = converter.toMessage(new AsyncQuery<>("query.name", data)); |
| 163 | + // Act |
| 164 | + final AsyncQuery<Object> query = converter.readAsyncQueryStructure(message); |
| 165 | + // Assert |
| 166 | + assertThat(query.getQueryData()).isInstanceOf(JsonNode.class); |
| 167 | + assertThat(query.getResource()).isEqualTo("query.name"); |
| 168 | + final JsonNode jsonNode = (JsonNode) query.getQueryData(); |
| 169 | + assertThat(jsonNode.findValue("name").asText()).isEqualTo(name); |
| 170 | + } |
| 171 | +} |
0 commit comments