Ejemplo n.º 1
0
        public Task PostAsync_NoAvailableAddress_ShouldServiceUnavailable()
        {
            return(AsynchronousTesting.WithCancellationTokenAsync(async cancellationToken =>
            {
                // Arrange.
                var request = new ReceivingRequest()
                {
                    TargetAmount = new PropertyAmount(100),
                };

                this.pool
                .Setup(p => p.TryLockAddressAsync(It.IsAny <CancellationToken>()))
                .ReturnsAsync((ReceivingAddressReservation)null);

                // Act.
                var result = await Subject.PostAsync(request, cancellationToken);

                // Assert.
                var status = Assert.IsType <StatusCodeResult>(result);

                Assert.Equal((int)HttpStatusCode.ServiceUnavailable, status.StatusCode);

                this.pool.Verify(p => p.TryLockAddressAsync(cancellationToken), Times.Once());
            }));
        }
Ejemplo n.º 2
0
        public Task PostAsync_WithCallback_ShouldWatchWithCallback()
        {
            return(AsynchronousTesting.WithCancellationTokenAsync(async cancellationToken =>
            {
                // Arrange.
                var request = new ReceivingRequest()
                {
                    TargetAmount = new PropertyAmount(1000),
                };

                var callback = new Callback(
                    Guid.NewGuid(),
                    IPAddress.Parse("192.168.1.2"),
                    DateTime.Now,
                    false,
                    new Uri("http://localhost/a"));

                this.pool
                .Setup(p => p.TryLockAddressAsync(It.IsAny <CancellationToken>()))
                .ReturnsAsync(this.reservation);

                MockCallback(callback);

                // Act.
                var result = await Subject.PostAsync(request, cancellationToken);

                // Assert.
                var callbackId = Assert.Contains(CallbackId, ResponseHeaders);
                var accepted = Assert.IsType <AcceptedResult>(result);
                var response = Assert.IsType <ReceivingResponse>(accepted.Value);

                Assert.Equal(callback.Id.ToString(), callbackId);
                Assert.Null(accepted.Location);
                Assert.Equal(this.address.Address, response.Address);
                Assert.Equal(DateTime.UtcNow + TimeSpan.FromHours(1), response.Deadline, TimeSpan.FromSeconds(1));

                this.pool.Verify(p => p.TryLockAddressAsync(cancellationToken), Times.Once());

                this.Callbacks.Verify(
                    r => r.AddAsync(callback.RegisteredIp, callback.Url, CancellationToken.None),
                    Times.Once());

                this.watcher.Verify(
                    w => w.StartWatchAsync(
                        this.reservation,
                        new PropertyAmount(1000),
                        6,
                        TimeSpan.FromHours(1),
                        new TokenReceivingCallback(callback, ReceivingController.TimeoutStatus),
                        CancellationToken.None),
                    Times.Once());

                this.pool.Verify(
                    p => p.ReleaseAddressAsync(this.reservation.Id, It.IsAny <CancellationToken>()),
                    Times.Never());
            }));
        }
Ejemplo n.º 3
0
        public Task PostAsync_ErrorWhileStoringCallback_ShouldReleaseAddressReservation()
        {
            return(AsynchronousTesting.WithCancellationTokenAsync(async cancellationToken =>
            {
                // Arrange.
                var requester = IPAddress.Parse("192.168.1.2");
                var callback = new Uri("http://localhost/a");
                var ex = new Exception();

                var request = new ReceivingRequest()
                {
                    TargetAmount = new PropertyAmount(10000),
                };

                RequestHeaders.Add(CallbackUrl, callback.ToString());
                Connection.SetupGet(c => c.RemoteIpAddress).Returns(requester);

                this.pool
                .Setup(p => p.TryLockAddressAsync(It.IsAny <CancellationToken>()))
                .ReturnsAsync(this.reservation);

                this.Callbacks
                .Setup(r => r.AddAsync(It.IsAny <IPAddress>(), It.IsAny <Uri>(), It.IsAny <CancellationToken>()))
                .ThrowsAsync(ex);

                // Act.
                var result = await Assert.ThrowsAnyAsync <Exception>(
                    () => Subject.PostAsync(request, cancellationToken));

                // Assert.
                Assert.Same(ex, result);

                this.pool.Verify(p => p.TryLockAddressAsync(cancellationToken), Times.Once());
                this.Callbacks.Verify(r => r.AddAsync(requester, callback, CancellationToken.None), Times.Once());

                this.watcher.Verify(
                    w => w.StartWatchAsync(
                        It.IsAny <ReceivingAddressReservation>(),
                        It.IsAny <PropertyAmount>(),
                        It.IsAny <int>(),
                        It.IsAny <TimeSpan>(),
                        It.IsAny <TokenReceivingCallback>(),
                        It.IsAny <CancellationToken>()),
                    Times.Never());

                this.pool.Verify(p => p.ReleaseAddressAsync(this.reservation.Id, CancellationToken.None), Times.Once());
            }));
        }
