public void SerializesMessageCorrectly_Presence(params PresenceMessage[] messages) { // Arrange JsonMessageSerializer serializer = new JsonMessageSerializer(); ProtocolMessage message = new ProtocolMessage() { Presence = messages }; StringBuilder expectedMessage = new StringBuilder("{\"action\":0,\"msgSerial\":0"); expectedMessage.Append(",\"presence\":["); foreach (PresenceMessage msg in messages) { expectedMessage.AppendFormat("{{\"action\":{0}}},", (byte)msg.Action); } expectedMessage.Remove(expectedMessage.Length - 1, 1) // last comma .Append("]}"); // Act object result = serializer.SerializeProtocolMessage(message); // Assert Assert.IsType <string>(result); Assert.Equal <string>(expectedMessage.ToString(), result as string); }
public void SerializesMessageCorrectly_Messages(params Message[] messages) { // Arrange JsonMessageSerializer serializer = new JsonMessageSerializer(); ProtocolMessage message = new ProtocolMessage() { Messages = messages }; StringBuilder expectedMessage = new StringBuilder("{\"action\":0,\"msgSerial\":0"); var validMessages = messages.Where(c => !string.IsNullOrEmpty(c.Name)); if (validMessages.Any()) { expectedMessage.Append(",\"messages\":["); foreach (Message msg in validMessages) { expectedMessage.AppendFormat("{{\"name\":\"{0}\"}},", msg.Name); } expectedMessage.Remove(expectedMessage.Length - 1, 1) // last comma .Append("]"); } expectedMessage.Append("}"); // Act object result = serializer.SerializeProtocolMessage(message); // Assert Assert.IsType <string>(result); Assert.Equal <string>(expectedMessage.ToString(), result as string); }
public void SerializesMessageCorrectly_Action(ProtocolMessage.MessageAction messageAction) { // Arrange JsonMessageSerializer serializer = new JsonMessageSerializer(); ProtocolMessage message = new ProtocolMessage(messageAction); string expectedMessage = string.Format("{{\"action\":{0},\"msgSerial\":0}}", (int)messageAction); // Act object result = serializer.SerializeProtocolMessage(message); // Assert Assert.IsType <string>(result); Assert.Equal <string>(expectedMessage, result as string); }
public void SerializesMessageCorrectly_NoMessages_DoesNotThrowException() { // Arrange JsonMessageSerializer serializer = new JsonMessageSerializer(); ProtocolMessage message = new ProtocolMessage() { Messages = null }; // Act object result = serializer.SerializeProtocolMessage(message); // Assert Assert.IsType <string>(result); Assert.Equal <string>("{\"action\":0,\"msgSerial\":0}", result as string); }
public void SerializesMessageCorrectly_MsgSerial(long msgSerial) { // Arrange JsonMessageSerializer serializer = new JsonMessageSerializer(); ProtocolMessage message = new ProtocolMessage() { MsgSerial = msgSerial }; StringBuilder expectedMessage = new StringBuilder(); expectedMessage.Append("{\"action\":0") .AppendFormat(",\"msgSerial\":{0}", msgSerial) .Append("}"); // Act object result = serializer.SerializeProtocolMessage(message); // Assert Assert.IsType <string>(result); Assert.Equal <string>(expectedMessage.ToString(), result as string); }
public void SerializesMessageCorrectly_Channel(string channel) { // Arrange JsonMessageSerializer serializer = new JsonMessageSerializer(); ProtocolMessage message = new ProtocolMessage() { Channel = channel }; StringBuilder expectedMessage = new StringBuilder(); expectedMessage.Append("{\"action\":0"); if (!string.IsNullOrEmpty(channel)) { expectedMessage.Append(",\"channel\":").AppendFormat("\"{0}\"", channel); } expectedMessage.Append(",\"msgSerial\":0}"); // Act object result = serializer.SerializeProtocolMessage(message); // Assert Assert.IsType <string>(result); Assert.Equal <string>(expectedMessage.ToString(), result as string); }