Exemplo n.º 1
0
        public async void ShouldReturnNotFoundWhenUpdatingWithInvalideEmpNo()
        {
            // Arrange
            VwEmpDetails invalidEmployeeUpdateRequest =
                new VwEmpDetails {
                EmpNo            = invalidEmpNo,
                FirstName        = "Jane",
                LastName         = "Doe",
                BirthDate        = DateTime.Now.AddDays(-1000),
                HireDate         = DateTime.Now.AddDays(-1000),
                Gender           = "Female",
                Salary           = 99999,
                Title            = "New Title",
                DeptNo           = "d999",
                DeptName         = "New Department",
                ManagerFirstName = string.Empty,
                ManagerLastName  = string.Empty,
                ManagerEmpNo     = managerEmpNo
            };

            mockEmployeeDetailRepository
            .Setup(x => x.PutAsync(invalidEmpNo.ToString(), invalidEmployeeUpdateRequest))
            .ReturnsAsync(null as VwEmpDetails);

            // Act
            ActionResult <VwEmpDetails> updateResult = await _controller.Put(invalidEmpNo, invalidEmployeeUpdateRequest);

            NotFoundResult notFoundResult      = updateResult.Result as NotFoundResult;
            VwEmpDetails   updatedVwEmpDetails = updateResult.Value;

            // Assert
            Assert.NotNull(notFoundResult);
            Assert.Equal(404, notFoundResult.StatusCode);
            Assert.Null(updateResult.Value);
        }
 private bool employeeBasicInfoChanged(Employees employee, VwEmpDetails employeeDetailUpdateRequest)
 {
     return
         (employee.BirthDate != employeeDetailUpdateRequest.BirthDate ||
          employee.FirstName != employeeDetailUpdateRequest.FirstName ||
          employee.LastName != employeeDetailUpdateRequest.LastName ||
          employee.Gender != employeeDetailUpdateRequest.Gender ||
          employee.HireDate != employeeDetailUpdateRequest.HireDate);
 }
        public async Task <ActionResult <VwEmpDetails> > Post(VwEmpDetails employeeDetailCreateRequest)
        {
            Employees employeeCreateRequest = new Employees {
                // EmpNo = -1,
                BirthDate = employeeDetailCreateRequest.BirthDate,
                FirstName = employeeDetailCreateRequest.FirstName,
                LastName  = employeeDetailCreateRequest.LastName,
                Gender    = employeeDetailCreateRequest.Gender,
                HireDate  = employeeDetailCreateRequest.HireDate
            };

            Employees employeeCreateResult = await _employeeRepository.PostAsync(employeeCreateRequest);

            int?empNo = employeeCreateResult?.EmpNo;

            if (empNo == null)
            {
                return(BadRequest());
            }

            VwDeptEmpCurrent deptEmpCreateRequest = new VwDeptEmpCurrent {
                EmpNo  = empNo.Value,
                DeptNo = employeeDetailCreateRequest.DeptNo
            };
            VwDeptEmpCurrent deptEmpCreateResult = await
                                                   _deptEmpRepository
                                                   .PostAsync(deptEmpCreateRequest);

            VwSalariesCurrent salaryCreateRequest = new VwSalariesCurrent {
                EmpNo  = empNo.Value,
                Salary = employeeDetailCreateRequest.Salary
            };
            VwSalariesCurrent salaryCreateResult = await
                                                   _salaryRepository
                                                   .PostAsync(salaryCreateRequest);

            VwTitlesCurrent titleCreateRequest = new VwTitlesCurrent {
                EmpNo = empNo.Value,
                Title = employeeDetailCreateRequest.Title
            };
            VwTitlesCurrent titleCreateResult = await
                                                _titleRepository
                                                .PostAsync(titleCreateRequest);

            VwEmpDetails result = await _employeeDetailRepository.GetAsync(empNo.Value.ToString());

            return(CreatedAtAction(
                       nameof(this.Post),
                       new { id = result.EmpNo },
                       result));
        }
        public async Task <ActionResult <VwEmpDetails> > Put(int id, VwEmpDetails employeeDetailUpdateRequest)
        {
            Employees employee = await _employeeRepository.GetAsync(id.ToString());

            VwSalariesCurrent salary = await _salaryRepository.GetAsync(id.ToString());

            VwTitlesCurrent title = await _titleRepository.GetAsync(id.ToString());

            VwDeptEmpCurrent deptEmp = await _deptEmpRepository.GetAsync(id.ToString());

            if (employee == null)
            {
                return(NotFound());
            }

            if (employeeBasicInfoChanged(employee, employeeDetailUpdateRequest))
            {
                Employees employeeUpdateRequest = new Employees {
                    EmpNo     = id,
                    BirthDate = employeeDetailUpdateRequest.BirthDate,
                    FirstName = employeeDetailUpdateRequest.FirstName,
                    LastName  = employeeDetailUpdateRequest.LastName,
                    Gender    = employeeDetailUpdateRequest.Gender,
                    HireDate  = employeeDetailUpdateRequest.HireDate
                };
                Employees employeeUpdateResult = await
                                                 _employeeRepository
                                                 .PutAsync(id.ToString(), employeeUpdateRequest);
            }

            if (deptEmp?.DeptNo != employeeDetailUpdateRequest.DeptNo)
            {
                VwDeptEmpCurrent deptEmpCreateRequest = new VwDeptEmpCurrent {
                    EmpNo  = employeeDetailUpdateRequest.EmpNo,
                    DeptNo = employeeDetailUpdateRequest.DeptNo
                };
                VwDeptEmpCurrent deptEmpCreateResult = await
                                                       _deptEmpRepository
                                                       .PostAsync(deptEmpCreateRequest);
            }

            if (salary?.Salary != employeeDetailUpdateRequest.Salary)
            {
                VwSalariesCurrent salaryCreateRequest = new VwSalariesCurrent {
                    EmpNo  = employeeDetailUpdateRequest.EmpNo,
                    Salary = employeeDetailUpdateRequest.Salary
                };
                VwSalariesCurrent salaryCreateResult = await
                                                       _salaryRepository
                                                       .PostAsync(salaryCreateRequest);
            }

            if (title?.Title != employeeDetailUpdateRequest.Title)
            {
                VwTitlesCurrent titleCreateRequest = new VwTitlesCurrent {
                    EmpNo = employeeDetailUpdateRequest.EmpNo,
                    Title = employeeDetailUpdateRequest.Title
                };
                VwTitlesCurrent titleCreateResult = await
                                                    _titleRepository
                                                    .PostAsync(titleCreateRequest);
            }

            VwEmpDetails result = await
                                  _employeeDetailRepository
                                  .GetAsync(id.ToString());

            return(result);
        }
