コード例 #1
0
    /// <inheritdoc/>
    public async Task <ResponseModel> ExecuteRequestAsync()
    {
        var requestLogger            = _requestLoggerFactory.GetRequestLogger();
        var foundStubs               = new List <(StubModel, IEnumerable <ConditionCheckResultModel>)>();
        var stubs                    = (await _stubContainer.GetStubsAsync()).Where(s => s.Stub.Enabled).ToArray();
        var orderedConditionCheckers = _conditionCheckers.OrderByDescending(c => c.Priority).ToArray();

        foreach (var fullStub in stubs)
        {
            var stub = fullStub.Stub;
            try
            {
                var validationResults = new List <ConditionCheckResultModel>();
                foreach (var checker in orderedConditionCheckers)
                {
                    var validationResult = checker.Validate(stub);
                    validationResult.CheckerName = checker.GetType().Name;
                    validationResults.Add(validationResult);
                    if (validationResult.ConditionValidation == ConditionValidationType.Invalid)
                    {
                        // If any condition is invalid, skip the rest.
                        break;
                    }
                }

                var passed = (validationResults.All(r =>
                                                    r.ConditionValidation != ConditionValidationType.Invalid) &&
                              validationResults.Any(r =>
                                                    r.ConditionValidation != ConditionValidationType.NotExecuted &&
                                                    r.ConditionValidation != ConditionValidationType.NotSet)) ||
                             validationResults.All(
                    r => r.ConditionValidation == ConditionValidationType.NotExecuted);
                if (passed)
                {
                    foundStubs.Add((stub, validationResults));
                }

                requestLogger.SetStubExecutionResult(stub.Id, passed, validationResults);
            }
            catch (Exception e)
            {
                _logger.LogWarning($"Exception thrown while executing checks for stub '{stub.Id}': {e}");
            }
        }

        if (!foundStubs.Any())
        {
            // If the resulting list is not null, but empty, the condition did not pass and the response should be returned prematurely.
            throw new RequestValidationException(
                      $"The '{nameof(foundStubs)}' array for condition was empty, which means the condition was configured and the request did not pass or no conditions are configured at all.");
        }

        var finalStub = _finalStubDeterminer.DetermineFinalStub(foundStubs);

        _scenarioService.IncreaseHitCount(finalStub.Scenario);
        requestLogger.SetExecutingStubId(finalStub.Id);
        var response = await _stubResponseGenerator.GenerateResponseAsync(finalStub);

        return(response);
    }
コード例 #2
0
        public async Task <ResponseModel> ExecuteRequestAsync()
        {
            var requestLogger = _requestLoggerFactory.GetRequestLogger();

            var foundStubs = new List <(StubModel, IEnumerable <ConditionCheckResultModel>)>();
            var stubs      = await _stubContainer.GetStubsAsync();

            foreach (var fullStub in stubs)
            {
                var stub = fullStub.Stub;
                try
                {
                    var passed                    = false;
                    var validationResults         = new List <ConditionCheckResultModel>();
                    var negativeValidationResults = new List <ConditionCheckResultModel>();
                    foreach (var checker in _conditionCheckers)
                    {
                        var result = CheckConditions(stub.Id, checker, stub.Conditions, false);
                        validationResults.Add(result);
                        if (result.ConditionValidation == ConditionValidationType.Invalid)
                        {
                            // If any condition is invalid, skip the rest.
                            break;
                        }

                        // Then check the "negative" conditions. These conditions are the "not" scenarios.
                        result = CheckConditions(stub.Id, checker, stub.NegativeConditions, true);
                        negativeValidationResults.Add(result);
                        if (result.ConditionValidation == ConditionValidationType.Invalid)
                        {
                            // If any condition is invalid, skip the rest.
                            break;
                        }
                    }

                    var allValidationResults       = validationResults.Concat(negativeValidationResults);
                    var conditionCheckResultModels = allValidationResults as ConditionCheckResultModel[] ??
                                                     allValidationResults.ToArray();
                    if ((conditionCheckResultModels.All(r =>
                                                        r.ConditionValidation != ConditionValidationType.Invalid) &&
                         validationResults.Any(r =>
                                               r.ConditionValidation != ConditionValidationType.NotExecuted &&
                                               r.ConditionValidation != ConditionValidationType.NotSet)) ||
                        conditionCheckResultModels.All(
                            r => r.ConditionValidation == ConditionValidationType.NotExecuted))
                    {
                        passed = true;
                        foundStubs.Add((stub, conditionCheckResultModels));
                    }

                    requestLogger.SetStubExecutionResult(stub.Id, passed, validationResults, negativeValidationResults);
                }
                catch (Exception e)
                {
                    _logger.LogWarning($"Exception thrown while executing checks for stub '{stub.Id}': {e}");
                }
            }

            if (!foundStubs.Any())
            {
                // If the resulting list is not null, but empty, the condition did not pass and the response should be returned prematurely.
                throw new RequestValidationException(
                          $"The '{nameof(foundStubs)}' array for condition was empty, which means the condition was configured and the request did not pass or no conditions are configured at all.");
            }

            var finalStub = _finalStubDeterminer.DetermineFinalStub(foundStubs);

            requestLogger.SetExecutingStubId(finalStub.Id);
            var response = await _stubResponseGenerator.GenerateResponseAsync(finalStub);

            return(response);
        }