public interface IWebAPIAsync<t> { Task<List<t>> Load(); Task Create(T obj); Task<t> Read(int key); Task Update(int key, T obj); Task Delete(int key); } public class WebAPITest<t> { #region Instance fields private IWebAPIAsync<t> _webAPI; #endregion #region Constructor public WebAPITest(IWebAPIAsync<t> webApi) { _webAPI = webApi; } public WebAPITest() { } #endregion #region Test methods for all five REST API methods public async Task RunAPITestLoad() { await LoadAndPrint(); } public async Task RunAPITestCreate(T obj) { await RunAPITest(() => _webAPI.Create(obj)); } public async Task RunAPITestRead(int key) { Task<t> readTask = _webAPI.Read(key); await readTask; PrintSingleRecord(readTask.Result); } public async Task RunAPITestUpdate(int key, T obj) { await RunAPITest(() => _webAPI.Update(key, obj)); } public async Task RunAPITestDelete(int key) { await RunAPITest(() => _webAPI.Delete(key)); } #endregion #region Private helper methods for test execution private async Task RunAPITest(Func<task> APIMethod) { await APIMethod(); } private async Task LoadAndPrint() { Task<List<t>> loadTask = _webAPI.Load(); await loadTask; PrintMultipleRecords(loadTask.Result); } private void PrintMultipleRecords(List<t> records) { foreach (var record in records) { Console.WriteLine(record); } } private void PrintSingleRecord(T record) { { Console.WriteLine(record); } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)