Exemplo n.º 5
0
        public EmployeeDetailControllerTest()
        {
            empNo            = 0;
            invalidEmpNo     = -1;
            newEmpNo         = 199;
            managerEmpNo     = 99;
            deptNo           = "0";
            salary           = 10000;
            title            = "Test Title";
            timeNow          = DateTime.Now;
            gender           = "M";
            managerFirstName = "John";
            managerLastName  = "Smith";

            vwEmpDetails = new VwEmpDetails {
                EmpNo            = empNo,
                FirstName        = string.Empty,
                LastName         = string.Empty,
                BirthDate        = timeNow,
                HireDate         = timeNow,
                Gender           = gender,
                Salary           = salary,
                Title            = title,
                DeptNo           = deptNo,
                DeptName         = string.Empty,
                ManagerEmpNo     = managerEmpNo,
                ManagerFirstName = managerFirstName,
                ManagerLastName  = managerLastName
            };

            vwEmpDetailsShort = new VwEmpDetailsShort {
                EmpNo     = empNo,
                FirstName = string.Empty,
                LastName  = string.Empty,
                Title     = string.Empty,
                Salary    = salary,
                DeptNo    = deptNo,
                DeptName  = string.Empty,
                // ManagerEmpNo =  managerEmpNo
            };


            mockLogger = new Mock <ILogger <EmployeeDetailController> >();

            mockEmployeeDetailRepository      = new Mock <IRepository <VwEmpDetails> >();
            mockEmployeeDetailShortRepository = new Mock <IRepository <VwEmpDetailsShort> >();
            mockEmployeeRepository            = new Mock <IRepository <Employees> >();
            mockDeptEmpCurrentRepository      = new Mock <IRepository <VwDeptEmpCurrent> >();
            mockSalaryRepository = new Mock <IRepository <VwSalariesCurrent> >();
            mockTitleRepository  = new Mock <IRepository <VwTitlesCurrent> >();
            mockUnitOfWoirk      = new Mock <IUnitOfWorkEmployees>();

            mockEmployeeDetailRepository
            .Setup(x => x.GetAsync(It.Is <string>(x => x == empNo.ToString())))
            .ReturnsAsync(vwEmpDetails);

            _controller = new EmployeeDetailController(
                mockLogger.Object,
                mockEmployeeRepository.Object,
                mockEmployeeDetailRepository.Object,
                mockEmployeeDetailShortRepository.Object,
                mockDeptEmpCurrentRepository.Object,
                mockSalaryRepository.Object,
                mockTitleRepository.Object,
                mockUnitOfWoirk.Object);
        }
