Esempio n. 1
0
    private static string GetJsonPathValue(Match match, JToken token)
    {
        var jsonPathQuery = match.Groups[2].Value;
        var foundValue    = token?.SelectToken(jsonPathQuery);

        return(foundValue != null?JsonUtilities.ConvertFoundValue(foundValue) : string.Empty);
    }
    public void ConvertFoundValue_TokenIsJArray_ArrayHasNoValues_ShouldReturnEmptyString()
    {
        // Arrange
        var jArray = JArray.FromObject(Array.Empty <string>());

        // Act
        var result = JsonUtilities.ConvertFoundValue(jArray);

        // Assert
        Assert.AreEqual(string.Empty, result);
    }
    public void ConvertFoundValue_TokenIsJArray_ArrayHasValues_ShouldReturnValueCorrectly()
    {
        // Arrange
        var jArray = JArray.FromObject(new[] { "val1", "val2" });

        // Act
        var result = JsonUtilities.ConvertFoundValue(jArray);

        // Assert
        Assert.AreEqual("val1", result);
    }
    public void ConvertFoundValue_TokenIsJValue_ShouldReturnValueCorrectly()
    {
        // Arrange
        var jValue = JValue.CreateString("value");

        // Act
        var result = JsonUtilities.ConvertFoundValue(jValue);

        // Assert
        Assert.AreEqual("value", result);
    }
    public void ConvertFoundValue_TokenIsJObject_ShouldThrowInvalidOperationException()
    {
        // Arrange
        var jObject = JObject.FromObject(new { });

        // Act
        var exception =
            Assert.ThrowsException <InvalidOperationException>(() =>
                                                               JsonUtilities.ConvertFoundValue(jObject));

        // Assert
        Assert.AreEqual("JSON type 'Newtonsoft.Json.Linq.JObject' not supported.", exception.Message);
    }
Esempio n. 6
0
    /// <inheritdoc />
    public ConditionCheckResultModel Validate(StubModel stub)
    {
        var result             = new ConditionCheckResultModel();
        var jsonPathConditions = stub.Conditions?.JsonPath?.ToArray();

        if (jsonPathConditions == null || jsonPathConditions?.Any() != true)
        {
            return(result);
        }

        var validJsonPaths = 0;
        var body           = _httpContextService.GetBody();
        var jsonObject     = JObject.Parse(body);

        foreach (var condition in jsonPathConditions)
        {
            if (condition is string conditionString)
            {
                // Condition is a string, so perform plain text JSONPath condition check.
                var elements = jsonObject.SelectToken(conditionString);
                if (elements == null)
                {
                    // No suitable JSON results found.
                    result.Log = $"No suitable JSON results found with JSONPath query '{conditionString}'.";
                    break;
                }

                validJsonPaths++;
            }
            else
            {
                // Condition is an object, so first convert the condition to a StubJsonPathModel before executing the condition checker.
                var jsonPathCondition = ConvertJsonPathCondition(stub.Id, condition);

                var passed = false;
                if (jsonPathCondition != null)
                {
                    var elements = jsonObject.SelectToken(jsonPathCondition.Query);
                    if (elements != null)
                    {
                        // Retrieve the value from the JSONPath result.
                        var foundValue = JsonUtilities.ConvertFoundValue(elements);

                        // If a value is set for the condition, check if the found JSONPath value matches the value in the condition.
                        passed = string.IsNullOrWhiteSpace(jsonPathCondition.ExpectedValue) ||
                                 StringHelper.IsRegexMatchOrSubstring(
                            foundValue,
                            jsonPathCondition.ExpectedValue);
                    }
                }

                if (!passed)
                {
                    result.Log = "No suitable JSON results found.";
                }

                validJsonPaths += passed ? 1 : 0;
            }
        }

        // If the number of succeeded conditions is equal to the actual number of conditions,
        // the header condition is passed and the stub ID is passed to the result.
        result.ConditionValidation = validJsonPaths == jsonPathConditions.Length
            ? ConditionValidationType.Valid
            : ConditionValidationType.Invalid;

        return(result);
    }