예제 #1
0
        public void Inquiry_WhenCalled__WithValidIdAndEmail_MustReturnsCustomer()
        {
            //Arrange
            var mockCustomerService = new Mock <ICustomerService>();

            var validId    = 1;
            var validEmail = "*****@*****.**";
            var customer   = new Customer {
                CustomerId = validId, Name = "Customer 1", Email = validEmail, MobileNo = "0891234567"
            };

            mockCustomerService.Setup(s => s.GetCustomerByIdAndEmail(validId, validEmail)).Returns(customer);

            CustomersController controller = new CustomersController(mockCustomerService.Object);

            var criteria = new CustomerInquiryRequestMessage {
                CustomerID = validId.ToString(), Email = validEmail
            };

            //Act
            var action = controller.Inquiry(criteria);
            var result = action.Result as OkObjectResult;

            var actual   = result?.Value as Customer;
            var expected = customer;

            //Assert
            Assert.Equal(expected, actual);
        }
예제 #2
0
        public void Inquiry_WhenCalled__WithInvalidEmail_MustReturnsErrorResponseMessage_InvalidEmail()
        {
            //Arrange
            var mockCustomerService = new Mock <ICustomerService>();

            var validId    = 1;
            var validEmail = "*****@*****.**";
            var customer   = new Customer {
                CustomerId = validId, Name = "Customer 1", Email = validEmail, MobileNo = "0891234567"
            };

            mockCustomerService.Setup(s => s.GetCustomerByIdAndEmail(validId, validEmail)).Returns(customer);

            CustomersController controller = new CustomersController(mockCustomerService.Object);

            var invalidEmail = "invalid@email";
            var criteria     = new CustomerInquiryRequestMessage {
                Email = invalidEmail
            };

            //Act
            var action           = controller.Inquiry(criteria);
            var badRequestResult = action.Result as BadRequestObjectResult;

            var actual   = badRequestResult?.Value as ErrorResponseMessage;
            var expected = new ErrorResponseMessage {
                Message = "Invalid Email"
            };

            //Assert
            Assert.Equal(expected.Message, actual?.Message);
        }
예제 #3
0
        public void Inquiry_WhenCalled__WithInvalidEmail_MustReturnsBadRequestObjectResult()
        {
            //Arrange
            var mockCustomerService = new Mock <ICustomerService>();

            var validId    = 1;
            var validEmail = "*****@*****.**";
            var customer   = new Customer {
                CustomerId = validId, Name = "Customer 1", Email = validEmail, MobileNo = "0891234567"
            };

            mockCustomerService.Setup(s => s.GetCustomerByIdAndEmail(validId, validEmail)).Returns(customer);

            CustomersController controller = new CustomersController(mockCustomerService.Object);

            var invalidEmail = "invalid@email";
            var criteria     = new CustomerInquiryRequestMessage {
                Email = invalidEmail
            };

            //Act
            var actual = controller.Inquiry(criteria);

            //Assert
            Assert.IsType <BadRequestObjectResult>(actual.Result);
        }
예제 #4
0
        public void Inquiry_WhenCalled__WithEmptyRequestParameter_MustReturnsErrorResponseMessage_NoInquiryCriteria()
        {
            //Arrange
            var mockCustomerService        = new Mock <ICustomerService>();
            CustomersController controller = new CustomersController(mockCustomerService.Object);

            var criteria = new CustomerInquiryRequestMessage();

            //Act
            var action           = controller.Inquiry(criteria);
            var badRequestResult = action.Result as BadRequestObjectResult;

            var actual   = badRequestResult?.Value as ErrorResponseMessage;
            var expected = new ErrorResponseMessage {
                Message = "No inquiry criteria"
            };

            //Assert
            Assert.Equal(expected.Message, actual?.Message);
        }
