Пример #1
0
        public IHttpActionResult Post(Baker baker)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }

            return CreatedAtRoute("Default Api", new { id = baker.Id }, _service.AddBaker(baker));
        }
Пример #2
0
        public void ServiceAddBakerShouldReturnABaker()
        {
            Baker bakerToAdd = new Baker("Andreas", "*****@*****.**");
            _bakerMock
                .Setup(bakerRepository => bakerRepository.AddBaker(bakerToAdd))
                .Returns(bakerToAdd);

            Baker result = _bakerService.AddBaker(bakerToAdd);

            Assert.AreSame(bakerToAdd, result);
        }
Пример #3
0
        public void BakerNamePropertyDoesNotAllowEmtyStrings()
        {
            string emtyName = "";
            string validEmail = "*****@*****.**";
            Baker baker = new Baker(emtyName, validEmail);

            List<ValidationResult> validationResults = new List<ValidationResult>();
            bool actual = Validator.TryValidateObject(baker, new ValidationContext(baker), validationResults, true);

            Assert.IsFalse(actual);
            Assert.AreEqual(1, validationResults.Count);
        }
Пример #4
0
        public void BakerEmailPropertyDoesNotAllowStringsShorterThenFourChracters()
        {
            string validName = "Olle";
            string toShortEmail = "a@s";
            Baker baker = new Baker(validName, toShortEmail);

            List<ValidationResult> validationResults = new List<ValidationResult>();
            bool actual = Validator.TryValidateObject(baker, new ValidationContext(baker), validationResults, true);

            Assert.IsFalse(actual);
            Assert.AreEqual(1, validationResults.Count);
        }
Пример #5
0
        public void BakerControllerActionDeleteReturnsStatusCodeOkIfBakerToDeleteExists()
        {
            int existingId = 1;
            Baker existingBaker = new Baker("Erik", "*****@*****.**");
            _service
                .Setup(service => service.DeleteBaker(existingId))
                .Returns(existingBaker);

            IHttpActionResult result = _controller.Delete(existingId);

            Assert.IsInstanceOfType(result, typeof(OkResult));
        }
Пример #6
0
        public void BakerNamePropertyDoesNotAllowStringsLongerThenThanTwentyChars()
        {
            string toLongName = "abcdefghijklmnopqrstuvxyzåäö";
            string validEmail = "*****@*****.**";
            Baker baker = new Baker(toLongName, validEmail);

            List<ValidationResult> validationResults = new List<ValidationResult>();
            bool actual = Validator.TryValidateObject(baker, new ValidationContext(baker), validationResults, true);

            Assert.IsFalse(actual);
            Assert.AreEqual(1, validationResults.Count);
        }
Пример #7
0
        public void BakerControllerActionDeleteReturnsNotFoundOnNonExistingBaker()
        {
            int existingId = 1;
            int nonExistingId = 2;
            Baker existingBaker = new Baker("Erik", "*****@*****.**");
            _service
                .Setup(service => service.DeleteBaker(existingId))
                .Returns(existingBaker);

            IHttpActionResult result = _controller.Delete(nonExistingId);

            Assert.IsInstanceOfType(result, typeof(NotFoundResult));
        }
Пример #8
0
        public void BakerEmailPropertyDoesNotAllowMoreThen254Characters()
        {
            string validName = "Olle";
            string toLongEmail = String.Format
                ("MoreThen254CharactersMoreThen254CharactersMoreThen254CharactersMoreThen254CharactersMoreThen254CharactersMoreThen254CharactersMoreThen254CharactersMoreThen254CharactersMoreThen254CharactersMoreThen254CharactersMoreThen254CharactersMoreThen254CharactersMoR");
            Baker baker = new Baker(validName, toLongEmail);

            List<ValidationResult> validationResults = new List<ValidationResult>();
            bool actual = Validator.TryValidateObject(baker, new ValidationContext(baker), validationResults, true);

            Assert.IsFalse(actual);
            Assert.AreEqual(1, validationResults.Count);
        }
