public void Read_action_property_and_create_the_corresponding_object_from_json() { // Json.Net requires you know the .NET object type when attempting to deserialize the json // This method inspects the XenMessage's action property, and creates an object of that type. var json = @"{ 'stringProperty': 'Michael Davis', 'intProperty': 31, 'time': '2015-05-03T00:00:00', 'action': 'SomeXenMessage', 'messageId': '123' }"; var finder = new XenMessageFinder(); // Act XenMessage msg; var created = XenMessage.TryParseFromAction(finder, json, out msg); Assert.IsTrue(created, "The json did not parse."); Assert.IsInstanceOf <SomeXenMessage>(msg, "The resultant XenMessage was not the right type."); var castedMsg = msg as SomeXenMessage; if (castedMsg == null) { Assert.Fail("Stopping test execution for failed conversion."); } Assert.IsTrue ( castedMsg.IntProperty == 31 && castedMsg.StringProperty == "Michael Davis", "Property inspection failed." ); }
/// <summary> /// Return the corresponding XenMessage. /// </summary> /// <param name="message"></param> /// <returns></returns> private XenMessage Parse(string message) { XenMessage request = null; if (!string.IsNullOrWhiteSpace(message)) { XenMessage.TryParseFromAction(_messageFinder, message, out request); } return(request); }
/// <summary> /// Send the <paramref name="message"/> to the appropriate <see cref="ToolboxAction"/> for execution. /// </summary> /// <param name="message"></param> /// <param name="next"></param> /// <returns> /// True if the message was executed; otherwise, false. /// </returns> public override bool Queue(string message, Action <XenMessage> next = null) { _callback = next; var parsed = false; var executed = false; XenMessage xMessage = null; if (string.IsNullOrWhiteSpace(message)) { parsed = false; } else { parsed = XenMessage.TryParseFromAction(MessageFinder, message, out xMessage); } if (parsed) { var matches = Registrar.FindAll(r => r.Message.Name.Equals(xMessage.Action)); if (matches.Count == 0) { ExecuteNextMessage(xMessage); } foreach (var match in matches) { var action = Activator.CreateInstance(match.Action) as ToolboxAction; if (action == null) { continue; } GetServices?.Invoke(action); action.Execute(xMessage); executed = true; ExecuteNextMessage(xMessage); } } if (!executed) { Log.Warn("A received message was not dispatched."); } return(executed); }