public async void WhenRequestMessagesProvided_AndReturnXmlConent_ServiceShouldReturnOffers() { //Arrange string xmlContent = @"<xml><source>Source Address 1</source><destination /><quote>73.28</quote><packages><package/></packages></xml>"; var mockRequestMessage = new HttpRequestMessage(HttpMethod.Get, mockUri); var mockHttpRequestMessages = new List <HttpRequestMessage> { mockRequestMessage }; var httpClientFactoryMock = Substitute.For <IHttpClientFactory>(); var mockHttpMessageHandler = new MockHttpMessageHandler(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent(xmlContent, Encoding.UTF8, "application/xml") }); var fakeHttpClient = new HttpClient(mockHttpMessageHandler); httpClientFactoryMock.CreateClient().Returns(fakeHttpClient); //Act var service = new OfferService(httpClientFactoryMock); var result = await service.GetAllOffers(mockHttpRequestMessages); //Assert result .Should() .BeOfType <List <OfferDto> >() .And .HaveCount(1) .And .Contain(x => x.OfferAmount == 73.28 && x.SourceAddress.Equals("Source Address 1")); }
public IHttpActionResult Get() { OfferService offerService = CreateOfferService(); var offers = offerService.GetAllOffers(); return(Ok(offers)); }
public async void WhenRequestMessagesProvidedButNoOutputReturn_ServiceShouldReturnNoResult() { //Arrange var offerDtos = new List <OfferDto>(); var mockRequestMessage = new HttpRequestMessage(HttpMethod.Get, mockUri); var mockHttpRequestMessages = new List <HttpRequestMessage> { mockRequestMessage }; var httpClientFactoryMock = Substitute.For <IHttpClientFactory>(); var mockHttpMessageHandler = new MockHttpMessageHandler(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(offerDtos), Encoding.UTF8, "application/json") }); var fakeHttpClient = new HttpClient(mockHttpMessageHandler); httpClientFactoryMock.CreateClient().Returns(fakeHttpClient); //Act var service = new OfferService(httpClientFactoryMock); var result = await service.GetAllOffers(mockHttpRequestMessages); //Assert result .Should() .BeOfType <List <OfferDto> >() .And .HaveCount(0); }
public async void WhenNoRequestMessagesProvided_ServiceShouldReturnNoResult() { //Arrange var httpClientFactoryMock = Substitute.For <IHttpClientFactory>(); var mockHttpMessageHandler = new MockHttpMessageHandler(new HttpResponseMessage() { StatusCode = HttpStatusCode.BadRequest }); var fakeHttpClient = new HttpClient(mockHttpMessageHandler); httpClientFactoryMock.CreateClient().Returns(fakeHttpClient); //Act var service = new OfferService(httpClientFactoryMock); var result = await service.GetAllOffers(null); //Assert result .Should() .BeOfType <List <OfferDto> >() .And .HaveCount(0); }
// GET: Offer public ActionResult Index() { OfferListViewModel model = new OfferListViewModel(); model.OfferList = new List <OfferViewModel>(); //Pull data from database and display in table. CompanyService companyService = new CompanyService(); OfferService offerService = new OfferService(); var company = base.UserCompany; var offerList = offerService.GetAllOffers(company.Id); foreach (var item in offerList) { OfferViewModel offerViewModel = new OfferViewModel(); offerViewModel.Title = item.Title; offerViewModel.Description = item.Description; offerViewModel.Url = item.Url; offerViewModel.Category = item.Category; offerViewModel.TotalClaimed = item.TotalClaimed; offerViewModel.TotalCodes = item.TotalCodes; model.OfferList.Add(offerViewModel); offerViewModel.Id = item.Id; } return(View(model)); }
public async void WhenRequestMessagesProvided_ServiceShouldReturnAllOffers() { //Arrange var offerDtos = new List <IOfferDto>() { offer1, offer2, offer3 }; var mockRequestMessage = new HttpRequestMessage(HttpMethod.Get, mockUri); var mockHttpRequestMessages = new List <HttpRequestMessage> { mockRequestMessage }; var httpClientFactoryMock = Substitute.For <IHttpClientFactory>(); var mockHttpMessageHandler = new MockHttpMessageHandler(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(offerDtos), Encoding.UTF8, "application/json") }); var fakeHttpClient = new HttpClient(mockHttpMessageHandler); httpClientFactoryMock.CreateClient().Returns(fakeHttpClient); //Act var service = new OfferService(httpClientFactoryMock); var result = await service.GetAllOffers(mockHttpRequestMessages); //Assert result .Should() .BeOfType <List <OfferDto> >() .And .HaveCount(3) .And .Contain(x => x.OfferAmount == 73.28 && x.SourceAddress.Equals("Source Address 1")) .And .Contain(x => x.OfferAmount == 30.12 && x.SourceAddress.Equals("Source Address 2")) .And .Contain(x => x.OfferAmount == 54.37 && x.SourceAddress.Equals("Source Address 3")); }
public void GetAllOffers() { CompanyService companyService = new CompanyService(); OfferService offerService = new OfferService(); //Get all companies List <Company> testCompanyList = new List <Company>(); testCompanyList = companyService.GetAll(); foreach (var item in testCompanyList) { companyService.Delete(item.Id); } //Create companies and verify offer count Company testCompany1 = new Company(); testCompany1.Name = "No. One inc."; testCompany1.UserId = "co1" + RandomDigits(); Company testCompany2 = new Company(); testCompany2.Name = "No. Two inc."; testCompany2.UserId = "co2" + RandomDigits(); companyService.Save(testCompany1); //Id = 41 companyService.Save(testCompany2); //Id = 42 Offer testOffer11 = new Offer(); testOffer11.Title = "Banana peelers 5% off!"; testOffer11.Description = "Peel yer' nanners!"; testOffer11.Url = "fakebananapeeler.bamazon.com/"; testOffer11.Category = "Kitchen Supplies"; testOffer11.CompanyId = testCompany1.Id; //Saving offers of first company(First digit = company number, second digit = offer number) offerService.SaveOffer(testOffer11); //Creating offers for second company Offer testOffer21 = new Offer(); testOffer21.Title = "Fishing Rods 10% off!"; testOffer21.Description = "Get yer' fishin' rods here!"; testOffer21.Url = "fakefishingrod.bamazon.com/"; testOffer21.Category = "Fishing"; testOffer21.CompanyId = testCompany2.Id; Offer testOffer22 = new Offer(); testOffer22.Title = "Fishing bait 20% off!"; testOffer22.Description = "We got worms here real cheap!"; testOffer22.Url = "fakefishingbait.bamazon.com/"; testOffer22.Category = "Fishing"; testOffer22.CompanyId = testCompany2.Id; //Saving offers of second company(First digit = company number, second digit = offer number) offerService.SaveOffer(testOffer21); offerService.SaveOffer(testOffer22); List <OfferExtended> offerList1 = new List <OfferExtended>(); offerList1 = offerService.GetAllOffers(testCompany1.Id);//41 because id of company1 == 41 Assert.IsTrue(offerList1.Count == 1); List <OfferExtended> offerList2 = new List <OfferExtended>(); offerList2 = offerService.GetAllOffers(testCompany2.Id); Assert.IsTrue(offerList2.Count == 2); var companyList = companyService.GetAll(); foreach (var item in companyList) { var offerList = offerService.GetAllOffers(item.Id); foreach (var item2 in offerList1) { offerService.DeleteOffer(item2.Id); } companyService.Delete(item.Id); } }