Exemplo n.º 6
0
        public async void ShouldCreateEmployeeWithDetails()
        {
            DateTime mockDate = DateTime.Now.AddDays(-1000);
            // Arrange
            VwEmpDetails employeeDetailCreateRequest =
                new VwEmpDetails {
                EmpNo            = -1,
                FirstName        = "Jane",
                LastName         = "Doe",
                BirthDate        = mockDate,
                HireDate         = mockDate,
                Gender           = "Female",
                Salary           = 99999,
                Title            = "New Title",
                DeptNo           = "d999",
                DeptName         = "New Department",
                ManagerFirstName = string.Empty,
                ManagerLastName  = string.Empty,
                ManagerEmpNo     = managerEmpNo
            };

            VwEmpDetails employeeDetailCreateResult =
                new VwEmpDetails {
                EmpNo            = newEmpNo,
                FirstName        = employeeDetailCreateRequest.FirstName,
                LastName         = employeeDetailCreateRequest.LastName,
                BirthDate        = employeeDetailCreateRequest.BirthDate,
                HireDate         = employeeDetailCreateRequest.HireDate,
                Gender           = employeeDetailCreateRequest.Gender,
                Salary           = employeeDetailCreateRequest.Salary,
                Title            = employeeDetailCreateRequest.Title,
                DeptNo           = employeeDetailCreateRequest.DeptNo,
                DeptName         = employeeDetailCreateRequest.DeptName,
                ManagerFirstName = employeeDetailCreateRequest.ManagerFirstName,
                ManagerLastName  = employeeDetailCreateRequest.ManagerLastName,
                ManagerEmpNo     = employeeDetailCreateRequest.ManagerEmpNo
            };

            Employees newEmployee =
                new Employees {
                EmpNo     = -1,
                FirstName = employeeDetailCreateRequest.FirstName,
                LastName  = employeeDetailCreateRequest.LastName,
                BirthDate = employeeDetailCreateRequest.BirthDate,
                HireDate  = employeeDetailCreateRequest.HireDate,
                Gender    = employeeDetailCreateRequest.Gender,
            };

            Employees employeeCreateResult =
                new Employees {
                EmpNo     = newEmpNo,
                FirstName = newEmployee.FirstName,
                LastName  = newEmployee.LastName,
                BirthDate = newEmployee.BirthDate,
                HireDate  = newEmployee.HireDate,
                Gender    = newEmployee.Gender,
            };


            mockEmployeeRepository
            .Setup(x => x.PostAsync(It.IsAny <Employees>()))
            .ReturnsAsync(employeeCreateResult);
            mockEmployeeDetailRepository
            .Setup(x => x.GetAsync(It.Is <string>(x => x == employeeCreateResult.EmpNo.ToString())))
            .ReturnsAsync(employeeDetailCreateResult);

            // Act
            ActionResult <VwEmpDetails> postResult = await _controller.Post(employeeDetailCreateRequest);

            CreatedAtActionResult createdResult   = postResult.Result as CreatedAtActionResult;
            VwEmpDetails          createdEmployee = createdResult.Value as VwEmpDetails;

            // Assert
            Assert.NotNull(createdResult);
            Assert.Equal(201, createdResult.StatusCode);
            // Assert.NotEqual(createResult.EmpNo, employeeCreateRequest.EmpNo);
            Assert.Equal(employeeDetailCreateRequest.BirthDate, createdEmployee.BirthDate);
            Assert.Equal(employeeDetailCreateRequest.FirstName, createdEmployee.FirstName);
            Assert.Equal(employeeDetailCreateRequest.LastName, createdEmployee.LastName);
            Assert.Equal(employeeDetailCreateRequest.Gender, createdEmployee.Gender);
            Assert.Equal(employeeDetailCreateRequest.HireDate, createdEmployee.HireDate);
        }
