示例#1
0
        public void InheritedMap()
        {
            var definitions = new MapDefinitionCollection(new IMapDefinition[]
            {
                new MapperDefinition1(),
            });
            var mapper = new UmbracoMapper(definitions);

            var thing3 = new Thing3 {
                Value = "value"
            };
            var thing2 = mapper.Map <Thing3, Thing2>(thing3);

            Assert.IsNotNull(thing2);
            Assert.AreEqual("value", thing2.Value);

            thing2 = mapper.Map <Thing2>(thing3);

            Assert.IsNotNull(thing2);
            Assert.AreEqual("value", thing2.Value);

            thing2 = new Thing2();
            mapper.Map(thing3, thing2);
            Assert.AreEqual("value", thing2.Value);
        }
示例#2
0
    public void EnumerableMap()
    {
        var definitions = new MapDefinitionCollection(() => new IMapDefinition[] { new MapperDefinition1() });
        var mapper      = new UmbracoMapper(definitions, _scopeProvider);

        var thing1A = new Thing1 {
            Value = "valueA"
        };
        var thing1B = new Thing1 {
            Value = "valueB"
        };

        Thing1[] thing1 = { thing1A, thing1B };
        var      thing2 = mapper.Map <IEnumerable <Thing1>, IEnumerable <Thing2> >(thing1).ToList();

        Assert.IsNotNull(thing2);
        Assert.AreEqual(2, thing2.Count);
        Assert.AreEqual("valueA", thing2[0].Value);
        Assert.AreEqual("valueB", thing2[1].Value);

        thing2 = mapper.Map <IEnumerable <Thing2> >(thing1).ToList();

        Assert.IsNotNull(thing2);
        Assert.AreEqual(2, thing2.Count);
        Assert.AreEqual("valueA", thing2[0].Value);
        Assert.AreEqual("valueB", thing2[1].Value);

        thing2 = mapper.MapEnumerable <Thing1, Thing2>(thing1).ToList();

        Assert.IsNotNull(thing2);
        Assert.AreEqual(2, thing2.Count);
        Assert.AreEqual("valueA", thing2[0].Value);
        Assert.AreEqual("valueB", thing2[1].Value);
    }
示例#3
0
        public void SimpleMap()
        {
            var definitions = new MapDefinitionCollection(() => new IMapDefinition[]
            {
                new MapperDefinition1(),
            });
            var mapper = new UmbracoMapper(definitions, _scopeProvider);

            var thing1 = new Thing1 {
                Value = "value"
            };
            Thing2 thing2 = mapper.Map <Thing1, Thing2>(thing1);

            Assert.IsNotNull(thing2);
            Assert.AreEqual("value", thing2.Value);

            thing2 = mapper.Map <Thing2>(thing1);

            Assert.IsNotNull(thing2);
            Assert.AreEqual("value", thing2.Value);

            thing2 = new Thing2();
            mapper.Map(thing1, thing2);
            Assert.AreEqual("value", thing2.Value);
        }
示例#4
0
        public void OrderCustomerWithDefinition()
        {
            using (var context = new TestEntities()) {
                var query = context.Orders.Include(o => o.Customer);

                var mapDefinitions = new MapDefinitionCollection();
                mapDefinitions.Register(typeof(Order), typeof(OrderDto), new MapDefinition(
                                            new Dictionary <string, string> {
                    { "CustomerId", "Id" }     // set OrderNo with Id
                }
                                            ));
                mapDefinitions.Register(typeof(Customer), typeof(CustomerDto), new MapDefinition(
                                            new Dictionary <string, string> {
                    { "Id", "Id" }     // get only Id field
                }, true
                                            ));

                var dtoQuery = query.ProjectTo <OrderDto>(mapDefinitions);
                var orders   = dtoQuery.ToList();

                Assert.AreEqual(2, orders.Count);
                Assert.IsTrue(orders.All(o => o.CustomerId == o.Id));
                Assert.IsTrue(orders.All(o => o.Customer != null && o.Customer.Name == null));
                Assert.IsTrue(orders.All(o => !o.OrderDetails.Any()));
            }
        }
示例#5
0
    public void CollectionsMap()
    {
        var definitions = new MapDefinitionCollection(() => new IMapDefinition[] { new MapperDefinition2() });
        var mapper      = new UmbracoMapper(definitions, _scopeProvider);

        // can map a PropertyCollection
        var source = new PropertyCollection();
        var target = mapper.Map <IEnumerable <ContentPropertyDto> >(source);
    }
示例#6
0
    public void NullPropertyMap()
    {
        var definitions = new MapDefinitionCollection(() => new IMapDefinition[] { new MapperDefinition5() });
        var mapper      = new UmbracoMapper(definitions, _scopeProvider);

        var thing7 = new Thing7();

        var thing8 = mapper.Map <Thing7, Thing8>(thing7);

        Assert.IsNotNull(thing8);
        Assert.IsNull(thing8.Things);
    }
