public void HandleBothUniqueAttributeAndMapping()
        {
            var metadata = SagaMetadata.Create(typeof(MySagaWithMappedAndUniqueProperty));

            Assert.True(metadata.TryGetCorrelationProperty(out var correlatedProperty));
            Assert.AreEqual("UniqueProperty", correlatedProperty.Name);
        }
        public void DetectUniquePropertiesByAttribute()
        {
            var metadata = SagaMetadata.Create(typeof(MySaga));

            Assert.True(metadata.TryGetCorrelationProperty(out var correlatedProperty));
            Assert.AreEqual("UniqueProperty", correlatedProperty.Name);
        }
        public void Throws_friendly_exception_when_IFindSagas_FindBy_returns_null()
        {
            var availableTypes = new List <Type>
            {
                typeof(ReturnsNullFinder)
            };

            var messageType = typeof(StartSagaMessage);

            var messageConventions = new Conventions
            {
                IsCommandTypeAction = t => t == messageType
            };

            var sagaMetadata = SagaMetadata.Create(typeof(TestSaga), availableTypes, messageConventions);

            if (!sagaMetadata.TryGetFinder(messageType.FullName, out var finderDefinition))
            {
                throw new Exception("Finder not found");
            }

            var builder = new FakeBuilder();

            builder.Register(() => new ReturnsNullFinder());

            var customerFinderAdapter = new CustomFinderAdapter <TestSaga.SagaData, StartSagaMessage>();

            Assert.That(async() => await customerFinderAdapter.Find(builder, finderDefinition, new InMemorySynchronizedStorageSession(), new ContextBag(), new StartSagaMessage()),
                        Throws.Exception.With.Message.EqualTo("Return a Task or mark the method as async."));
        }
Exemplo n.º 4
0
        public void Throws_friendly_exception_when_IFindSagas_FindBy_returns_null()
        {
            var availableTypes = new List <Type>
            {
                typeof(ReturnsNullFinder)
            };

            var messageType = typeof(StartSagaMessage);

            var messageConventions = new Conventions();

            messageConventions.DefineCommandTypeConventions(t => t == messageType);

            var sagaMetadata = SagaMetadata.Create(typeof(TestSaga), availableTypes, messageConventions);

            if (!sagaMetadata.TryGetFinder(messageType.FullName, out var finderDefinition))
            {
                throw new Exception("Finder not found");
            }

            var services = new ServiceCollection();

            services.AddTransient(sp => new ReturnsNullFinder());

            var customerFinderAdapter = new CustomFinderAdapter <TestSaga.SagaData, StartSagaMessage>();

            Assert.That(async() => await customerFinderAdapter.Find(services.BuildServiceProvider(), finderDefinition, new FakeSynchronizedStorageSession(), new ContextBag(), new StartSagaMessage(), new Dictionary <string, string>(), default),
                        Throws.Exception.With.Message.EqualTo("Return a Task or mark the method as async."));
        }
        public void AutomaticallyAddUniqueForMappedProperties()
        {
            var metadata = SagaMetadata.Create(typeof(MySagaWithMappedProperty));

            Assert.True(metadata.TryGetCorrelationProperty(out var correlatedProperty));
            Assert.AreEqual("UniqueProperty", correlatedProperty.Name);
        }
        static ActiveSagaInstance AssociateSagaWithMessage(FakeSaga saga, IInvokeHandlerContext behaviorContext)
        {
            var sagaInstance = new ActiveSagaInstance(saga, SagaMetadata.Create(typeof(FakeSaga), new List <Type>(), new Conventions()), () => DateTime.UtcNow);

            behaviorContext.Extensions.Set(sagaInstance);
            return(sagaInstance);
        }
Exemplo n.º 7
0
        public void When_a_finder_and_a_mapping_exists_for_same_property()
        {
            var availableTypes = new List <Type>
            {
                typeof(SagaWithMappingAndFinder.Finder)
            };
            var exception = Assert.Throws <Exception>(() => { SagaMetadata.Create(typeof(SagaWithMappingAndFinder), availableTypes, new Conventions()); });

            Assert.AreEqual("A custom IFindSagas and an existing mapping where found for message 'NServiceBus.Core.Tests.Sagas.TypeBasedSagas.SagaMetadataCreationTests+SagaWithMappingAndFinder+StartSagaMessage'. Either remove the message mapping or remove the finder. Finder name 'NServiceBus.Core.Tests.Sagas.TypeBasedSagas.SagaMetadataCreationTests+SagaWithMappingAndFinder+Finder'.", exception.Message);
        }
Exemplo n.º 8
0
        public void ValidateThrowsWhenSagaCustomFinderMapsMessageItDoesntHandle()
        {
            var availableTypes = new List <Type>
            {
                typeof(SagaWithCustomFinderForMessageItDoesntHandle.Finder)
            };
            var ex = Assert.Throws <Exception>(() => SagaMetadata.Create(typeof(SagaWithCustomFinderForMessageItDoesntHandle), availableTypes, new Conventions()));

            Assert.That(ex.Message.Contains("does not handle that message") && ex.Message.Contains("Custom saga finder"));
        }
