protected virtual BaseMessage CreateNmsMessage(Message message) { BaseMessage result = null; if ("amqp/map" == message.ContentType) { Dictionary <string, object> dict = new Dictionary <string, object>(); message.GetContent(dict); PrimitiveMap bodyMap = new PrimitiveMap(); SetNmsPrimitiveMap(bodyMap, dict); MapMessage mapMessage = new MapMessage(); mapMessage.Body = bodyMap; result = mapMessage; } else if ("amqp/list" == message.ContentType) { Collection <object> coll = new Collection <object>(); message.GetContent(coll); ObjectMessage objMessage = new ObjectMessage(); objMessage.Body = ConvertAmqpListToObject(coll); result = objMessage; } else { TextMessage textMessage = new TextMessage(); textMessage.Text = message.GetContent(); result = textMessage; } return(result); }
public void ShouldProcessMessagesAccordingToFilter() { const string typeName = "SomeTypeName"; var consumer = A.Fake <IMessageConsumer>(); A.CallTo(() => _session.CreateConsumer(_destination, null, false)).Returns(consumer); IBytesMessage bytesMessage = A.Fake <IBytesMessage>(); ITestMessage message = A.Fake <ITestMessage>(); Func <IDictionary, bool> filter = properties => (int?)properties["Id"] == 123; const string serializedFixtureString = "<xml>Some fixture XML</xml>"; var bytes = Encoding.UTF8.GetBytes(serializedFixtureString); var messageProperties = new PrimitiveMap(); messageProperties.SetString(MessagePropertyNames.TypeName, typeName); messageProperties.SetInt("Id", 123); A.CallTo(() => bytesMessage.Content).Returns(bytes); A.CallTo(() => bytesMessage.Properties).Returns(messageProperties); A.CallTo(() => _deserializer.Deserialize(A <Stream> ._)).Returns(message); A.CallTo(() => _deserializer.GetTypeName()).Returns(typeName); _source = new MessageSource <ITestMessage>(_lazyConnection, new[] { _deserializer }, _destination, _acknowledgementMode, null, filter, false); _source.Messages.Subscribe(_observer); consumer.Listener += Raise.FreeForm.With((Apache.NMS.IMessage)bytesMessage); A.CallTo(() => _deserializer.Deserialize(A <Stream> ._)).MustHaveHappened(); A.CallTo(() => _observer.OnNext(message)).MustHaveHappened(); }
public override void Visit(PrimitiveMap <TrackDto, Track> map) { map.Dto.Name = map.Entity.Name; map.Dto.Composer = map.Entity.Composer; map.Dto.Milliseconds = map.Entity.Milliseconds; map.Dto.Bytes = map.Entity.Bytes; map.Dto.UnitPrice = map.Entity.UnitPrice; }
public void TestMarshalled() { PrimitiveMap map = CreatePrimitiveMap(); byte[] data = map.Marshal(); map = PrimitiveMap.Unmarshal(data); AssertPrimitiveMap(map); }
protected void AssertPrimitiveMap(PrimitiveMap map) { // use generic API to access entries Assert.AreEqual(a, map["a"], "generic map entry: a"); Assert.AreEqual(b, map["b"], "generic map entry: b"); Assert.AreEqual(c, map["c"], "generic map entry: c"); Assert.AreEqual(d, map["d"], "generic map entry: d"); Assert.AreEqual(e, map["e"], "generic map entry: e"); Assert.AreEqual(f, map["f"], "generic map entry: f"); Assert.AreEqual(g, map["g"], "generic map entry: g"); Assert.AreEqual(h, map["h"], "generic map entry: h"); Assert.AreEqual(i, map["i"], "generic map entry: i"); Assert.AreEqual(j, map["j"], "generic map entry: j"); Assert.AreEqual(k, map["k"], "generic map entry: k"); Assert.AreEqual(l, map["l"], "generic map entry: l"); //Assert.AreEqual(m, map["m"], "generic map entry: m"); //Assert.AreEqual(n, map["n"], "generic map entry: n"); // use type safe APIs Assert.AreEqual(a, map.GetBool("a"), "map entry: a"); Assert.AreEqual(b, map.GetByte("b"), "map entry: b"); Assert.AreEqual(c, map.GetChar("c"), "map entry: c"); Assert.AreEqual(d, map.GetShort("d"), "map entry: d"); Assert.AreEqual(e, map.GetInt("e"), "map entry: e"); Assert.AreEqual(f, map.GetLong("f"), "map entry: f"); Assert.AreEqual(g, map.GetString("g"), "map entry: g"); Assert.AreEqual(h, map.GetBool("h"), "map entry: h"); Assert.AreEqual(i, map.GetByte("i"), "map entry: i"); Assert.AreEqual(j, map.GetShort("j"), "map entry: j"); Assert.AreEqual(k, map.GetInt("k"), "map entry: k"); Assert.AreEqual(l, map.GetLong("l"), "map entry: l"); //Assert.AreEqual(m, map.GetList("m"), "map entry: m"); //Assert.AreEqual(n, map.GetDictionary("n"), "map entry: n"); IList list = map.GetList("m"); Assert.AreEqual(2, list.Count, "list size"); Assert.IsTrue(list.Contains("Item1")); Assert.IsTrue(list.Contains("Item2")); IDictionary dictionary = map.GetDictionary("n"); Assert.AreEqual(5, dictionary.Count, "dictionary size"); IDictionary childMap = (IDictionary)dictionary["childMap"]; Console.WriteLine("Found childMap: " + childMap); Assert.IsNotNull(childMap); Assert.AreEqual("childMap", childMap["name"], "childMap[name]"); IList childList = (IList)dictionary["childList"]; Console.WriteLine("Found childList: " + childList); Assert.IsNotNull(childList); Assert.IsTrue(childList.Contains("childListElement1")); }
public void TestMarshalled() { PrimitiveMap map = CreatePrimitiveMap(); Console.WriteLine("data: " + map); byte[] data = map.Marshal(); map = PrimitiveMap.Unmarshal(data); Console.WriteLine("data: " + map); AssertPrimitiveMap(map); }
public void TestMarshalledWithBigString() { PrimitiveMap map = CreatePrimitiveMap(); String test = new String('a', 65538); map.SetString("BIG_STRING", test); byte[] data = map.Marshal(); map = PrimitiveMap.Unmarshal(data); AssertPrimitiveMap(map); Assert.AreEqual(test, map.GetString("BIG_STRING")); }
public void Decode_WhenControlMessage_ThenTrue() { var primitiveMap = new PrimitiveMap(); primitiveMap[Headers.ControlMessageHeader] = null; var message = Mock.Of <IBytesMessage>(x => x.Properties == primitiveMap); var result = testee.Decode(new TransportMessage(), message); Assert.IsTrue(result); }
public T _Get <T> (Type type, string fieldName) { PrimitiveMap map = Array.Find <PrimitiveMap>(injectMap.primitive, (x) => x.fieldPath.Equals(string.Format("{0}.{1}", type.ToString(), fieldName))); if (map != null) { Log(string.Format("<Get>Found path {0}, for field {2} of type {1} to value {3}", map.fieldPath, type.ToString(), fieldName, map.fieldValue)); return((T)Convert.ChangeType(map.fieldValue, typeof(T))); } else { throw new PrimitiveMapNotDeclareException(); } }
protected virtual Message CreateAmqpMessage(IMessage message) { if (message is TextMessage) { TextMessage textMessage = message as TextMessage; Message result = new Message(textMessage.Text); return(result); } else if (message is BytesMessage) { BytesMessage bytesMessage = message as BytesMessage; Message result = new Message(bytesMessage.Content, 0, bytesMessage.Content.Length); return(result); } else if (message is ObjectMessage) { ObjectMessage objectMessage = message as ObjectMessage; Collection <object> objs = new Collection <object>(); objs = ConvertObjectToAmqpList(objectMessage.Body); Message result = new Message(objs); return(result); } else if (message is MapMessage) { MapMessage mapMessage = message as MapMessage; PrimitiveMap mapBody = mapMessage.Body as PrimitiveMap; Dictionary <string, object> dict = FromNmsPrimitiveMap(mapBody); Message result = new Message(dict); return(result); } else if (message is StreamMessage) { StreamMessage streamMessage = message as StreamMessage; Message result = new Message(streamMessage.Content, 0, streamMessage.Content.Length); return(result); } else if (message is BaseMessage) { Message result = new Message(); return(result); } else { throw new Exception("unhandled message type"); } }
public void Decode_WhenControlMessageWithContent_ThenAssignBody() { var transportMessage = new TransportMessage { Body = null }; var primitiveMap = new PrimitiveMap(); primitiveMap[Headers.ControlMessageHeader] = null; var bytesMessage = Mock.Of <IBytesMessage>(m => m.Properties == primitiveMap && m.Content == new byte[] { 1 }); testee.Decode(transportMessage, bytesMessage); Assert.NotNull(transportMessage.Body); }
public BaseMessage(BaseMessage copy) { this.propertiesMap = copy.propertiesMap; this.destination = copy.destination; this.correlationId = copy.correlationId; this.timeToLive = copy.timeToLive; this.messageId = copy.messageId; this.deliveryMode = copy.deliveryMode; this.priority = copy.priority; this.replyTo = copy.replyTo; this.content = copy.content; this.type = copy.type; this.Acknowledger = copy.Acknowledger; this.timestamp = copy.timestamp; this.readOnlyMsgBody = copy.readOnlyMsgBody; }
public void MapConversion() { IMapMessage message = A.Fake <IMapMessage>(); IPrimitiveMap primitiveMap = new PrimitiveMap(); IDictionary content = new Hashtable(); content["key1"] = "value1"; content["key2"] = "value2"; A.CallTo(() => session.CreateMapMessage()).Returns(message).Once(); A.CallTo(() => message.Body).Returns(primitiveMap); //can't seem to mock indexer... IMessage msg = converter.ToMessage(content, session); Assert.AreEqual(content, converter.FromMessage(msg)); }
public void MapConversion() { IMapMessage message = mocks.StrictMock <IMapMessage>(); IPrimitiveMap primitiveMap = new PrimitiveMap(); IDictionary content = new Hashtable(); content["key1"] = "value1"; content["key2"] = "value2"; Expect.Call(session.CreateMapMessage()).Return(message).Repeat.Once(); Expect.Call(message.Body).Return(primitiveMap).Repeat.Any(); //can't seem to mock indexer... mocks.ReplayAll(); IMessage msg = converter.ToMessage(content, session); Assert.AreEqual(content, converter.FromMessage(msg)); mocks.VerifyAll(); }
protected PrimitiveMap CreatePrimitiveMap() { PrimitiveMap map = new PrimitiveMap(); map["a"] = a; map["b"] = b; map["c"] = c; map["d"] = d; map["e"] = e; map["f"] = f; map["g"] = g; map["h"] = h; map["i"] = i; map["j"] = j; map["k"] = k; map["l"] = l; map["m"] = m; map["n"] = n; return(map); }
public void TestMarshalPrimitiveMap() { XmlPrimitiveMapMarshaler marshaler = new XmlPrimitiveMapMarshaler(); PrimitiveMap map = new PrimitiveMap(); map.SetBool("boolean", true); map.SetByte("byte", (byte)1); map["bytes1"] = new byte[1]; map.SetChar("char", 'a'); map.SetDouble("double", 1.5); map.SetFloat("float", 1.5f); map.SetInt("int", 1); map.SetLong("long", 1); map["object"] = "stringObj"; map.SetShort("short", (short)1); map.SetString("string", "string"); byte[] result = marshaler.Marshal(map); Assert.IsNotNull(result); result = marshaler.Marshal(null); Assert.IsNull(result); }
public override void ClearBody() { this.body = null; this.typeConverter = null; base.ClearBody(); }
private static Type GetType(string name) { return(PrimitiveMap.ContainsKey(name) ? PrimitiveMap[name] : Type.GetType(name)); }
public void TestNotMarshalled() { PrimitiveMap map = CreatePrimitiveMap(); AssertPrimitiveMap(map); }
/// <summary> /// Performs mapping for primitive properties. /// </summary> /// <param name="map">The primitive map.</param> public abstract void Visit(PrimitiveMap <TDto, TEntity> map);
public IPrimitiveMap Unmarshal(byte[] mapContent) { string xmlString = this.encoder.GetString(mapContent, 0, mapContent.Length); PrimitiveMap result = new PrimitiveMap(); if (xmlString == null || xmlString == "") { return(result); } XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true; settings.IgnoreWhitespace = true; settings.IgnoreProcessingInstructions = true; XmlReader reader = XmlReader.Create(new StringReader(xmlString), settings); reader.MoveToContent(); reader.ReadStartElement("map"); while (reader.Name == "entry") { reader.ReadStartElement(); string key = reader.ReadElementContentAsString("string", ""); Object value = null; switch (reader.Name) { case "char": value = Convert.ToChar(reader.ReadElementContentAsString()); reader.ReadEndElement(); break; case "double": value = Convert.ToDouble(reader.ReadElementContentAsString()); reader.ReadEndElement(); break; case "float": value = Convert.ToSingle(reader.ReadElementContentAsString()); reader.ReadEndElement(); break; case "long": value = Convert.ToInt64(reader.ReadElementContentAsString()); reader.ReadEndElement(); break; case "int": value = Convert.ToInt32(reader.ReadElementContentAsString()); reader.ReadEndElement(); break; case "short": value = Convert.ToInt16(reader.ReadElementContentAsString()); reader.ReadEndElement(); break; case "byte": value = (byte)Convert.ToInt16(reader.ReadElementContentAsString()); reader.ReadEndElement(); break; case "boolean": value = Convert.ToBoolean(reader.ReadElementContentAsString()); reader.ReadEndElement(); break; case "byte-array": value = Convert.FromBase64String(reader.ReadElementContentAsString()); reader.ReadEndElement(); break; default: value = reader.ReadElementContentAsString(); reader.ReadEndElement(); break; } ; // Now store the value into our new PrimitiveMap. result[key] = value; } reader.Close(); return(result); }
public override void Visit(PrimitiveMap <MediaTypeDto, MediaType> map) { map.Dto.Name = map.Entity.Name; }
public void _Read(string settings) { if (string.IsNullOrEmpty(settings)) { return; } InjectMap outsideSettings = parser.Parse <InjectMap>(settings); foreach (BindMap bindMapCurr in injectMap.bind) { // If there hasn't setting, leave loop. if (outsideSettings.bind == null) { break; } BindMap bindMapNew = null; bindMapNew = Array.Find( outsideSettings.bind, (BindMap bm) => bm.bind.Equals(bindMapCurr.bind) ); if (bindMapNew != null) { bindMapCurr.to = String.Copy(bindMapNew.to); } } foreach (PrimitiveMap primitiveMapCurr in injectMap.primitive) { // If there hasn't setting, leave loop. if (outsideSettings.primitive == null) { break; } PrimitiveMap primitiveMapNew = null; primitiveMapNew = Array.Find( outsideSettings.primitive, (PrimitiveMap pm) => pm.fieldPath.Equals(primitiveMapCurr.fieldPath) ); if (primitiveMapNew != null) { primitiveMapCurr.fieldValue = String.Copy(primitiveMapNew.fieldValue); } } foreach (PrimitiveArrayMap primitiveArrayMapCurr in injectMap.primitiveArray) { // If there hasn't setting, leave loop. if (outsideSettings.primitiveArray == null) { break; } PrimitiveArrayMap PrimitiveArrayMapNew = null; PrimitiveArrayMapNew = Array.Find( outsideSettings.primitiveArray, (PrimitiveArrayMap pam) => pam.fieldPath.Equals(primitiveArrayMapCurr.fieldPath) ); if (PrimitiveArrayMapNew != null) { Array.Copy(PrimitiveArrayMapNew.fieldValue, primitiveArrayMapCurr.fieldValue, PrimitiveArrayMapNew.fieldValue.Length); } } //Due to new settings changed, reinject level of logger Inject(logger); }
public override void Visit(PrimitiveMap <GenreDto, Genre> map) { map.Dto.Name = map.Entity.Name; }
public MapMessage(PrimitiveMap body) { this.body = body; this.typeConverter = new PrimitiveMapInterceptor(this, this.body); }
/// <summary> /// Create nms message object /// </summary> /// <param name="message"> /// zmq message object /// </param> /// <returns> /// nms message object /// </returns> protected virtual IMessage ToNmsMessage(byte[] msgData) { BaseMessage nmsMessage = null; int messageType = WireFormat.MT_UNKNOWN; int fieldType = WireFormat.MFT_NONE; DateTime messageTimestamp = DateTime.UtcNow; string messageNMSType = null; string messageCorrelationId = null; IDestination messageReplyTo = null; MsgDeliveryMode messageDeliveryMode = MsgDeliveryMode.NonPersistent; MsgPriority messagePriority = MsgPriority.Normal; TimeSpan messageTimeToLive = TimeSpan.FromTicks(0); byte[] messageProperties = null; int fieldLen; int index = 0; string messageID = string.Empty; byte[] messageBody = null; try { // Parse the commond message fields do { fieldType = ReadInt(msgData, ref index); switch (fieldType) { case WireFormat.MFT_NONE: break; case WireFormat.MFT_MESSAGEID: messageID = ReadString(msgData, ref index); break; case WireFormat.MFT_TIMESTAMP: fieldLen = ReadInt(msgData, ref index); Debug.Assert(sizeof(long) == fieldLen); messageTimestamp = DateTime.FromBinary(ReadLong(msgData, ref index)); break; case WireFormat.MFT_NMSTYPE: messageNMSType = ReadString(msgData, ref index); break; case WireFormat.MFT_CORRELATIONID: messageCorrelationId = ReadString(msgData, ref index); break; case WireFormat.MFT_REPLYTO: string replyToDestName = ReadString(msgData, ref index); messageReplyTo = this.session.GetDestination(replyToDestName); break; case WireFormat.MFT_DELIVERYMODE: fieldLen = ReadInt(msgData, ref index); Debug.Assert(sizeof(int) == fieldLen); messageDeliveryMode = (MsgDeliveryMode)ReadInt(msgData, ref index); break; case WireFormat.MFT_PRIORITY: fieldLen = ReadInt(msgData, ref index); Debug.Assert(sizeof(int) == fieldLen); messagePriority = (MsgPriority)ReadInt(msgData, ref index); break; case WireFormat.MFT_TIMETOLIVE: fieldLen = ReadInt(msgData, ref index); Debug.Assert(sizeof(long) == fieldLen); messageTimeToLive = TimeSpan.FromTicks(ReadLong(msgData, ref index)); break; case WireFormat.MFT_HEADERS: fieldLen = ReadInt(msgData, ref index); messageProperties = new byte[fieldLen]; for (int propIndex = 0; propIndex < fieldLen; propIndex++, index++) { messageProperties[propIndex] = msgData[index]; } break; case WireFormat.MFT_MSGTYPE: fieldLen = ReadInt(msgData, ref index); Debug.Assert(sizeof(int) == fieldLen); messageType = ReadInt(msgData, ref index); break; case WireFormat.MFT_BODY: messageBody = ReadBytes(msgData, ref index); break; default: // Skip past this field. Tracer.WarnFormat("Unknown message field type: {0}", fieldType); fieldLen = ReadInt(msgData, ref index); index += fieldLen; break; } } while(WireFormat.MFT_NONE != fieldType && index < msgData.Length); } catch (Exception ex) { Tracer.ErrorFormat("Exception parsing message: {0}", ex.Message); } // Instantiate the message type switch (messageType) { case WireFormat.MT_MESSAGE: nmsMessage = new BaseMessage(); break; case WireFormat.MT_TEXTMESSAGE: nmsMessage = new TextMessage(); if (null != messageBody) { ((TextMessage)nmsMessage).Text = Encoding.UTF8.GetString(messageBody); } break; case WireFormat.MT_BYTESMESSAGE: nmsMessage = new BytesMessage(); if (null != messageBody) { ((BytesMessage)nmsMessage).Content = messageBody; } break; case WireFormat.MT_UNKNOWN: default: break; } // Set the common headers. if (null != nmsMessage) { try { nmsMessage.NMSMessageId = messageID; nmsMessage.NMSCorrelationID = messageCorrelationId; nmsMessage.NMSDestination = this.destination; nmsMessage.NMSReplyTo = messageReplyTo; nmsMessage.NMSDeliveryMode = messageDeliveryMode; nmsMessage.NMSPriority = messagePriority; nmsMessage.NMSTimestamp = messageTimestamp; nmsMessage.NMSTimeToLive = messageTimeToLive; nmsMessage.NMSType = messageNMSType; if (null != messageProperties) { nmsMessage.PropertiesMap = PrimitiveMap.Unmarshal(messageProperties); } } catch (InvalidOperationException) { // Log error } if (null != this.ConsumerTransformer) { IMessage transformedMessage = ConsumerTransformer(this.session, this, nmsMessage); if (null != transformedMessage) { nmsMessage = transformedMessage as BaseMessage; } } nmsMessage.ReadOnlyBody = true; nmsMessage.ReadOnlyProperties = true; } return(nmsMessage); }