Example #1
0
        public IHttpActionResult Post(Baker baker)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }

            return CreatedAtRoute("Default Api", new { id = baker.Id }, _service.AddBaker(baker));
        }
Example #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);
        }
Example #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);
        }
Example #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);
        }
        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));
        }
Example #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);
        }
        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));
        }
Example #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);
        }
Example #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);
        }
Example #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));
        }
Example #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);
        }
        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));
        }
        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);
        }
        public void BakerServicePostBakerShouldBeInvokedOnceWhenBakerControllerGetActionIsCalled()
        {
            Baker bakerToAdd = new Baker("Erik", "*****@*****.**");

            _controller.Post(bakerToAdd);

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

            Assert.IsInstanceOfType(result, typeof(BadRequestResult));
        }
        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));
        }
        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));
        }
        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);
        }
Example #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);
        }
Example #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);
        }
Example #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);
        }
Example #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);
        }
Example #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);
        }