public async Task MerchantListControllerTest_NoDataFound() { // Arrange int CustomerID = 191809; IConfigurationRoot configurationRoot = Substitute.For <IConfigurationRoot>(); configurationRoot = GetConfiguration(configurationRoot); MockMerchantListRepository mockMerchantListRepository = new MockMerchantListRepository(); ApiResult <GenericPaginationResponse <Merchant> > expectedResult = mockMerchantListRepository.GetMockData(CustomerID); PaginationMerchant page = mockMerchantListRepository.GetPagination(); MerchantListInput pageinput = new MerchantListInput(); pageinput.LIDValue = CustomerID.ToString(); pageinput.lidTypeEnum = Wp.CIS.LynkSystems.Model.Enums.LidTypeEnum.Customer; pageinput.Page = page; IDistributedCache mockCache = Substitute.For <IDistributedCache>(); IStringLocalizer <MerchantListController> localizer = Substitute.For <IStringLocalizer <MerchantListController> >(); string key = "NoDataFound"; string value = "No data found for provided ID"; var localizedString = new LocalizedString(key, value); localizer[Arg.Any <string>()].ReturnsForAnyArgs(localizedString); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); IMerchantListApi merchantListApi = Substitute.For <IMerchantListApi>(); IOperation fakeOperation = Substitute.For <Operation>(mockCache); fakeOperation.WhenForAnyArgs(x => x.RetrieveCache(Arg.Any <string>(), Arg.Any <ICollection <Merchant> >())).DoNotCallBase(); ApiResult <GenericPaginationResponse <Merchant> > response = new ApiResult <GenericPaginationResponse <Merchant> >(); response.Result = new GenericPaginationResponse <Merchant>(); merchantListApi.GetMerchantListAsync(CustomerID, page).ReturnsForAnyArgs(response); MerchantListController controller = new MerchantListController(mockCache, merchantListApi, localizer, fakeOperation, loggingFacade); // Act var merchList = await controller.GetMerchantList(pageinput); // Assert Assert.Equal(((Microsoft.AspNetCore.Mvc.ObjectResult)merchList).StatusCode, 200); var actualMerchantList = ((Microsoft.AspNetCore.Mvc.ObjectResult)merchList).Value; Assert.Equal(((GenericPaginationResponse <Merchant>)actualMerchantList).ModelMessage, localizer["NoDataFound"].Value); }
//Mock API Call and unit test for the API call with returning mock MerchantList. public async Task MerchantListControllerTest_Success() { // Arrange int CustomerID = 191809; string mid = "191807"; IConfigurationRoot configurationRoot = Substitute.For <IConfigurationRoot>(); configurationRoot = GetConfiguration(configurationRoot); MockMerchantListRepository mockMerchantListRepository = new MockMerchantListRepository(); ApiResult <GenericPaginationResponse <Merchant> > expectedResult = mockMerchantListRepository.GetMockData(CustomerID); PaginationMerchant page = mockMerchantListRepository.GetPagination(); MerchantListInput pageinput = new MerchantListInput(); pageinput.LIDValue = CustomerID.ToString(); pageinput.lidTypeEnum = Wp.CIS.LynkSystems.Model.Enums.LidTypeEnum.Customer; pageinput.Page = page; IDistributedCache mockCache = Substitute.For <IDistributedCache>(); IMerchantListRepository mockRepo = Substitute.For <IMerchantListRepository>(); IStringLocalizer <MerchantListController> localizer = Substitute.For <IStringLocalizer <MerchantListController> >(); IMerchantListApi mockMerchantListApi = Substitute.For <IMerchantListApi>(); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); IOperation fakeOperation = Substitute.For <Operation>(mockCache); fakeOperation.WhenForAnyArgs(x => x.RetrieveCache(Arg.Any <string>(), Arg.Any <GenericPaginationResponse <Merchant> >())).DoNotCallBase(); fakeOperation.WhenForAnyArgs(x => x.AddCacheAsync(Arg.Any <string>(), Arg.Any <GenericPaginationResponse <Merchant> >())).DoNotCallBase(); MerchantListController controller = new MerchantListController(mockCache, mockMerchantListApi, localizer, fakeOperation, loggingFacade); mockMerchantListApi.GetMerchantListAsync(CustomerID, page).ReturnsForAnyArgs(expectedResult); // Act var merchList = await controller.GetMerchantList(pageinput); var actualRecord = ((Microsoft.AspNetCore.Mvc.ObjectResult)merchList).Value; string merchInfo = ((IList <Merchant>)((GenericPaginationResponse <Merchant>)actualRecord).ReturnedRecords).Where(x => x.MID == mid).FirstOrDefault().Name; // Assert var recordCount = ((GenericPaginationResponse <Merchant>)actualRecord).ReturnedRecords; Assert.Equal(recordCount.ToList().Count, 2); Assert.Equal(merchInfo, "ABC Corp"); }
public async Task <IActionResult> GetMerchantList([FromBody] MerchantListInput pageinput) { try { int custId = Convert.ToInt32(pageinput.LIDValue); await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "start of calling the Merchant List controller for CustomerID - " + custId + " resultset", "MerchantListController.cs", "GetMerchantList"), CancellationToken.None); PaginationMerchant page = pageinput.Page; var key = UniqueCachingKey(pageinput); if (!ModelState.IsValid) { await _loggingFacade.LogAsync(new LogEntry(LogLevels.Error, ModelState.ToString(), "MerchantListController.cs", "GetMerchantList"), CancellationToken.None); return(BadRequest(ModelState)); } //first check if the data is in cache.. var data = _operation.RetrieveCache(key, new GenericPaginationResponse <Merchant>()); await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "calling the service(GetMerchantListAsync) for Merchant List resultset", "MerchantListController.cs", "GetMerchantList"), CancellationToken.None); var result = await _merchantListApi.GetMerchantListAsync(custId, page); if (result.ErrorMessages.Count == 0) { if (result.Result != null && result.Result.TotalNumberOfRecords > 0) { await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "Fetched the Merchant List resultset", "MerchantListController.cs", "GetMerchantList"), CancellationToken.None); await _operation.AddCacheAsync(key, result.Result); return(Ok(result.Result)); } else { var msg = this._localizer["NoDataFound"]?.Value; await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, msg + "while Fetching the Merchant List resultset", "MerchantListController.cs", "GetMerchantList"), CancellationToken.None); result.Result.ModelMessage = msg; return(Ok(result.Result)); } } await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "Fetched the Merchant List resultset from Cache key for CustomerID - " + key, "MerchantListController.cs", "GetMerchantList"), CancellationToken.None); return(Ok(data)); } catch (Exception ex) { var msg = this._localizer?["InternalServerError"]?.Value; await _loggingFacade.LogExceptionAsync(ex, this.HttpContext?.Request?.Headers["UserName"], LogLevels.Error, "Error in GetMerchantList()", CancellationToken.None); return(this.StatusCode((int)System.Net.HttpStatusCode.InternalServerError, msg)); } }