示例#7
0
        public void ManyToOneTypeMismatchCheck()
        {
            using (var context = new TestEntities()) {
                var query = context.Set <OrderDetail>().Include(od => od.Supplier);

                var mapDefinitions = new MapDefinitionCollection();
                mapDefinitions.Register(typeof(OrderDetail), typeof(OrderDto), new MapDefinition(
                                            new Dictionary <string, string> {
                    { "OrderDetails", "Supplier" }     // set single to collection
                }, true
                                            ));

                query.ProjectTo <OrderDto>(mapDefinitions);
            }
        }
示例#8
0
        public void OneToManyTypeMismatchCheck()
        {
            using (var context = new TestEntities()) {
                var query = context.Orders.Include(o => o.OrderDetails);

                var mapDefinitions = new MapDefinitionCollection();
                mapDefinitions.Register(typeof(Order), typeof(OrderDetailDto), new MapDefinition(
                                            new Dictionary <string, string> {
                    { "Supplier", "OrderDetails" }     // set collection to single
                }, true
                                            ));

                query.ProjectTo <OrderDetailDto>(mapDefinitions);
            }
        }
示例#9
0
    public void EnumMap()
    {
        var definitions = new MapDefinitionCollection(() => new IMapDefinition[] { new MapperDefinition4() });
        var mapper      = new UmbracoMapper(definitions, _scopeProvider);

        var thing5 = new Thing5 {
            Fruit1 = Thing5Enum.Apple, Fruit2 = Thing5Enum.Banana, Fruit3 = Thing5Enum.Cherry
        };

        var thing6 = mapper.Map <Thing5, Thing6>(thing5);

        Assert.IsNotNull(thing6);
        Assert.AreEqual(Thing6Enum.Apple, thing6.Fruit1);
        Assert.AreEqual(Thing6Enum.Banana, thing6.Fruit2);
        Assert.AreEqual(Thing6Enum.Cherry, thing6.Fruit3);
    }
示例#10
0
        public void OrderDetailsWithDefinition()
        {
            using (var context = new TestEntities()) {
                var query = context.Orders.Include(o => o.OrderDetails.Select(od => od.Supplier));

                var mapDefinitions = new MapDefinitionCollection();
                mapDefinitions.Register(typeof(OrderDetail), typeof(OrderDetailDto), new MapDefinition(
                                            new Dictionary <string, string> {
                    { "Id", "OrderId" }     // write OrderId into Id field
                }, true
                                            ));

                var dtoQuery = query.ProjectTo <OrderDto>(mapDefinitions);
                var orders   = dtoQuery.ToList();

                Assert.AreEqual(2, orders.Count);
                Assert.IsTrue(orders.All(o => o.OrderDetails.All(od => od.Id == o.Id)));
                // suppliers should be null even if it is included, because we wanted only explicit properties.
                Assert.IsTrue(orders.All(o => o.OrderDetails.All(od => od.Supplier == null)));
            }
        }
