public async Task UpdateClassifier_NormalValues_UpdateItems()
        {
            // arrange
            var cancellationToken           = new CancellationToken();
            var unitOfWorkFactory           = new TransactionScopeUnitOfWorkFactory();
            var dbContextFactory            = new DefaultDbContextFactory();
            var generator                   = new MasterDataDbGenerator(unitOfWorkFactory, dbContextFactory);
            var classifierRepositoryFactory = CreateClassifierRepositoryFactory(dbContextFactory);
            var handler = new UpdateClassifierHandler(unitOfWorkFactory, classifierRepositoryFactory);

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

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

                // act
                var command = new UpdateClassifier
                {
                    UserUid = generator.UserUid,
                    Item    = new Classifier
                    {
                        // ReSharper disable once PossibleInvalidOperationException
                        Uid    = insertItem.Uid.Value,
                        Type   = generator.TypeCode,
                        Code   = "001",
                        Name   = "Test Classifier",
                        Fields = new FieldData
                        {
                            { "test1", "value1" },
                            { "test2", "value2" },
                            { "test3", "value3" }
                        }
                    }
                };

                var result = await handler.Handle(command, cancellationToken);

                // assert
                Assert.IsNotNull(result);
                Assert.IsTrue(result.Success);
                Assert.AreEqual(1, result.AffectedRows);
            }
        }
