public async Task Handle_WithoutExistingLink_ShouldInsertNewLink()
        {
            // arrange
            var cancellationToken        = new CancellationToken();
            var unitOfWorkFactory        = new TransactionScopeUnitOfWorkFactory();
            var dbContextFactory         = new DefaultDbContextFactory();
            var classifierTypeRepository = new DbClassifierTypeRepository(dbContextFactory);
            var classifierTypeService    = new DbClassifierTypeService(dbContextFactory, classifierTypeRepository);
            var generator = new MasterDataDbGenerator(unitOfWorkFactory, dbContextFactory);
            var handler   = new InsertClassifierLinkHandler(unitOfWorkFactory, dbContextFactory, classifierTypeService);

            using (var _ = unitOfWorkFactory.Create())
            {
                await generator.InsertType(HierarchyType.Groups, cancellationToken);

                var root = await generator.FindTree(ClassifierTree.DefaultCode, cancellationToken);

                var group1 = await generator.InsertGroup(root.Uid, "001", null, cancellationToken);

                var root2 = await generator.InsertTree("root2", cancellationToken);

                // ReSharper disable once PossibleInvalidOperationException
                var group2 = await generator.InsertGroup(root2.Uid.Value, "002", null, cancellationToken);

                var item1 = await generator.InsertItem("001", null, cancellationToken);

                await generator.InsertLink(group1.Uid, item1.Uid, cancellationToken);

                // assert - initially new items belongs to default group
                var links = await generator.GetLinks(null, item1.Uid, cancellationToken);

                Assert.AreEqual(1, links.TotalCount);
                Assert.AreEqual(group1.Uid, links.Rows[0].Group.Uid);
                Assert.AreEqual(item1.Uid, links.Rows[0].Item.Uid);

                // act - link with new group in same hierarchy
                var result = await handler.Handle(new InsertClassifierLink
                {
                    UserUid  = generator.UserUid,
                    TypeCode = generator.TypeCode,
                    // ReSharper disable once PossibleInvalidOperationException
                    GroupUid = group2.Uid.Value,
                    // ReSharper disable once PossibleInvalidOperationException
                    ItemUid = item1.Uid.Value
                }, cancellationToken);

                // assert - new link inserted
                Assert.IsTrue(result.Success);

                // assert - item linked to new group, link with default root still exists
                links = await generator.GetLinks(null, item1.Uid, cancellationToken);

                Assert.AreEqual(2, links.TotalCount);
                var groups = links.Rows.Select(x => x.Group.Uid).ToList();
                CollectionAssert.Contains(groups, group1.Uid);
                CollectionAssert.Contains(groups, group2.Uid);
            }
        }
Beispiel #2
0
        public MasterDataDbGenerator(IUnitOfWorkFactory unitOfWorkFactory, IDbContextFactory dbContextFactory)
        {
            _dbContextFactory = dbContextFactory;

            var dateTimeProvider = new DefaultDateTimeProvider();
            var jsonSerializer   = new NewtonsoftJsonSerializer();

            var classifierTreeRepository = new DbClassifierTreeRepository(dbContextFactory);
            var classifierTypeRepository = new DbClassifierTypeRepository(dbContextFactory);

            var fieldProviderRegistry = new DefaultFieldProviderRegistry();

            fieldProviderRegistry.AddFieldType(typeof(TextField));
            fieldProviderRegistry.AddFieldType(typeof(TextAreaField));

            var dbFieldMetadataRepository = new DbFieldMetadataRepository(dbContextFactory, fieldProviderRegistry, jsonSerializer);
            var dbFieldDataRepository     = new DbFieldDataRepository(dbContextFactory, fieldProviderRegistry);
            var dbFieldMetadataService    = new DbFieldMetadataService(dbContextFactory, dateTimeProvider, jsonSerializer);

            _classifierTypeService           = new DbClassifierTypeService(dbContextFactory, classifierTypeRepository);
            _getClassifierTreeListHandler    = new GetClassifierTreeListHandler(classifierTreeRepository);
            _insertClassifierTreeTypeHandler = new InsertClassifierTreeHandler(unitOfWorkFactory, dbContextFactory, _classifierTypeService);
            _insertClassifierTypeHandler     = new InsertClassifierTypeHandler(unitOfWorkFactory, _classifierTypeService, dbFieldMetadataService);
            _insertClassifierGroupHandler    = new InsertClassifierGroupHandler(unitOfWorkFactory, dbContextFactory, _classifierTypeService);

            _classifierTypeRegistrator = new DefaultClassifierTypeRegistrator(new NullLogger <DefaultClassifierTypeRegistrator>(),
                                                                              unitOfWorkFactory, _classifierTypeService, dbFieldMetadataService);

            var classifierTypeMetadataService = new ClassifierTypeMetadataService(dbFieldMetadataRepository);
            var classifierTreeService         = new DefaultClassifierTreeService(classifierTreeRepository);
            var dbNumberGenerator             = new DbNumberGenerator(dbContextFactory, null, dateTimeProvider, null);

            var classifierRepositoryFactory = new ClassifierRepositoryFactory(new DbClassifierRepository <Classifier>(
                                                                                  dbContextFactory, _classifierTypeService, classifierTreeService,
                                                                                  classifierTypeMetadataService, dbFieldDataRepository, dbNumberGenerator));

            _insertClassifierHandler      = new InsertClassifierHandler(unitOfWorkFactory, classifierRepositoryFactory);
            _updateClassifierGroupHandler = new UpdateClassifierGroupHandler(unitOfWorkFactory, dbContextFactory, _classifierTypeService);
            _deleteClassifierGroupHandler = new DeleteClassifierGroupHandler(unitOfWorkFactory, dbContextFactory, _classifierTypeService);
            _insertClassifierLinkHandler  = new InsertClassifierLinkHandler(unitOfWorkFactory, dbContextFactory, _classifierTypeService);
            _getClassifierLinkListHandler = new GetClassifierLinkListHandler(dbContextFactory, _classifierTypeService);
        }