Exemplo n.º 7
0
        public async void ShouldUpdateEmployeeInfo()
        {
            int      empNoUpdate = 1;
            DateTime dateTimeNow = DateTime.Now;
            string   newTitle    = "New Title";
            string   newDeptName = "New Department";
            int      newSalary   = 99999;
            string   newDeptNo   = "d999";

            // Arrange
            VwEmpDetails employeeUpdateRequest =
                new VwEmpDetails {
                EmpNo            = empNoUpdate,
                FirstName        = "Jane",
                LastName         = "Doe",
                BirthDate        = dateTimeNow.AddDays(-1000),
                HireDate         = dateTimeNow.AddDays(-1000),
                Gender           = "Female",
                Salary           = newSalary,
                Title            = newTitle,
                DeptNo           = newDeptNo,
                DeptName         = newDeptName,
                ManagerFirstName = string.Empty,
                ManagerLastName  = string.Empty,
                ManagerEmpNo     = managerEmpNo
            };

            Employees employee =
                new Employees {
                EmpNo     = empNoUpdate,
                FirstName = "Jane",
                LastName  = "Doe",
                BirthDate = dateTimeNow.AddDays(-1000),
                HireDate  = dateTimeNow.AddDays(-1000),
                Gender    = "Female"
            };

            mockEmployeeDetailRepository
            .Setup(x => x.GetAsync(It.Is <string>(x => x == empNoUpdate.ToString())))
            .ReturnsAsync(employeeUpdateRequest);

            mockEmployeeRepository
            .Setup(x => x.GetAsync(It.Is <string>(x => x == empNoUpdate.ToString())))
            .ReturnsAsync(employee);

            mockSalaryRepository
            .Setup(x => x.GetAsync(It.Is <string>(x => x == empNoUpdate.ToString())))
            .ReturnsAsync(new VwSalariesCurrent {
                EmpNo = empNoUpdate, Salary = newSalary
            });

            mockTitleRepository
            .Setup(x => x.GetAsync(It.Is <string>(x => x == empNoUpdate.ToString())))
            .ReturnsAsync(new VwTitlesCurrent {
                EmpNo = empNoUpdate, Title = newTitle
            });

            mockDeptEmpCurrentRepository
            .Setup(x => x.GetAsync(It.Is <string>(x => x == empNoUpdate.ToString())))
            .ReturnsAsync(new VwDeptEmpCurrent {
                EmpNo = empNoUpdate, DeptNo = newDeptNo
            });

            // mockEmployeeDetailRepository
            //     .Setup(x => x.PutAsync(
            //         It.Is<int>(i => i == empNoUpdate),
            //         It.Is<VwEmpDetails>(i => i == vwEmpDetails)))
            //     .ReturnsAsync(employeeUpdateRequest);

            // Act
            ActionResult <VwEmpDetails> updateResult = await _controller.Put(empNoUpdate, employeeUpdateRequest);

            NotFoundResult notFoundResult      = updateResult.Result as NotFoundResult;
            VwEmpDetails   updatedVwEmpDetails = updateResult.Value;

            // Assert
            Assert.Null(notFoundResult);
            Assert.NotNull(updateResult.Value);
            Assert.Equal(employeeUpdateRequest.EmpNo, updatedVwEmpDetails.EmpNo);
            Assert.Equal(employeeUpdateRequest.FirstName, updatedVwEmpDetails.FirstName);
            Assert.Equal(employeeUpdateRequest.LastName, updatedVwEmpDetails.LastName);
            Assert.Equal(employeeUpdateRequest.BirthDate, updatedVwEmpDetails.BirthDate);
            Assert.Equal(employeeUpdateRequest.HireDate, updatedVwEmpDetails.HireDate);
            Assert.Equal(employeeUpdateRequest.Gender, updatedVwEmpDetails.Gender);

            Assert.Equal(employeeUpdateRequest.Salary, updatedVwEmpDetails.Salary);
            Assert.Equal(employeeUpdateRequest.Title, updatedVwEmpDetails.Title);
            Assert.Equal(employeeUpdateRequest.DeptNo, updatedVwEmpDetails.DeptNo);
        }