示例#11
0
        /// <summary>
        /// Create member controller to test
        /// </summary>
        /// <param name="memberService">Member service</param>
        /// <param name="memberTypeService">Member type service</param>
        /// <param name="memberGroupService">Member group service</param>
        /// <param name="membersUserManager">Members user manager</param>
        /// <param name="dataTypeService">Data type service</param>
        /// <param name="backOfficeSecurityAccessor">Back office security accessor</param>
        /// <param name="mockPasswordChanger">Password changer class</param>
        /// <returns>A member controller for the tests</returns>
        private MemberController CreateSut(
            IMemberService memberService,
            IMemberTypeService memberTypeService,
            IMemberGroupService memberGroupService,
            IUmbracoUserManager <MemberIdentityUser> membersUserManager,
            IDataTypeService dataTypeService,
            IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
            IPasswordChanger <MemberIdentityUser> passwordChanger,
            IOptions <GlobalSettings> globalSettings,
            IUser user)
        {
            var httpContextAccessor = new HttpContextAccessor();

            var mockShortStringHelper          = new MockShortStringHelper();
            var textService                    = new Mock <ILocalizedTextService>();
            var contentTypeBaseServiceProvider = new Mock <IContentTypeBaseServiceProvider>();

            contentTypeBaseServiceProvider.Setup(x => x.GetContentTypeOf(It.IsAny <IContentBase>())).Returns(new ContentType(mockShortStringHelper, 123));
            var contentAppFactories              = new Mock <List <IContentAppFactory> >();
            var mockContentAppFactoryCollection  = new Mock <ILogger <ContentAppFactoryCollection> >();
            var hybridBackOfficeSecurityAccessor = new BackOfficeSecurityAccessor(httpContextAccessor);
            var contentAppFactoryCollection      = new ContentAppFactoryCollection(
                () => contentAppFactories.Object,
                mockContentAppFactoryCollection.Object,
                hybridBackOfficeSecurityAccessor);
            var mockUserService = new Mock <IUserService>();
            var commonMapper    = new CommonMapper(
                mockUserService.Object,
                contentTypeBaseServiceProvider.Object,
                contentAppFactoryCollection,
                textService.Object);
            var mockCultureDictionary = new Mock <ICultureDictionary>();

            var mockPasswordConfig = new Mock <IOptions <MemberPasswordConfigurationSettings> >();

            mockPasswordConfig.Setup(x => x.Value).Returns(() => new MemberPasswordConfigurationSettings());
            IDataEditor dataEditor = Mock.Of <IDataEditor>(
                x => x.Type == EditorType.PropertyValue &&
                x.Alias == Constants.PropertyEditors.Aliases.Label);

            Mock.Get(dataEditor).Setup(x => x.GetValueEditor()).Returns(new TextOnlyValueEditor(new DataEditorAttribute(Constants.PropertyEditors.Aliases.TextBox, "Test Textbox", "textbox"), textService.Object, Mock.Of <IShortStringHelper>(), Mock.Of <IJsonSerializer>(), Mock.Of <IIOHelper>()));

            var propertyEditorCollection = new PropertyEditorCollection(new DataEditorCollection(() => new[] { dataEditor }));

            IMapDefinition memberMapDefinition = new MemberMapDefinition(
                commonMapper,
                new CommonTreeNodeMapper(Mock.Of <LinkGenerator>()),
                new MemberTabsAndPropertiesMapper(
                    mockCultureDictionary.Object,
                    backOfficeSecurityAccessor,
                    textService.Object,
                    memberTypeService,
                    memberService,
                    memberGroupService,
                    mockPasswordConfig.Object,
                    contentTypeBaseServiceProvider.Object,
                    propertyEditorCollection));

            var map = new MapDefinitionCollection(() => new List <IMapDefinition>()
            {
                new global::Umbraco.Cms.Core.Models.Mapping.MemberMapDefinition(),
                memberMapDefinition,
                new ContentTypeMapDefinition(
                    commonMapper,
                    propertyEditorCollection,
                    dataTypeService,
                    new Mock <IFileService>().Object,
                    new Mock <IContentTypeService>().Object,
                    new Mock <IMediaTypeService>().Object,
                    memberTypeService,
                    new Mock <ILoggerFactory>().Object,
                    mockShortStringHelper,
                    globalSettings,
                    new Mock <IHostingEnvironment>().Object)
            });
            var scopeProvider = Mock.Of <IScopeProvider>(x => x.CreateScope(
                                                             It.IsAny <IsolationLevel>(),
                                                             It.IsAny <RepositoryCacheMode>(),
                                                             It.IsAny <IEventDispatcher>(),
                                                             It.IsAny <IScopedNotificationPublisher>(),
                                                             It.IsAny <bool?>(),
                                                             It.IsAny <bool>(),
                                                             It.IsAny <bool>()) == Mock.Of <IScope>());

            _mapper = new UmbracoMapper(map, scopeProvider);

            return(new MemberController(
                       new DefaultCultureDictionary(
                           new Mock <ILocalizationService>().Object,
                           NoAppCache.Instance),
                       new LoggerFactory(),
                       mockShortStringHelper,
                       new DefaultEventMessagesFactory(
                           new Mock <IEventMessagesAccessor>().Object),
                       textService.Object,
                       propertyEditorCollection,
                       _mapper,
                       memberService,
                       memberTypeService,
                       (IMemberManager)membersUserManager,
                       dataTypeService,
                       backOfficeSecurityAccessor,
                       new ConfigurationEditorJsonSerializer(),
                       passwordChanger,
                       scopeProvider
                       ));
        }
示例#12
0
    public void ConcurrentMap()
    {
        var definitions = new MapDefinitionCollection(() => new IMapDefinition[]
        {
            new MapperDefinition1(),
            new MapperDefinition3(),
        });
        var mapper = new UmbracoMapper(definitions, _scopeProvider);

        // the mapper currently has a map from Thing1 to Thing2
        // because Thing3 inherits from Thing1, it will map a Thing3 instance,
        // and register a new map from Thing3 to Thing2,
        // thus modifying its internal dictionaries

        // if timing is good, and mapper does have non-concurrent dictionaries, it fails
        // practically, to reproduce, one needs to add a 1s sleep in the mapper's loop
        // hence, this test is explicit
        var thing3 = new Thing3 {
            Value = "value"
        };
        var       thing4 = new Thing4();
        Exception caught = null;

        void ThreadLoop()
        {
            // keep failing at mapping - and looping through the maps
            for (var i = 0; i < 10; i++)
            {
                try
                {
                    mapper.Map <Thing2>(thing4);
                }
                catch (Exception e)
                {
                    caught = e;
                    Console.WriteLine($"{e.GetType().Name} {e.Message}");
                }
            }

            Console.WriteLine("done");
        }

        var thread = new Thread(ThreadLoop);

        thread.Start();
        Thread.Sleep(1000);

        try
        {
            Console.WriteLine($"{DateTime.Now:O} mapping");
            var thing2 = mapper.Map <Thing2>(thing3);
            Console.WriteLine($"{DateTime.Now:O} mapped");

            Assert.IsNotNull(thing2);
            Assert.AreEqual("value", thing2.Value);
        }
        finally
        {
            thread.Join();
        }
    }