public async Task Process_CompleteSuccessfully_WhenOutgoingLinkQueueFound() { const string linkName = "abcd"; const string entity = "entity"; ISecurityContext securityContext = Substitute.For <ISecurityContext>(); IEntityLookup entityLookup = Substitute.For <IEntityLookup>(); ILoggerProvider loggerProvider = Substitute.For <ILoggerProvider>(); Entities.IEntity fakeEntity = Substitute.For <Entities.IEntity>(); var deliveryQueue = new DeliveryQueue(); var linkProcessor = new LinkProcessor(securityContext, entityLookup, loggerProvider); entityLookup.Find(Arg.Any <string>()).Returns(fakeEntity); securityContext.IsAuthorized(Arg.Any <Connection>()).Returns(true); fakeEntity.DeliveryQueue.Returns(deliveryQueue); Session session = await TestAmqpHost.OpenAndLinkProcessorAsync(linkProcessor); try { var receiver = new ReceiverLink(session, linkName, entity); deliveryQueue.Enqueue(new Delivery(new Message { Properties = new Properties { MessageId = "msgid6746" } })); Message message = await receiver.ReceiveAsync(); message.Properties.MessageId.ShouldBe("msgid6746"); } finally { await session.Connection.CloseAsync(); } }
public async Task Process_CompleteWithError_WhenOutgoingLinkQueueNotFound() { const string linkName = "abcd"; const string entity = "entity"; ISecurityContext securityContext = Substitute.For <ISecurityContext>(); IEntityLookup entityLookup = Substitute.For <IEntityLookup>(); ILoggerProvider loggerProvider = Substitute.For <ILoggerProvider>(); Entities.IEntity fakeEntity = Substitute.For <Entities.IEntity>(); var linkProcessor = new LinkProcessor(securityContext, entityLookup, loggerProvider); fakeEntity.DeliveryQueue.Returns((DeliveryQueue)null); entityLookup.Find(Arg.Any <string>()).Returns(fakeEntity); securityContext.IsAuthorized(Arg.Any <Connection>()).Returns(true); AmqpException exception = null; Session session = await TestAmqpHost.OpenAndLinkProcessorAsync(linkProcessor); try { var receiver = new ReceiverLink(session, linkName, entity); Func <Task> action = async() => await receiver.ReceiveAsync(); linkProcessor.ShouldSatisfyAllConditions( () => exception = action.ShouldThrow <AmqpException>(), () => exception.Error.Condition.ShouldBe((Symbol)ErrorCode.NotFound), () => exception.Error.Description.ShouldBe("Queue not found.") ); } finally { await session.Connection.CloseAsync(); } }
public void Process(AttachContext attachContext) { if (string.IsNullOrEmpty(attachContext.Attach.LinkName)) { attachContext.Complete(new Error(ErrorCode.InvalidField) { Description = "Empty link name not allowed." }); _logger.LogError($"Could not attach empty link to {GetType().Name}."); return; } if (!_securityContext.IsAuthorized(attachContext.Link.Session.Connection)) { attachContext.Complete(new Error(ErrorCode.UnauthorizedAccess) { Description = "Not authorized." }); _logger.LogError($"Could not attach unathorized link to {GetType().Name}."); return; } if (attachContext.Link.Role) { AttachIncomingLink(attachContext, (Target)attachContext.Attach.Target); } else { AttachOutgoingLink(attachContext, (Source)attachContext.Attach.Source); } }
public async Task Process_CompleteWithError_WhenIncomingLinkEntityNotFound() { const string linkName = "abcd"; const string entity = "entity"; ISecurityContext securityContext = Substitute.For <ISecurityContext>(); IEntityLookup entityLookup = Substitute.For <IEntityLookup>(); ILoggerProvider loggerProvider = Substitute.For <ILoggerProvider>(); var linkProcessor = new LinkProcessor(securityContext, entityLookup, loggerProvider); entityLookup.Find(Arg.Any <string>()).Returns((Entities.IEntity)null); securityContext.IsAuthorized(Arg.Any <Connection>()).Returns(true); AmqpException exception = null; Session session = await TestAmqpHost.OpenAndLinkProcessorAsync(linkProcessor); try { var sender = new SenderLink(session, linkName, entity); var message = new Message { Properties = new Properties { MessageId = "message1" }, BodySection = new Data { Binary = Encoding.UTF8.GetBytes("hello!") } }; Func <Task> action = async() => await sender.SendAsync(message); linkProcessor.ShouldSatisfyAllConditions( () => exception = action.ShouldThrow <AmqpException>(), () => exception.Error.Condition.ShouldBe((Symbol)ErrorCode.NotFound), () => exception.Error.Description.ShouldBe("Entity not found.") ); } finally { await session.Connection.CloseAsync(); } }
public async Task Process_CompleteSuccessfully_WhenIncomingLinkEntityFound() { const string linkName = "abcd"; const string entity = "myEntity"; ISecurityContext securityContext = Substitute.For <ISecurityContext>(); IEntityLookup entityLookup = Substitute.For <IEntityLookup>(); ILoggerProvider loggerProvider = Substitute.For <ILoggerProvider>(); Entities.IEntity fakeEntity = Substitute.For <Entities.IEntity>(); var linkProcessor = new LinkProcessor(securityContext, entityLookup, loggerProvider); entityLookup.Find(entity).Returns(fakeEntity); securityContext.IsAuthorized(Arg.Any <Connection>()).Returns(true); Session session = await TestAmqpHost.OpenAndLinkProcessorAsync(linkProcessor); try { var sender = new SenderLink(session, linkName, entity); var message = new Message { Properties = new Properties { MessageId = "message173" }, BodySection = new Data { Binary = Encoding.UTF8.GetBytes("hello!") } }; await sender.SendAsync(message); fakeEntity .Received(1) .Post(Arg.Is <Message>(m => m.Properties.MessageId == "message173")); } finally { await session.Connection.CloseAsync(); } }