Ejemplo n.º 1
0
        public async Task GetCandleAsync_WhenExceptionIsBeingThrown_ShouldThrowApiException()
        {
            // Arrange
            var importer = new Mock <IImporter>();

            importer
            .Setup(i =>
                   i.ImportAsync(
                       It.IsAny <string>(),
                       It.IsAny <DateTime>(),
                       It.IsAny <DateTime>(),
                       PeriodOption.Daily,
                       CancellationToken.None))
            .ThrowsAsync(new Exception());

            var service = new ExternalFinanceService(importer.Object, _loggerMock.Object);

            // Act
            var exception =
                await Should.ThrowAsync <ApiException>(async() =>
                                                       await service.GetCandleAsync(CompanySymbol, _today));

            // Assert
            this.ShouldSatisfyAllConditions(
                () =>
            {
                exception.Code.ShouldBe(HttpStatusCode.NotFound);
                exception.Errors.ShouldBe(
                    $"Quote data not found [{CompanySymbol}/{_today:yyyy-MM-dd}].");
            });
        }
Ejemplo n.º 2
0
        public async Task GetCandleAsync_WhenArgumentExceptionIsBeingThrown_ShouldThrowApiException()
        {
            // Arrange
            const string message = "message";

            var importer = new Mock <IImporter>();

            importer
            .Setup(i =>
                   i.ImportAsync(
                       It.IsAny <string>(),
                       It.IsAny <DateTime>(),
                       It.IsAny <DateTime>(),
                       PeriodOption.Daily,
                       CancellationToken.None))
            .ThrowsAsync(new ArgumentException(message));

            var service = new ExternalFinanceService(importer.Object, _loggerMock.Object);

            // Act
            var exception =
                await Should.ThrowAsync <ApiException>(async() =>
                                                       await service.GetCandleAsync(CompanySymbol, _today));

            // Assert
            this.ShouldSatisfyAllConditions(
                () =>
            {
                exception.Code.ShouldBe(HttpStatusCode.BadRequest);
                exception.Errors.ShouldBe(message);
            });
        }
Ejemplo n.º 3
0
        public async Task GetCandleAsync_WhenParametersAreValid_ShouldReturnCandle()
        {
            // Arrange
            var utcNowOffset = DateTimeOffset.UtcNow;

            var importer = new Mock <IImporter>();

            importer
            .Setup(i =>
                   i.ImportAsync(
                       It.IsAny <string>(),
                       It.IsAny <DateTime>(),
                       It.IsAny <DateTime>(),
                       PeriodOption.Daily,
                       CancellationToken.None))
            .ReturnsAsync(GetCandles(utcNowOffset));

            var service = new ExternalFinanceService(importer.Object, _loggerMock.Object);

            // Act
            var result = await service.GetCandleAsync(CompanySymbol, _today);

            // Assert
            this.ShouldSatisfyAllConditions(
                () =>
            {
                result.Close.ShouldBe(1m);
                result.DateTime = utcNowOffset;
            }
                );
        }
Ejemplo n.º 4
0
        public static bool Purchase(string userName, string creditCard)
        {
            lock (DataHandler.Instance.InefficientLock)
            {
                var user = DataHandler.Instance.GetUser(userName);

                foreach (var basket in user.shoppingCart.shoppingBaskets.Values)
                {
                    var store = DataHandler.Instance.GetStore(basket.StoreName);
                    foreach (var productAndAmount in basket.Products)
                    {
                        var productBarcode = productAndAmount.Key;
                        var amount         = productAndAmount.Value;
                        if (!store.GetInventory().ContainsKey(productBarcode) ||
                            store.GetInventory()[productBarcode] < amount)
                        {
                            return(false);
                        }
                    }
                }

                var supply  = ExternalSupplyService.CreateConnection();
                var finance = ExternalFinanceService.CreateConnection();

                if (supply == null || finance == null)
                {
                    return(false);
                }

                var cost = GetCartPrice(userName);

                if (cost < 0 || !supply.MakeOrder() || !finance.AcceptPurchase(cost, creditCard))
                {
                    return(false);
                }

                foreach (var basket in user.shoppingCart.shoppingBaskets.Values)
                {
                    var store = DataHandler.Instance.GetStore(basket.StoreName);

                    foreach (var policy in store.GetPurchasePolicies())
                    {
                        if (!policy.Validate(basket))
                        {
                            return(false);
                        }
                    }

                    foreach (var product in basket.Products.Keys.ToList())
                    {
                        var amount = basket.Products[product];

                        RemoveProductFromBasket(userName, store.GetName(), product);
                        store.GetInventory()[product] -= amount;
                    }

                    store.AddPurchase(new Purchase());
                }

                if (DataHandler.Instance.IsGuest(userName) < 0)
                {
                    ((User)user).history.Add(new Purchase());
                }

                return(true);
            }
        }