コード例 #1
0
        public async void UpdateBillableActivityAsync_Returns_NotFound_404_When_Update_With_null_BillableActivity()
        {
            //declare a BillableActivity
            BillableActivity expectedBillableActivity = null;
            //expected return error message
            string expectedResponseMessage = "No Billable Activity was updated";

            ///set mockRepo return for Update action
            _mockRepository.Setup(repo => repo.UpdateTAsync(expectedBillableActivity)).ReturnsAsync(0);

            //instantiate the controller, and call the method
            var controller = new BillableActivitiesController(_mockRepository.Object, _mapper, _logger);

            //Create Custom ControllerContext and add it to Controller for logging in the Controller in case of error
            controller.ControllerContext = new ControllerContextModel();

            //Call the SUT method
            //returns ActionResult<BillableActivity> type
            var actionResult = await controller.UpdateBillableActivityAsync(1, expectedBillableActivity);

            //Get the int result from the posted ActionResult
            var    notFoundObjectResult  = actionResult.Result as NotFoundObjectResult;
            var    statusCode            = notFoundObjectResult.StatusCode;
            string actualResponseMessage = (string)notFoundObjectResult.Value;

            //Assert the result
            Assert.NotNull(actionResult);

            //Validate the return status code
            Assert.Equal(404, statusCode);

            //Validate the actual BillableActivity
            Assert.Equal(expectedResponseMessage, actualResponseMessage);
        }
コード例 #2
0
        public async void AddBillableActivityAsync_Creates_One_BillableActivity_Returns_201_And_BA_Created()
        {
            //declare a BillableActivity
            var expectedBillableActivity = new BillableActivity
            {
                Id              = 1,
                LegalCase_Id    = 1,
                Employee_Id     = 1,
                Title           = "Billable Activity 1",
                Description     = "this is the Billable Activity 1",
                Price           = 120.50m,
                Start_DateTime  = new DateTime(2020, 06, 20, 9, 30, 00),
                Finish_DateTime = new DateTime(2020, 06, 20, 16, 00, 00)
            };
            BillableActivityDTO expectedBaDTO = _mapper.Map <BillableActivityDTO>(expectedBillableActivity);

            //set mockRepo return for Add action
            _mockRepository.Setup(repo => repo.AddTAsync(expectedBillableActivity)).ReturnsAsync(1);

            //set repo return for getting the newly created object
            _mockRepository.Setup(repo => repo.GetOneByAsync(ba =>
                                                             ba.Title == expectedBillableActivity.Title &&
                                                             ba.Employee_Id == expectedBillableActivity.Employee_Id &&
                                                             ba.LegalCase_Id == expectedBillableActivity.LegalCase_Id))
            .ReturnsAsync(expectedBillableActivity);

            //instantiate the controller, passing the repo object
            var controller = new BillableActivitiesController(_mockRepository.Object, _mapper, _logger);

            //Call the SUT method
            //returns ActionResult<BillableActivity> type
            var actionResult = await controller.AddBillableActivityAsync(expectedBillableActivity);

            //Get the int result from the posted ActionResult
            var createdResult = actionResult.Result as CreatedResult;
            var statusCode    = createdResult.StatusCode;
            BillableActivityDTO actualBaDTO = createdResult.Value as BillableActivityDTO;

            //Assert the result
            Assert.NotNull(actionResult);

            //Validate the return is 1 BillableActivity created
            Assert.Equal(201, statusCode);

            //Validate the actual BillableActivity
            actualBaDTO.Should().BeEquivalentTo(expectedBaDTO, options => options.ComparingByMembers <BillableActivityDTO>());
        }
コード例 #3
0
        public async void DeleteBillableActivityAsync_Deletes_One_BillableActivity_And_Returns_Number_Of_Deletions(int id)
        {
            //declare a BillableActivity
            var expectedBillableActivity = new BillableActivity
            {
                Id              = 1,
                LegalCase_Id    = 1,
                Employee_Id     = 1,
                Title           = "Billable Activity 1",
                Description     = "this is the Billable Activity 1",
                Price           = 120.50m,
                Start_DateTime  = new DateTime(2020, 06, 20, 9, 30, 00),
                Finish_DateTime = new DateTime(2020, 06, 20, 16, 00, 00)
            };
            BillableActivityDTO expectedBaDTO = _mapper.Map <BillableActivityDTO>(expectedBillableActivity);

            //set repo return for getting the object to delete
            _mockRepository.Setup(repo => repo.GetOneByAsync(ba => ba.Id == id))
            .ReturnsAsync(expectedBillableActivity);

            //set mockRepo return for Delete action
            _mockRepository.Setup(repo => repo.DeleteTAsync(expectedBillableActivity)).ReturnsAsync(1);

            //instantiate the controller, passing the repo object
            var controller = new BillableActivitiesController(_mockRepository.Object, _mapper, _logger);

            //Call the controller method
            var actionResult = await controller.DeleteBillableActivityAsync(id);

            //Get the int result
            var okObjectResult = actionResult.Result as OkObjectResult;
            var statusCode     = okObjectResult.StatusCode;
            int actualDeleted  = (int)okObjectResult.Value;

            //Assert the result
            Assert.NotNull(actionResult);

            //Validate StatusCode
            Assert.Equal(200, statusCode);

            //Validate the number of BAs deleted
            Assert.Equal(1, actualDeleted);
        }
