public async Task MerchantListApiTest_Exception() { // Arrange int CustomerID = 191809; MockMerchantListRepository mockMerchantListRepository = new MockMerchantListRepository(); ApiResult <GenericPaginationResponse <Merchant> > expectedResult = mockMerchantListRepository.GetMockData(CustomerID); PaginationMerchant page = mockMerchantListRepository.GetPagination(); IOptions <Settings> optionsAccessor = Substitute.For <IOptions <Settings> >(); IMerchantListRepository mockRepo = Substitute.For <IMerchantListRepository>(); IMerchantListApi merchantListApi = Substitute.For <IMerchantListApi>(); IDistributedCache mockCache = Substitute.For <IDistributedCache>(); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); mockRepo.GetMerchantListAsync(CustomerID, page).Throws(new Exception()); merchantListApi = new MerchantListApi(optionsAccessor, mockRepo, loggingFacade); //Assert await Assert.ThrowsAsync <Exception>(() => merchantListApi.GetMerchantListAsync(CustomerID, page)); }
//Would be revisiting to modify the actual way of call method. public void MerchantListRepositoryTest_Success() { // Arrange int CustomerID = 191809; string mid = "191807"; MockMerchantListRepository mockMerchantListRepository = new MockMerchantListRepository(); ApiResult <GenericPaginationResponse <Merchant> > expectedResult = mockMerchantListRepository.GetMockData(CustomerID); PaginationMerchant page = mockMerchantListRepository.GetPagination(); IOptions <DataContext> optionsAccessor = Substitute.For <IOptions <DataContext> >(); IDatabaseConnectionFactory connectionFactory = Substitute.For <IDatabaseConnectionFactory>(); IMerchantListRepository mockRepo = Substitute.For <IMerchantListRepository>(); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); mockRepo.GetMerchantListAsync(CustomerID, page).ReturnsForAnyArgs(expectedResult.Result); // Act var merchList = mockRepo.GetMerchantListAsync(CustomerID, page).Result; var actualRecord = (IList <Wp.CIS.LynkSystems.Model.Merchant>)merchList.ReturnedRecords; string merchInfo = actualRecord.Where(x => x.MID == mid).FirstOrDefault().Name; //// Assert Assert.Equal(((IList <Merchant>)actualRecord).Count, 2); Assert.Equal(merchInfo, "ABC Corp"); }
public PaginationMerchant GetPagination() { PaginationMerchant page = new PaginationMerchant(); page.FilterMID = "MerchantID"; page.FilterName = ""; return(page); }
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"); }
//Forming the Unique ChacheId private string UniqueCachingKey(MerchantListInput pageinput) { int custId = Convert.ToInt32(pageinput.LIDValue); PaginationMerchant page = pageinput.Page; var key = _localizer["UniqueKeyMerchantList"] + "_" + custId; if (page.PageSize > 0) { key = key + "_PageSize_" + page.PageSize; } if (page.SkipRecordNumber > 0) { key = key + "_SkipRecord_" + page.SkipRecordNumber; } if (page.SortField != null) { key = key + "_" + page.SortField + "_" + page.SortFieldByAsc; } if (page.FilterMID != null) { key = key + "_FilterMID_" + page.FilterMID; } if (page.FilterName != null) { key = key + "_FilterName_" + page.FilterName; } if (page.FilterState != null) { key = key + "_FilterState_" + page.FilterState; } if (page.FilterStatusIndicator != null) { key = key + "_FilterStatusIndicator_" + page.FilterStatusIndicator; } if (page.FilterZipCode != null) { key = key + "_FilterZipCode_" + page.FilterZipCode; } return(key); }
public void MerchantListControllerTest_ModelState_Invalid() { //Arrange int CustomerID = 191807; IConfigurationRoot configurationRoot = Substitute.For <IConfigurationRoot>(); configurationRoot = GetConfiguration(configurationRoot); MockMerchantListRepository mockMerchantListRepository = new MockMerchantListRepository(); ApiResult <GenericPaginationResponse <Merchant> > expectedResult = mockMerchantListRepository.GetMockData(CustomerID); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); PaginationMerchant page = mockMerchantListRepository.GetPagination(); MerchantListInput pageinput = new MerchantListInput(); pageinput.LIDValue = CustomerID.ToString(); pageinput.lidTypeEnum = Wp.CIS.LynkSystems.Model.Enums.LidTypeEnum.Customer; pageinput.Page = page; IStringLocalizer <MerchantListController> localizer = Substitute.For <IStringLocalizer <MerchantListController> >(); IMerchantListApi merchantListApi = Substitute.For <IMerchantListApi>(); IDistributedCache mockCache = FakeCache(); IOperation fakeOperation = Substitute.ForPartsOf <Operation>(mockCache); MerchantListController controller = new MerchantListController(mockCache, merchantListApi, localizer, fakeOperation, loggingFacade); //Act controller.ModelState.AddModelError("key", "error message"); var result = controller.GetMerchantList(pageinput); //Assert Assert.Equal(((Microsoft.AspNetCore.Mvc.ObjectResult)result.Result).StatusCode.ToString(), "400"); }
/// <summary> /// Model - Merchant /// </summary> /// <param name="custId"></param> /// <returns></returns> public async Task <GenericPaginationResponse <Merchant> > GetMerchantListAsync(int custId, PaginationMerchant page) { try { var response = new GenericPaginationResponse <Merchant> { SkipRecords = page.SkipRecordNumber, PageSize = page.PageSize }; return(await this._connectionFactory.GetConnection(async c => { // await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "Open Dapper Connection of SQL server for Merchant List Repository for CustomerID - " + custId, // "MerchantListRepository.cs", "GetMerchantListAsync()"), CancellationToken.None); var p = new DynamicParameters(); p.Add("CustomerID", custId, DbType.String); if (page.SortField != null) { p.Add("SortField", page.SortField, DbType.String); p.Add("@SortByAsc", page.SortFieldByAsc, DbType.Boolean); } if (page.FilterMID != null) { p.Add("FilterByMID", page.FilterMID, DbType.String); } if (page.FilterName != null) { p.Add("FilterByName", page.FilterName, DbType.String); } if (page.FilterState != null) { p.Add("FilterByState", page.FilterState, DbType.String); } if (page.FilterStatusIndicator != null) { p.Add("FilterByStatusIndicator", page.FilterStatusIndicator, DbType.String); } if (page.FilterZipCode != null) { p.Add("FilterByZipCode", page.FilterZipCode, DbType.String); } if (page.PageSize != 0) { p.Add("PageSize", page.PageSize, DbType.Int16); } if (page.SkipRecordNumber != 0) { p.Add("SkipRecordNumber", page.SkipRecordNumber, DbType.Int16); } p.Add("TotalRecordsCount", DbType.Int32, direction: ParameterDirection.Output); p.Add("MaximumRecsLimit", maxRecordCount, DbType.Int16); await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "Dapper Connection parameterized Query for Merchant List Repository for CustomerID - " + custId, "MerchantListRepository.cs", "GetMerchantListAsync()"), CancellationToken.None); response.ReturnedRecords = await c.QueryAsync <Merchant>(sql: "[CISPlus].[uspCISPlusGetMerchantListByCustomer]", param: p, commandType: CommandType.StoredProcedure); response.TotalNumberOfRecords = p.Get <int>("TotalRecordsCount"); await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "Successful DB call for Merchant List Repository Query result for CustomerID - " + custId, "MerchantListRepository.cs", "GetMerchantListAsync()"), CancellationToken.None); return response; })); } catch (Exception ex) { var msg = String.Format("{0}.GetMerchantListAsync() experienced an exception (not a timeout) for CustomerId " + custId, GetType().FullName); await _loggingFacade.LogAsync(new LogEntry(LogLevels.Error, msg + ex.ToString() + ex.Message, "MerchantListRepository.cs", "GetMerchantListAsync()"), CancellationToken.None); throw; } }
/// <summary> /// CustomerId is input to retrieve the List of Merchant. /// </summary> /// <param name="CustId"></param> /// <returns></returns> public async Task <ApiResult <GenericPaginationResponse <Merchant> > > GetMerchantListAsync(int CustId, PaginationMerchant page) { await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "Starting Merchant List GetMerchantListAsync for CustomerID -" + CustId, "MerchantListApi.cs", "GetMerchantListAsync"), CancellationToken.None); ApiResult <GenericPaginationResponse <Merchant> > response = new ApiResult <GenericPaginationResponse <Merchant> >(); try { response.Result = await _merchantRepository.GetMerchantListAsync(CustId, page); await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "Fetched the Merchant List resultset from DB for CustomerID -" + CustId, "MerchantListApi.cs", "GetMerchantListAsync"), CancellationToken.None); } catch (Exception) { throw; } return(response); }
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)); } }