Пример #9
0
        public void BakerRepositoryDeleteBakerShouldBeInvokedOnceWhenBakerServiceDeleteMethodIsCalled()
        {
            int existingId = 1;
            Baker existingBaker = new Baker("Erik", "*****@*****.**");
            existingBaker.Id = existingId;

            _bakerMock
            .Setup(bakerRepository => bakerRepository.GetBaker(existingId))
            .Returns(existingBaker);

            _bakerService.DeleteBaker(existingId);

            _bakerMock.Verify(bakerRepository => bakerRepository.DeleteBaker(existingBaker), Times.Once);
        }
Пример #10
0
        public IHttpActionResult Put(Baker baker)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }

            Baker existingBaker = _service.GetBaker(baker.Id);
            if(existingBaker == null)
            {
                return NotFound();
            }
            return Ok(_service.PutBaker(baker.Id));
        }
Пример #11
0
        public void BakerRepositoryPutMethodShouldBeInvokedOnceWhenBakerServicePutMethodIsCalled()
        {
            int existingId = 1;
            Baker existingBaker = new Baker("Andreas", "*****@*****.**");
            existingBaker.Id = existingId;

            _bakerMock
                .Setup(bakerRepository => bakerRepository.GetBaker(existingId))
                .Returns(existingBaker);

            _bakerService.PutBaker(existingId);

            _bakerMock.Verify(bakerRepository => bakerRepository.PutBaker(existingBaker), Times.Once);
        }
Пример #12
0
        public void BakerControllerGetBakerActionShouldReturnABakerAsContentForExistingIdAndStatusCodeOk()
        {
            int existingBakerId = 25;
            Baker expectedBaker = new Baker("David", "*****@*****.**");
            _service
                .Setup(service => service.GetBaker(existingBakerId))
                .Returns(expectedBaker);

            var baker = _controller.Get(existingBakerId) as OkNegotiatedContentResult<Baker>;

            // Assert that status code is 200 Ok
            Assert.IsNotNull(baker);
            Assert.IsNotNull(baker.Content);
            Assert.IsInstanceOfType(baker.Content, typeof(Baker));
        }
Пример #13
0
        public void BakerServicePutBakerShouldBeInvokedOnceWhenBakerControllerPutActionIsCalled()
        {
            Baker baker = new Baker("David", "*****@*****.**");
            _service.Setup(service => service.GetBaker(It.IsAny<int>())).Returns(baker);
            _controller.Put(baker);

            _service
                .Verify(service => service.PutBaker(It.IsAny<int>()), Times.Once);
        }
Пример #14
0
        public void BakerServicePostBakerShouldBeInvokedOnceWhenBakerControllerGetActionIsCalled()
        {
            Baker bakerToAdd = new Baker("Erik", "*****@*****.**");

            _controller.Post(bakerToAdd);

            _service
                .Verify(service => service.AddBaker(bakerToAdd), Times.Once);
        }
Пример #15
0
        public void BakerControllerPutShouldReturnBadRequestIfModelStateIsNotValid()
        {
            Baker baker = new Baker("Andreas", "*****@*****.**");
            _controller.ModelState.AddModelError("", "Error");
            IHttpActionResult result = _controller.Put(baker);

            Assert.IsInstanceOfType(result, typeof(BadRequestResult));
        }
Пример #16
0
        public void BakerControllerPutMethodShouldReturnABakerAsContentForNonExistingIdAndStatusCodeNotFound()
        {
            int existingId = 1;
            int nonExistingId = 2;
            Baker existingBaker = new Baker("David", "*****@*****.**");
            existingBaker.Id = nonExistingId;
            Baker updatedBaker = new Baker("David Grenmyr", "*****@*****.**");
            updatedBaker.Id = existingId;
            _service
                .Setup(service => service.GetBaker(existingId))
                .Returns(existingBaker);
            _service
                .Setup(service => service.PutBaker(existingId))
                .Returns(updatedBaker);

            IHttpActionResult result = _controller.Put(existingBaker);
            Assert.IsInstanceOfType(result, typeof(NotFoundResult));
        }
