public void CreateSale_WhenCustomHeaderIsNull_ShouldSendToApiServiceWithCorrecltyHeaders()
        {
            var validCreditCardSaleResponse = ValidCreateSaleResponse(CardTransactionHelper.CreateCreditCardPaymentResponse());

            _mockRestClient.Setup(m => m.Execute <Sale>(It.IsAny <IRestRequest>())).Returns(new RestResponse <Sale>()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new JsonSerializer().Serialize(validCreditCardSaleResponse),
                Data       = validCreditCardSaleResponse
            });

            var merchantAuthentication = MerchantAuthenticationHelper.CreateMerchantAuthentication();

            _service.CreateSale(merchantAuthentication,
                                SaleHelper.CreateOrder(CardTransactionHelper.CreateCreditCardPaymentRequest()),
                                null);

            _mockRestClient.Verify(m => m.Execute <Sale>(It.IsAny <RestRequest>()), Times.Once);

            _mockRestClient.Verify(m => m.Execute <Sale>(It.Is <RestRequest>(request => request.Method == Method.POST)), Times.Once);

            _mockRestClient.Verify(m => m.Execute <Sale>(It.Is <RestRequest>(request => request.Resource == @"sales")), Times.Once);

            _mockRestClient.Verify
                (m => m.Execute <Sale>(
                    It.Is <RestRequest>(
                        request => request.Parameters[0].Name == "Content-Type" &&
                        request.Parameters[0].Value == "application/json" &&
                        request.Parameters[1].Name == "MerchantId" &&
                        request.Parameters[2].Name == "MerchantKey")
                    ), Times.Once);
        }
        private static Sale ValidGetSaleResponse()
        {
            var sale = SaleHelper.CreateOrder(CardTransactionHelper.CreateCreditCardPaymentRequest());

            sale.Customer.IdentityType = null;

            sale.Payment = CardTransactionHelper.CreateCreditCardPaymentResponse();

            return(sale);
        }
        public void CreateSale_Send_CreditCardTransaction_Return_Valid_Reponse()
        {
            var validCreditCardSaleResponse = ValidCreateSaleResponse(CardTransactionHelper.CreateCreditCardPaymentResponse());

            _mockRestClient.Setup(m => m.Execute <Sale>(It.IsAny <IRestRequest>())).Returns(new RestResponse <Sale>()
            {
                StatusCode = HttpStatusCode.Created,
                Content    = new JsonSerializer().Serialize(validCreditCardSaleResponse),
                Data       = validCreditCardSaleResponse
            });

            var result = _service.CreateSale(MerchantAuthenticationHelper.CreateMerchantAuthentication(), SaleHelper.CreateOrder(CardTransactionHelper.CreateCreditCardPaymentRequest()));

            result.Should().NotBeNull();
            result.MerchantOrderId.Should().NotBeEmpty();
            result.Customer.Should().NotBeNull();
            result.Customer.Address.Should().NotBeNull();
            result.Payment.Should().NotBeNull();
            result.Payment.ExtraDataCollection.Should().NotBeNull();
            result.Payment.Links.Should().NotBeNull();

            result.Customer.Address.City.Should().Be(CustomerHelper.CreateCustomer().Address.City);
            result.Customer.Address.Complement.Should().Be(CustomerHelper.CreateCustomer().Address.Complement);
            result.Customer.Address.Country.Should().Be(CustomerHelper.CreateCustomer().Address.Country);
            result.Customer.Address.District.Should().Be(CustomerHelper.CreateCustomer().Address.District);
            result.Customer.Address.Number.Should().Be(CustomerHelper.CreateCustomer().Address.Number);
            result.Customer.Address.State.Should().Be(CustomerHelper.CreateCustomer().Address.State);
            result.Customer.Address.Street.Should().Be(CustomerHelper.CreateCustomer().Address.Street);
            result.Customer.Address.ZipCode.Should().Be(CustomerHelper.CreateCustomer().Address.ZipCode);

            result.Customer.Birthdate.Should().NotBe(new DateTime());
            result.Customer.Email.Should().Be(CustomerHelper.CreateCustomer().Email);
            result.Customer.Identity.Should().Be(CustomerHelper.CreateCustomer().Identity);
            result.Customer.IdentityType.Should().Be(CustomerHelper.CreateCustomer().IdentityType);
            result.Customer.Name.Should().Be(CustomerHelper.CreateCustomer().Name);

            result.Payment.Amount.Should().Be(CardTransactionHelper.CreateCreditCardPaymentResponse().Amount);
            result.Payment.CapturedAmount.Should().Be(CardTransactionHelper.CreateCreditCardPaymentResponse().CapturedAmount);
            result.Payment.Provider.Should().Be(CardTransactionHelper.CreateCreditCardPaymentResponse().Provider);
            result.Payment.Country.Should().Be(CardTransactionHelper.CreateCreditCardPaymentResponse().Country);
            result.Payment.Credentials.Should().Be(CardTransactionHelper.CreateCreditCardPaymentResponse().Credentials);
            result.Payment.Currency.Should().Be(CardTransactionHelper.CreateCreditCardPaymentResponse().Currency);
            result.Payment.ReasonCode.Should().Be(CardTransactionHelper.CreateCreditCardPaymentResponse().ReasonCode);
            result.Payment.ReasonMessage.Should().Be("Successful");
            result.Payment.ReturnUrl.Should().Be(CardTransactionHelper.CreateCreditCardPaymentResponse().ReturnUrl);
            result.Payment.Status.Should().Be(1);
            result.Payment.Type.Should().Be(CardTransactionHelper.CreateCreditCardPaymentResponse().Type);
            result.Payment.VoidedAmount.Should().Be(CardTransactionHelper.CreateCreditCardPaymentResponse().VoidedAmount);
        }
        public void CreateSale_Send_Corrrectly_ApiService()
        {
            var validCreditCardSaleResponse = ValidCreateSaleResponse(CardTransactionHelper.CreateCreditCardPaymentResponse());

            _mockRestClient.Setup(m => m.Execute <Sale>(It.IsAny <IRestRequest>())).Returns(new RestResponse <Sale>()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new JsonSerializer().Serialize(validCreditCardSaleResponse),
                Data       = validCreditCardSaleResponse
            });

            _service.CreateSale(MerchantAuthenticationHelper.CreateMerchantAuthentication(), SaleHelper.CreateOrder(CardTransactionHelper.CreateCreditCardPaymentRequest()));

            _mockRestClient.Verify(m => m.Execute <Sale>(It.IsAny <RestRequest>()), Times.Once);

            _mockRestClient.Verify(m => m.Execute <Sale>(It.Is <RestRequest>(request => request.Method == Method.POST)), Times.Once);

            _mockRestClient.Verify(m => m.Execute <Sale>(It.Is <RestRequest>(request => request.Resource == @"sales")), Times.Once);

            _mockRestClient.Verify(m => m.Execute <Sale>(It.Is <RestRequest>(request => request.Parameters.Any(
                                                                                 param => param.Type == ParameterType.RequestBody && param.Name == @"application/json"))), Times.Once);
        }
        public void CreateSale_WhenRequestContainsCustomHeaders_ShouldSendToApiServiceWithCorrecltyHeaders()
        {
            var validCreditCardSaleResponse = ValidCreateSaleResponse(CardTransactionHelper.CreateCreditCardPaymentResponse());

            _mockRestClient.Setup(m => m.Execute <Sale>(It.IsAny <IRestRequest>())).Returns(new RestResponse <Sale>()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new JsonSerializer().Serialize(validCreditCardSaleResponse),
                Data       = validCreditCardSaleResponse
            });

            _service.CreateSale(MerchantAuthenticationHelper.CreateMerchantAuthentication(),
                                SaleHelper.CreateOrder(CardTransactionHelper.CreateCreditCardPaymentRequest()),
                                new Dictionary <string, string>
            {
                { "CustomHeader1", "HeaderValid1" },
                { "CustomHeader2", "HeaderValid2" },
                { "CustomHeader3", "HeaderValid3" }
            });

            _mockRestClient.Verify(m => m.Execute <Sale>(It.IsAny <RestRequest>()), Times.Once);

            _mockRestClient.Verify(m => m.Execute <Sale>(It.Is <RestRequest>(request => request.Method == Method.POST)), Times.Once);

            _mockRestClient.Verify(m => m.Execute <Sale>(It.Is <RestRequest>(request => request.Resource == @"sales")), Times.Once);

            _mockRestClient.Verify
                (m => m.Execute <Sale>(
                    It.Is <RestRequest>(
                        request => request.Parameters[3].Name == "CustomHeader1" &&
                        request.Parameters[3].Value == "HeaderValid1" &&
                        request.Parameters[4].Name == "CustomHeader2" &&
                        request.Parameters[4].Value == "HeaderValid2" &&
                        request.Parameters[5].Name == "CustomHeader3" &&
                        request.Parameters[5].Value == "HeaderValid3")
                    ), Times.Once);
        }
        public void CreateSale_WhenHttpStatusCodeEqualsTo0_ReturnsMappedErrorResponse()
        {
            var restResponse = new RestResponse <Sale>
            {
                StatusCode     = 0,
                ErrorException = new Exception("Exceção de teste"),
                ErrorMessage   = "Undefined Error"
            };

            _mockRestClient.Setup(m => m.Execute <Sale>(It.IsAny <IRestRequest>())).Returns(restResponse);

            var response = _service.CreateSale(MerchantAuthenticationHelper.CreateMerchantAuthentication(), SaleHelper.CreateOrder(CardTransactionHelper.CreateCreditCardPaymentRequest()));

            response.Should().NotBeNull();
            response.MerchantOrderId.Should().BeNullOrEmpty();
            response.Customer.Should().BeNull();
            response.Payment.Should().BeNull();
            Assert.AreEqual(response.HttpStatus, restResponse.StatusCode);
            response.ErrorDataCollection.Should().NotBeNull();
            response.ErrorDataCollection[0].Code.Should().Be(-1);
            response.ErrorDataCollection[0].Message.Should().Be("ErrorMessage: Undefined Error | ErrorException: System.Exception: Exceção de teste");
        }
        public void CreateSale_Send_CreditCardTransaction_Return_Error_Reponse()
        {
            var validCreditCardSaleResponse = ErrorCreateSaleResponse();

            _mockRestClient.Setup(m => m.Execute <Sale>(It.IsAny <IRestRequest>())).Returns(new RestResponse <Sale>()
            {
                StatusCode = HttpStatusCode.BadRequest,
                Content    = new JsonSerializer().Serialize(validCreditCardSaleResponse),
                Data       = validCreditCardSaleResponse
            });

            var response = _service.CreateSale(MerchantAuthenticationHelper.CreateMerchantAuthentication(), SaleHelper.CreateOrder(CardTransactionHelper.CreateCreditCardPaymentRequest()));

            response.Should().NotBeNull();
            response.MerchantOrderId.Should().BeNullOrEmpty();
            response.Customer.Should().BeNull();
            response.Payment.Should().BeNull();
            response.HttpStatus.Should().Be(HttpStatusCode.BadRequest);
            response.ErrorDataCollection.Count.Should().BeGreaterThan(0);
        }