public async Task Return_InternalServerEror_When_AcquiringBank_sent_duplicated_PaymentId_Using_StupidBank(BankPaymentStatus bankPaymentStatus, PaymentStatus expectedPaymentStatusReturnedByGateway)
        {
            var requestId            = Guid.Parse("9cb25b95-45ba-4100-a180-deb13259d0e1");
            var paymentRequest       = TestsUtils.BuildPaymentRequest(requestId, MerchantsRepository.FailFromThe2ndPaymentMerchant);
            var secondRequestId      = Guid.Parse("9c940897-b1c4-4598-96a7-82502ca51845");
            var secondPaymentRequest = TestsUtils.BuildPaymentRequest(secondRequestId, MerchantsRepository.FailFromThe2ndPaymentMerchant);

            var           gatewayPaymentId          = Guid.Parse("002ee45f-fdfb-4666-b504-70aa26ecf646");
            var           secondGatewayPaymentId    = Guid.Parse("4cc3dd04-b3d8-4be4-8c68-e744c387fb6c");
            IGenerateGuid gatewayPaymentIdGenerator = Substitute.For <IGenerateGuid>();

            gatewayPaymentIdGenerator.Generate().Returns(gatewayPaymentId, secondGatewayPaymentId);

            var cqrs = await PaymentCQRS.Build(bankPaymentStatus, new DefaultBankPaymentIdGenerator(), new AlwaysSuccessBankConnectionBehavior(), new DelayProviderForTesting(TimeSpan.FromMilliseconds(1)), PaymentCQRS.TimeoutProviderForBankResponseWaiting(TimeSpan.FromMilliseconds(200)), Substitute.For <IKnowBufferAndReprocessPaymentRequest>(), Substitute.For <IAmCircuitBreakers>());

            await cqrs.RequestsController.ProceedPaymentRequest(paymentRequest, gatewayPaymentIdGenerator, cqrs.PaymentRequestsMemory, cqrs.PaymentProcessor);

            await cqrs.RequestsController.ProceedPaymentRequest(secondPaymentRequest, gatewayPaymentIdGenerator, cqrs.PaymentRequestsMemory, cqrs.PaymentProcessor);

            var secondFailedPayment = (await cqrs.PaymentReadController.GetPaymentInfo(secondGatewayPaymentId)).Value;

            Check.That(secondFailedPayment.RequestId).IsEqualTo(secondRequestId);
            Check.That(secondFailedPayment.GatewayPaymentId).IsEqualTo(secondGatewayPaymentId);

            Check.That(secondFailedPayment.Status).IsEqualTo(expectedPaymentStatusReturnedByGateway);
            Check.That(secondFailedPayment.Approved).IsEqualTo(secondFailedPayment.Status == PaymentGateway.Domain.PaymentStatus.Success);
            Check.That(secondFailedPayment.AcquiringBankPaymentId).IsNull();
        }
        public async Task Create_payment_When_handling_PaymentRequest()
        {
            var requestId      = Guid.NewGuid();
            var paymentRequest = TestsUtils.BuildPaymentRequest(requestId, MerchantsRepository.Apple);

            var           gatewayPaymentId = Guid.NewGuid();
            IGenerateGuid guidGenerator    = new GuidGeneratorForTesting(gatewayPaymentId);

            var cqrs = await PaymentCQRS.Build(BankPaymentStatus.Accepted, new BankPaymentIdGeneratorForTests(Guid.Parse("3ec8c76c-7dc2-4769-96f8-7e0649ecdfc0")), new AlwaysSuccessBankConnectionBehavior(), new DelayProviderForTesting(TimeSpan.FromMilliseconds(1)), PaymentCQRS.TimeoutProviderForBankResponseWaiting(TimeSpan.FromMilliseconds(200)), Substitute.For <IKnowBufferAndReprocessPaymentRequest>(), Substitute.For <IAmCircuitBreakers>());

            var response = await cqrs.RequestsController.ProceedPaymentRequest(paymentRequest, guidGenerator, new PaymentRequestsMemory(), cqrs.PaymentProcessor);

            CheckThatPaymentResourceIsCorrectlyCreated(response, gatewayPaymentId, requestId);
        }
        public async Task Return_all_payments_s_GatewayId()
        {
            var requestId      = Guid.NewGuid();
            var paymentRequest = TestsUtils.BuildPaymentRequest(requestId, MerchantsRepository.Amazon);

            var           gatewayPaymentId          = Guid.NewGuid();
            IGenerateGuid gatewayPaymentIdGenerator = new GuidGeneratorForTesting(gatewayPaymentId);

            var cqrs = await PaymentCQRS.Build(BankPaymentStatus.Accepted, new BankPaymentIdGeneratorForTests(Guid.Parse("3ec8c76c-7dc2-4769-96f8-7e0649ecdfc0")), new AlwaysSuccessBankConnectionBehavior(), new DelayProviderForTesting(TimeSpan.FromMilliseconds(1)), PaymentCQRS.TimeoutProviderForBankResponseWaiting(TimeSpan.FromMilliseconds(200)), Substitute.For <IKnowBufferAndReprocessPaymentRequest>(), Substitute.For <IAmCircuitBreakers>());

            await cqrs.RequestsController.ProceedPaymentRequest(paymentRequest, gatewayPaymentIdGenerator, new PaymentRequestsMemory(), cqrs.PaymentProcessor);

            var gatewayPaymentsIds = await cqrs.GatewayPaymentsIdsController.Get();

            Check.That(gatewayPaymentsIds).ContainsExactly(gatewayPaymentId);
        }
        //[Repeat(10)]
        public async Task Return_AcceptedAtRouteResult_And_have_correct_PaymentStatus_When_Processing_PaymentRequest_timeout()
        {
            var           requestId        = Guid.NewGuid();
            var           paymentRequest   = TestsUtils.BuildPaymentRequest(requestId, MerchantsRepository.Amazon);
            var           gatewayPaymentId = Guid.NewGuid();
            IGenerateGuid guidGenerator    = new GuidGeneratorForTesting(gatewayPaymentId);

            var bankPaymentId = Guid.Parse("3ec8c76c-7dc2-4769-96f8-7e0649ecdfc0");

            var timeoutTolerance         = TimeSpan.FromMilliseconds(80);
            var delayBiggerThanTolerance = timeoutTolerance * 5;

            var delayProvider = Substitute.For <IProvideBankResponseTime>();

            delayProvider.Delays().Returns(delayBiggerThanTolerance,   //timeout
                                           delayBiggerThanTolerance,   //timeout
                                           delayBiggerThanTolerance,   //timeout
                                           timeoutTolerance.Divide(20) // NO TIMEOUT
                                           );

            var knowBufferAndReprocessPaymentRequest = Substitute.For <IKnowBufferAndReprocessPaymentRequest>();
            var circuitBreakerRepository             = new CircuitBreakerRepository();

            var cqrs = await PaymentCQRS.Build(BankPaymentStatus.Accepted,
                                               new BankPaymentIdGeneratorForTests(bankPaymentId),
                                               new AlwaysSuccessBankConnectionBehavior(),
                                               delayProvider,
                                               PaymentCQRS.TimeoutProviderForBankResponseWaiting(timeoutTolerance),
                                               knowBufferAndReprocessPaymentRequest,
                                               circuitBreakerRepository);

            var actionResult = await cqrs.RequestsController.ProceedPaymentRequest(paymentRequest, guidGenerator, cqrs.PaymentRequestsMemory, cqrs.PaymentProcessor);

            Check.That(actionResult).IsInstanceOf <AcceptedAtRouteResult>();

            PaymentDto payment = (await cqrs.PaymentReadController.GetPaymentInfo(gatewayPaymentId)).Value;

            Check.That(payment.RequestId).IsEqualTo(requestId);
            Check.That(payment.GatewayPaymentId).IsEqualTo(gatewayPaymentId);

            Check.That(payment.Status).IsEqualTo(PaymentStatus.Pending); // circuit breaker should open
            Check.That(payment.Approved).IsNull();
            Check.That(payment.AcquiringBankPaymentId).IsNull();

            knowBufferAndReprocessPaymentRequest.ReceivedWithAnyArgs(1).Buffer(default, default);
        public async Task Not_handle_a_PaymentRequest_more_than_once()
        {
            var requestId      = Guid.NewGuid();
            var paymentRequest = TestsUtils.BuildPaymentRequest(requestId, MerchantsRepository.Amazon);

            var cqrs = await PaymentCQRS.Build(BankPaymentStatus.Accepted, new BankPaymentIdGeneratorForTests(Guid.Parse("3ec8c76c-7dc2-4769-96f8-7e0649ecdfc0")), new AlwaysSuccessBankConnectionBehavior(), new DelayProviderForTesting(TimeSpan.FromMilliseconds(1)), PaymentCQRS.TimeoutProviderForBankResponseWaiting(TimeSpan.FromMilliseconds(200)), Substitute.For <IKnowBufferAndReprocessPaymentRequest>(), Substitute.For <IAmCircuitBreakers>());

            var           gatewayPaymentId = Guid.NewGuid();
            IGenerateGuid guidGenerator    = new GuidGeneratorForTesting(gatewayPaymentId);
            await cqrs.RequestsController.ProceedPaymentRequest(paymentRequest, guidGenerator, cqrs.PaymentRequestsMemory, cqrs.PaymentProcessor);

            var actionResult = await cqrs.RequestsController.ProceedPaymentRequest(paymentRequest, guidGenerator, cqrs.PaymentRequestsMemory, cqrs.PaymentProcessor);

            Check.That(actionResult).IsInstanceOf <BadRequestObjectResult>();
            var badRequest = (BadRequestObjectResult)actionResult;
            var failDetail = (ProblemDetails)badRequest.Value;

            Check.That(failDetail.Detail).IsEqualTo("Identical payment request will not be handled more than once");
        }
        public async Task Return_PaymentFaulted_When_AcquiringBank_rejects_payment()
        {
            var           requestId        = Guid.NewGuid();
            var           paymentRequest   = TestsUtils.BuildPaymentRequest(requestId, MerchantsRepository.Amazon);
            var           gatewayPaymentId = Guid.NewGuid();
            IGenerateGuid guidGenerator    = new GuidGeneratorForTesting(gatewayPaymentId);

            var cqrs = await PaymentCQRS.Build(BankPaymentStatus.Accepted, new BankPaymentIdGeneratorForTests(Guid.Parse("3ec8c76c-7dc2-4769-96f8-7e0649ecdfc0")), new AlwaysSuccessBankConnectionBehavior(), new DelayProviderForTesting(TimeSpan.FromMilliseconds(1)), PaymentCQRS.TimeoutProviderForBankResponseWaiting(TimeSpan.FromMilliseconds(200)), Substitute.For <IKnowBufferAndReprocessPaymentRequest>(), Substitute.For <IAmCircuitBreakers>(), new SimulateGatewayException());

            await cqrs.RequestsController.ProceedPaymentRequest(paymentRequest, guidGenerator, cqrs.PaymentRequestsMemory, cqrs.PaymentProcessor);


            var payment = (await cqrs.PaymentReadController.GetPaymentInfo(gatewayPaymentId)).Value;

            Check.That(payment.RequestId).IsEqualTo(requestId);
            Check.That(payment.GatewayPaymentId).IsEqualTo(gatewayPaymentId);

            Check.That(payment.Status).IsEqualTo(PaymentStatus.FaultedOnGateway);
            Check.That(payment.Approved.Value).IsFalse();
            Check.That(payment.AcquiringBankPaymentId).IsNull();
        }
        public async Task Return_proper_payment_status_When_AcquiringBank_accepts_or_reject_payment(BankPaymentStatus bankPaymentStatus, PaymentStatus expectedPaymentStatusReturnedByGateway)
        {
            var           requestId        = Guid.NewGuid();
            var           paymentRequest   = TestsUtils.BuildPaymentRequest(requestId, MerchantsRepository.Apple);
            var           gatewayPaymentId = Guid.NewGuid();
            IGenerateGuid guidGenerator    = new GuidGeneratorForTesting(gatewayPaymentId);

            var bankPaymentId = Guid.Parse("3ec8c76c-7dc2-4769-96f8-7e0649ecdfc0");
            var cqrs          = await PaymentCQRS.Build(bankPaymentStatus, new BankPaymentIdGeneratorForTests(bankPaymentId), new AlwaysSuccessBankConnectionBehavior(), new DelayProviderForTesting(TimeSpan.FromMilliseconds(1)), PaymentCQRS.TimeoutProviderForBankResponseWaiting(TimeSpan.FromMilliseconds(200)), Substitute.For <IKnowBufferAndReprocessPaymentRequest>(), Substitute.For <IAmCircuitBreakers>());

            await cqrs.RequestsController.ProceedPaymentRequest(paymentRequest, guidGenerator, cqrs.PaymentRequestsMemory, cqrs.PaymentProcessor);


            var payment = (await cqrs.PaymentReadController.GetPaymentInfo(gatewayPaymentId)).Value;

            Check.That(payment.RequestId).IsEqualTo(requestId);
            Check.That(payment.GatewayPaymentId).IsEqualTo(gatewayPaymentId);

            Check.That(payment.Status).IsEqualTo(expectedPaymentStatusReturnedByGateway);
            Check.That(payment.Approved).IsEqualTo(payment.Status == PaymentGateway.Domain.PaymentStatus.Success);
            Check.That(payment.AcquiringBankPaymentId).IsEqualTo(bankPaymentId);
        }