public async Task TerminalListApiTest_GetAnException() { // Arrange int TerminalNbr = 589587; MockTerminalListRepository mockTerminalListRepository = new MockTerminalListRepository(); ApiResult <GenericPaginationResponse <Terminal> > expectedResult = mockTerminalListRepository.GetMockData(TerminalNbr); PaginationTerminal page = mockTerminalListRepository.GetPagination(); IOptions <Settings> optionsAccessor = Substitute.For <IOptions <Settings> >(); ITerminalListRepository mockRepo = Substitute.For <ITerminalListRepository>(); 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(); ITerminalListApi terminalListApi = Substitute.For <ITerminalListApi>(); terminalListApi.WhenForAnyArgs(x => x.GetTerminalListAsync(Arg.Any <int>(), Arg.Any <PaginationTerminal>())).DoNotCallBase(); mockRepo.GetTerminalListAsync(TerminalNbr, page).Throws(new Exception()); terminalListApi = new TerminalListApi(optionsAccessor, mockRepo, loggingFacade); // Act var terminalList = await terminalListApi.GetTerminalListAsync(TerminalNbr, page); // Assert Assert.Equal(((IList <string>)terminalList.ErrorMessages).First(), "InternalServerError"); }
//Would be revisiting to modify the actual way of call method. public void TestTerminalListRepositoryTest_Success() { // Arrange int terminalNbr = 589587; string tid = "LK429486"; MockTerminalListRepository mockTerminalListRepository = new MockTerminalListRepository(); ApiResult <GenericPaginationResponse <Terminal> > expectedResult = mockTerminalListRepository.GetMockData(terminalNbr); PaginationTerminal page = mockTerminalListRepository.GetPagination(); IOptions <DataContext> optionsAccessor = Substitute.For <IOptions <DataContext> >(); IDatabaseConnectionFactory connectionFactory = Substitute.For <IDatabaseConnectionFactory>(); ITerminalListRepository mockRepo = Substitute.For <ITerminalListRepository>(); mockRepo.GetTerminalListAsync(terminalNbr, page).ReturnsForAnyArgs(expectedResult.Result); // Act var terminalList = mockRepo.GetTerminalListAsync(terminalNbr, page).Result; var actualRecord = (IList <Wp.CIS.LynkSystems.Model.Terminal>)terminalList.ReturnedRecords; string merchInfo = actualRecord.Where(x => x.TerminalID == tid).FirstOrDefault().Software; //// Assert Assert.Equal(((IList <Terminal>)actualRecord).Count, 1); Assert.Equal(merchInfo, "LSPR3271"); }
public PaginationTerminal GetPagination() { PaginationTerminal page = new PaginationTerminal(); page.FilterTID = "MerchantID"; page.FilterSoftware = ""; return(page); }
public async Task TerminalListControllerTest_NoDataFound() { // Arrange int TerminalNbr = 589587; MockTerminalListRepository mockTerminalListRepository = new MockTerminalListRepository(); ApiResult <GenericPaginationResponse <Terminal> > expectedResult = mockTerminalListRepository.GetMockData(TerminalNbr); PaginationTerminal page = mockTerminalListRepository.GetPagination(); TerminalListInput pageinput = new TerminalListInput(); pageinput.LIDValue = TerminalNbr.ToString(); pageinput.lidTypeEnum = Wp.CIS.LynkSystems.Model.Enums.LidTypeEnum.Customer; pageinput.Page = page; IDistributedCache mockCache = Substitute.For <IDistributedCache>(); IOptions <Settings> appSettings = Substitute.For <IOptions <Settings> >(); IOperation fakeOperation = Substitute.For <Operation>(mockCache); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); IStringLocalizer <TerminalListController> localizer = Substitute.For <IStringLocalizer <TerminalListController> >(); string key = "NoDataFound"; string value = "No data found for provided ID"; var localizedString = new LocalizedString(key, value); localizer[Arg.Any <string>()].ReturnsForAnyArgs(localizedString); ITerminalListApi terminalListApi = Substitute.For <ITerminalListApi>(); ApiResult <GenericPaginationResponse <Terminal> > response = new ApiResult <GenericPaginationResponse <Terminal> >(); response.Result = new GenericPaginationResponse <Terminal>(); terminalListApi.GetTerminalListAsync(TerminalNbr, page).ReturnsForAnyArgs(response); TerminalListController fakecontroller = FakeController(mockCache, terminalListApi, localizer, fakeOperation, loggingFacade); // Act var terminalList = await fakecontroller.GetTerminalList(pageinput); // Assert Assert.Equal(((Microsoft.AspNetCore.Mvc.ObjectResult)terminalList).StatusCode, 200); var actualTerminalList = ((Microsoft.AspNetCore.Mvc.ObjectResult)terminalList).Value; Assert.Equal(((GenericPaginationResponse <Terminal>)actualTerminalList).ModelMessage, localizer["NoDataFound"].Value); }
public async Task TerminalListControllerTest_Success() { // Arrange int TerminalNbr = 589587; string TerminalID = "LK429486"; MockTerminalListRepository mockTerminalListRepository = new MockTerminalListRepository(); ApiResult <GenericPaginationResponse <Terminal> > expectedResult = mockTerminalListRepository.GetMockData(TerminalNbr); PaginationTerminal page = mockTerminalListRepository.GetPagination(); TerminalListInput pageinput = new TerminalListInput(); pageinput.LIDValue = TerminalNbr.ToString(); pageinput.lidTypeEnum = Wp.CIS.LynkSystems.Model.Enums.LidTypeEnum.Customer; pageinput.Page = page; IDistributedCache mockCache = Substitute.For <IDistributedCache>(); IOptions <Settings> appSettings = Substitute.For <IOptions <Settings> >(); IStringLocalizer <TerminalListController> localizer = Substitute.For <IStringLocalizer <TerminalListController> >(); 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 <ICollection <Terminal> >())).DoNotCallBase(); fakeOperation.WhenForAnyArgs(x => x.AddCacheAsync(Arg.Any <string>(), Arg.Any <ICollection <Terminal> >())).DoNotCallBase(); ITerminalListApi terminalListApi = Substitute.For <ITerminalListApi>(); TerminalListController controller = new TerminalListController(mockCache, terminalListApi, localizer, fakeOperation, loggingFacade); terminalListApi.GetTerminalListAsync(TerminalNbr, page).ReturnsForAnyArgs(expectedResult); // Act var terminalList = await controller.GetTerminalList(pageinput); var actualRecord = ((Microsoft.AspNetCore.Mvc.ObjectResult)terminalList).Value; string terminalInfo = ((IList <Terminal>)((GenericPaginationResponse <Terminal>)actualRecord).ReturnedRecords).Where(x => x.TerminalID == TerminalID).FirstOrDefault().Software; // Assert var recordCount = ((GenericPaginationResponse <Terminal>)actualRecord).ReturnedRecords; Assert.Equal(recordCount.ToList().Count, 1); //Assert.Equal(((IList<Terminal>)actualRecord).Count, 1); Assert.Equal(terminalInfo, "LSPR3271"); }
//Forming the Unique ChacheId private string UniqueCachingKey(TerminalListInput pageinput) { int merchantId = Convert.ToInt32(pageinput.LIDValue); PaginationTerminal page = pageinput.Page; var key = _localizer["UniqueKeyTerminalList"] + "_" + merchantId; 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.FilterDate != null) { key = key + "_FilterDate_" + page.FilterDate; } if (page.FilterSoftware != null) { key = key + "_FilterSoftware_" + page.FilterSoftware; } if (page.FilterStatus != null) { key = key + "_FilterStatus_" + page.FilterStatus; } if (page.FilterStatusEquipment != null) { key = key + "_FilterStatusEquipment_" + page.FilterStatusEquipment; } if (page.FilterTID != null) { key = key + "_FilterTID_" + page.FilterTID; } return(key); }
///Unit Test for the RetrieveCache() public async Task MerchantListControllerTest_GetDataFromCache() { int TerminalNbr = 589587; MockTerminalListRepository mockTerminalListRepository = new MockTerminalListRepository(); ApiResult <GenericPaginationResponse <Terminal> > expectedResult = mockTerminalListRepository.GetMockData(TerminalNbr); PaginationTerminal page = mockTerminalListRepository.GetPagination(); TerminalListInput pageinput = new TerminalListInput(); pageinput.LIDValue = TerminalNbr.ToString(); pageinput.lidTypeEnum = Wp.CIS.LynkSystems.Model.Enums.LidTypeEnum.Customer; pageinput.Page = page; IDistributedCache mockCache = Substitute.For <IDistributedCache>(); ITerminalListRepository mockRepo = Substitute.For <ITerminalListRepository>(); IStringLocalizer <TerminalListController> localizer = Substitute.For <IStringLocalizer <TerminalListController> >(); ITerminalListApi mockTerminalListApi = Substitute.For <ITerminalListApi>(); 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 <ICollection <Terminal> >())).DoNotCallBase(); fakeOperation.RetrieveCache("FakeStringID", new GenericPaginationResponse <Terminal>()).ReturnsForAnyArgs(expectedResult.Result); TerminalListController controller = new TerminalListController(mockCache, mockTerminalListApi, localizer, fakeOperation, loggingFacade); //ACT var terminalList = await controller.GetTerminalList(pageinput); var actualRecord = ((Microsoft.AspNetCore.Mvc.ObjectResult)terminalList).Value; //Assert Assert.Equal(JsonConvert.SerializeObject(actualRecord), JsonConvert.SerializeObject(expectedResult.Result)); }
//UnitTest for validating the Invalid Model Data. public void TerminalListController_ModelState_Invalid() { //Arrange int TerminalNbr = 589587; MockTerminalListRepository mockTerminalListRepository = new MockTerminalListRepository(); ApiResult <GenericPaginationResponse <Terminal> > expectedResult = mockTerminalListRepository.GetMockData(TerminalNbr); PaginationTerminal page = mockTerminalListRepository.GetPagination(); TerminalListInput pageinput = new TerminalListInput(); pageinput.LIDValue = TerminalNbr.ToString(); pageinput.lidTypeEnum = Wp.CIS.LynkSystems.Model.Enums.LidTypeEnum.Customer; pageinput.Page = page; IOptions <Settings> appSettings = Substitute.For <IOptions <Settings> >(); IStringLocalizer <TerminalListController> localizer = Substitute.For <IStringLocalizer <TerminalListController> >(); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); IDistributedCache mockCache = FakeCache(); ITerminalListApi terminalListApi = Substitute.For <ITerminalListApi>(); IOperation fakeOperation = Substitute.For <Operation>(mockCache); TerminalListController controller = new TerminalListController(mockCache, terminalListApi, localizer, fakeOperation, loggingFacade); //Act controller.ModelState.AddModelError("key", "error message"); var result = controller.GetTerminalList(pageinput); //Assert Assert.Equal(((Microsoft.AspNetCore.Mvc.ObjectResult)result.Result).StatusCode.ToString(), "400"); }
public void TerminalListApiTest_Success() { // Arrange int TerminalNbr = 589587; string TerminalID = "LK429486"; MockTerminalListRepository mockTerminalListRepository = new MockTerminalListRepository(); ApiResult <GenericPaginationResponse <Terminal> > expectedResult = mockTerminalListRepository.GetMockData(TerminalNbr); PaginationTerminal page = mockTerminalListRepository.GetPagination(); IOptions <Settings> optionsAccessor = Substitute.For <IOptions <Settings> >(); ITerminalListRepository mockRepo = Substitute.For <ITerminalListRepository>(); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); ITerminalListApi mockTerminalListApi = Substitute.For <ITerminalListApi>(); mockTerminalListApi.WhenForAnyArgs(x => x.GetTerminalListAsync(Arg.Any <int>(), Arg.Any <PaginationTerminal>())).DoNotCallBase(); mockRepo.GetTerminalListAsync(TerminalNbr, page).ReturnsForAnyArgs(expectedResult.Result); mockTerminalListApi = new TerminalListApi(optionsAccessor, mockRepo, loggingFacade); // Act var terminalList = mockTerminalListApi.GetTerminalListAsync(TerminalNbr, page).Result; var actualRecord = (IList <Wp.CIS.LynkSystems.Model.Terminal>)terminalList.Result.ReturnedRecords; string merchInfo = actualRecord.Where(x => x.TerminalID == TerminalID).FirstOrDefault().Software; //// Assert Assert.Equal(((IList <Terminal>)actualRecord).Count, 1); Assert.Equal(merchInfo, "LSPR3271"); }
/// <summary> /// Model - Terminal /// </summary> /// <param name="merchantId"></param> /// <returns></returns> public async Task <GenericPaginationResponse <Terminal> > GetTerminalListAsync(int merchantId, PaginationTerminal page) { try { await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "Starting Terminal List GetTransactionHistoryAsync for MerchantID " + merchantId, "TerminalListRepository.cs", "GetTerminalListAsync"), CancellationToken.None); var response = new GenericPaginationResponse <Terminal> { 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 Terminal List Repository for MerchantID " + merchantId, "TerminalListRepository.cs", "GetTerminalListAsync"), CancellationToken.None); var p = new DynamicParameters(); p.Add("MerchantID", merchantId, DbType.Int32); if (page.SortField != null) { p.Add("SortField", page.SortField, DbType.String); p.Add("SortByAsc", page.SortFieldByAsc, DbType.Boolean); } if (page.FilterTID != null) { p.Add("FilterByTID", page.FilterTID, DbType.String); } if (page.FilterDate != null) { p.Add("FilterByDate", page.FilterDate, DbType.String); } if (page.FilterSoftware != null) { p.Add("FilterBySoftware", page.FilterSoftware, DbType.String); } if (page.FilterStatus != null) { p.Add("FilterByStatus", page.FilterStatus, DbType.String); } if (page.FilterStatusEquipment != null) { p.Add("FilterByEquipment", page.FilterStatusEquipment, 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("MaximumRecsLimit", maxRecordCount, DbType.Int16); p.Add("TotalRecordsCount", DbType.Int32, direction: ParameterDirection.Output); await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "Dapper Connection parameterized Query for Terminal List Repository for MerchantID " + merchantId, "TerminalListRepository.cs", "GetTerminalListAsync()"), CancellationToken.None); response.ReturnedRecords = await c.QueryAsync <Terminal>(sql: "[CISPlus].[uspCISPlusGetTerminalListByMerchant]", param: p, commandType: CommandType.StoredProcedure); response.TotalNumberOfRecords = p.Get <int>("TotalRecordsCount"); await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "Successful DB call for Terminal List Repository Query result for MerchantID " + merchantId, "TerminalListRepository.cs", "GetTerminalListAsync()"), CancellationToken.None); return response; })); } catch (Exception ex) { var msg = String.Format("{0}.GetTerminalListAsync() experienced an exception (not a timeout) for MerchantID " + merchantId, GetType().FullName); await _loggingFacade.LogAsync(new LogEntry(LogLevels.Error, msg + ex.ToString() + ex.Message, "TerminalListRepository.cs", "GetTerminalListAsync()"), CancellationToken.None); throw; } }
/// <summary> /// /// </summary> /// <param name="TerminalID"></param> /// <returns></returns> public async Task <ApiResult <GenericPaginationResponse <Terminal> > > GetTerminalListAsync(int merchantID, PaginationTerminal Page) { await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "Starting Terminal List GetTerminalListAsync for MerchantID - " + merchantID, "TerminalListApi.cs", "GetTerminalListAsync"), CancellationToken.None); ApiResult <GenericPaginationResponse <Terminal> > response = new ApiResult <GenericPaginationResponse <Terminal> >(); var errorkey = GlobalErrorCode.Succeeded; try { response.Result = await _terminalRepository.GetTerminalListAsync(merchantID, Page); await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "fteched Terminal List resultset from DB for MerchantID - " + merchantID, "TerminalListApi.cs", "GetTerminalListAsync"), CancellationToken.None); return(response); } catch (Exception ex) { errorkey = GlobalErrorCode.InternalServerError; await _loggingFacade.LogAsync(new LogEntry(LogLevels.Error, "Error Occured for MerchantID - " + merchantID + " " + errorkey.ToString() + ex.Message, "TerminalListApi.cs", "GetTerminalListAsync"), CancellationToken.None); response.AddErrorMessage(errorkey.ToString()); return(response); } }
public async Task <IActionResult> GetTerminalList([FromBody] TerminalListInput pageinput) { try { await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "Starting Terminal List Controller", "TerminalListController.cs", "TerminalListController_HttpPost"), CancellationToken.None); int merchantId = Convert.ToInt32(pageinput.LIDValue); PaginationTerminal page = pageinput.Page; var key = UniqueCachingKey(pageinput); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //first check if the data is in cache.. var data = _operation.RetrieveCache(key, new GenericPaginationResponse <Terminal>()); if (data == null) { await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "Calling the Services(GetTerminalListAsync) the Terminal List for MerchantID - " + merchantId, "TerminalListController.cs", "GetTerminalList_HttpPost"), CancellationToken.None); var result = await _terminalListApi.GetTerminalListAsync(merchantId, page); if (result.ErrorMessages.Count == 0) { if (result.Result != null && result.Result.TotalNumberOfRecords > 0) { await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "Fetched the Terminal List resultset for MerchantID - " + merchantId, "TerminalListController.cs", "GetTerminalList_HttpPost"), CancellationToken.None); await _operation.AddCacheAsync(key, result.Result); await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "Added to Cache for the Terminal List resultset for MerchantID - " + merchantId, "TerminalListController.cs", "GetTerminalList_HttpPost"), CancellationToken.None); return(Ok(result.Result)); } else { var msg = this._localizer["NoDataFound"]?.Value; await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, msg + "while Fetching the Terminal List resultset for MerchantID - " + merchantId, "TerminalListController.cs", "GetTerminalList_HttpPost"), CancellationToken.None); result.Result.ModelMessage = msg; return(Ok(result.Result)); } } else { var msg = this._localizer?["InternalServerError"]?.Value; await _loggingFacade.LogAsync(new LogEntry(LogLevels.Error, "Error occured for MerchantID - " + merchantId + ", " + msg, "TerminalListController.cs", "GetTerminalList_HttpPost"), CancellationToken.None); return(this.StatusCode((int)System.Net.HttpStatusCode.InternalServerError, msg)); } } await _loggingFacade.LogAsync(new LogEntry(LogLevels.Info, "Fetched the Terminal List resultset from Cache key for MerchantID - " + key, "TerminalListController.cs", "GetTerminalList_HttpPost"), 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 GetTerminalList()", CancellationToken.None); return(this.StatusCode((int)System.Net.HttpStatusCode.InternalServerError, msg)); } }