public void ContactListControllerTest_ModelState_Invalid() { //Arrange var page = GetContactListObject(); string ssn = "3425"; MockContactListRepository mockContactListRepository = new MockContactListRepository(); ApiResult <GenericPaginationResponse <Demographics> > expectedResult = mockContactListRepository.GetMockData(ssn); IStringLocalizer <ContactListController> localizer = Substitute.For <IStringLocalizer <ContactListController> >(); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); IContactListApi contactListApi = Substitute.For <IContactListApi>(); IDistributedCache mockCache = FakeCache(); IOperation fakeOperation = Substitute.ForPartsOf <Operation>(mockCache); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); ContactListController controller = new ContactListController(mockCache, contactListApi, localizer, fakeOperation, loggingFacade); //Act controller.ModelState.AddModelError("key", "error message"); var result = controller.GetContactList(page); //Assert Assert.Equal(((Microsoft.AspNetCore.Mvc.ObjectResult)result.Result).StatusCode.ToString(), "400"); }
public async Task ContactListApiTest_Exception() { // Arrange LidTypeEnum LIDType = LidTypeEnum.CustomerNbr; string LID = ""; PaginationDemographics page = new PaginationDemographics(); string ssn = "3425"; MockContactListRepository mockContactListRepository = new MockContactListRepository(); ApiResult <GenericPaginationResponse <Demographics> > expectedResult = mockContactListRepository.GetMockData(ssn); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); IOptions <Settings> optionsAccessor = Substitute.For <IOptions <Settings> >(); IContactListRepository mockRepo = Substitute.For <IContactListRepository>(); IContactListApi contactListApi = Substitute.For <IContactListApi>(); IDistributedCache mockCache = Substitute.For <IDistributedCache>(); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); mockRepo.GetContactListAsync(LIDType, LID, page).Throws(new Exception()); contactListApi = new ContactListApi(optionsAccessor, mockRepo, loggingFacade); //Assert await Assert.ThrowsAsync <Exception>(() => contactListApi.GetContactListAsync(LIDType, LID, page)); }
//Would be revisiting to modify the actual way of call method. public void ContactListRepositoryTest_Success() { // Arrange LidTypeEnum LIDType = LidTypeEnum.CustomerNbr; string LID = ""; PaginationDemographics page = new PaginationDemographics(); int NameAddressID = 3301636; string ssn = "3425"; MockContactListRepository mockContactListRepository = new MockContactListRepository(); ApiResult <GenericPaginationResponse <Demographics> > expectedResult = mockContactListRepository.GetMockData(ssn); IOptions <DataContext> optionsAccessor = Substitute.For <IOptions <DataContext> >(); IDatabaseConnectionFactory connectionFactory = Substitute.For <IDatabaseConnectionFactory>(); IContactListRepository mockRepo = Substitute.For <IContactListRepository>(); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); mockRepo.GetContactListAsync(LIDType, LID, page).ReturnsForAnyArgs(expectedResult.Result); // Act var contactList = mockRepo.GetContactListAsync(LIDType, LID, page).Result; var actualRecord = (IList <Wp.CIS.LynkSystems.Model.Demographics>)contactList.ReturnedRecords; string contactInfo = actualRecord.Where(x => x.NameAddressID == NameAddressID).FirstOrDefault().Name; //// Assert Assert.Equal(((IList <Demographics>)actualRecord).Count, 3); Assert.Equal(contactInfo, "Golden Corral Corporation"); }
public async Task ContactListControllerTest_NoDataFound() { // Arrange var page = GetContactListObject(); page.lidTypeEnum = LidTypeEnum.Terminal; string ssn = "3425"; IConfigurationRoot configurationRoot = Substitute.For <IConfigurationRoot>(); configurationRoot = GetConfiguration(configurationRoot); MockContactListRepository mockContactListRepository = new MockContactListRepository(); ApiResult <GenericPaginationResponse <Demographics> > expectedResult = mockContactListRepository.GetMockData(ssn); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); IDistributedCache mockCache = Substitute.For <IDistributedCache>(); IStringLocalizer <ContactListController> localizer = Substitute.For <IStringLocalizer <ContactListController> >(); string key = "NoDataFound"; string value = "No data found for provided ID"; var localizedString = new LocalizedString(key, value); localizer[Arg.Any <string>()].ReturnsForAnyArgs(localizedString); IContactListApi contactListApi = Substitute.For <IContactListApi>(); IOperation fakeOperation = Substitute.For <Operation>(mockCache); fakeOperation.WhenForAnyArgs(x => x.RetrieveCache(Arg.Any <string>(), Arg.Any <GenericPaginationResponse <Demographics> >())).DoNotCallBase(); ApiResult <GenericPaginationResponse <Demographics> > response = new ApiResult <GenericPaginationResponse <Demographics> >(); response.Result = new GenericPaginationResponse <Demographics>(); contactListApi.GetContactListAsync(page.lidTypeEnum, page.LIDValue, page.Page).ReturnsForAnyArgs(response); ContactListController controller = new ContactListController(mockCache, contactListApi, localizer, fakeOperation, loggingFacade); // Act var contactList = await controller.GetContactList(page); // Assert Assert.Equal(((Microsoft.AspNetCore.Mvc.ObjectResult)contactList).StatusCode, 200); var actualContactList = ((Microsoft.AspNetCore.Mvc.ObjectResult)contactList).Value; Assert.Equal(((GenericPaginationResponse <Demographics>)actualContactList).ModelMessage, localizer["NoDataFound"].Value); }
public async Task ContactListControllerTest_APICallHasErrorMessage() { // Arrange var page = GetContactListObject(); string ssn = "3425"; IConfigurationRoot configurationRoot = Substitute.For <IConfigurationRoot>(); configurationRoot = GetConfiguration(configurationRoot); MockContactListRepository mockContactListRepository = new MockContactListRepository(); ApiResult <GenericPaginationResponse <Demographics> > expectedResult = mockContactListRepository.GetMockData(ssn); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); IDistributedCache mockCache = Substitute.For <IDistributedCache>(); IOperation fakeOperation = Substitute.For <Operation>(mockCache); fakeOperation.WhenForAnyArgs(x => x.RetrieveCache(Arg.Any <string>(), Arg.Any <ICollection <Demographics> >())).DoNotCallBase(); IStringLocalizer <ContactListController> localizer = Substitute.For <IStringLocalizer <ContactListController> >(); string key = "InternalServerError"; string value = "Some Server Error Occurs while retrieving the data"; var localizedString = new LocalizedString(key, value); localizer[Arg.Any <string>()].ReturnsForAnyArgs(localizedString); IContactListApi contactListApi = Substitute.For <IContactListApi>(); ContactListController controller = new ContactListController(mockCache, contactListApi, localizer, fakeOperation, loggingFacade); ApiResult <GenericPaginationResponse <Demographics> > response = new ApiResult <GenericPaginationResponse <Demographics> >(); var errorkey = GlobalErrorCode.InternalServerError; response.AddErrorMessage(errorkey.ToString()); contactListApi.GetContactListAsync(page.lidTypeEnum, page.LIDValue, page.Page).ReturnsForAnyArgs(response);; // Act var contactList = await controller.GetContactList(page); // Assert Assert.Equal(((Microsoft.AspNetCore.Mvc.ObjectResult)contactList).StatusCode, 500); Assert.Equal(((Microsoft.AspNetCore.Mvc.ObjectResult)contactList).Value, localizer["InternalServerError"].Value); }
//Mock API Call and unit test for the API call with returning mock ContactList. public async Task ContactListControllerTest_Success() { // Arrange var page = GetContactListObject(); int NameAddressID = 3301636; string ssn = "3425"; IConfigurationRoot configurationRoot = Substitute.For <IConfigurationRoot>(); configurationRoot = GetConfiguration(configurationRoot); MockContactListRepository mockContactListRepository = new MockContactListRepository(); ApiResult <GenericPaginationResponse <Demographics> > expectedResult = mockContactListRepository.GetMockData(ssn); ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); loggingFacade.WhenForAnyArgs(x => x.LogAsync(Arg.Any <LogLevels>(), Arg.Any <string>(), Arg.Any <CancellationToken>())).DoNotCallBase(); IDistributedCache mockCache = Substitute.For <IDistributedCache>(); IContactListRepository mockRepo = Substitute.For <IContactListRepository>(); IStringLocalizer <ContactListController> localizer = Substitute.For <IStringLocalizer <ContactListController> >(); IContactListApi mockContactListApi = Substitute.For <IContactListApi>(); IOperation fakeOperation = Substitute.For <Operation>(mockCache); fakeOperation.WhenForAnyArgs(x => x.RetrieveCache(Arg.Any <string>(), Arg.Any <GenericPaginationResponse <Demographics> >())).DoNotCallBase(); fakeOperation.WhenForAnyArgs(x => x.AddCacheAsync(Arg.Any <string>(), Arg.Any <GenericPaginationResponse <Demographics> >())).DoNotCallBase(); ContactListController controller = new ContactListController(mockCache, mockContactListApi, localizer, fakeOperation, loggingFacade); mockContactListApi.GetContactListAsync(page.lidTypeEnum, page.LIDValue, page.Page).ReturnsForAnyArgs(expectedResult); // Act var contactList = await controller.GetContactList(page); var actualRecord = ((Microsoft.AspNetCore.Mvc.ObjectResult)contactList).Value; string contactInfo = ((IList <Demographics>)(((GenericPaginationResponse <Demographics>)actualRecord).ReturnedRecords)).Where(x => x.NameAddressID == NameAddressID).FirstOrDefault().Name; // Assert var recordCount = ((GenericPaginationResponse <Demographics>)actualRecord).ReturnedRecords; Assert.Equal(recordCount.ToList().Count, 3); Assert.Equal(contactInfo, "Golden Corral Corporation"); }
//Unit Test for the RetrieveCache() public async Task ContactListControllerTest_GetDataFromCache() { var page = GetContactListObject(); page.lidTypeEnum = LidTypeEnum.Terminal; page.LIDValue = "123456"; string ssn = "3425"; ILoggingFacade loggingFacade = Substitute.For <ILoggingFacade>(); MockContactListRepository mockContactListRepository = new MockContactListRepository(); ApiResult <GenericPaginationResponse <Demographics> > expectedResult = mockContactListRepository.GetMockData(ssn); IDistributedCache mockCache = Substitute.For <IDistributedCache>(); IContactListRepository mockRepo = Substitute.For <IContactListRepository>(); IStringLocalizer <ContactListController> localizer = Substitute.For <IStringLocalizer <ContactListController> >(); IContactListApi mockContactListApi = Substitute.For <IContactListApi>(); 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 <Demographics> >())).DoNotCallBase(); fakeOperation.RetrieveCache("_123456", new GenericPaginationResponse <Demographics>()).ReturnsForAnyArgs(expectedResult.Result); ContactListController controller = new ContactListController(mockCache, mockContactListApi, localizer, fakeOperation, loggingFacade); //ACT var contactList = await controller.GetContactList(page); var actualRecord = ((Microsoft.AspNetCore.Mvc.ObjectResult)contactList).Value; //Assert Assert.Equal(JsonConvert.SerializeObject(actualRecord), JsonConvert.SerializeObject(expectedResult.Result)); }