Example #1
0
        public async Task GetByIdAsync_ReturnsDomainBottle_WhenBottleReturnedFromCollection()
        {
            //Arrange

            //Create the Id of the object to retrieve from the mock collection
            var idString = "507f1f77bcf86cd799439011";
            var objectId = new ObjectId(idString);

            var mongoBottleReturnedByCollection = new BottleMongoModel {
                BottleId = objectId, Name = "bottleName"
            };

            var mockCursor = CreateAndSetUpMockCursor(new List <BottleMongoModel> {
                mongoBottleReturnedByCollection
            });
            var mockCollection = CreateAndSetupMockMongoCollection(mockCursor.Object);

            _mockDbContext.SetupGet(c => c.Collection).Returns(mockCollection.Object);


            //Setup the domain Bottle we expect to be returned by the method
            var expectedDomainBottle = new BottleDomainModel {
                BottleId = idString, Name = "bottleName"
            };

            var repo = new BottleMongoRepository(_mockDbContext.Object, _toMongoModelMapper, _toDomainModelMapper);

            //Act
            var result = await repo.GetByIdAsync("507f1f77bcf86cd799439010");

            //Assert
            Assert.AreEqual(expectedDomainBottle.BottleId, result.BottleId);
            Assert.AreEqual(expectedDomainBottle.Name, result.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);
        }
Example #3
0
        public async Task UpdateAsync_CallsReplaceOneAsyncOnCollectionOnce_WhenValidIdAndNonNullEntityPassed()
        {
            //Arrange
            //Create the BottleDomainModel that we will insert into the repo. This same entity should be returned by the method on a successful run.
            var bottle = new BottleDomainModel {
                BottleId = "507f1f77bcf86cd799439010", AlcoholCategory = AlcoholCategory.Whisky
            };

            var mockCollection = new Mock <IMongoCollection <BottleMongoModel> >();

            _mockDbContext.SetupGet(c => c.Collection).Returns(mockCollection.Object);

            var repo = new BottleMongoRepository(_mockDbContext.Object, _toMongoModelMapper, _toDomainModelMapper);

            //Act
            await repo.UpdateAsync("507f1f77bcf86cd799439010", bottle);

            //Assert
            //Verify that the db context Collection's InsertoneAsync method is called exactly once.
            //Any more or less will throw meaning the test will fail.
            mockCollection.Verify(c => c.ReplaceOneAsync(
                                      It.IsAny <FilterDefinition <BottleMongoModel> >(),
                                      It.IsAny <BottleMongoModel>(),
                                      It.IsAny <ReplaceOptions>(),
                                      It.IsAny <CancellationToken>()), Times.Once());
        }
        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);
        }