Пример #17
0
        public void BakerControllerPutMethodShouldReturnABakerAsContentForExistingIdAndStatusCodeOk()
        {
            int existingId = 1;
            Baker existingBaker = new Baker("David", "*****@*****.**");
            existingBaker.Id = existingId;
            Baker updatedBaker = new Baker("David Grenmyr", "*****@*****.**");
            updatedBaker.Id = existingId;
            _service
                .Setup(service => service.GetBaker(existingId))
                .Returns(existingBaker);
            _service
                .Setup(service => service.PutBaker(existingId))
                .Returns(updatedBaker);

            var baker = _controller.Put(existingBaker) as OkNegotiatedContentResult<Baker>;

            // Assert that status code is 200 Ok
            Assert.IsNotNull(baker);
            Assert.IsNotNull(baker.Content);
            Assert.IsInstanceOfType(baker.Content, typeof(Baker));
        }
Пример #18
0
        public void BakerControllerPostActionShouldReturnPostedBakerAsContentAndStatusCodeCreatedIfCreateIsSuccessful()
        {
            Baker bakerToAdd = new Baker("Andreas", "*****@*****.**");
            _service
                .Setup(service => service.AddBaker(bakerToAdd))
                .Returns(bakerToAdd);

            var baker = _controller.Post(bakerToAdd) as CreatedAtRouteNegotiatedContentResult<Baker>;

            Assert.IsNotNull(baker);
            Assert.IsNotNull(baker.Content);
            //TODO: Break out validation of data to two seperate tests.
            Assert.AreSame(bakerToAdd, baker.Content);
            Assert.AreEqual(bakerToAdd.Email, baker.Content.Email);
        }
Пример #19
0
        public void ServiceGetBakerMethodShouldReturnBakerForExistingId()
        {
            int existingBakerId = 20;
            Baker expectedBaker = new Baker("David", "*****@*****.**");
            _bakerMock
                .Setup(service => service.GetBaker(existingBakerId))
                .Returns(expectedBaker);

            var baker = _bakerService.GetBaker(existingBakerId);

            Assert.AreSame(expectedBaker, baker);
        }
Пример #20
0
        public void ServicePutBakerMethodShouldReturnUpdatedBakerForExistingIdOrNullForNonExistingId()
        {
            int existingId = 1;
            int nonExistingId = 2;
            Baker existingBaker = new Baker("Andreas", "*****@*****.**");
            Baker updatedBaker = new Baker("Andreas Fridlund", "*****@*****.**");
            existingBaker.Id = existingId;
            _bakerMock
                .Setup(bakerRepository => bakerRepository.GetBaker(existingId))
                .Returns(existingBaker);

            _bakerMock
                .Setup(bakerRepository => bakerRepository.PutBaker(existingBaker))
                .Returns(updatedBaker);

            Baker baker = _bakerService.PutBaker(existingId);
            Baker nullValue = _bakerService.PutBaker(nonExistingId);

            Assert.AreSame(updatedBaker, baker);
            Assert.IsNull(nullValue);
        }
Пример #21
0
        public void ServiceDeleteBakerMethodReturnsBakerForExistingId()
        {
            int existingId = 1;
            Baker expectedBaker = new Baker("Erik", "*****@*****.**");
            expectedBaker.Id = existingId;
            _bakerMock
                .Setup(bakerRepository => bakerRepository.GetBaker(existingId))
                .Returns(expectedBaker);

            Baker baker = _bakerService.DeleteBaker(existingId);

            Assert.AreSame(expectedBaker, baker);
        }
Пример #22
0
        public void BakerShouldSetValidBaker()
        {
            string validName = "Olle";
            string validEmail = "*****@*****.**";
            Baker baker = new Baker(validName, validEmail);

            List<ValidationResult> validationResults = new List<ValidationResult>();
            bool actual = Validator.TryValidateObject(baker, new ValidationContext(baker), validationResults, true);

            Assert.IsTrue(actual);
            Assert.AreEqual(0, validationResults.Count);
        }
Пример #23
0
        public void ServiceDeleteBakerMethodReturnsNullForNonExistingId()
        {
            int existingId = 1;
            int nonExistingId = 2;
            Baker existingBaker = new Baker("Erik", "*****@*****.**");
            existingBaker.Id = existingId;
            _bakerMock
                .Setup(bakerRepository => bakerRepository.GetBaker(existingId))
                .Returns(existingBaker);

            Baker baker = _bakerService.DeleteBaker(nonExistingId);

            Assert.IsNull(baker);
            Assert.AreNotSame(existingBaker, baker);
        }