Esempio n. 1
0
        public void RunSubmitAdRequest_Returns_Empty_List_If_No_Ads()
        {
            var mockery = new MockRepository();
              var restClient = mockery.StrictMock<IRestClient>();
              var restRequestFactory = mockery.StrictMock<IRestRequestFactory>();
              var restRequest = mockery.StrictMock<IRestRequest>();
              var serializer = mockery.Stub<ISerializer>();

              var str = "some data";

              var adRequest = new AdRequest { NetworkId = Guid.NewGuid().ToString() };

              var advertisementResponse = new AdvertisementResponseMessage { advertisement = null };
              var restResponse = new RestResponse<AdvertisementResponseMessage>();
              restResponse.Data = advertisementResponse;

              using (mockery.Record()) {
            Expect.Call(restRequestFactory.Create(null, Method.POST))
              .Constraints(Is.Anything(), Is.Equal(Method.POST))
              .Return(restRequest);
            restRequest.RequestFormat = DataFormat.Json;
            Expect.Call(restRequest.JsonSerializer).Return(serializer);
            Expect.Call(serializer.Serialize(null)).Constraints(
              Rhino.Mocks.Constraints.Property.Value("network_id", adRequest.NetworkId) &&
              Is.TypeOf<AdRequestMessage>()
            ).Return(str);
            Expect.Call(restRequest.AddParameter("text/json", str, ParameterType.RequestBody)).Return(new RestRequest());
            Expect.Call(restClient.Execute<AdvertisementResponseMessage>(restRequest)).Return(restResponse);
              }

              using (mockery.Playback()) {
            var results = new AdRequestor(restClient, restRequestFactory).RunSubmitAdRequest(adRequest);
            Assert.IsEmpty(results);
              }
        }
Esempio n. 2
0
        public void SubmitAdRequest_Success()
        {
            var mockery = new MockRepository();
              var adRequestor = mockery.StrictMock<IAdRequestor>();

              var adRequest = new AdRequest();

              var ads = new List<Advertisement> { new Advertisement() };

              using (mockery.Record()) {
            Expect.Call(adRequestor.RunSubmitAdRequest(adRequest)).Return(ads);
              }

              using (mockery.Playback()) {
            var client = new ApiClient(adRequestor);
            var rtn = client.SubmitAdRequest(adRequest);
            Assert.AreEqual(ads, rtn);
              }
        }
Esempio n. 3
0
        public List<Advertisement> RunSubmitAdRequest(AdRequest request)
        {
            var restRequest = requestFactory.Create(RESOURCE, METHOD);
              restRequest.RequestFormat = DataFormat.Json;
              string data = restRequest.JsonSerializer.Serialize(request.ToMessage());
              restRequest.AddParameter("text/json", data, ParameterType.RequestBody);

              try {
            var response = restClient
              .Execute<AdvertisementResponseMessage>(restRequest);

            Console.WriteLine("{0}\t{1}\t{2}", DateTime.Now, (int)response.StatusCode, response.Content);

            var ads = new List<Advertisement>();
            if (response.Data == null && response.ErrorMessage != null) {
              if (VistarGlobals.IsDebug) {
            Console.WriteLine("Raw Response: {0}", response.Content);
              }
              throw new ApiException("AdRequest Failed: " + response.ErrorMessage);
            }
            else if (response.Data == null) {
              if (VistarGlobals.IsDebug) {
            Console.WriteLine("Raw Response: {0}", response.Content);
              }
              throw new ApiException("AdRequest Failed.");
            }

            if (response.Data.advertisement != null) {
              ads = response.Data.advertisement
            .Select(Advertisement.FromMessage).ToList();
            }
            return ads;
              }
              catch (Exception ex) {
            throw new ApiException(ex.Message);
              }
        }
Esempio n. 4
0
 public List<Advertisement> SubmitAdRequest(AdRequest request)
 {
     return adRequestor.RunSubmitAdRequest(request);
 }
Esempio n. 5
0
        public void RunSubmitAdRequest_Throws_ApiException_When_Error()
        {
            var mockery = new MockRepository();
              var restClient = mockery.StrictMock<IRestClient>();
              var restRequestFactory = mockery.StrictMock<IRestRequestFactory>();
              var restRequest = mockery.DynamicMock<IRestRequest>();
              var serializer = mockery.Stub<ISerializer>();

              var str = "some data";

              var error = "Test error message";
              var adRequest = new AdRequest { NetworkId = Guid.NewGuid().ToString() };

              using (mockery.Record()) {
            Expect.Call(restRequestFactory.Create(null, Method.POST))
              .Constraints(Is.Anything(), Is.Equal(Method.POST))
              .Return(restRequest);
            restRequest.RequestFormat = DataFormat.Json;
            Expect.Call(restRequest.JsonSerializer).Return(serializer);
            Expect.Call(serializer.Serialize(null)).Constraints(
              Rhino.Mocks.Constraints.Property.Value("network_id", adRequest.NetworkId) &&
              Is.TypeOf<AdRequestMessage>()
            ).Return(str);
            Expect.Call(restRequest.AddParameter("text/json", str, ParameterType.RequestBody)).Return(new RestRequest());
            Expect.Call(restClient.Execute<AdvertisementResponseMessage>(restRequest)).Throw(new Exception(error));
              }

              using (mockery.Playback()) {
            var ex = Assert.Throws(typeof(ApiException), () => {
              new AdRequestor(restClient, restRequestFactory).RunSubmitAdRequest(adRequest);
            });

            Assert.AreEqual(error, ex.Message);
              }
        }
 public Task<List<Advertisement>> StartNew(Func<AdRequest, List<Advertisement>> method, AdRequest adRequest)
 {
     return Task.Factory.StartNew(() => method(adRequest));
 }