Exemplo n.º 9
0
        public void When_finder_for_non_message()
        {
            var availableTypes = new List <Type>
            {
                typeof(SagaWithNonMessageFinder.Finder)
            };
            var exception = Assert.Throws <Exception>(() => { SagaMetadata.Create(typeof(SagaWithNonMessageFinder), availableTypes, new Conventions()); });

            Assert.AreEqual("A custom IFindSagas must target a valid message type as defined by the message conventions. Change 'NServiceBus.Core.Tests.Sagas.TypeBasedSagas.SagaMetadataCreationTests+SagaWithNonMessageFinder+StartSagaMessage' to a valid message type or add it to the message conventions. Finder name 'NServiceBus.Core.Tests.Sagas.TypeBasedSagas.SagaMetadataCreationTests+SagaWithNonMessageFinder+Finder'.", exception.Message);
        }
Exemplo n.º 10
0
        public void DetectAndRegisterPropertyFinders()
        {
            var metadata = SagaMetadata.Create(typeof(MySagaWithMappedProperty));

            var finder = GetFinder(metadata, typeof(SomeMessage).FullName);

            Assert.AreEqual(typeof(PropertySagaFinder <MySagaWithMappedProperty.SagaData>), finder.Type);
            Assert.NotNull(finder.Properties["property-accessor"]);
            Assert.AreEqual("UniqueProperty", finder.Properties["saga-property-name"]);
        }
        public void Should_throw()
        {
            if (Environment.OSVersion.Platform != PlatformID.Win32NT)
            {
                Assert.Ignore("ApprovalTests only works on Windows");
            }

            var exception = Assert.Throws <Exception>(() => SagaMetadata.Create(typeof(SagaWithMultipleCorrelatedProperties), new List <Type>(), new Conventions()));

            TestApprover.Verify(exception.Message);
        }
Exemplo n.º 12
0
        public void DetectAndRegisterHeaderFinders()
        {
            var metadata = SagaMetadata.Create(typeof(MySagaWithMappedHeader));

            var finder = GetFinder(metadata, typeof(SomeMessage).FullName);

            Assert.AreEqual(typeof(HeaderPropertySagaFinder <MySagaWithMappedHeader.SagaData>), finder.Type);
            Assert.AreEqual("CorrelationHeader", finder.Properties["message-header-name"]);
            Assert.AreEqual("UniqueProperty", finder.Properties["saga-property-name"]);
            Assert.AreEqual(typeof(int), finder.Properties["saga-property-type"]);
        }
Exemplo n.º 13
0
        public void When_message_only_has_custom_finder()
        {
            var availableTypes = new List <Type>
            {
                typeof(SagaWithFinderOnly.Finder)
            };
            var metadata = SagaMetadata.Create(typeof(SagaWithFinderOnly), availableTypes, new Conventions());

            Assert.AreEqual(1, metadata.Finders.Count);
            Assert.AreEqual(typeof(CustomFinderAdapter <SagaWithFinderOnly.SagaData, SagaWithFinderOnly.StartSagaMessage>), metadata.Finders.First().Type);
        }
Exemplo n.º 14
0
        public void DetectAndRegisterCustomFindersUsingScanning()
        {
            var availableTypes = new List <Type>
            {
                typeof(MySagaWithScannedFinder.CustomFinder)
            };
            var metadata = SagaMetadata.Create(typeof(MySagaWithScannedFinder), availableTypes, new Conventions());

            var finder = GetFinder(metadata, typeof(SomeMessage).FullName);

            Assert.AreEqual(typeof(CustomFinderAdapter <MySagaWithScannedFinder.SagaData, SomeMessage>), finder.Type);
            Assert.AreEqual(typeof(MySagaWithScannedFinder.CustomFinder), finder.Properties["custom-finder-clr-type"]);
        }
        public static SagaCorrelationProperty GetMetadata <T>(IContainSagaData entity)
        {
            var metadata = SagaMetadata.Create(typeof(T));

            if (!metadata.TryGetCorrelationProperty(out var correlatedProp))
            {
                return(SagaCorrelationProperty.None);
            }
            var prop = entity.GetType().GetProperty(correlatedProp.Name);

            var value = prop.GetValue(entity);

            return(new SagaCorrelationProperty(correlatedProp.Name, value));
        }
        public static SagaCorrelationProperty CreateMetadata <T>(this RavenDBPersistenceTestBase test, IContainSagaData sagaEntity)
        {
            var metadata = SagaMetadata.Create(typeof(T));

            SagaMetadata.CorrelationPropertyMetadata correlationPropertyMetadata;

            metadata.TryGetCorrelationProperty(out correlationPropertyMetadata);

            var propertyInfo = metadata.SagaEntityType.GetProperty(correlationPropertyMetadata.Name);
            var value        = propertyInfo.GetValue(sagaEntity);

            var correlationProperty = new SagaCorrelationProperty(correlationPropertyMetadata.Name, value);

            return(correlationProperty);
        }
