Beispiel #1
0
        /// <summary>
        /// Verify/compare expected versus sent.
        /// </summary>
        private static void AreSentCompare(List <ExpectedEvent> expectedEvents, string?correlationId, string testText, string expectText, string actualText, string checkText)
        {
            if (expectedEvents == null)
            {
                throw new ArgumentNullException(nameof(expectedEvents));
            }

            var actualEvents = GetSentEvents(correlationId);

            if (actualEvents.Count != expectedEvents.Count)
            {
                Assert.Fail($"{testText} {expectedEvents.Count} Event(s) {checkText} {expectText}; there were {actualEvents.Count} {actualText}.");
            }

            for (int i = 0; i < actualEvents.Count; i++)
            {
                // Assert subject and action.
                var exp = expectedEvents[i].EventData;
                var act = actualEvents[i];

                if (!EventSubjectMatcher.Match(_eventPublisher.TemplateWildcard, _eventPublisher.PathSeparator, exp.Subject, act.Subject))
                {
                    Assert.Fail($"{testText} {expectText} Event[{i}].Subject '{exp.Subject}' is not equal to actual '{act.Subject}' {actualText}.");
                }

                if (!string.IsNullOrEmpty(exp.Action) && string.CompareOrdinal(exp.Action, act.Action) != 0)
                {
                    Assert.Fail($"{testText} {expectText} Event[{i}].Action '{exp.Action}' is not equal to actual '{act.Action}' {actualText}.");
                }

                // Where there is *no* expected value then skip value comparison.
                if (!exp.HasValue)
                {
                    continue;
                }

                // Assert value.
                var eVal = exp.GetValue();
                var aVal = act.GetValue();

                var comparisonConfig = TestSetUp.GetDefaultComparisonConfig();
                comparisonConfig.AttributesToIgnore.AddRange(new Type[] { typeof(ReferenceDataInterfaceAttribute) });

                var type = eVal?.GetType() ?? aVal?.GetType();
                if (type != null)
                {
                    TestSetUp.InferAdditionalMembersToIgnore(comparisonConfig, type);
                }

                var cl = new CompareLogic(comparisonConfig);
                var cr = cl.Compare(eVal, aVal);
                if (!cr.AreEqual)
                {
                    Assert.Fail($"{testText} {expectText} Event[{i}].Value is not equal to actual {actualText}: {cr.DifferencesString}");
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Verifies that at least one event was sent that matched the <paramref name="template"/> which may contain wildcards (<see cref="IEventPublisher.TemplateWildcard"/>) and optional <paramref name="action"/>.
        /// </summary>
        /// <param name="template">The expected subject template (or fully qualified subject).</param>
        /// <param name="action">The optional expected action; <c>null</c> indicates any.</param>
        /// <param name="correlationId">The correlation identifier.</param>
        public static void IsSent(string template, string?action = null, string?correlationId = null)
        {
            Check.NotEmpty(template, nameof(template));

            var events = GetSentEvents(correlationId);

            if (events == null || events.Count == 0 || !events.Any(x => EventSubjectMatcher.Match(_eventPublisher.TemplateWildcard, _eventPublisher.PathSeparator, template, x.Subject) && (action == null || StringComparer.OrdinalIgnoreCase.Compare(action, x.Action) == 0)))
            {
                Assert.Fail($"Event with a subject template '{template}' and Action '{action ?? "any"}' was expected and did not match one of the events sent.");
            }
        }
Beispiel #3
0
        public void Match()
        {
            Assert.IsFalse(EventSubjectMatcher.Match("*", ".", "domain.entity.123", "domain.entity.000"));
            Assert.IsFalse(EventSubjectMatcher.Match("*", ".", "domain.ytitne.123", "domain.entity.123"));
            Assert.IsFalse(EventSubjectMatcher.Match("*", ".", "domain.ytitne.*", "domain.entity.123"));
            Assert.IsFalse(EventSubjectMatcher.Match("*", ".", "domain.entity.123.*", "domain.entity.123"));

            Assert.IsTrue(EventSubjectMatcher.Match("*", ".", "domain.entity.123", "domain.entity.123"));
            Assert.IsTrue(EventSubjectMatcher.Match("*", ".", "domain.entity.*", "domain.entity.123"));
            Assert.IsTrue(EventSubjectMatcher.Match("*", ".", "domain.*.123", "domain.entity.123"));
            Assert.IsTrue(EventSubjectMatcher.Match("*", ".", "domain.entity.*", "domain.entity.123.456"));
        }
Beispiel #4
0
        /// <summary>
        /// Verifies that the no event was published that matches the <paramref name="template"/> which may contain wildcards (<see cref="IEventPublisher.TemplateWildcard"/>) and optional <paramref name="action"/>.
        /// </summary>
        /// <param name="template">The expected subject template (or fully qualified subject).</param>
        /// <param name="action">The optional expected action; <c>null</c> indicates any.</param>
        /// <param name="correlationId">The correlation identifier.</param>
        public static void IsNotPublished(string template, string?action = null, string?correlationId = null)
        {
            Check.NotEmpty(template, nameof(template));

            var events = GetEvents(correlationId);

            if (events == null || events.Count == 0)
            {
                return;
            }

            if (events.Any(x => EventSubjectMatcher.Match(EventPublisher.TemplateWildcard, EventPublisher.PathSeparator, template, x.Subject) && (action == null || StringComparer.OrdinalIgnoreCase.Compare(action, x.Action) == 0)))
            {
                Assert.Fail($"Event with a subject template '{template}' and Action '{action ?? "any"}' was not expected; however, it was published.");
            }
        }
Beispiel #5
0
        /// <summary>
        /// Finds and creates the <see cref="IEventSubscriber"/> where found for the <paramref name="subject"/> and <paramref name="action"/>.
        /// </summary>
        /// <param name="subjectTemplateWildcard">The template wildcard (see <see cref="IEventPublisher.TemplateWildcard"/>).</param>
        /// <param name="subjectPathSeparator">The path separator (see <see cref="IEventPublisher.PathSeparator"/>.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="action">The action.</param>
        /// <returns>An instantiated <see cref="IEventSubscriber"/> where found; otherwise, <c>null</c>.</returns>
        internal IEventSubscriber?CreateEventSubscriber(string subjectTemplateWildcard, string subjectPathSeparator, string subject, string?action)
        {
            var subscribers = _subscribers.Where(
                x => EventSubjectMatcher.Match(subjectTemplateWildcard, subjectPathSeparator, x.SubjectTemplate, subject) &&
                (x.Actions == null || x.Actions.Count == 0 || x.Actions.Contains(action, StringComparer.InvariantCultureIgnoreCase))).Select(x => x.EventSubscriberType).ToArray();

            var type = subscribers.Length == 1
                ? subscribers[0]
                : subscribers.Length == 0 ? (Type?)null : throw new EventSubscriberException($"There are {subscribers.Length} {nameof(IEventSubscriber)} instances subscribing to Subject '{subject}' and Action '{action}'; there must be only a single subscriber.");

            if (type == null)
            {
                return(null);
            }

            return((IEventSubscriber)ServiceProvider.GetService(type)
                   ?? throw new InvalidOperationException($"Subscriber {type.Name} was unable to be instantiated through the ServiceProvider; please ensure correctly configured."));
        }