public async Task TestPostingNewBatch()
        {
            var jsonInString = "{\"BatchCount\":5,\"NumberPerBatch\": 2}";
            var postResponse = await _client.PostAsync("api/orders/", new StringContent(jsonInString, Encoding.UTF8, "application/json"));

            postResponse.EnsureSuccessStatusCode();
            var body    = postResponse.Content.ReadAsStringAsync().Result;
            var jObject = JObject.Parse(body);

            var batch    = new BatchNumbersGenerated((Guid)jObject["orderId"], "1", 123);
            var jsonBody = JsonConvert.SerializeObject(batch);
            var response = await _client.PostAsync("api/Generate", new StringContent(jsonBody, Encoding.UTF8, "application/json"));

            response.EnsureSuccessStatusCode();
        }
        public async Task TestPostingInvalidBatchId()
        {
            var jsonInString = "{\"BatchCount\":5,\"NumberPerBatch\": 2}";
            var postResponse = await _client.PostAsync("api/orders/", new StringContent(jsonInString, Encoding.UTF8, "application/json"));

            postResponse.EnsureSuccessStatusCode();
            var body    = postResponse.Content.ReadAsStringAsync().Result;
            var jObject = JObject.Parse(body);

            var batch    = new BatchNumbersGenerated((Guid)jObject["orderId"], "33", 123);
            var jsonBody = JsonConvert.SerializeObject(batch);
            var response = await _client.PostAsync("api/Generate", new StringContent(jsonBody, Encoding.UTF8, "application/json"));

            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);

            var resultMessage = response.Content.ReadAsStringAsync().Result;

            Assert.True(resultMessage.Contains("Invalid Range, Values must be between 1-10"), $"Expected message changed, current response message is: '{resultMessage}'");
        }
예제 #3
0
        public async Task <IActionResult> PostBatchTask([FromBody] BatchNumbersGenerated order)
        {
            if (!ModelState.IsValid)
            {
                _logger.Log(LogLevel.Error, $"Error {ModelState}");
                return(BadRequest(ModelState));
            }
            try
            {
                _cancellationService.GetToken().ThrowIfCancellationRequested();
                await _orderService.UpdateBatch(_cancellationService.GetToken(), order.OrderId, order.BatchId, order.MultiplicationNumber);
            }
            catch (OperationCanceledException)
            {
                _logger.Log(LogLevel.Information, $"All services are canceled.");
            }


            return(Accepted());
        }
        public async Task GenerateRandomNumber(CancellationToken token, Guid id, int numberOfBatches)
        {
            _logger.Log(LogLevel.Information, $"Started Generate Simulated work, number of batches:'{numberOfBatches}' the order id is: '{id}'");

            Parallel.For(0, numberOfBatches,
                         index =>
            {
                token.ThrowIfCancellationRequested();
                var delay           = GenerateRandomInt(5, 10);
                var generatedNumber = GenerateRandomInt(1, 100);

                Thread.Sleep(delay * 1000);
                var batch = new BatchNumbersGenerated(id, index.ToString(), generatedNumber);

                var path = _section["GeneratePath"];
                _logger.Log(LogLevel.Information,
                            $"Generate work completed for batch'{index.ToString()}', generated number is:{generatedNumber} the order id is: '{id}'");
                _serverClient.PostAsync(token, batch, path: path);
                //TODO: Think if this is real service then we should log this to DB so my TODO to add to DB
            });
        }