コード例 #4
0
        public async void DeleteBillableActivityAsync_Returns_NotFound_404_When_Delete_With_null_BillableActivity(int id)
        {
            //declare a BillableActivity
            BillableActivity expectedBillableActivity = null;

            //response error message:
            string expectedResponseMessage = "No data was found for the id";

            //set repo return for getting the object to delete
            _mockRepository.Setup(repo => repo.GetOneByAsync(ba => ba.Id == id))
            .ReturnsAsync(expectedBillableActivity);

            //set mockRepo return for Delete action
            _mockRepository.Setup(repo => repo.DeleteTAsync(expectedBillableActivity)).ReturnsAsync(0);

            //instantiate the controller, and call the method
            var controller = new BillableActivitiesController(_mockRepository.Object, _mapper, _logger);

            //Create Custom ControllerContext and add it to Controller for logging in the Controller in case of error
            controller.ControllerContext = new ControllerContextModel();

            //Call the controller method
            var actionResult = await controller.DeleteBillableActivityAsync(id);

            //Get the int result
            var    notFoundObjectResult  = actionResult.Result as NotFoundObjectResult;
            var    statusCode            = notFoundObjectResult.StatusCode;
            string actualResponseMessage = (string)notFoundObjectResult.Value;

            //Assert the result
            Assert.NotNull(actionResult);

            //Validate StatusCode
            Assert.Equal(404, statusCode);

            //Validate the number of BAs deleted
            Assert.Equal(expectedResponseMessage, actualResponseMessage);
        }
コード例 #5
0
        public async Task <ActionResult <int> > DeleteBillableActivityAsync([FromRoute] int id)
        {
            BillableActivity billableActivity = await _genericRepository.GetOneByAsync(tf => tf.Id == id);

            if (billableActivity is null)
            {
                var logData = new
                {
                    Id = id,
                    NewBillableActivity = billableActivity,
                    Action       = ControllerContext.ActionDescriptor.DisplayName,
                    Verb         = HttpContext.Request.Method,
                    EndpointPath = HttpContext.Request.Path.Value,
                    User         = HttpContext.User.Claims.First(usr => usr.Type == "preferred_username").Value
                };
                _logger.LogInformation("No BillableActivity was found for id {id}. Data: {@logData}", id, logData);

                return(NotFound("No data was found for the id"));
            }

            var billableActivitiesDeleted = await _genericRepository.DeleteTAsync(billableActivity);

            return(Ok(billableActivitiesDeleted));
        }
コード例 #6
0
        public async Task <ActionResult <BillableActivityDTO> > UpdateBillableActivityAsync([FromRoute] int id, [FromBody] BillableActivity billableActivity)
        {
            var billableActivityResult = await _genericRepository.GetOneByAsync(ba => ba.Id == billableActivity.Id);

            if (billableActivityResult is null)
            {
                var logData = new
                {
                    Id = id,
                    NewBillableActivity = billableActivity,
                    Action       = ControllerContext.ActionDescriptor.DisplayName,
                    Verb         = HttpContext.Request.Method,
                    EndpointPath = HttpContext.Request.Path.Value,
                    User         = HttpContext.User.Claims.First(usr => usr.Type == "preferred_username").Value
                };
                _logger.LogInformation("No BillableActivity was updated for id {id} and BillableActivity {billableActivity}. Data: {@logData}", id, billableActivity, logData);

                return(NotFound("No Billable Activity was updated"));
            }

            int updated = await _genericRepository.UpdateTAsync(billableActivity);

            BillableActivityDTO billableActivityDTO = _mapper.Map <BillableActivityDTO>(billableActivity);

            return(Ok(billableActivityDTO));
        }
コード例 #7
0
        public async Task <ActionResult <BillableActivityDTO> > AddBillableActivityAsync([FromBody] BillableActivity billableActivity)
        {
            int created = await _genericRepository.AddTAsync(billableActivity);

            if (created == 0)
            {
                var logData = new
                {
                    NewBillableActivity = billableActivity,
                    Action       = ControllerContext.ActionDescriptor.DisplayName,
                    Verb         = HttpContext.Request.Method,
                    EndpointPath = HttpContext.Request.Path.Value,
                    User         = HttpContext.User.Claims.First(usr => usr.Type == "preferred_username").Value
                };
                _logger.LogInformation("No BillableActivity was created for {billableActivity}. Data: {@logData}", billableActivity, logData);

                return(NotFound("No Billable Activity was created"));
            }

            var newBillableActivity = await _genericRepository.GetOneByAsync(ba =>
                                                                             ba.Title == billableActivity.Title &&
                                                                             ba.Employee_Id == billableActivity.Employee_Id &&
                                                                             ba.LegalCase_Id == billableActivity.LegalCase_Id);

            BillableActivityDTO billableActivityDTO = _mapper.Map <BillableActivityDTO>(newBillableActivity);

            return(Created("", billableActivityDTO));
        }