package dslab.rmi.dmap; import dslab.protocol.Message; import dslab.protocol.ProtocolException; import dslab.protocol.dmap.MessageMetadata; import dslab.rmi.serialize.dmap.DmapClientSerializer; import dslab.rmi.serialize.dmap.DmapServerSerializer.DmapCommand; import org.junit.Test; import java.util.List; import static dslab.TestObjects.*; import static dslab.rmi.serialize.dmap.DmapServerSerializer.DmapCommand.list; import static dslab.rmi.serialize.dmap.DmapServerSerializer.DmapCommand.show; import static org.junit.Assert.assertEquals; public class DmapClientSerializerTest { private static class TestableDmapClientSerializer extends DmapClientSerializer { void setCurrentCommand(DmapCommand command) { currentCommand.set(command); } } private final TestableDmapClientSerializer serializer = new TestableDmapClientSerializer(); @Test public void shouldDeserializeShowResponse() throws ProtocolException { serializer.setCurrentCommand(show); //so the serializer knows how to parse the response var message = (Message) serializer.deserialize( "from arthur@earth.planet\n" + "to zaphod@univer.ze , trillian@earth.planet\n" + "subject my answer\n" + "data i have thought about it and the answer is clearly 42" ).get(); assertEquals(message.getSender(), arthurAtEarthPlanet); assertEquals(message.getRecipients(), List.of(zaphodAtUniverZe, trillianAtEarthPlanet)); assertEquals(message.getSubject(), "my answer"); assertEquals(message.getData(), "i have thought about it and the answer is clearly 42"); } @Test @SuppressWarnings("unchecked") public void shouldDeserializeListResponse() throws ProtocolException { serializer.setCurrentCommand(list); var metadata = (List<MessageMetadata>) serializer.deserialize( "1 arthur@earth.planet my answer\n" + "2 zaphod@univer.ze pan galactic gargle blaster recipe\n" + "ok" ).get(); assertEquals(metadata, List.of( new MessageMetadata(1, arthurAtEarthPlanet, "my answer"), new MessageMetadata(2, zaphodAtUniverZe, "pan galactic gargle blaster recipe"))); } }