Example #5
0
        public async Task InsertAsync_CallsInsertOneAsyncOnCollectionOnce_ThenReturnsEntityThatWasPassed()
        {
            //Arrange
            //Create the BottleDomainModel that we will insert into the repo. This same entity should be returned by the method on a successful run.
            var bottle = new BottleDomainModel {
                BottleId = "507f1f77bcf86cd799439010", AlcoholCategory = AlcoholCategory.Whisky
            };

            var mockCollection = new Mock <IMongoCollection <BottleMongoModel> >();

            _mockDbContext.SetupGet(c => c.Collection).Returns(mockCollection.Object);

            var repo = new BottleMongoRepository(_mockDbContext.Object, _toMongoModelMapper, _toDomainModelMapper);

            //Act
            var result = await repo.InsertAsync(bottle);

            //Assert
            //Verify that the db context Collection's InsertoneAsync method is called exactly once.
            //Any more or less will throw meaning the test will fail.
            mockCollection.Verify(c => c.InsertOneAsync(
                                      It.IsAny <BottleMongoModel>(),
                                      It.IsAny <InsertOneOptions>(),
                                      It.IsAny <CancellationToken>()), Times.Once());

            Assert.AreEqual(bottle.BottleId, result.BottleId);
            Assert.AreEqual(bottle.AlcoholCategory, result.AlcoholCategory);
        }
        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 void MapOne_ReturnsNull_IfNullParamPassed()
        {
            //Arrange
            BottleDomainModel fromBottle = null;

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

            //Assert
            Assert.IsNull(result);
        }
        public void MapOne_ReturnsBottleApiModelWithCorrectName_WhenDomainBottleMapped()
        {
            //Arrange
            BottleDomainModel fromBottle = new BottleDomainModel {
                Name = "bottleName"
            };

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

            //Assert
            Assert.AreEqual("bottleName", result.Name);
        }
        public void MapOne_CorrectlyTranslatesAlcoholCategoryToString()
        {
            //Arrange
            BottleDomainModel fromBottle = new BottleDomainModel {
                AlcoholCategory = AlcoholCategory.Whisky
            };

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

            //Assert
            Assert.AreEqual("Whisky", result.AlcoholCategory);
        }
        public void Map_ReturnsMongoBottleWithEmptyObjectId_WhenDomainIdNotParseable()
        {
            //Arrange
            var fromBottle = new BottleDomainModel {
                BottleId = "bottleId"
            };

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

            //Assert
            Assert.AreEqual(ObjectId.Empty, result.BottleId);
        }
        public void MapOne_ReturnsBottleApiModelWithCorrectRegion_WhenDomainBottleMapped()
        {
            //Arrange
            BottleDomainModel fromBottle = new BottleDomainModel {
                Region = "bottleRegion"
            };

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

            //Assert
            Assert.AreEqual("bottleRegion", result.Region);
        }
        public void Map_ReturnsMongoBottleWithRegionPropertySet_WhenBottleWithNonNullRegionPassed()
        {
            //Arrange
            var expectedBottleRegion = "bottleRegion";
            var fromBottle           = new BottleDomainModel {
                Region = expectedBottleRegion
            };

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

            //Assert
            Assert.AreEqual(expectedBottleRegion, result.Region);
        }
        public void Map_ReturnsMongoBottleWithNamePropertySet_WhenBottleWithNonNullNamePassed()
        {
            //Arrange
            var expectedBottleName = "bottleName";
            var fromBottle         = new BottleDomainModel {
                Name = expectedBottleName
            };

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

            //Assert
            Assert.AreEqual(expectedBottleName, result.Name);
        }
        public void Map_ReturnsMongoBottleWithAlcoholCategorySet_WhenBottleWithNonNullCategoryPassed()
        {
            //Arrange
            var expectedAlcoholCategory = AlcoholCategory.Whisky;
            var fromBottle = new BottleDomainModel {
                AlcoholCategory = expectedAlcoholCategory
            };

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

            //Assert
            Assert.AreEqual(expectedAlcoholCategory, result.AlcoholCategory);
        }
        public void Map_ReturnsMongoBottleWithValidObjectId_WhenDomainIdParseable()
        {
            //Arrange
            var idAsString = "507f1f77bcf86cd799439011";
            var fromBottle = new BottleDomainModel {
                BottleId = idAsString
            };

            var expectedObjectId = new ObjectId(idAsString);

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

            //Assert
            Assert.AreEqual(expectedObjectId, result.BottleId);
        }
        public async Task GetBottleAsync_ReturnsResultReturnedByRepository_WhenNotNull()
        {
            //Arrange
            var bottleToReturn = new BottleDomainModel {
                BottleId = "bottleId"
            };

            _mockRepository.Setup(
                c => c.GetByIdAsync(It.IsAny <string>()))
            .ReturnsAsync(bottleToReturn);

            var bottleService = new Domain.Services.BottleService(_mockRepository.Object);

            //Act
            var result = await bottleService.GetBottleAsync("bottleId");

            //Assert
            Assert.AreEqual(bottleToReturn.BottleId, result.BottleId);
        }
Example #17
0
 /// <summary>
 /// Updates an existing Bottle in the repository.
 /// </summary>
 /// <param name="bottleId">The id of the Bottle to update.</param>
 /// <param name="bottle">The new Bottle to associate to the given id.</param>
 public async Task UpdateBottleAsync(string bottleId, BottleDomainModel bottle)
 {
     await this._repository.UpdateAsync(bottleId, bottle);
 }
Example #18
0
 /// <summary>
 /// Posts a new Bottle to the repository.
 /// </summary>
 /// <param name="bottle">The Bottle object to post.</param>
 /// <returns>Task of Bottle containing the Bottle that has been posted.</returns>
 public async Task <BottleDomainModel> PostBottleAsync(BottleDomainModel bottle)
 {
     return(await this._repository.InsertAsync(bottle));
 }