public async Task ShouldReturnJsonListOfBranchOffices() { // https://github.com/richardszalay/mockhttp var mockHttp = new MockHttpMessageHandler(); // Setup a respond for the user api (including a wildcard in the URL) var mockedRequest = mockHttp.When(CommonHelpers.baseUrl + "/api/branch_offices") .Respond("application/json", "{ 'branch_offices': [ {'id' : 1, 'name' : 'BranchOffice1'}, {'id' : 2, 'name' : 'BranchOffice2'} ] }"); // Respond with JSON // Inject the handler or client into your application code var client = mockHttp.ToHttpClient(); using (var hqClient = new HQAPIClient(CommonHelpers.MockConfServ(), client)) { var branchOffices = await hqClient.ListBranchOffices(); // GetMatchCount will return the number of times a mocked request (returned by When / Expect) was called // https://github.com/richardszalay/mockhttp#verifying-matches Assert.Equal(1, mockHttp.GetMatchCount(mockedRequest)); Assert.Equal(2, branchOffices.Count); Assert.Equal(1, branchOffices[0].ID); Assert.Equal(2, branchOffices[1].ID); Assert.Equal("BranchOffice1", branchOffices[0].Name); Assert.Equal("BranchOffice2", branchOffices[1].Name); } }
public async Task ShouldReturnJsonOneEmployee() { // https://github.com/richardszalay/mockhttp var mockHttp = new MockHttpMessageHandler(); // Setup a respond for the user api (including a wildcard in the URL) var mockedRequest = mockHttp.When(CommonHelpers.baseUrl + "/api/employees/*") .Respond("application/json", "{'name' : 'Jan Kow', 'id': 123, 'email': '*****@*****.**', 'isManager': false }"); // Respond with JSON // Inject the handler or client into your application code var client = mockHttp.ToHttpClient(); using (var hqClient = new HQAPIClient(CommonHelpers.MockConfServ(), client)) { var employee = await hqClient.GetEmployee(123); // GetMatchCount will return the number of times a mocked request (returned by When / Expect) was called // https://github.com/richardszalay/mockhttp#verifying-matches Assert.Equal(1, mockHttp.GetMatchCount(mockedRequest)); Assert.Equal(123, employee.ID); Assert.Equal("Jan Kow", employee.Name); Assert.Equal("*****@*****.**", employee.Email); Assert.Equal(false, employee.IsManager); } }
public async Task ShouldReturnJsonListOfSalaries() { // https://github.com/richardszalay/mockhttp var mockHttp = new MockHttpMessageHandler(); // Setup a respond for the user api (including a wildcard in the URL) var mockedRequest = mockHttp.When(CommonHelpers.baseUrl + "/api/salaries/list/*") .Respond("application/json", "{ 'salaries': [ {'id' : 98, 'timePeriod': '2019-Jun-15_2019-Jun-21', 'value': 300, 'employeeId': 1 }," + "{'id' : 99, 'timePeriod': '2019-Jun-22_2019-Jun-28', 'value': 320, 'employeeId': 1}" + " ] }"); // Respond with JSON // Inject the handler or client into your application code var client = mockHttp.ToHttpClient(); using (var hqClient = new HQAPIClient(CommonHelpers.MockConfServ(), client)) { var salaries = await hqClient.ListSalariesForEmployee(1); // GetMatchCount will return the number of times a mocked request (returned by When / Expect) was called // https://github.com/richardszalay/mockhttp#verifying-matches Assert.Equal(1, mockHttp.GetMatchCount(mockedRequest)); Assert.Equal(2, salaries.Count); Assert.Equal(98, salaries[0].ID); Assert.Equal(99, salaries[1].ID); Assert.Equal("2019-Jun-15_2019-Jun-21", salaries[0].TimePeriod); Assert.Equal("2019-Jun-22_2019-Jun-28", salaries[1].TimePeriod); Assert.Equal(1, salaries[0].EmployeeID); Assert.Equal(1, salaries[1].EmployeeID); Assert.Equal(300f, salaries[0].Value); Assert.Equal(320f, salaries[1].Value); } }
public void ShouldReturnNiceUrl_WhenBaseUrlEndsWithSlash() { HQAPIClient client = new HQAPIClient(CommonHelpers.MockConfServ()); string url = client.BuildUrl("/api/branch_offices/list"); Assert.Equal("http://localhost:1234/api/branch_offices/list", url); }
public async Task ShouldReturnJsonListOfEmployees() { // https://github.com/richardszalay/mockhttp var mockHttp = new MockHttpMessageHandler(); // Setup a respond for the user api (including a wildcard in the URL) var mockedRequest = mockHttp.When(CommonHelpers.baseUrl + "/api/employees/list/*") .Respond("application/json", "[" + "{'employee_id': 1, 'name' : 'Jan Kow', 'email': '*****@*****.**', 'date_of_birth': '1996-01-01', " + " 'isManager': false, 'pay': 30.0, 'branch_office_id': 2 }, " + "{'employee_id': 123, 'name' : 'Jan Kow2', 'email': '*****@*****.**', 'date_of_birth': '1996-01-01', " + " 'isManager': true, 'pay': 30.0, 'branch_office_id': 1 }, " + "]"); // Respond with JSON // Inject the handler or client into your application code var client = mockHttp.ToHttpClient(); using (var hqClient = new HQAPIClient(CommonHelpers.MockConfServ(), client)) { var employees = await hqClient.ListEmployees(100); // GetMatchCount will return the number of times a mocked request (returned by When / Expect) was called // https://github.com/richardszalay/mockhttp#verifying-matches Assert.Equal(1, mockHttp.GetMatchCount(mockedRequest)); Assert.Equal(2, employees.Count); Assert.Equal(1, employees[0].ID); Assert.Equal(123, employees[1].ID); Assert.False(employees[0].IsManager); Assert.True(employees[1].IsManager); } }
public async Task Synchronize_Works_StartWithEmplyDb_HQEmpty() { var cs = CommonHelpers.MockConfServ(false); var hqApiClient = new Moq.Mock <IHQAPIClient>(); // no employees in HQ hqApiClient.Setup(m => m.ListEmployees(cs.GetBranchOfficeId())).Returns( Task.FromResult(new List <HQEmployee>()) ); // no salaries in HQ hqApiClient.Setup(m => m.ListSalariesForEmployee(Moq.It.IsAny <int>())).Returns( Task.FromResult(new List <HQSalary>()) ); var dao = new PostgresDataAccessObjectService(dbContext); var ss = new SynchronizatorService(hqApiClient.Object, cs, dao); await ss.Synchronize(); hqApiClient.Verify(m => m.ListEmployees(0), Moq.Times.Once); // ListEmployees did not succeed, so no new http requests are handled hqApiClient.Verify(m => m.ListSalariesForEmployee(Moq.It.IsAny <int>()), Moq.Times.Never); ss.Dispose(); var emps = dao.GetAllEmployees(); var eh = dao.GetAllEmployeeHours(); Assert.Empty(emps); Assert.Empty(eh); }
public async Task Synchronize_Works_StartWithEmplyDb_HQNotEmpty() { var cs = CommonHelpers.MockConfServ(false); var hqApiClient = new Moq.Mock <IHQAPIClient>(); var hqEmp1 = new HQEmployee { Name = "Jan K", Email = "*****@*****.**", IsManager = false, ID = 1 }; var hqEmp2 = new HQEmployee { Name = "Ela K", Email = "*****@*****.**", IsManager = true, ID = 2 }; var hqEmps = new List <HQEmployee>(); hqEmps.Add(hqEmp1); hqEmps.Add(hqEmp2); var hqSalary1 = new HQSalary { ID = 1, EmployeeID = 1, Value = 1000, TimePeriod = "01.02.2019-06.02.2019" }; var hqSalary2 = new HQSalary { ID = 2, EmployeeID = 1, Value = 1000, TimePeriod = "07.02.2019-09.02.2019" }; var hqSalaries = new List <HQSalary>(); hqSalaries.Add(hqSalary1); hqSalaries.Add(hqSalary2); hqApiClient.Setup(m => m.ListEmployees(cs.GetBranchOfficeId())).Returns( Task.FromResult(hqEmps) ); hqApiClient.Setup(m => m.ListSalariesForEmployee(1)).Returns( Task.FromResult(hqSalaries) ); var dao = new PostgresDataAccessObjectService(dbContext); var ss = new SynchronizatorService(hqApiClient.Object, cs, dao); await ss.Synchronize(); hqApiClient.Verify(m => m.ListEmployees(0), Moq.Times.Once); // 1 for each hq employee hqApiClient.Verify(m => m.ListSalariesForEmployee(Moq.It.IsAny <int>()), Moq.Times.Exactly(2)); ss.Dispose(); var emps = dao.GetAllEmployees(); var eh = dao.GetAllEmployeeHours(); var salaries = dao.GetAllSalaries(); Assert.Equal(2, emps.Count); Assert.Equal("*****@*****.**", emps[0].Email); Assert.Equal("*****@*****.**", emps[1].Email); Assert.Empty(eh); Assert.Equal(2, salaries.Count); }
public async Task SynchronizeDoesNotThrowExceptionWhenNoHQServer() { var cs = CommonHelpers.MockConfServ(false); var hqApiClient = new Moq.Mock <IHQAPIClient>(); var dao = new Moq.Mock <IDataAccessObjectService>(); var ss = new SynchronizatorService(hqApiClient.Object, cs, dao.Object); await ss.Synchronize(); hqApiClient.Verify(m => m.ListEmployees(0), Moq.Times.Once); // ListEmployees did not suceed, so no new http requests are handled hqApiClient.Verify(m => m.ListSalariesForEmployee(0), Moq.Times.Never); ss.Dispose(); }
public async Task ListSalariesForEmployee_ShouldNotThrowException() { // https://github.com/richardszalay/mockhttp var mockHttp = new MockHttpMessageHandler(); // Setup a respond for the user api (including a wildcard in the URL) var mockedRequest = mockHttp.When(CommonHelpers.baseUrl + "/api/salaries/list/*") .Respond(HttpStatusCode.NotFound); // Inject the handler or client into your application code var client = mockHttp.ToHttpClient(); using (var hqClient = new HQAPIClient(CommonHelpers.MockConfServ(), client)) { var salaries = await hqClient.ListSalariesForEmployee(1); Assert.Empty(salaries); } }
public async Task ListSalariesForEmployee_ShouldReturnEmptyListOfSalaries_WhenNoSalaries() { // https://github.com/richardszalay/mockhttp var mockHttp = new MockHttpMessageHandler(); // Setup a respond for the user api (including a wildcard in the URL) var mockedRequest = mockHttp.When(CommonHelpers.baseUrl + "/api/salaries/list/*") .Respond("application/json", "[]"); // Respond with JSON // Inject the handler or client into your application code var client = mockHttp.ToHttpClient(); using (var hqClient = new HQAPIClient(CommonHelpers.MockConfServ(), client)) { var salaries = await hqClient.ListSalariesForEmployee(1); // GetMatchCount will return the number of times a mocked request (returned by When / Expect) was called // https://github.com/richardszalay/mockhttp#verifying-matches Assert.Equal(1, mockHttp.GetMatchCount(mockedRequest)); Assert.Empty(salaries); } }
public async Task Synchronize_Works_StartWithNotEmplyDb_HQNotEmpty() { var cs = CommonHelpers.MockConfServ(false); var hqApiClient = new Moq.Mock <IHQAPIClient>(); // setup HQ state var hqEmp1 = new HQEmployee { Name = "Jan K", Email = "*****@*****.**", IsManager = false, ID = 1 }; var hqEmp2 = new HQEmployee { Name = "Ela K", Email = "*****@*****.**", IsManager = true, ID = 2 }; var hqEmps = new List <HQEmployee>(); hqEmps.Add(hqEmp1); hqEmps.Add(hqEmp2); hqApiClient.Setup(m => m.ListEmployees(cs.GetBranchOfficeId())).Returns( Task.FromResult(hqEmps) ); var hqSalary1 = new HQSalary { ID = 1, EmployeeID = 1, Value = 1000, TimePeriod = "hqsalary1" }; var hqSalary2 = new HQSalary { ID = 2, EmployeeID = 1, Value = 1000, TimePeriod = "hqsalary2" }; var hqSalaries = new List <HQSalary>(); hqSalaries.Add(hqSalary1); hqSalaries.Add(hqSalary2); hqApiClient.Setup(m => m.ListSalariesForEmployee(1)).Returns( Task.FromResult(hqSalaries) ); var dao = new PostgresDataAccessObjectService(dbContext); // setup BO state var boEmp1 = new Employee { Name = "BO Employee1", Email = "*****@*****.**", EmployeeId = 4 }; // will be deleted var boEmp2 = new Employee { Name = "Ela K", Email = "*****@*****.**", EmployeeId = 5, IsManager = true }; dao.AddEmployee(boEmp1, true); dao.AddEmployee(boEmp2, true); var eh1 = new EmployeeHours { EmployeeHoursId = 102, Value = 100f, TimePeriod = "01.01.2019_01.01.2019", EmployeeId = 4 }; // will be deleted var eh2 = new EmployeeHours { EmployeeHoursId = 103, Value = 100f, TimePeriod = "02.01.2019_02.01.2019", EmployeeId = 4 }; // will be deleted var eh3 = new EmployeeHours { EmployeeHoursId = 104, Value = 100f, TimePeriod = "03.01.2019_03.01.2019", EmployeeId = 4 }; // will be deleted var eh4 = new EmployeeHours { EmployeeHoursId = 105, Value = 100f, TimePeriod = "02.01.2019_08.01.2019", EmployeeId = 5 }; dao.AddEmployeeHours(eh1, true); dao.AddEmployeeHours(eh2, true); dao.AddEmployeeHours(eh3, true); dao.AddEmployeeHours(eh4, true); var salary1 = new Salary { SalaryId = 1, EmployeeId = 4, TimePeriod = "bosalary1", Value = 111 }; // will be deleted var salary2 = new Salary { SalaryId = 2, EmployeeId = 4, TimePeriod = "bosalary2", Value = 222 }; // will be deleted var salary3 = new Salary { SalaryId = 3, EmployeeId = 5, TimePeriod = "bosalary3", Value = 333 }; dao.AddSalary(salary1, true); dao.AddSalary(salary2, true); dao.AddSalary(salary3, true); var ss = new SynchronizatorService(hqApiClient.Object, cs, dao); await ss.Synchronize(); hqApiClient.Verify(m => m.ListEmployees(0), Moq.Times.Once); // 1 for each hq employee hqApiClient.Verify(m => m.ListSalariesForEmployee(Moq.It.IsAny <int>()), Moq.Times.Exactly(2)); ss.Dispose(); var emps = dao.GetAllEmployees(); var eh = dao.GetAllEmployeeHours(); var salaries = dao.GetAllSalaries(); Assert.Equal(2, emps.Count); Assert.Equal("*****@*****.**", emps[1].Email); Assert.Equal("*****@*****.**", emps[0].Email); Assert.Single(eh); Assert.Equal(105, eh[0].EmployeeHoursId); Assert.Equal(3, salaries.Count); }