Exemplo n.º 1
0
        /// <summary>
        /// Sends an order for Konduto and gets it analyzed
        /// (i.e with recommendation, score, device, geolocation and navigation info).
        /// @see <a href="http://docs.konduto.com">Konduto API Spec</a>
        /// </summary>
        /// <param name="order">a {@link KondutoOrder} instance</param>
        /// <exception cref="KondutoInvalidEntityException"></exception>
        /// <exception cref="KondutoHTTPException"></exception>
        /// <exception cref="KondutoUnexpectedAPIResponseException"></exception>
        public KondutoOrder Analyze(KondutoOrder order)
        {
            HttpClient httpClient = CreateHttpClient();

            var x = order.ToJson();

            var response = httpClient.PostAsync(KondutoPostOrderUrl(),
                                                new StringContent(order.ToJson(),
                                                                  Encoding.UTF8,
                                                                  "application/json"));

            this.requestBody = order.ToJson();

            if (response.Result.IsSuccessStatusCode)
            {
                // by calling. Result you are performing a synchronous call
                var responseContent = response.Result.Content;

                // by calling. Result you are synchronously reading the result
                String responseString = responseContent.ReadAsStringAsync().Result;

                this.responseBody = responseString;

                if (order.Analyze)
                {
                    order.MergeKondutoOrderResponse(KondutoAPIFullResponse.FromJson <KondutoAPIFullResponse>(responseString).Order);
                }

                return(order);
            }
            else
            {
                var responseContentError = response.Result.Content != null?response.Result.Content.ReadAsStringAsync().Result : "Error with response";

                throw KondutoHTTPExceptionFactory.buildException((int)response.Result.StatusCode,
                                                                 responseContentError);
            }
        }
Exemplo n.º 2
0
        public void AnalyzeSuccessfullyTest()
        {
            var fakeResponseHandler = new FakeResponseHandler();
            var message             = new HttpResponseMessage(HttpStatusCode.OK);

            message.Content = new StringContent(ANALYZE_ORDER_RESPONSE.ToString());

            fakeResponseHandler.AddFakeResponse(konduto.KondutoPostOrderUrl(), message);
            konduto.__MessageHandler = fakeResponseHandler;

            KondutoOrder orderToSend   = KondutoOrderFactory.basicOrder();
            String       s             = orderToSend.ToJson();
            KondutoOrder orderResponse = null;

            Assert.IsTrue(orderToSend.Recommendation == KondutoRecommendation.none, "basic order should have no recommendation");
            Assert.IsTrue(orderToSend.Score == null, "basic order should have no score");
            Assert.IsNull(orderToSend.Geolocation, "basic order should have no geolocation");
            Assert.IsNull(orderToSend.Device, "basic order should have no device");
            Assert.IsNull(orderToSend.NavigationInfo, "basic order should have no navigation info");
            Assert.IsTrue(orderToSend.Analyze, "basic order should have analyze set to true");

            try
            {
                orderResponse = konduto.Analyze(orderToSend); // do analyze
            }
            catch (KondutoInvalidEntityException e)
            {
                Assert.Fail("order should be valid");
            }
            catch (KondutoUnexpectedAPIResponseException e)
            {
                Assert.Fail("server should respond with status 200");
            }
            catch (KondutoHTTPException e)
            {
                Assert.Fail("server should respond with status 200");
            }

            Double?actualScore = ORDER_FROM_FILE.Score;
            KondutoRecommendation?actualRecommendation = ORDER_FROM_FILE.Recommendation;
            KondutoGeolocation    actualGeolocation    = ORDER_FROM_FILE.Geolocation;
            KondutoDevice         actualDevice         = ORDER_FROM_FILE.Device;
            KondutoNavigationInfo actualNavigationInfo = ORDER_FROM_FILE.NavigationInfo;

            Assert.IsTrue(orderResponse.Geolocation.Equals(actualGeolocation));
            Assert.AreEqual(orderResponse.Recommendation, actualRecommendation);
            Assert.AreEqual(orderResponse.Device, actualDevice);
            Assert.AreEqual(orderResponse.NavigationInfo, actualNavigationInfo);
            Assert.AreEqual(orderResponse.Score, actualScore);
        }
Exemplo n.º 3
0
        public void SerializationTestWithSeller()
        {
            KondutoOrder order = KondutoOrderFactory.completeOrder();

            order.Seller = KondutoSellerFactory.Create();

            try
            {
                order.ToJson();
                //ok
            }
            catch (KondutoInvalidEntityException e)
            {
                Assert.Fail("order should be invalid");
            }
        }
Exemplo n.º 4
0
        public void SerializationTest()
        {
            KondutoOrder order     = KondutoOrderFactory.completeOrder();
            String       orderJSON = KondutoUtils.LoadJson <KondutoOrder>(Properties.Resources.order).ToJson();

            try
            {
                Assert.AreEqual(orderJSON, order.ToJson(), "serialization failed");
            }
            catch (KondutoInvalidEntityException e)
            {
                Assert.Fail("order should be valid");
            }

            KondutoOrder deserializedOrder = KondutoModel.FromJson <KondutoOrder>(orderJSON);

            Assert.IsTrue(order.Equals(deserializedOrder), "deserialization failed");
        }
Exemplo n.º 5
0
        public void SerializationTestWithShoppingAndFlight()
        {
            KondutoOrder order = KondutoOrderFactory.completeOrder();

            order.Travel = KondutoFlightFactory.CreateFlight();

            try
            {
                order.ToJson();
                Assert.Fail("order should be invalid");
            }
            catch (KondutoInvalidEntityException e)
            {
                //ok
            }

            order              = KondutoOrderFactory.completeOrder();
            order.Travel       = KondutoFlightFactory.CreateFlight();
            order.ShoppingCart = null;
            //ok
        }
Exemplo n.º 6
0
        public void invalidOrderSerializationThrowsExceptionTest()
        {
            KondutoOrder order = new KondutoOrder();

            order.ToJson();         // triggers invalid customer exception
        }