|
| 1 | +package io.eventdriven.eventsversioning.transformations; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import io.eventdriven.eventsversioning.serialization.Serializer; |
| 5 | +import io.eventdriven.eventsversioning.v1.ShoppingCartEvent; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Optional; |
| 11 | +import java.util.UUID; |
| 12 | +import java.util.function.Function; |
| 13 | + |
| 14 | +import static java.util.stream.Collectors.toMap; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 17 | + |
| 18 | +public class MultipleTransformationsWithDifferentEventTypesTests { |
| 19 | + public record Client( |
| 20 | + UUID id, |
| 21 | + String name |
| 22 | + ) { |
| 23 | + public Client { |
| 24 | + if (name == null) { |
| 25 | + name = "Unknown"; |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + public record ShoppingCartOpened( |
| 31 | + UUID shoppingCartId, |
| 32 | + Client client |
| 33 | + ) { |
| 34 | + } |
| 35 | + |
| 36 | + public enum ShoppingCartStatus { |
| 37 | + Pending, |
| 38 | + Opened, |
| 39 | + Confirmed, |
| 40 | + Cancelled |
| 41 | + } |
| 42 | + |
| 43 | + public record ShoppingCartInitializedWithStatus( |
| 44 | + UUID shoppingCartId, |
| 45 | + Client client, |
| 46 | + // Adding new not required property as nullable |
| 47 | + ShoppingCartStatus status |
| 48 | + ) { |
| 49 | + } |
| 50 | + |
| 51 | + public static ShoppingCartInitializedWithStatus upcastV1( |
| 52 | + JsonNode oldEvent |
| 53 | + ) { |
| 54 | + return new ShoppingCartInitializedWithStatus( |
| 55 | + UUID.fromString(oldEvent.at("/shoppingCartId").asText()), |
| 56 | + new Client( |
| 57 | + UUID.fromString(oldEvent.at("/clientId").asText()), |
| 58 | + null |
| 59 | + ), |
| 60 | + ShoppingCartStatus.Opened |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + public static ShoppingCartInitializedWithStatus upcastV2( |
| 65 | + ShoppingCartOpened oldEvent |
| 66 | + ) { |
| 67 | + return new ShoppingCartInitializedWithStatus( |
| 68 | + oldEvent.shoppingCartId(), |
| 69 | + oldEvent.client(), |
| 70 | + ShoppingCartStatus.Opened |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + public class EventTransformations { |
| 75 | + private final Map<String, Function<byte[], Object>> jsonTransformations = new HashMap<>(); |
| 76 | + |
| 77 | + public Optional<Object> tryTransform(String eventTypeName, byte[] json) { |
| 78 | + if (!jsonTransformations.containsKey(eventTypeName)) { |
| 79 | + return Optional.empty(); |
| 80 | + } |
| 81 | + |
| 82 | + var transformJson = jsonTransformations.get(eventTypeName); |
| 83 | + |
| 84 | + return Optional.of(transformJson.apply(json)); |
| 85 | + } |
| 86 | + |
| 87 | + public <Event> EventTransformations register( |
| 88 | + String eventTypeName, |
| 89 | + Function<JsonNode, Event> transformJson |
| 90 | + ) { |
| 91 | + jsonTransformations.put( |
| 92 | + eventTypeName, |
| 93 | + json -> transformJson.apply(Serializer.deserialize(json)) |
| 94 | + ); |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + public <OldEvent, Event> EventTransformations register( |
| 99 | + Class<OldEvent> oldEventClass, |
| 100 | + String eventTypeName, |
| 101 | + Function<OldEvent, Event> transformEvent |
| 102 | + ) { |
| 103 | + jsonTransformations.put( |
| 104 | + eventTypeName, |
| 105 | + json -> transformEvent.apply(Serializer.deserialize(oldEventClass, json) |
| 106 | + .orElseThrow(() -> new RuntimeException("Error deserializing"))) |
| 107 | + ); |
| 108 | + return this; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public class EventTypeMapping { |
| 113 | + private final Map<String, Class> mappings = new HashMap<>(); |
| 114 | + |
| 115 | + public <Event> EventTypeMapping register(Class<Event> eventClass, String... typeNames) { |
| 116 | + |
| 117 | + for (var typeName : typeNames) { |
| 118 | + mappings.put(typeName, eventClass); |
| 119 | + } |
| 120 | + |
| 121 | + return this; |
| 122 | + } |
| 123 | + |
| 124 | + public Class map(String eventType) { |
| 125 | + return mappings.get(eventType); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public record EventSerializer( |
| 130 | + EventTypeMapping mapping, |
| 131 | + EventTransformations transformations |
| 132 | + ) { |
| 133 | + public Optional<Object> deserialize(String eventTypeName, byte[] json) { |
| 134 | + return transformations.tryTransform(eventTypeName, json) |
| 135 | + .or(() -> Serializer.deserialize(mapping().map(eventTypeName), json)); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void UpcastObjects_Should_BeForwardCompatible() { |
| 141 | + // Given |
| 142 | + final String eventTypeV1Name = "shopping_cart_initialized_v1"; |
| 143 | + final String eventTypeV2Name = "shopping_cart_initialized_v2"; |
| 144 | + final String eventTypeV3Name = "shopping_cart_initialized_v3"; |
| 145 | + |
| 146 | + var mapping = new EventTypeMapping() |
| 147 | + .register(ShoppingCartInitializedWithStatus.class, |
| 148 | + eventTypeV1Name, |
| 149 | + eventTypeV2Name, |
| 150 | + eventTypeV3Name |
| 151 | + ); |
| 152 | + |
| 153 | + var transformations = new EventTransformations() |
| 154 | + .register(eventTypeV1Name, MultipleTransformationsWithDifferentEventTypesTests::upcastV1) |
| 155 | + .register(ShoppingCartOpened.class, eventTypeV2Name, MultipleTransformationsWithDifferentEventTypesTests::upcastV2); |
| 156 | + |
| 157 | + var serializer = new EventSerializer(mapping, transformations); |
| 158 | + |
| 159 | + var eventV1 = new ShoppingCartEvent.ShoppingCartOpened( |
| 160 | + UUID.randomUUID(), |
| 161 | + UUID.randomUUID() |
| 162 | + ); |
| 163 | + var eventV2 = new ShoppingCartOpened( |
| 164 | + UUID.randomUUID(), |
| 165 | + new Client(UUID.randomUUID(), "Oscar the Grouch") |
| 166 | + ); |
| 167 | + var eventV3 = new ShoppingCartInitializedWithStatus( |
| 168 | + UUID.randomUUID(), |
| 169 | + new Client(UUID.randomUUID(), "Big Bird"), |
| 170 | + ShoppingCartStatus.Pending |
| 171 | + ); |
| 172 | + |
| 173 | + var events = new HashMap<String, byte[]>() {{ |
| 174 | + put(eventTypeV1Name, Serializer.serialize(eventV1)); |
| 175 | + put(eventTypeV2Name, Serializer.serialize(eventV2)); |
| 176 | + put(eventTypeV3Name, Serializer.serialize(eventV3)); |
| 177 | + }}; |
| 178 | + |
| 179 | + // When |
| 180 | + var deserializedEvents = events.entrySet().stream() |
| 181 | + .map(event -> serializer.deserialize(event.getKey(), event.getValue())) |
| 182 | + .filter(Optional::isPresent) |
| 183 | + .map(Optional::get) |
| 184 | + .filter(ShoppingCartInitializedWithStatus.class::isInstance) |
| 185 | + .map(ShoppingCartInitializedWithStatus.class::cast) |
| 186 | + .collect(toMap(ShoppingCartInitializedWithStatus::shoppingCartId, event -> event)); |
| 187 | + |
| 188 | + // Then |
| 189 | + assertEquals(3, deserializedEvents.size()); |
| 190 | + |
| 191 | + assertTrue(deserializedEvents.containsKey(eventV1.shoppingCartId())); |
| 192 | + var upcastedV1Event = deserializedEvents.get(eventV1.shoppingCartId()); |
| 193 | + |
| 194 | + assertEquals(eventV1.shoppingCartId(), upcastedV1Event.shoppingCartId()); |
| 195 | + assertEquals(eventV1.clientId(), upcastedV1Event.client().id()); |
| 196 | + assertEquals("Unknown", upcastedV1Event.client().name()); |
| 197 | + assertEquals(ShoppingCartStatus.Opened, upcastedV1Event.status()); |
| 198 | + |
| 199 | + assertTrue(deserializedEvents.containsKey(eventV2.shoppingCartId())); |
| 200 | + var upcastedV2Event = deserializedEvents.get(eventV2.shoppingCartId()); |
| 201 | + |
| 202 | + assertEquals(eventV2.shoppingCartId(), upcastedV2Event.shoppingCartId()); |
| 203 | + assertEquals(eventV2.client(), upcastedV2Event.client()); |
| 204 | + assertEquals(ShoppingCartStatus.Opened, upcastedV2Event.status()); |
| 205 | + |
| 206 | + assertTrue(deserializedEvents.containsKey(eventV3.shoppingCartId())); |
| 207 | + assertEquals(eventV3, deserializedEvents.get(eventV3.shoppingCartId())); |
| 208 | + } |
| 209 | +} |
0 commit comments