Exemple #2
0
        public async Task ManageRole_NormalValues_ManageItems()
        {
            // arrange
            var cancellationToken = new CancellationToken();
            var unitOfWorkFactory = new TransactionScopeUnitOfWorkFactory();
            var dbContextFactory  = new DefaultDbContextFactory();
            var generator         = new MasterDataDbGenerator(unitOfWorkFactory, dbContextFactory);
            var classifierRepositoryFactoryBuilder = new ClassifierRepositoryFactoryBuilder(dbContextFactory);
            var classifierRepositoryFactory        = classifierRepositoryFactoryBuilder.Build();
            var insertHandler = new InsertClassifierHandler(unitOfWorkFactory, classifierRepositoryFactory);
            var updateHandler = new UpdateClassifierHandler(unitOfWorkFactory, classifierRepositoryFactory);
            var deleteHandler = new DeleteClassifierHandler(unitOfWorkFactory, classifierRepositoryFactory);

            using (var _ = unitOfWorkFactory.Create())
            {
                // arrange
                generator.TypeCode = classifierRepositoryFactoryBuilder.RoleTypeCode;

                await generator.InsertType(HierarchyType.None, cancellationToken);

                // act - insert
                var insertedIds = new List <Guid>();
                for (var i = 0; i < 3; i++)
                {
                    var insertResult = await insertHandler.Handle(new InsertClassifier
                    {
                        UserUid = generator.UserUid,
                        Item    = new Role
                        {
                            Type = generator.TypeCode,
                            Code = "00" + i,
                            Name = "00" + i + " - Test Role"
                        }
                    }, cancellationToken);

                    Assert.IsNotNull(insertResult);
                    Assert.IsTrue(insertResult.Success, string.Join(",", insertResult.Errors.SelectMany(x => x.Messages)));

                    // ReSharper disable once PossibleInvalidOperationException
                    insertedIds.Add(insertResult.Uid.Value);
                }

                var searchResult = await classifierRepositoryFactory.GetNamedOrDefaultService(generator.TypeCode)
                                   .Search(new ClassifierSearchRequest {
                    TypeCode = generator.TypeCode
                }, cancellationToken);

                // assert
                Assert.IsNotNull(searchResult);
                Assert.AreEqual(insertedIds.Count, searchResult.Rows.Count);

                // act - update
                foreach (var item in searchResult.Rows.Cast <Role>())
                {
                    item.Name = item.Name.Replace("Test", "Updated");

                    var updateCommand = new UpdateClassifier
                    {
                        UserUid = generator.UserUid,
                        Item    = item
                    };

                    var updateResult = await updateHandler.Handle(updateCommand, cancellationToken);

                    Assert.IsNotNull(updateResult);
                    Assert.IsTrue(updateResult.Success, string.Join(",", updateResult.Errors.SelectMany(x => x.Messages)));
                }

                searchResult = await classifierRepositoryFactory.GetNamedOrDefaultService(generator.TypeCode)
                               .Search(new ClassifierSearchRequest {
                    TypeCode = generator.TypeCode
                }, cancellationToken);

                // assert
                Assert.IsNotNull(searchResult);
                Assert.AreEqual(insertedIds.Count, searchResult.Rows.Count);
                Assert.AreEqual(0, searchResult.Rows.Count(x => x.Name.Contains("Test")));
                Assert.AreEqual(insertedIds.Count, searchResult.Rows.Count(x => x.Name.Contains("Updated")));

                // act - delete
                var command = new DeleteClassifier
                {
                    UserUid  = generator.UserUid,
                    TypeCode = generator.TypeCode,
                    Uids     = insertedIds.ToArray()
                };

                var deleteResult = await deleteHandler.Handle(command, cancellationToken);

                // assert
                Assert.IsNotNull(deleteResult);
                Assert.IsTrue(deleteResult.Success, string.Join(",", deleteResult.Errors?.SelectMany(x => x.Messages) ?? Array.Empty <string>()));
                Assert.AreEqual(insertedIds.Count, deleteResult.AffectedRows);

                searchResult = await classifierRepositoryFactory.GetNamedOrDefaultService(generator.TypeCode)
                               .Search(new ClassifierSearchRequest {
                    TypeCode = generator.TypeCode
                }, cancellationToken);

                // assert
                Assert.IsNotNull(searchResult);
                Assert.AreEqual(0, searchResult.Rows.Count);
            }
        }
        public async Task ManageNumerator_NormalValues_ManageItems()
        {
            // arrange
            var cancellationToken           = new CancellationToken();
            var unitOfWorkFactory           = new TransactionScopeUnitOfWorkFactory();
            var dbContextFactory            = new DefaultDbContextFactory();
            var generator                   = new MasterDataDbGenerator(unitOfWorkFactory, dbContextFactory);
            var classifierRepositoryFactory = CreateClassifierRepositoryFactory(dbContextFactory);
            var insertHandler               = new InsertClassifierHandler(unitOfWorkFactory, classifierRepositoryFactory);
            var updateHandler               = new UpdateClassifierHandler(unitOfWorkFactory, classifierRepositoryFactory);
            var deleteHandler               = new DeleteClassifierHandler(unitOfWorkFactory, classifierRepositoryFactory);

            using (var _ = unitOfWorkFactory.Create())
            {
                // arrange
                generator.TypeCode = NumeratorTypeCode;

                await generator.InsertType(HierarchyType.None, cancellationToken);

                // act - insert
                var insertedIds = new List <Guid>();
                for (var i = 0; i < 5; i++)
                {
                    var insertResult = await insertHandler.Handle(new InsertClassifier
                    {
                        UserUid = generator.UserUid,
                        Item    = new Numerator
                        {
                            Type           = generator.TypeCode,
                            Code           = "00" + i,
                            Name           = "00" + i + " - Test Numerator",
                            EntityTypeCode = "DocumentType",
                            Pattern        = "{Number}"
                        }
                    }, cancellationToken);

                    Assert.IsNotNull(insertResult);
                    Assert.AreEqual(true, insertResult.Success);

                    // ReSharper disable once PossibleInvalidOperationException
                    insertedIds.Add(insertResult.Uid.Value);
                }

                var searchResult = await classifierRepositoryFactory.GetNamedOrDefaultService(generator.TypeCode)
                                   .Search(new ClassifierSearchRequest {
                    TypeCode = generator.TypeCode
                }, cancellationToken);

                // assert
                Assert.IsNotNull(searchResult);
                Assert.AreEqual(insertedIds.Count, searchResult.Rows.Count);

                // act - update
                foreach (var classifier in searchResult.Rows.Cast <Numerator>())
                {
                    classifier.Name    = classifier.Name.Replace("Test", "Updated");
                    classifier.Pattern = "No. " + classifier.Pattern;

                    var updateCommand = new UpdateClassifier
                    {
                        UserUid = generator.UserUid,
                        Item    = classifier
                    };

                    var updateResult = await updateHandler.Handle(updateCommand, cancellationToken);

                    Assert.IsNotNull(updateResult);
                    Assert.AreEqual(true, updateResult.Success);
                }

                searchResult = await classifierRepositoryFactory.GetNamedOrDefaultService(generator.TypeCode)
                               .Search(new ClassifierSearchRequest {
                    TypeCode = generator.TypeCode
                }, cancellationToken);

                // assert
                Assert.IsNotNull(searchResult);
                Assert.AreEqual(insertedIds.Count, searchResult.Rows.Count);
                Assert.AreEqual(0, searchResult.Rows.Count(x => x.Name.Contains("Test")));
                Assert.AreEqual(insertedIds.Count, searchResult.Rows.Count(x => x.Name.Contains("Updated")));
                Assert.AreEqual(insertedIds.Count, searchResult.Rows.Cast <Numerator>().Count(x => x.Pattern.Contains("No.")));

                // act - delete
                var command = new DeleteClassifier
                {
                    UserUid  = generator.UserUid,
                    TypeCode = generator.TypeCode,
                    Uids     = insertedIds.ToArray()
                };

                var result = await deleteHandler.Handle(command, cancellationToken);

                // assert
                Assert.IsNotNull(result);
                Assert.IsTrue(result.Success);
                Assert.AreEqual(insertedIds.Count, result.AffectedRows);

                searchResult = await classifierRepositoryFactory.GetNamedOrDefaultService(generator.TypeCode)
                               .Search(new ClassifierSearchRequest {
                    TypeCode = generator.TypeCode
                }, cancellationToken);

                // assert
                Assert.IsNotNull(searchResult);
                Assert.AreEqual(0, searchResult.Rows.Count);
            }
        }