Ejemplo n.º 4
0
        public Task PostAsync_NoCallback_ShouldNotWatchWithCallback()
        {
            return(AsynchronousTesting.WithCancellationTokenAsync(async cancellationToken =>
            {
                // Arrange.
                var request = new ReceivingRequest()
                {
                    TargetAmount = new PropertyAmount(100),
                };

                this.pool
                .Setup(p => p.TryLockAddressAsync(It.IsAny <CancellationToken>()))
                .ReturnsAsync(this.reservation);

                // Act.
                var result = await Subject.PostAsync(request, cancellationToken);

                // Assert.
                var accepted = Assert.IsType <AcceptedResult>(result);
                var response = Assert.IsType <ReceivingResponse>(accepted.Value);

                Assert.Null(accepted.Location);
                Assert.Equal(this.address.Address, response.Address);
                Assert.Equal(DateTimeKind.Utc, response.Deadline.Kind);
                Assert.Equal(DateTime.UtcNow + TimeSpan.FromHours(1), response.Deadline, TimeSpan.FromSeconds(1));

                this.pool.Verify(p => p.TryLockAddressAsync(cancellationToken), Times.Once());

                this.Callbacks.Verify(
                    r => r.AddAsync(It.IsAny <IPAddress>(), It.IsAny <Uri>(), It.IsAny <CancellationToken>()),
                    Times.Never());

                this.watcher.Verify(
                    w => w.StartWatchAsync(
                        this.reservation,
                        new PropertyAmount(100),
                        6,
                        TimeSpan.FromHours(1),
                        null,
                        CancellationToken.None),
                    Times.Once());

                this.pool.Verify(
                    p => p.ReleaseAddressAsync(this.reservation.Id, It.IsAny <CancellationToken>()),
                    Times.Never());
            }));
        }
Ejemplo n.º 5
0
        public Task PostAsync_ErrorWhileStartWatching_ShouldReleaseAddressReservation()
        {
            return(AsynchronousTesting.WithCancellationTokenAsync(async cancellationToken =>
            {
                // Arrange.
                var ex = new Exception();

                var request = new ReceivingRequest()
                {
                    TargetAmount = new PropertyAmount(5),
                };

                this.pool
                .Setup(p => p.TryLockAddressAsync(It.IsAny <CancellationToken>()))
                .ReturnsAsync(this.reservation);

                this.watcher
                .Setup(w => w.StartWatchAsync(
                           It.IsAny <ReceivingAddressReservation>(),
                           It.IsAny <PropertyAmount>(),
                           It.IsAny <int>(),
                           It.IsAny <TimeSpan>(),
                           It.IsAny <TokenReceivingCallback>(),
                           It.IsAny <CancellationToken>()))
                .ThrowsAsync(ex);

                // Act.
                var result = await Assert.ThrowsAnyAsync <Exception>(
                    () => Subject.PostAsync(request, cancellationToken));

                // Assert.
                this.pool.Verify(p => p.TryLockAddressAsync(cancellationToken), Times.Once());

                this.watcher.Verify(
                    w => w.StartWatchAsync(
                        this.reservation,
                        new PropertyAmount(5),
                        6,
                        TimeSpan.FromHours(1),
                        null,
                        CancellationToken.None),
                    Times.Once());

                this.pool.Verify(p => p.ReleaseAddressAsync(this.reservation.Id, CancellationToken.None), Times.Once());
            }));
        }