Exemplo n.º 17
0
        public void DetectMessagesStartingTheSaga()
        {
            var metadata = SagaMetadata.Create(typeof(SagaWith2StartersAnd1Handler));

            var messages = metadata.AssociatedMessages;

            Assert.AreEqual(4, messages.Count);

            Assert.True(metadata.IsMessageAllowedToStartTheSaga(typeof(SagaWith2StartersAnd1Handler.StartMessage1).FullName));

            Assert.True(metadata.IsMessageAllowedToStartTheSaga(typeof(SagaWith2StartersAnd1Handler.StartMessage2).FullName));

            Assert.False(metadata.IsMessageAllowedToStartTheSaga(typeof(SagaWith2StartersAnd1Handler.Message3).FullName));

            Assert.False(metadata.IsMessageAllowedToStartTheSaga(typeof(SagaWith2StartersAnd1Handler.MyTimeout).FullName));
        }
Exemplo n.º 18
0
        public void GetEntityClrTypeFromInheritanceChain()
        {
            var metadata = SagaMetadata.Create(typeof(SagaWithInheritanceChain));

            Assert.AreEqual(typeof(SagaWithInheritanceChain.SagaData), metadata.SagaEntityType);
        }
        public void Should_throw()
        {
            var exception = Assert.Throws <Exception>(() => SagaMetadata.Create(typeof(SagaWithMultipleCorrelatedProperties), new List <Type>(), new Conventions()));

            Approver.Verify(exception.Message);
        }
Exemplo n.º 20
0
        public void GetEntityClrType()
        {
            var metadata = SagaMetadata.Create(typeof(MySaga));

            Assert.AreEqual(typeof(MySaga.MyEntity), metadata.SagaEntityType);
        }
Exemplo n.º 21
0
        public void ValidateThrowsWhenSagaMapsMessageItDoesntHandle(Type sagaType)
        {
            var ex = Assert.Throws <Exception>(() => SagaMetadata.Create(sagaType));

            Assert.That(ex.Message.Contains("does not handle that message") && ex.Message.Contains("in the ConfigureHowToFindSaga method"));
        }
Exemplo n.º 22
0
        public void RequireFinderForMessagesStartingTheSaga()
        {
            var ex = Assert.Throws <Exception>(() => SagaMetadata.Create(typeof(MySagaWithUnmappedStartProperty)));

            Assert.True(ex.Message.Contains(typeof(MySagaWithUnmappedStartProperty.MessageThatStartsTheSaga).FullName));
        }
        public void Should_throw()
        {
            var ex = Assert.Throws <Exception>(() => SagaMetadata.Create(typeof(SagaWithNoStartMessage), new List <Type>(), new Conventions()));

            StringAssert.Contains("Sagas must have at least one message that is allowed to start the saga", ex.Message);
        }
Exemplo n.º 24
0
 public void Throws_when_does_not_implement_generic_saga()
 {
     Assert.Throws <Exception>(() => SagaMetadata.Create(typeof(MyNonGenericSaga)));
 }
Exemplo n.º 25
0
        public void HandleNonExistingFinders()
        {
            var ex = Assert.Throws <Exception>(() => SagaMetadata.Create(typeof(MySagaWithUnmappedStartProperty)));

            Assert.That(ex.Message.Contains("mapper.ConfigureMapping"));
        }
Exemplo n.º 26
0
        public void GetSagaClrType()
        {
            var metadata = SagaMetadata.Create(typeof(MySaga));

            Assert.AreEqual(typeof(MySaga), metadata.SagaType);
        }
Exemplo n.º 27
0
        public void ValidateThatMappingOnSagaIdHasTypeGuidForMessageFields()
        {
            var ex = Assert.Throws <InvalidOperationException>(() => SagaMetadata.Create(typeof(SagaWithIdMappedToNonGuidMessageField)));

            StringAssert.Contains(typeof(SomeMessage).Name, ex.Message);
        }
Exemplo n.º 28
0
        public void ValidateThatSagaPropertyIsNotAField()
        {
            var ex = Assert.Throws <InvalidOperationException>(() => SagaMetadata.Create(typeof(SagaWithSagaDataMemberAsFieldInsteadOfProperty)));

            StringAssert.Contains(typeof(SagaWithSagaDataMemberAsFieldInsteadOfProperty.SagaData).FullName, ex.Message);
        }
Exemplo n.º 29
0
        public void ValidateThatMappingOnNonSagaIdGuidPropertyFromStringToGuidForMessagePropsThrowsException()
        {
            var ex = Assert.Throws <InvalidOperationException>(() => SagaMetadata.Create(typeof(SagaWithNonIdPropertyMappedToStringMessageProperty)));

            StringAssert.Contains(typeof(SomeMessage).FullName, ex.Message);
        }
        public void Should_throw()
        {
            var ex = Assert.Throws <Exception>(() => SagaMetadata.Create(typeof(SagaWithNoStartMessage), new List <Type>(), new Conventions()));

            StringAssert.Contains("DateTimeOffset is not supported for correlated properties", ex.Message);
        }