public async Task GetBottleAsync_Returns200ContainingBottleData_WhenServiceFindsBottle()
        {
            //Arrange
            var domainBottleReturnedByService = new BottleDomainModel
            {
                BottleId = "bottleId"
            };

            var expectedApiBottle = new BottleApiModel
            {
                BottleId = "bottleId"
            };

            _mockBottleService.Setup(
                c => c.GetBottleAsync(It.IsAny <string>()))
            .ReturnsAsync(domainBottleReturnedByService);

            var bottlesController = new BottlesController(_mockBottleService.Object,
                                                          _mockValidator.Object,
                                                          _toApiModelMapper,
                                                          _toDomainModelMapper);

            //Act
            var actionResult = await bottlesController.GetBottleAsync("bottleId");

            var objectResult = actionResult as OkObjectResult;
            var actualBottle = objectResult.Value as BottleApiModel;

            //Assert
            Assert.IsInstanceOf(typeof(OkObjectResult), actionResult);
            Assert.IsNotNull(actualBottle);
            Assert.AreEqual(expectedApiBottle.BottleId, actualBottle.BottleId);
        }
        public async Task PostBottleAsync_Returns201ContainingPostedBottle_WhenValidBottlePosted()
        {
            //Arrange
            var domainBottleReturnedByService = new BottleDomainModel {
                BottleId = "bottleId", Name = "myBottle"
            };

            _mockBottleService.Setup(
                c => c.PostBottleAsync(It.IsAny <BottleDomainModel>()))
            .ReturnsAsync(domainBottleReturnedByService);

            var bottlesController = new BottlesController(_mockBottleService.Object,
                                                          _mockValidator.Object,
                                                          _toApiModelMapper,
                                                          _toDomainModelMapper);

            var apiBottleToPost = new BottleApiModel {
                BottleId = "bottleId", Name = "myBottle"
            };

            //Act
            var actionResult = await bottlesController.PostBottleAsync(apiBottleToPost);

            var createdResult   = actionResult as CreatedResult;
            var createdLocation = createdResult.Location;
            var createdBottle   = createdResult.Value as BottleApiModel;

            //Assert
            Assert.IsInstanceOf(typeof(CreatedResult), actionResult);
            Assert.AreEqual($"api/bottles/{apiBottleToPost.BottleId}", createdLocation);
            Assert.AreEqual(apiBottleToPost.Name, createdBottle.Name);
        }
        public async Task UpdateBottleAsync_Returns200ContainingBottleData_WhenServiceFindsAndUpdatesBottle()
        {
            //Arrange
            var apiBottleToPassController = new BottleApiModel {
                BottleId = "bottleId", Name = "bottleName"
            };
            var domainBottleReturnedByService = new BottleDomainModel {
                BottleId = "bottleId", Name = "bottleName"
            };

            _mockBottleService.Setup(
                c => c.GetBottleAsync(It.IsAny <string>()))
            .ReturnsAsync(domainBottleReturnedByService);

            var bottlesController = new BottlesController(_mockBottleService.Object,
                                                          _mockValidator.Object,
                                                          _toApiModelMapper,
                                                          _toDomainModelMapper);

            //Act
            var actionResult = await bottlesController.UpdateBottleAsync("bottleId", apiBottleToPassController);

            var okResult     = actionResult as OkObjectResult;
            var actualBottle = okResult.Value as BottleApiModel;

            //Assert
            Assert.IsInstanceOf(typeof(OkObjectResult), actionResult);
            Assert.IsInstanceOf(typeof(BottleApiModel), actualBottle);
            Assert.AreEqual(apiBottleToPassController.Name, actualBottle.Name);
        }
        public void MapOne_ReturnsNull_IfNullParamPassed()
        {
            //Arrange
            BottleApiModel fromBottle = null;

            //Act
            var result = _mapper.MapOne(fromBottle);

            //Assert
            Assert.IsNull(result);
        }
        public void MapOne_ParsesCategoryInNonCaseSensitiveWay()
        {
            //Arrange
            var apiBottle = new BottleApiModel {
                AlcoholCategory = "whIsKy"
            };

            //Act
            var result = _mapper.MapOne(apiBottle);

            //Assert
            Assert.AreEqual(AlcoholCategory.Whisky, result.AlcoholCategory);
        }
        public void MapOne_ReturnsBottleDomainModel_WithParsedAlcoholEnum_WhenCategoryCanBeParsed()
        {
            //Arrange
            var apiBottle = new BottleApiModel {
                AlcoholCategory = "Whisky"
            };

            //Act
            var result = _mapper.MapOne(apiBottle);

            //Assert
            Assert.AreEqual(AlcoholCategory.Whisky, result.AlcoholCategory);
        }
        public void MapOne_ReturnsBottleDomainModel_WithUnknownAlcoholCategory_WhenCategoryCantbeParsed()
        {
            //Arrange
            var apiBottle = new BottleApiModel {
                AlcoholCategory = "Unparseable Category"
            };

            //Act
            var result = _mapper.MapOne(apiBottle);

            //Assert
            Assert.AreEqual(AlcoholCategory.Unknown, result.AlcoholCategory);
        }
        public void MapOne_ReturnsBottleDomainModelWithCorrectId_WhenApiBottleMapped()
        {
            //Arrange
            var apiBottle = new BottleApiModel {
                BottleId = "507f1f77bcf86cd799439011"
            };

            //Act
            var result = _mapper.MapOne(apiBottle);

            //Assert
            Assert.AreEqual(apiBottle.BottleId, result.BottleId);
        }
        public void MapOne_ReturnsBottleDomainModelWithCorrectRegion_WhenApiBottleMapped()
        {
            //Arrange
            var apiBottle = new BottleApiModel {
                Region = "bottleRegion"
            };

            //Act
            var result = _mapper.MapOne(apiBottle);

            //Assert
            Assert.AreEqual(apiBottle.Region, result.Region);
        }
        public void MapOne_ReturnsBottleDomainModelWithCorrectName_WhenApiBottleMapped()
        {
            //Arrange
            var apiBottle = new BottleApiModel {
                Name = "bottleName"
            };

            //Act
            var result = _mapper.MapOne(apiBottle);

            //Assert
            Assert.AreEqual(apiBottle.Name, result.Name);
        }
