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 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)); } }