public void EventNotificationWithArgsTest()
        {
            // Arrange
            string objectId = "objId1";

            _clientMock.Setup(o => o.Request(It.IsAny <IRequest>())).Returns <IRequest>((req) =>
            {
                var resp = new GetBindingDescriptionsResponse();
                resp.bindings.Add(objectId, ObjectDescriptor.GetObjectDescription <IRegisteredObject>());
                return(resp);
            });
            var sender = new Sender(_clientMock.Object);

            sender.Register <IRegisteredObject>();
            sender.SynchronizeBindings();
            _clientMock.Setup(o => o.Request(It.IsAny <SubscribeRequest>())).Returns <SubscribeRequest>(req =>
            {
                return(new SubscribeResponse()
                {
                    objectId = objectId,
                    eventId = ObjectDescriptor.GetEventId <IRegisteredObject>(nameof(IRegisteredObject.NotifyOnDataChanged))
                });
            });
            var  objProxy     = sender.GetBindingsByType <IRegisteredObject>()[objectId];
            bool argsReceived = false;

            objProxy.NotifyOnDataChanged += (s, e) => { argsReceived = e as DataChangedEventArgs != null; };
            var notification = new EventNotification()
            {
                objectId  = objectId,
                eventId   = ObjectDescriptor.GetEventId <IRegisteredObject>(nameof(IRegisteredObject.NotifyOnDataChanged)),
                eventArgs = new DataChangedEventArgs("dataChanged", 2)
            };

            // Act
            _clientMock.Raise(o => o.NotificationReceived += null, null, new Messages.NotificationEventArgs(notification));

            // Assert
            _clientMock.Verify();
            Assert.IsTrue(argsReceived);
        }
Esempio n. 2
0
        public IResponse Request(IRequest request, IRecipientCallback callback)
        {
            try
            {
                switch (request)
                {
                case GetBindingDescriptionsRequest getBindingsReq:
                    var getBindingsResp = new GetBindingDescriptionsResponse();
                    foreach (var obj in _registeredObjects)
                    {
                        getBindingsResp.bindings.Add(obj.Key, obj.Value.Description);
                    }
                    return(getBindingsResp);

                case SubscribeRequest subscribeReq:
                    var subscribeObj = _registeredObjects[subscribeReq.objectId];
                    subscribeObj.Subscribe(subscribeReq.eventId, callback);
                    var subscribeResponse = new SubscribeResponse()
                    {
                        objectId = subscribeReq.objectId,
                        eventId  = subscribeReq.eventId
                    };
                    return(subscribeResponse);

                case UnsubscribeRequest unsubscribeReq:
                    var unsubscribeObj = _registeredObjects[unsubscribeReq.objectId];
                    unsubscribeObj.Unsubscribe(unsubscribeReq.eventId, callback);
                    var unsubscribeResponse = new UnsubscribeResponse()
                    {
                        objectId = unsubscribeReq.objectId,
                        eventId  = unsubscribeReq.eventId
                    };
                    return(unsubscribeResponse);

                case PropertyGetRequest propertyGetReq:
                    var    propertyGetObj      = _registeredObjects[propertyGetReq.objectId];
                    object propertyGetValue    = propertyGetObj.GetValue(propertyGetReq.propertyId);
                    var    propertyGetResponse = new PropertyGetResponse()
                    {
                        objectId   = propertyGetReq.objectId,
                        propertyId = propertyGetReq.propertyId,
                        value      = propertyGetValue
                    };
                    return(propertyGetResponse);

                case PropertySetRequest propertySetReq:
                    var propertySetObj = _registeredObjects[propertySetReq.objectId];
                    propertySetObj.SetValue(propertySetReq.propertyId, propertySetReq.value);
                    var propertySetResponse = new PropertySetResponse()
                    {
                        objectId   = propertySetReq.objectId,
                        propertyId = propertySetReq.propertyId
                    };
                    return(propertySetResponse);

                case InvokeRequest invokeReq:
                    var    invokeObj      = _registeredObjects[invokeReq.objectId];
                    object invokeResult   = invokeObj.Invoke(invokeReq.methodId, invokeReq.methodArgs);
                    var    invokeResponse = new InvokeResponse()
                    {
                        objectId = invokeReq.objectId,
                        methodId = invokeReq.methodId,
                        result   = invokeResult
                    };
                    return(invokeResponse);

                default:
                    if (request != null)
                    {
                        throw new RecipientBindingException($"Unsupported request type: {request.requestType}");
                    }
                    else
                    {
                        throw new RecipientBindingException($"Request failed to deserialize.");
                    }
                }
            }
            catch (RecipientBindingException ex)
            {
                var exceptionResponse = new ExceptionResponse()
                {
                    exception = ex
                };

#pragma warning disable EA003 // Catch block swallows an exception
                return(exceptionResponse);
            }
#pragma warning restore EA003 // Catch block swallows an exception
        }