示例#1
0
        public async Task PrimaryObjectService_UpdateAsync_InputModel_Name_WhiteSpace()
        {
            // This test verifies that the UpdateAsync will not accept invalid inputs
            var primaryObjectService = new PrimaryObjectService(_primaryObjectRepository.Object, _secondaryObjectRepository.Object, _unitOfWork.Object);
            var source = new ApiModels.PrimaryObject()
            {
                Description = "New Description",
                Name        = "     ",
            };

            _primaryObjectRepository.Setup(_ => _.GetByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync((Guid id) => new DomainModels.PrimaryObject(id)
            {
                Description      = "Description",
                Name             = "Name",
                SecondaryObjects = new List <DomainModels.SecondaryObject>()
                {
                    new DomainModels.SecondaryObject(Guid.NewGuid())
                    {
                        Description = "Description 1",
                        Name        = "Name 1",
                    },
                    new DomainModels.SecondaryObject(Guid.NewGuid())
                    {
                        Description = "Description 2",
                        Name        = "Name 2",
                    },
                },
            });

            await primaryObjectService.UpdateAsync(Guid.NewGuid(), source);
        }
示例#2
0
        public async Task PrimaryObjectService_CreateAsync_InputModel_Null()
        {
            // This test verifies that the CreateAsync will not accept null dependencies
            var primaryObjectService = new PrimaryObjectService(_primaryObjectRepository.Object, _secondaryObjectRepository.Object, _unitOfWork.Object);

            ApiModels.PrimaryObject source = null;
            var destination = await primaryObjectService.CreateAsync(source);
        }
示例#3
0
        public async Task PrimaryObjectService_UpdateAsync_InputModel_Null()
        {
            // This test verifies that the UpdateAsync will not accept null dependencies
            var primaryObjectService = new PrimaryObjectService(_primaryObjectRepository.Object, _secondaryObjectRepository.Object, _unitOfWork.Object);

            ApiModels.PrimaryObject source = null;

            await primaryObjectService.UpdateAsync(Guid.NewGuid(), source);
        }
        public async Task <ApiModels.PrimaryObject> UpdateAsync(Guid id, ApiModels.PrimaryObject inputModel)
        {
            var domainPrimaryObject = await _primaryObjectService.UpdateAsync(id, inputModel);

            Ensure.That(domainPrimaryObject, nameof(domainPrimaryObject))
            .WithException(_ => new HttpResponseException(HttpStatusCode.NotFound))
            .IsNotNull();

            return(this.Map(domainPrimaryObject));
        }
        public async Task <ApiModels.PrimaryObject> CreateAsync(ApiModels.PrimaryObject inputModel)
        {
            var domainPrimaryObject = await _primaryObjectService.CreateAsync(inputModel);

            Ensure.That(domainPrimaryObject, nameof(domainPrimaryObject))
            .WithException(_ => new HttpResponseException(HttpStatusCode.BadRequest))
            .IsNotNull();

            return(this.Map(domainPrimaryObject));
        }
        private static void Map(ApiModels.PrimaryObject source, DomainModels.PrimaryObject target)
        {
            Ensure.That(source, nameof(source)).IsNotNull();
            Ensure.That(target, nameof(target)).IsNotNull();
            Ensure.That(source.Name, $"{nameof(target)}.{nameof(ApiModels.PrimaryObject.Name)}").WithException(_ => new DemoInputValidationException()).IsNotNullOrWhiteSpace();
            Ensure.That(source.Description, $"{nameof(target)}.{nameof(ApiModels.PrimaryObject.Description)}").WithException(_ => new DemoInputValidationException()).IsNotNullOrWhiteSpace();

            target.Description = source.Description;
            target.Name        = source.Name;
        }
示例#7
0
 public async Task PrimaryObjectService_CreateAsync_InputModel_Description_Null()
 {
     // This test verifies that the CreateAsync will not accept invalid inputs
     var primaryObjectService = new PrimaryObjectService(_primaryObjectRepository.Object, _secondaryObjectRepository.Object, _unitOfWork.Object);
     var source = new ApiModels.PrimaryObject()
     {
         Description = null,
         Name        = "New Name",
     };
     var destination = await primaryObjectService.CreateAsync(source);
 }
示例#8
0
        public async Task PrimaryObjectService_UpdateAsync_Id_Empty()
        {
            // This test verifies that the UpdateAsync will not accept null dependencies
            var primaryObjectService = new PrimaryObjectService(_primaryObjectRepository.Object, _secondaryObjectRepository.Object, _unitOfWork.Object);
            var source = new ApiModels.PrimaryObject()
            {
                Description = "New Description",
                Name        = "New Name",
            };

            await primaryObjectService.UpdateAsync(Guid.Empty, source);
        }
示例#9
0
        public async Task PrimaryObjectService_UpdateAsync_NotFound()
        {
            // This test verifies that UpdateAsync throws an error when the object is not found
            var primaryObjectService = new PrimaryObjectService(_primaryObjectRepository.Object, _secondaryObjectRepository.Object, _unitOfWork.Object);
            var source = new ApiModels.PrimaryObject()
            {
                Description = "New Description",
                Name        = "New Name",
            };

            _primaryObjectRepository.Setup(_ => _.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(null as DomainModels.PrimaryObject);
            await primaryObjectService.UpdateAsync(Guid.NewGuid(), source);
        }
        public async Task <DomainModels.PrimaryObject> CreateAsync(ApiModels.PrimaryObject inputModel)
        {
            Ensure.That(inputModel, nameof(inputModel)).IsNotNull();

            var domainPrimaryObject = new DomainModels.PrimaryObject(Guid.NewGuid());

            Map(inputModel, domainPrimaryObject);
            _primaryObjectRepoistory.Add(domainPrimaryObject);

            await _unitOfWork.SaveChangesAsync();

            return(domainPrimaryObject);
        }
        public async Task PrimaryObjectsController_CreateAsync_Valid()
        {
            // This test verifies that the controller returns the correct result when IPrimaryObjectService.CreateAsync returns an object
            var primaryObjectsController = new PrimaryObjectsController(_primaryObjectService.Object);
            var sourcePrimaryObject      = new ApiModels.PrimaryObject()
            {
                Description = "Description 1",
                Name        = "Name 1",
            };

            _primaryObjectService.Setup(_ => _.CreateAsync(It.IsAny <ApiModels.PrimaryObject>())).ReturnsAsync(new DomainModels.PrimaryObject(Guid.NewGuid()));

            var destinationPrimaryObject = await primaryObjectsController.CreateAsync(sourcePrimaryObject);

            Assert.IsNotNull(destinationPrimaryObject);
        }
        public async Task <DomainModels.PrimaryObject> UpdateAsync(Guid id, ApiModels.PrimaryObject inputModel)
        {
            Ensure.That(id, nameof(id)).IsNotEmpty();
            Ensure.That(inputModel, nameof(inputModel)).IsNotNull();

            var domainPrimaryObject = await _primaryObjectRepoistory.GetByIdAsync(id);

            Ensure.That(domainPrimaryObject, nameof(domainPrimaryObject))
            .WithException(_ => GetDemoEntityNotFoundException(id))
            .IsNotNull();

            Map(inputModel, domainPrimaryObject);

            await _unitOfWork.SaveChangesAsync();

            return(domainPrimaryObject);
        }
示例#13
0
        public async Task PrimaryObjectService_CreateAsync_Valid()
        {
            // This test verifies that CreateAsync works with valid inputs
            var primaryObjectService = new PrimaryObjectService(_primaryObjectRepository.Object, _secondaryObjectRepository.Object, _unitOfWork.Object);
            var source = new ApiModels.PrimaryObject()
            {
                Description = "New Description",
                Name        = "New Name",
            };
            var destination = await primaryObjectService.CreateAsync(source);

            Assert.IsNotNull(destination);
            Assert.AreEqual(source.Description, destination.Description);
            Assert.AreNotEqual(Guid.Empty, destination.Id);
            Assert.AreEqual(source.Name, destination.Name);
            Assert.IsNotNull(destination.SecondaryObjects);
            Assert.AreEqual(0, destination.SecondaryObjects.Count);
            _unitOfWork.Verify(_ => _.SaveChangesAsync(), Times.Once);
        }
        public async Task PrimaryObjectsController_CreateAsync_NotCreated()
        {
            // This test verifies that the controller throws the correct HttpResponseException when no object is returned from IPrimaryObjectService.CreateAsync
            var primaryObjectsController = new PrimaryObjectsController(_primaryObjectService.Object);

            ApiModels.PrimaryObject sourcePrimaryObject = null;

            _primaryObjectService.Setup(_ => _.CreateAsync(It.IsAny <ApiModels.PrimaryObject>())).ReturnsAsync(null as DomainModels.PrimaryObject);

            try
            {
                await primaryObjectsController.CreateAsync(sourcePrimaryObject);

                Assert.Fail();
            }
            catch (HttpResponseException ex)
            {
                Assert.IsNotNull(ex.Response);
                Assert.AreEqual(HttpStatusCode.BadRequest, ex.Response.StatusCode);
            }
        }
示例#15
0
        public async Task PrimaryObjectService_UpdateAsync_Valid()
        {
            // This test verifies that UpdateAsync works with valid inputs
            var primaryObjectService = new PrimaryObjectService(_primaryObjectRepository.Object, _secondaryObjectRepository.Object, _unitOfWork.Object);
            var source = new ApiModels.PrimaryObject()
            {
                Description = "New Description",
                Name        = "New Name",
            };

            _primaryObjectRepository.Setup(_ => _.GetByIdAsync(It.IsAny <Guid>()))
            .ReturnsAsync((Guid id) => new DomainModels.PrimaryObject(id)
            {
                Description      = "Description",
                Name             = "Name",
                SecondaryObjects = new List <DomainModels.SecondaryObject>()
                {
                    new DomainModels.SecondaryObject(Guid.NewGuid())
                    {
                        Description = "Description 1",
                        Name        = "Name 1",
                    },
                    new DomainModels.SecondaryObject(Guid.NewGuid())
                    {
                        Description = "Description 2",
                        Name        = "Name 2",
                    },
                },
            });
            var destination = await primaryObjectService.UpdateAsync(Guid.NewGuid(), source);

            Assert.IsNotNull(destination);
            Assert.AreEqual(source.Description, destination.Description);
            Assert.AreNotEqual(Guid.Empty, destination.Id);
            Assert.AreEqual(source.Name, destination.Name);
            Assert.IsNotNull(destination.SecondaryObjects);
            _unitOfWork.Verify(_ => _.SaveChangesAsync(), Times.Once);
        }