Ejemplo n.º 6
0
        public SaveRespone InsertReceivingDetails(JObject input)
        {
            SaveRespone objresponse = new SaveRespone();

            try
            {
                ReceivingRequest objrequest = new ReceivingRequest();

                objrequest.CounterId           = Convert.ToInt32(CommonMethods.URLKeyDecrypt(Convert.ToString(input["CounterId"])));
                objrequest.UserLoginId         = Convert.ToInt32(CommonMethods.URLKeyDecrypt(Convert.ToString(input["UserLoginId"])));
                objrequest.DriverMobileNumber  = Convert.ToString(input["DriverMobileNumber"]);
                objrequest.DriverName          = Convert.ToString(input["DriverName"]);
                objrequest.BookingId           = Convert.ToInt32(input["BookingId"]);
                objrequest.FromCounter         = Convert.ToString(input["FromCounter"]);
                objrequest.GCBookingNumber     = Convert.ToString(input["GCBookingNumber"]);
                objrequest.GCType              = Convert.ToInt32(input["GCType"]);
                objrequest.ProductType         = Convert.ToInt32(input["ProductType"]);
                objrequest.Remarks             = Convert.ToString(input["Remarks"]);
                objrequest.NumberOfPieces      = Convert.ToInt32(input["NumberOfPieces"]);
                objrequest.DeliveryToName      = Convert.ToString(input["DeliveryToName"]);
                objrequest.DeliveryToNumber    = Convert.ToString(input["DeliveryToNumber"]);
                objrequest.HamaliCharges       = Convert.ToDecimal(input["HamaliCharges"]);
                objrequest.ReceivingType       = Convert.ToInt32(input["ReceivingType"]);
                objrequest.TranshipmentCharges = Convert.ToInt32(input["TranshipmentCharges"]);
                objrequest.TotalWeight         = Convert.ToInt32(input["TotalWeight"]);
                objrequest.VehicleNumber       = Convert.ToString(input["VehicleNumber"]);
                objrequest.ToBeReceiveId       = Convert.ToInt32(input["ToBeReceiveId"]);
                objrequest.BillAmount          = Convert.ToDecimal(input["BillAmount"]);

                objresponse = objBookingDal.InsertReceivingDetails(objrequest);
            }
            catch (Exception ex)
            {
                objresponse.StatusId      = 0;
                objresponse.StatusMessage = ex.Message;
            }
            return(objresponse);
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> PostAsync(
            [FromBody] ReceivingRequest req,
            CancellationToken cancellationToken)
        {
            var timeout = this.config.Default.TransactionTimeout;
            var reserve = await this.pool.TryLockAddressAsync(cancellationToken);

            if (reserve == null)
            {
                return(StatusCode((int)HttpStatusCode.ServiceUnavailable));
            }

            try
            {
                var callback = await this.helper.RegisterCallbackAsync(this, CancellationToken.None);

                await this.watcher.StartWatchAsync(
                    reserve,
                    req.TargetAmount.Value,
                    this.config.Default.RequiredConfirmation,
                    timeout,
                    callback != null?new TokenReceivingCallback(callback, TimeoutStatus) : null,
                    CancellationToken.None);
            }
            catch
            {
                await this.pool.ReleaseAddressAsync(reserve.Id, CancellationToken.None);

                throw;
            }

            return(Accepted(new ReceivingResponse()
            {
                Address = reserve.Address.Address,
                Deadline = DateTime.UtcNow + timeout,
            }));
        }
Ejemplo n.º 8
0
        public Task PostAsync_WithInvalidCallbackUrl_ShouldReleaseAddressReservation(string url)
        {
            return(AsynchronousTesting.WithCancellationTokenAsync(async cancellationToken =>
            {
                // Arrange.
                var request = new ReceivingRequest()
                {
                    TargetAmount = new PropertyAmount(1),
                };

                RequestHeaders.Add(CallbackUrl, url);

                this.pool
                .Setup(p => p.TryLockAddressAsync(It.IsAny <CancellationToken>()))
                .ReturnsAsync(this.reservation);

                // Act.
                await Assert.ThrowsAsync <InvalidCallbackUrlException>(
                    () => Subject.PostAsync(request, cancellationToken));

                // Assert.
                this.pool.Verify(p => p.TryLockAddressAsync(cancellationToken), Times.Once());

                this.watcher.Verify(
                    w => w.StartWatchAsync(
                        It.IsAny <ReceivingAddressReservation>(),
                        It.IsAny <PropertyAmount>(),
                        It.IsAny <int>(),
                        It.IsAny <TimeSpan>(),
                        It.IsAny <TokenReceivingCallback>(),
                        It.IsAny <CancellationToken>()),
                    Times.Never());

                this.pool.Verify(p => p.ReleaseAddressAsync(this.reservation.Id, CancellationToken.None), Times.Once());
            }));
        }