예제 #5
0
        public void Inquiry_WhenCalled__WithNotFoundEmailOnly_MustReturnsNotFound()
        {
            //Arrange
            var mockCustomerService = new Mock <ICustomerService>();

            var notFoundEmail = "*****@*****.**";

            mockCustomerService.Setup(s => s.GetCustomerByEmail(notFoundEmail)).Returns(() => null);

            CustomersController controller = new CustomersController(mockCustomerService.Object);
            var criteria = new CustomerInquiryRequestMessage {
                Email = notFoundEmail
            };

            //Act
            var actual = controller.Inquiry(criteria);

            //Assert
            Assert.IsType <NotFoundObjectResult>(actual.Result);
        }
예제 #6
0
        public void Inquiry_WhenCalled__WithNotFoundIdOnly_MustReturnsNotFound()
        {
            //Arrange
            var mockCustomerService = new Mock <ICustomerService>();

            var notFoundId = 10;

            mockCustomerService.Setup(s => s.GetCustomerById(notFoundId)).Returns(() => null);

            CustomersController controller = new CustomersController(mockCustomerService.Object);
            var criteria = new CustomerInquiryRequestMessage {
                CustomerID = notFoundId.ToString()
            };

            //Act
            var actual = controller.Inquiry(criteria);

            //Assert
            Assert.IsType <NotFoundObjectResult>(actual.Result);
        }
예제 #7
0
        public void Inquiry_WhenCalled_AndErrorOccurred_MustReturnsStatusCode500()
        {
            //Arrange
            var mockCustomerService = new Mock <ICustomerService>();

            string email = "*****@*****.**";

            mockCustomerService.Setup(s => s.GetCustomerByEmail(email)).Throws(new Exception("Any exception"));
            CustomersController controller = new CustomersController(mockCustomerService.Object);
            var criteria = new CustomerInquiryRequestMessage {
                Email = email
            };

            //Act
            var result   = controller.Inquiry(criteria);
            var actual   = result.Result as ObjectResult;
            var expected = 500;

            //Assert
            Assert.Equal(expected, actual?.StatusCode);
        }
        public ActionResult <Customer> Inquiry(CustomerInquiryRequestMessage criteria)
        {
            try
            {
                if (criteria == null || string.IsNullOrEmpty(criteria.CustomerID) && string.IsNullOrEmpty(criteria.Email))
                {
                    return(BadRequest(new ErrorResponseMessage {
                        Message = "No inquiry criteria"
                    }));
                }

                if (!string.IsNullOrEmpty(criteria.CustomerID) && !string.IsNullOrEmpty(criteria.Email))
                {
                    if (!InquiryCriteriaValidation.ValidateCustomerID(criteria.CustomerID))
                    {
                        return(BadRequest(new ErrorResponseMessage {
                            Message = "Invalid Customer ID"
                        }));
                    }
                    if (!InquiryCriteriaValidation.ValidateEmail(criteria.Email))
                    {
                        return(BadRequest(new ErrorResponseMessage {
                            Message = "Invalid Email"
                        }));
                    }

                    var customer = _customerService.GetCustomerByIdAndEmail(Convert.ToInt32(criteria.CustomerID), criteria.Email);
                    if (customer == null)
                    {
                        return(NotFound(null));
                    }

                    return(Ok(customer));
                }

                if (!string.IsNullOrEmpty(criteria.CustomerID))
                {
                    if (!InquiryCriteriaValidation.ValidateCustomerID(criteria.CustomerID))
                    {
                        return(BadRequest(new ErrorResponseMessage {
                            Message = "Invalid Customer ID"
                        }));
                    }

                    var customer = _customerService.GetCustomerById(Convert.ToInt32(criteria.CustomerID));
                    if (customer == null)
                    {
                        return(NotFound(null));
                    }

                    return(Ok(customer));
                }

                if (!string.IsNullOrEmpty(criteria.Email))
                {
                    if (!InquiryCriteriaValidation.ValidateEmail(criteria.Email))
                    {
                        return(BadRequest(new ErrorResponseMessage {
                            Message = "Invalid Email"
                        }));
                    }

                    var customer = _customerService.GetCustomerByEmail(criteria.Email);
                    if (customer == null)
                    {
                        return(NotFound(null));
                    }

                    return(Ok(customer));
                }

                return(BadRequest(null));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, new ErrorResponseMessage {
                    Message = "An error occured"
                }));
            }
        }