|
| 1 | +package com.vaadin.signals; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.fail; |
| 5 | + |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Set; |
| 10 | +import java.util.stream.Stream; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 15 | +import com.fasterxml.jackson.databind.JsonNode; |
| 16 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 17 | +import com.fasterxml.jackson.databind.node.TextNode; |
| 18 | +import com.vaadin.signals.ListSignal.ListPosition; |
| 19 | + |
| 20 | +public class SignalCommandTest { |
| 21 | + private final ObjectMapper mapper = new ObjectMapper(); |
| 22 | + private Set<Class<?>> assertedTypes = new HashSet<>(); |
| 23 | + |
| 24 | + @Test |
| 25 | + void json_serializeDeserializeCommands_commandsSerializable() { |
| 26 | + |
| 27 | + Id id = Id.random(); |
| 28 | + ListPosition pos = new ListPosition(id, id); |
| 29 | + JsonNode value = new TextNode("value"); |
| 30 | + String key = "key"; |
| 31 | + |
| 32 | + assertSerializable(new SignalCommand.AdoptAsCommand(id, id, id, key)); |
| 33 | + assertSerializable(new SignalCommand.AdoptAtCommand(id, id, id, pos)); |
| 34 | + assertSerializable(new SignalCommand.ClearCommand(id, id)); |
| 35 | + assertSerializable(new SignalCommand.ClearOwnerCommand(id, id)); |
| 36 | + assertSerializable(new SignalCommand.IncrementCommand(id, id, 0)); |
| 37 | + assertSerializable( |
| 38 | + new SignalCommand.InsertCommand(id, id, id, value, pos)); |
| 39 | + assertSerializable(new SignalCommand.KeyCondition(id, id, key, id)); |
| 40 | + assertSerializable(new SignalCommand.LastUpdateCondition(id, id, id)); |
| 41 | + assertSerializable( |
| 42 | + new SignalCommand.PositionCondition(id, id, id, pos)); |
| 43 | + assertSerializable(new SignalCommand.PutCommand(id, id, key, value)); |
| 44 | + assertSerializable( |
| 45 | + new SignalCommand.PutIfAbsentCommand(id, id, id, key, value)); |
| 46 | + assertSerializable(new SignalCommand.RemoveByKeyCommand(id, id, key)); |
| 47 | + assertSerializable(new SignalCommand.RemoveCommand(id, id, id)); |
| 48 | + assertSerializable(new SignalCommand.SetCommand(id, id, value)); |
| 49 | + assertSerializable(new SignalCommand.SnapshotCommand(id, |
| 50 | + Map.of(Id.random(), |
| 51 | + new Node.Data(id, id, id, value, List.of(), Map.of()), |
| 52 | + Id.random(), new Node.Alias(id)))); |
| 53 | + assertSerializable(new SignalCommand.TransactionCommand(id, List.of())); |
| 54 | + |
| 55 | + Stream.of(SignalCommand.class.getPermittedSubclasses()) |
| 56 | + .filter(type -> !type.isInterface()) |
| 57 | + .filter(type -> !assertedTypes.contains(type)) |
| 58 | + .forEach(type -> fail("Should test serialization for " + type)); |
| 59 | + } |
| 60 | + |
| 61 | + private void assertSerializable(SignalCommand command) { |
| 62 | + try { |
| 63 | + String json = mapper.writeValueAsString(command); |
| 64 | + SignalCommand deserialized = mapper.readValue(json, |
| 65 | + SignalCommand.class); |
| 66 | + assertEquals(command, deserialized); |
| 67 | + |
| 68 | + assertedTypes.add(command.getClass()); |
| 69 | + } catch (JsonProcessingException e) { |
| 70 | + fail(e); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments