Пример #1
0
        public void UpdateParcel_Should_Throw_Exception_When_Current_User_Is_Distributor_And_Parcel_Is_Assigned_To_Another_Distributor()
        {
            // Arrange
            var container       = ContainerMockFactory.Create();
            var securityService = container.Get <ISecurityService>();

            UserRoleMockFactory.OlesyaDistributor.Active   = true;
            UserRoleMockFactory.OlesyaPurchaser.Active     = false;
            UserRoleMockFactory.OlesyaAdministrator.Active = false;
            UserRoleMockFactory.OlesyaSeller.Active        = false;
            securityService.LogIn(UserMockFactory.Olesya.Login, EncryptServiceMockFactory.OlesyaPasswordData.Password);

            var updatedParcel =
                new DTO.Parcel
            {
                Id = ParcelMockFactory.Parcel_2.Id,
                RublesPerDollar          = 31.5m,
                PurchaserSpentOnDelivery = 149m,
                TrackingNumber           = "US867958674896Z",
                SentDate     = new DateTime(2013, 5, 31),
                Distributor  = container.Get <IDtoService>().CreateUser(UserMockFactory.Diana),
                Comments     = "Some comments",
                ReceivedDate = new DateTime(2013, 6, 30)
            };

            var parcelService = container.Get <IParcelService>();

            // Act
            // Assert
            ExceptionAssert.Throw <InvalidOperationException>(
                () => parcelService.UpdateParcel(updatedParcel),
                "Only purchaser and distributor can change parcel.");
        }
Пример #2
0
        public void UpdateParcel_Should_Update_Parcel_When_Current_User_Is_Purchaser_And_Distributor_And_Parcel_Is_Assigned_To_Another_Distributor()
        {
            // Arrange
            var container       = ContainerMockFactory.Create();
            var securityService = container.Get <ISecurityService>();

            UserRoleMockFactory.JenyaDistributor.Active   = true;
            UserRoleMockFactory.JenyaPurchaser.Active     = true;
            UserRoleMockFactory.JenyaAdministrator.Active = false;
            UserRoleMockFactory.JenyaSeller.Active        = false;
            securityService.LogIn(UserMockFactory.Jenya.Login, EncryptServiceMockFactory.JenyaPasswordData.Password);

            const decimal RUBLES_PER_DOLLAR           = 31.5m;
            const decimal PURCHASER_SPENT_ON_DELIVERY = 149m;
            const string  TRACK_NUMBER = "US867958674896Z";
            var           sentDate     = new DateTime(2013, 5, 31);
            var           distributor  = container.Get <IDtoService>().CreateUser(UserMockFactory.Diana);
            const string  COMMENTS     = "Some comments";
            var           receivedDate = ParcelMockFactory.Parcel_1.ReceivedDate;
            var           createDate   = ParcelMockFactory.Parcel_1.CreateDate;
            var           createdBy    = ParcelMockFactory.Parcel_1.CreatedBy;


            var updatedParcel =
                new DTO.Parcel
            {
                Id = ParcelMockFactory.Parcel_1.Id,
                RublesPerDollar          = RUBLES_PER_DOLLAR,
                PurchaserSpentOnDelivery = PURCHASER_SPENT_ON_DELIVERY,
                TrackingNumber           = TRACK_NUMBER,
                SentDate     = sentDate,
                Distributor  = distributor,
                Comments     = COMMENTS,
                ReceivedDate = new DateTime(2013, 6, 30)
            };

            var parcelService     = container.Get <IParcelService>();
            var persistentService = container.Get <IPersistentService>();
            var timeService       = container.Get <ITimeService>();

            // Act
            parcelService.UpdateParcel(updatedParcel);

            // Assert
            var actualParcel = persistentService.GetEntityById <DataAccess.Parcel>(ParcelMockFactory.Parcel_1.Id);

            Assert.AreEqual(RUBLES_PER_DOLLAR, actualParcel.RublesPerDollar);
            Assert.AreEqual(PURCHASER_SPENT_ON_DELIVERY, actualParcel.PurchaserSpentOnDelivery);
            Assert.AreEqual(TRACK_NUMBER, actualParcel.TrackingNumber);
            Assert.AreEqual(sentDate, actualParcel.SentDate);
            Assert.AreEqual(distributor.Id, actualParcel.DistributorId);
            Assert.AreEqual(COMMENTS, actualParcel.Comments);
            Assert.AreEqual(receivedDate, actualParcel.ReceivedDate);
            Assert.AreEqual(createDate, actualParcel.CreateDate);
            Assert.AreEqual(timeService.UtcNow, actualParcel.ChangeDate);
            Assert.AreEqual(createdBy, actualParcel.CreatedBy);
            Assert.AreEqual(UserMockFactory.Jenya, actualParcel.ChangedBy);

            Assert.AreEqual(updatedParcel, container.Get <IDtoService>().CreateParcel(actualParcel));
        }
Пример #3
0
        public void CreateParcel_Should_Throw_Exception_When_Current_User_Is_Not_Purchaser()
        {
            // Arrange
            var container       = ContainerMockFactory.Create();
            var securityService = container.Get <ISecurityService>();

            securityService.LogIn(UserMockFactory.Diana.Login, EncryptServiceMockFactory.DianaPasswordData.Password);

            var createdParcel =
                new DTO.Parcel
            {
                RublesPerDollar          = 31.5m,
                PurchaserSpentOnDelivery = 149m,
                TrackingNumber           = "US867958674896Z",
                SentDate    = new DateTime(2013, 5, 31),
                Distributor = container.Get <IDtoService>().CreateUser(UserMockFactory.Olesya),
                Comments    = "Some comments"
            };

            var parcelService = container.Get <IParcelService>();

            // Act
            // Assert
            ExceptionAssert.Throw <InvalidOperationException>(
                () => parcelService.CreateParcel(createdParcel),
                "Only purchaser can create parcel.");
        }
Пример #4
0
        public void UpdateParcel(DTO.Parcel updatedParcel)
        {
            CheckHelper.ArgumentNotNull(updatedParcel, "updatedParcel");
            CheckHelper.ArgumentWithinCondition(!updatedParcel.IsNew(), "Parcel is new.");
            Container.Get <IValidateService>().CheckIsValid(updatedParcel);

            CheckHelper.WithinCondition(SecurityService.IsLoggedIn, "User is not logged in.");
            CheckHelper.WithinCondition(
                SecurityService.IsCurrentUserPurchaser ||
                SecurityService.IsCurrentUserDistributor,
                "Only purchaser and distributor can change parcel.");

            var persistentService = Container.Get <IPersistentService>();

            var parcel = persistentService.GetEntityById <DataAccess.Parcel>(updatedParcel.Id);

            CheckHelper.NotNull(parcel, "Parcel does not exist.");
            CheckHelper.WithinCondition(
                SecurityService.IsCurrentUserPurchaser || IsCurrentUserDistributorForParcel(parcel),
                "Only purchaser and distributor can change parcel.");

            if (IsCurrentUserDistributorForParcel(parcel))
            {
                parcel.ReceivedDate = updatedParcel.ReceivedDate;
            }
            else
            {
                updatedParcel.ReceivedDate = parcel.ReceivedDate;
            }

            if (SecurityService.IsCurrentUserPurchaser)
            {
                parcel.RublesPerDollar          = updatedParcel.RublesPerDollar;
                parcel.PurchaserSpentOnDelivery = updatedParcel.PurchaserSpentOnDelivery;
                parcel.TrackingNumber           = updatedParcel.TrackingNumber;
                parcel.SentDate = updatedParcel.SentDate;
                parcel.Comments = updatedParcel.Comments;
                UpdateDistributor(parcel, updatedParcel.Distributor);
            }
            else
            {
                updatedParcel.RublesPerDollar          = parcel.RublesPerDollar;
                updatedParcel.PurchaserSpentOnDelivery = parcel.PurchaserSpentOnDelivery;
                updatedParcel.TrackingNumber           = parcel.TrackingNumber;
                updatedParcel.SentDate = parcel.SentDate;
                updatedParcel.Comments = parcel.Comments;

                updatedParcel.Distributor =
                    parcel.Distributor == null
                        ? null
                        : Container.Get <IDtoService>().CreateUser(parcel.Distributor);
            }

            parcel.UpdateTrackFields(Container);

            persistentService.SaveChanges();
        }
Пример #5
0
        public DTO.Order[] GetOrdersByParcel(DTO.Parcel parcel, string filter)
        {
            CheckHelper.ArgumentNotNull(parcel, "parcel");
            CheckHelper.ArgumentWithinCondition(!parcel.IsNew(), "Parcel is new.");

            CheckHelper.WithinCondition(SecurityService.IsLoggedIn, "User is not logged in.");
            CheckHelper.WithinCondition(
                SecurityService.IsCurrentUserSeller ||
                SecurityService.IsCurrentUserPurchaser ||
                SecurityService.IsCurrentUserDistributor,
                "Only seller, purchaser and distributor can get all orders.");

            var parcelId = parcel.Id;

            var currentParcel =
                Container
                .Get <IPersistentService>()
                .GetEntityById <DataAccess.Parcel>(parcelId);

            CheckHelper.NotNull(currentParcel, "Parcel does not exist.");

            var currentUserId = SecurityService.CurrentUser.Id;

            CheckHelper.WithinCondition(
                SecurityService.IsCurrentUserPurchaser ||
                (SecurityService.IsCurrentUserDistributor && currentParcel.Distributor != null && currentParcel.DistributorId == currentUserId) ||
                (SecurityService.IsCurrentUserSeller && currentParcel.Orders.Any(o => o.CreateUserId == currentUserId)),
                "Current user is either distributor and parcel is not assigned to him or seller and parcel does not have any order created by him.");

            var query =
                Container
                .Get <IPersistentService>()
                .GetEntitySet <DataAccess.Order>()
                .Where(o => o.Parcel != null && o.ParcelId == parcelId);

            ApplyOrderFilterCondition(ref query, filter);

            Func <DataAccess.Order, bool> predicate = null;

            if (SecurityService.IsCurrentUserSeller &&
                !SecurityService.IsCurrentUserPurchaser &&
                !(SecurityService.IsCurrentUserDistributor && currentParcel.Distributor != null && currentParcel.DistributorId == currentUserId))
            {
                query = query.Where(o => o.CreateUserId == currentUserId);

                predicate = o => o.CreateUserId == currentUserId;
            }

            var dtoService = Container.Get <IDtoService>();

            return
                (query
                 .OrderBy(o => o.Id)
                 .AsEnumerable()
                 .Select(o => dtoService.CreateOrder(o, false, predicate))
                 .ToArray());
        }
Пример #6
0
        public DTO.Parcel CreateParcel(DataAccess.Parcel parcel, bool includeOnlyActive = false, Func <DataAccess.Order, bool> predicate = null)
        {
            CheckHelper.ArgumentNotNull(parcel, "parcel");
            CheckHelper.ArgumentWithinCondition(!parcel.IsNew(), "!parcel.IsNew()");

            return
                (_dtoCache.Get(
                     parcel,
                     p =>
            {
                var result =
                    new DTO.Parcel
                {
                    Id = p.Id,
                    Comments = p.Comments,
                    RublesPerDollar = p.RublesPerDollar,
                    PurchaserSpentOnDelivery = p.PurchaserSpentOnDelivery,
                    SentDate = p.SentDate,
                    ReceivedDate = p.ReceivedDate,
                    TrackingNumber = p.TrackingNumber,
                    CreateUserId = p.CreateUserId
                };

                CopyTrackableFields(result, p);

                return result;
            },
                     (pDto, p) =>
            {
                pDto.Distributor = p.Distributor != null ? CreateUser(p.Distributor) : null;

                predicate = predicate ?? (o => true);
                pDto.Orders =
                    p.Orders
                    .Where(o => o.Active || !includeOnlyActive)
                    .Where(predicate)
                    .OrderBy(o => o.Id)
                    .Select(o => CreateOrder(o, includeOnlyActive))
                    .ToArray();
            }));
        }
Пример #7
0
        private void UpdateParcel(DataAccess.Order order, DTO.Parcel parcel)
        {
            CheckHelper.ArgumentNotNull(order, "order");

            if (parcel != null)
            {
                var parcelId = parcel.Id;
                CheckHelper.WithinCondition(parcelId > 0, "Parcel is new.");

                var persistentService = Container.Get <IPersistentService>();

                var p = persistentService.GetEntityById <DataAccess.Parcel>(parcelId);
                CheckHelper.NotNull(p, "Parcel user does not exist.");

                order.ParcelId = p.Id;
                order.Parcel   = p;
            }
            else
            {
                order.ParcelId = null;
                order.Parcel   = null;
            }
        }
Пример #8
0
        public void CreateParcel(DTO.Parcel createdParcel)
        {
            CheckHelper.ArgumentNotNull(createdParcel, "createdParcel");
            CheckHelper.ArgumentWithinCondition(createdParcel.IsNew(), "Parcel is not new.");
            Container.Get <IValidateService>().CheckIsValid(createdParcel);

            CheckHelper.WithinCondition(SecurityService.IsLoggedIn, "User is not logged in.");
            CheckHelper.WithinCondition(SecurityService.IsCurrentUserPurchaser, "Only purchaser can create parcel.");

            var persistentService = Container.Get <IPersistentService>();

            var parcel =
                new DataAccess.Parcel
            {
                RublesPerDollar          = createdParcel.RublesPerDollar,
                PurchaserSpentOnDelivery = createdParcel.PurchaserSpentOnDelivery,
                TrackingNumber           = createdParcel.TrackingNumber,
                SentDate = createdParcel.SentDate,
                Comments = createdParcel.Comments
            };

            parcel.UpdateTrackFields(Container);
            UpdateDistributor(parcel, createdParcel.Distributor);

            persistentService.Add(parcel);

            persistentService.SaveChanges();

            createdParcel.Id         = parcel.Id;
            createdParcel.CreateDate = parcel.CreateDate;
            createdParcel.CreateUser = parcel.CreatedBy.GetFullName();
            createdParcel.ChangeDate = parcel.ChangeDate;
            createdParcel.ChangeUser = parcel.ChangedBy.GetFullName();

            createdParcel.ReceivedDate = null;
        }
Пример #9
0
        public void UpdateParcel_Should_Update_Parcel_When_Current_User_Is_Distributor()
        {
            // Arrange
            var container       = ContainerMockFactory.Create();
            var securityService = container.Get <ISecurityService>();

            UserRoleMockFactory.OlesyaDistributor.Active   = true;
            UserRoleMockFactory.OlesyaPurchaser.Active     = false;
            UserRoleMockFactory.OlesyaAdministrator.Active = false;
            UserRoleMockFactory.OlesyaSeller.Active        = false;
            securityService.LogIn(UserMockFactory.Olesya.Login, EncryptServiceMockFactory.OlesyaPasswordData.Password);

            var rublesPerDollar          = ParcelMockFactory.Parcel_1.RublesPerDollar;
            var purchaserSpentOnDelivery = ParcelMockFactory.Parcel_1.PurchaserSpentOnDelivery;
            var trackingNumber           = ParcelMockFactory.Parcel_1.TrackingNumber;
            var sentDate      = ParcelMockFactory.Parcel_1.SentDate;
            var distributorId = ParcelMockFactory.Parcel_1.DistributorId;
            var comments      = ParcelMockFactory.Parcel_1.Comments;
            var createDate    = ParcelMockFactory.Parcel_1.CreateDate;
            var createdBy     = ParcelMockFactory.Parcel_1.CreatedBy;

            var receivedDate = new DateTime(2013, 6, 30);

            var updatedParcel =
                new DTO.Parcel
            {
                Id           = ParcelMockFactory.Parcel_1.Id,
                ReceivedDate = receivedDate,

                // Filled by Purchaser
                RublesPerDollar          = 31.5m,
                PurchaserSpentOnDelivery = 149m,
                TrackingNumber           = "US867958674896Z",
                SentDate    = new DateTime(2013, 5, 31),
                Distributor = container.Get <IDtoService>().CreateUser(UserMockFactory.Diana),
                Comments    = "Some comments"
            };

            var parcelService     = container.Get <IParcelService>();
            var persistentService = container.Get <IPersistentService>();
            var timeService       = container.Get <ITimeService>();

            // Act
            parcelService.UpdateParcel(updatedParcel);

            // Assert
            var actualParcel = persistentService.GetEntityById <DataAccess.Parcel>(ParcelMockFactory.Parcel_1.Id);

            Assert.AreEqual(rublesPerDollar, actualParcel.RublesPerDollar);
            Assert.AreEqual(purchaserSpentOnDelivery, actualParcel.PurchaserSpentOnDelivery);
            Assert.AreEqual(trackingNumber, actualParcel.TrackingNumber);
            Assert.AreEqual(sentDate, actualParcel.SentDate);
            Assert.AreEqual(distributorId, actualParcel.DistributorId);
            Assert.AreEqual(comments, actualParcel.Comments);
            Assert.AreEqual(receivedDate, actualParcel.ReceivedDate);
            Assert.AreEqual(createDate, actualParcel.CreateDate);
            Assert.AreEqual(timeService.UtcNow, actualParcel.ChangeDate);
            Assert.AreEqual(createdBy, actualParcel.CreatedBy);
            Assert.AreEqual(UserMockFactory.Olesya, actualParcel.ChangedBy);

            Assert.AreEqual(updatedParcel, container.Get <IDtoService>().CreateParcel(actualParcel));
        }