Exemplo n.º 1
0
        public void WhenInsufficientBalance_Then400()
        {
            bool changesPersisted = false;

            var accountState = new AccountState
                               (
                Currency: "EUR",
                Balance: 1000,
                Status: AccountStatus.Active
                               );

            var registry = new AccountRegistry(
                loadAccount: _ => Async(Some(accountState)),
                saveAndPublish: _ =>
            {
                changesPersisted = true;
                return(Async(Unit()));
            });

            var controller = new MakeTransferController(
                validate: cmd => Invalid("invalid"),
                getAccount: id => registry.Lookup(id));

            var response = controller.MakeTransfer(new MakeTransfer
            {
                Amount = 1200
            }).Result;

            Assert.IsFalse(changesPersisted);
            Assert.AreEqual(typeof(BadRequestObjectResult), response.GetType());
        }
Exemplo n.º 2
0
        public void WhenEverythingWorks_Then200()
        {
            bool changesPersisted = false;

            var accountState = new AccountState
                               (
                Currency: "EUR",
                Balance: 1000,
                Status: AccountStatus.Active
                               );

            var registry = new AccountRegistry(
                loadAccount: _ => Async(Some(accountState)),
                saveAndPublish: _ =>
            {
                changesPersisted = true;
                return(Async(Unit()));
            });

            var controller = new MakeTransferController(
                validate: cmd => Valid(cmd),
                getAccount: id => registry.Lookup(id));

            var response = controller.MakeTransfer(new MakeTransfer
            {
                Amount = 200
            }).Result;

            Assert.IsTrue(changesPersisted);
            Assert.AreEqual(typeof(OkObjectResult), response.GetType());
        }
Exemplo n.º 3
0
        public void WhenLoadingFails_Then500()
        {
            bool changesPersisted = false;

            var accountState = new AccountState
                               (
                Currency: "EUR",
                Balance: 1000,
                Status: AccountStatus.Active
                               );

            var registry = new AccountRegistry(
                loadAccount: _ => { throw new InvalidOperationException(); },
                saveAndPublish: _ =>
            {
                changesPersisted = true;
                return(Async(Unit()));
            });

            var controller = new MakeTransferController(
                validate: cmd => Valid(cmd),
                getAccount: id => registry.Lookup(id));

            var response = controller.MakeTransfer(new MakeTransfer
            {
                Amount = 200
            }).Result;

            Assert.IsFalse(changesPersisted);
            Assert.AreEqual(typeof(ObjectResult), response.GetType());
        }
Exemplo n.º 4
0
        public void WhenAccountDoesntExist_Then400()
        {
            var registry = new AccountRegistry(
                loadAccount: _ => Async((Option <AccountState>)None),
                saveAndPublish: _ => Async(Unit()));

            var controller = new MakeTransferController(
                validate: cmd => Valid(cmd),
                getAccount: id => registry.Lookup(id));

            var response = controller.MakeTransfer(new MakeTransfer()).Result;

            Assert.AreEqual(typeof(BadRequestObjectResult), response.GetType());
        }
Exemplo n.º 5
0
        public void WhenValidationFails_Then400()
        {
            bool changesPersisted = false;

            var registry = new AccountRegistry(
                loadAccount: _ => Async(Some(new AccountState(Currency: "EUR"))),
                saveAndPublish: _ =>
            {
                changesPersisted = true;
                return(Async(Unit()));
            });

            var controller = new MakeTransferController(
                validate: cmd => Invalid("invalid"),
                getAccount: id => registry.Lookup(id));

            var response = controller.MakeTransfer(new MakeTransfer()).Result;

            Assert.IsFalse(changesPersisted);
            Assert.AreEqual(typeof(BadRequestObjectResult), response.GetType());
        }
Exemplo n.º 6
0
        public void AccountIsOnlyLoadedOnce()
        {
            int accountLoaded    = 0;
            int changesPersisted = 0;

            var accountState = new AccountState
                               (
                Currency: "EUR",
                Balance: 1000,
                Status: AccountStatus.Active
                               );

            var registry = new AccountRegistry(
                loadAccount: _ =>
            {
                accountLoaded++;
                return(Async(Some(accountState)));
            },
                saveAndPublish: _ =>
            {
                changesPersisted++;
                return(Async(Unit()));
            });

            var controller = new MakeTransferController(
                validate: Valid,
                getAccount: id => registry.Lookup(id));

            // make 2 transfers
            var cmd = new MakeTransfer {
                Amount = 200
            };
            var x = controller.MakeTransfer(cmd).Result;
            var y = controller.MakeTransfer(cmd).Result;

            Assert.AreEqual(2, changesPersisted);
            Assert.AreEqual(1, accountLoaded);
        }
Exemplo n.º 7
0
        public void WhenPersistenceFails_Then500()
        {
            var accountState = new AccountState
                               (
                Currency: "EUR",
                Balance: 1000,
                Status: AccountStatus.Active
                               );

            var registry = new AccountRegistry(
                loadAccount: _ => Async(Some(accountState)),
                saveAndPublish: _ => { throw new InvalidOperationException(); });

            var controller = new MakeTransferController(
                validate: cmd => Valid(cmd),
                getAccount: id => registry.Lookup(id));

            var response = controller.MakeTransfer(new MakeTransfer
            {
                Amount = 200
            }).Result;

            Assert.AreEqual(typeof(ObjectResult), response.GetType());
        }