コード例 #1
0
        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);
            }
        }
コード例 #2
0
        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);
            }
        }
コード例 #3
0
        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);
            }
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        static void Main(string[] args)
        {
            // Create a client configuration with an OAuth Token Manager
            var config  = new HQAPIClientConfiguration("https://api.hqlabs.de");
            var manager = config.CreateOAuthTokenManager("AppId", "AppSecret",
                                                         "SyncUser-AccessToken",
                                                         "SyncUser-RefreshToken",
                                                         DateTime.UtcNow.AddSeconds(2500000));

            // Register a callback for when the token was refreshed
            manager.TokenRefreshed += Manager_TokenRefreshed;

            // Create the client with the configuration
            var client = new HQAPIClient(config);

            // Get the details about the currently authenticated user.
            var user = client.MeV1_GetAsync().Result;

            // Create and put a new UserReporting Example
            var newReproting = new CreateUserReportingAPISample();

            newReproting.CreateUserReportingApi(client);

            // Get and create Company Example
            var Company = new CompanyAPISample();

            Company.CompanyAPI(client);
        }
コード例 #6
0
        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);
            }
        }
コード例 #7
0
        public void CreateUserReportingApi(HQAPIClient client)
        {
            // Create a new UserReporting
            var newUserReporting = new ProjectV1CreateReportingParameters()
            {
                UserId      = 1,
                StartDate   = new DateTime(2018, 8, 24),
                Duration    = 1000,
                Description = "Description",
                TaskId      = 1
            };

            // POST the new UserReporting to create it
            var newUserReportingResul1 = client.ProjectsV1_CreateReportingByIdAsync(12, newUserReporting).Result;

            Console.WriteLine("UserReporting1 Id: " + newUserReportingResul1.Id);
        }
コード例 #8
0
        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);
            }
        }
コード例 #9
0
        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);
            }
        }
コード例 #10
0
        public void CompanyAPI(HQAPIClient client)
        {
            // Get a company by id
            var companyById = client.CompaniesV1_GetByIdAsync(1).Result;

            Console.WriteLine("Company by Id: " + companyById.Name);

            // Modify the company name and save it using a PUT
            companyById.Name = companyById.Name + " (API)";
            companyById.DefaultAddress.Country = "DE";
            companyById = client.CompaniesV1_PutByIdAsync(companyById.Id, companyById).Result;

            // Create a new company with a default address and a company type
            var newCompany = new Company()
            {
                Name             = "Unit Corp.",
                CreditorNumber   = 1234,
                DebitorNumber    = 3456,
                Description      = "A test corporation",
                IndustrialSector = "Testing",
                DefaultAddress   = new CompanyAddress()
                {
                    Street  = "Am Kaiserkai 70",
                    City    = "Hamburg",
                    Country = "DE",
                    DefaultForDocumentType = CompanyAddressDefaultForDocumentType.Invoice,
                },
                CompanyTypes = new System.Collections.ObjectModel.ObservableCollection <CompanyTypeOfCompany>()
                {
                    new CompanyTypeOfCompany()
                    {
                        Id = 4002, // Here, 4002 is the Id of type 'Customer'
                    }
                }
            };

            // POST the new company to create it
            var newCompanyResult = client.CompaniesV1_PostAsync(newCompany).Result;

            Console.WriteLine("New Company Id: " + newCompanyResult.Id);

            // Delete the new company
            var deletedCompany = client.CompaniesV1_DeleteByIdAsync(newCompanyResult.Id).Result;

            // Various filter and expand examples. See the OData docs for more information
            var companies = client.CompaniesV1_GetAsync().Result; // Get all companies

            //var companies = client.CompaniesV1_GetAsync(filter: "Id eq 65020").Result; // Get a company with a filter by Id
            //var companies = client.CompaniesV1_GetAsync(filter: "indexof(Name, 'Corp') ge 0").Result; // Get all companies where the name contains 'Corp'
            //var companies = client.CompaniesV1_GetAsync(filter: "UpdatedOn gt 2016-02-15T14:17:40+01:00").Result; // Get all companies modified after 15th of February 2016
            //var companies = client.CompaniesV1_GetAsync(expand: "CompanyTypes").Result; // Get all companies with their company types
            //var companies = client.CompaniesV1_GetAsync(expand: "CompanyTypes", filter: "CompanyTypes/any(companyType: companyType/Name eq 'Customer')").Result; // Returns all companies of type 'Customer'

            // Display the results
            foreach (var item in companies)
            {
                Console.WriteLine(item.Id + ": " + item.Name);
                if (item.CompanyTypes != null)
                {
                    foreach (var companyType in item.CompanyTypes)
                    {
                        Console.WriteLine("    " + companyType.Name);
                    }
                }
            }

            // Wait to show the results
            Console.ReadLine();
        }