Example #11
0
        public async Task <IActionResult> PostBottleAsync(BottleApiModel apiBottle)
        {
            //If the bottle data passed into the call is valid, map it to a domain Bottle model so it can be passed into the domain service.
            await _bottleValidator.ValidateAndThrowAsync(apiBottle);

            var domainBottle = _toDomainModelMapper.MapOne(apiBottle);

            var servicePostResult = await this._bottleService.PostBottleAsync(domainBottle);

            //If service returned null, the Bottle object has not been successfully created, so return a 400.
            if (servicePostResult != null)
            {
                //Map the result that was returned from the service back to an API model, so it can be sent back out of the API in the response.
                var apiBottleToReturn = _toApiModelMapper.MapOne(servicePostResult);
                return(Created($"api/bottles/{apiBottleToReturn.BottleId}", apiBottleToReturn));
            }
            return(BadRequest("Cannot insert duplicate record."));
        }
Example #12
0
        public async Task <IActionResult> UpdateBottleAsync(string bottleId, BottleApiModel apiBottle)
        {
            apiBottle.BottleId = bottleId;
            await _bottleValidator.ValidateAndThrowAsync(apiBottle);

            var domainBottle = _toDomainModelMapper.MapOne(apiBottle);


            //If service returns null, no Bottle has been found matching the passed id, so return a 404.
            if (await this._bottleService.GetBottleAsync(bottleId) == null)
            {
                return(NotFound(bottleId));
            }

            await this._bottleService.UpdateBottleAsync(bottleId, domainBottle);

            //Return API Bottle that was passed
            return(Ok(apiBottle));
        }