public static void AppendDataEqualNewListOfModel(
        int indentSpaces,
        StringBuilder sb,
        EndpointMethodMetadata endpointMethodMetadata,
        KeyValuePair <string, OpenApiSchema> schemaProperty,
        TrailingCharType trailingChar,
        int maxItemsForList,
        int depthHierarchy,
        int maxDepthHierarchy,
        KeyValuePair <string, OpenApiSchema>?badPropertySchema,
        bool asJsonBody)
    {
        var modelName       = schemaProperty.Value.GetModelName();
        var renderModelName = OpenApiDocumentSchemaModelNameHelper.EnsureModelNameWithNamespaceIfNeeded(endpointMethodMetadata, modelName);
        var propertyName    = schemaProperty.Key.EnsureFirstCharacterToUpper();
        var jsonSpacesCount = (depthHierarchy * 2) + 2;

        if (depthHierarchy > maxDepthHierarchy)
        {
            if (asJsonBody)
            {
                // TODO Missing Json support.
            }
            else
            {
                sb.AppendLine(
                    indentSpaces,
                    $"{propertyName} = new List<{renderModelName}>()" + GenerateCodeHelper.GetTrailingChar(trailingChar));
                return;
            }
        }

        if (asJsonBody)
        {
            var useForBadRequest = badPropertySchema is not null &&
                                   schemaProperty.Key.Equals(badPropertySchema.Value.Key, StringComparison.Ordinal);

            if (useForBadRequest)
            {
                sb.AppendLine(
                    indentSpaces - jsonSpacesCount,
                    GenerateXunitTestPartsHelper.WrapInStringBuilderAppendLineWithKeyQuotes(depthHierarchy - 1, propertyName, "null", trailingChar));
                return;
            }

            sb.AppendLine(
                indentSpaces - jsonSpacesCount,
                GenerateXunitTestPartsHelper.WrapInStringBuilderAppendLineWithKeyQuotes(depthHierarchy - 1, propertyName, "[", TrailingCharType.None));
        }
        else
        {
            GenerateXunitTestPartsHelper.AppendPartDataEqualNewListOf(
                indentSpaces,
                sb,
                propertyName,
                modelName);
            sb.AppendLine();
            sb.AppendLine(indentSpaces, "{");
        }

        var modelSchema         = endpointMethodMetadata.ComponentsSchemas.GetSchemaByModelName(modelName);
        var currentIndentSpaces = asJsonBody
            ? indentSpaces - jsonSpacesCount
            : indentSpaces + 4;

        for (int i = 0; i < maxItemsForList; i++)
        {
            var trailingCharForProperty = GenerateXunitTestPartsHelper.GetTrailingCharForProperty(asJsonBody, i, maxItemsForList);

            if (!asJsonBody)
            {
                GenerateXunitTestPartsHelper.AppendPartDataNew(currentIndentSpaces, sb);
            }

            AppendModel(
                currentIndentSpaces,
                sb,
                endpointMethodMetadata,
                modelName,
                modelSchema,
                trailingCharForProperty,
                i + 1,
                maxItemsForList,
                depthHierarchy,
                maxDepthHierarchy,
                badPropertySchema,
                asJsonBody);
        }

        if (asJsonBody)
        {
            var jsonSpaces = string.Empty.PadLeft((depthHierarchy - 1) * 2);
            sb.AppendLine(
                indentSpaces - jsonSpacesCount,
                GenerateXunitTestPartsHelper.WrapInStringBuilderAppendLine($"{jsonSpaces}  ]{GenerateCodeHelper.GetTrailingChar(trailingChar)}"));
        }
        else
        {
            sb.AppendLine(
                indentSpaces,
                $"}}{GenerateCodeHelper.GetTrailingChar(trailingChar)}");
        }
    }
    public static void AppendVarDataModelOrListOfModel(
        int indentSpaces,
        StringBuilder sb,
        EndpointMethodMetadata endpointMethodMetadata,
        OpenApiSchema schema,
        HttpStatusCode httpStatusCode,
        SchemaMapLocatedAreaType locatedAreaType,
        KeyValuePair <string, OpenApiSchema>?badPropertySchema = null,
        bool asJsonBody       = false,
        int maxItemsForList   = 3,
        int depthHierarchy    = 0,
        int maxDepthHierarchy = 2)
    {
        ArgumentNullException.ThrowIfNull(endpointMethodMetadata);

        var trailingChar = TrailingCharType.SemiColon;

        if (asJsonBody)
        {
            trailingChar = TrailingCharType.None;
        }

        switch (locatedAreaType)
        {
        case SchemaMapLocatedAreaType.Parameter:
            break;

        case SchemaMapLocatedAreaType.RequestBody:
            if (schema.IsTypeArray())
            {
                var indentSpacesForData = indentSpaces;
                if (asJsonBody)
                {
                    sb.AppendLine(indentSpaces, "var sb = new StringBuilder();");
                    indentSpacesForData -= 4;
                }

                if (schema.HasItemsWithFormatTypeBinary())
                {
                    if (asJsonBody)
                    {
                        throw new NotSupportedException("JSON not supported when RequestBody is type Array and format type is Binary.");
                    }

                    sb.AppendLine(indentSpaces, "var data = GetTestFiles();");
                }
                else
                {
                    AppendVarDataEqualNewListOfModel(
                        indentSpacesForData,
                        sb,
                        endpointMethodMetadata,
                        new KeyValuePair <string, OpenApiSchema>("data", schema),
                        trailingChar,
                        maxItemsForList,
                        depthHierarchy,
                        maxDepthHierarchy,
                        badPropertySchema,
                        asJsonBody);
                }

                if (asJsonBody)
                {
                    sb.AppendLine(indentSpaces, "var data = sb.ToString();");
                }
            }
            else
            {
                if (asJsonBody)
                {
                    sb.AppendLine(indentSpaces, "var sb = new StringBuilder();");
                }
                else
                {
                    GenerateXunitTestPartsHelper.AppendPartVarDataEqualNew(12, sb);
                }

                var modelName = schema.GetModelName();
                AppendModel(
                    indentSpaces,
                    sb,
                    endpointMethodMetadata,
                    modelName,
                    schema,
                    trailingChar,
                    0,
                    maxItemsForList,
                    depthHierarchy,
                    maxDepthHierarchy,
                    badPropertySchema,
                    asJsonBody);

                if (asJsonBody)
                {
                    sb.AppendLine(indentSpaces, "var data = sb.ToString();");
                }
            }

            break;

        case SchemaMapLocatedAreaType.Response:
            var contractReturnTypeName = endpointMethodMetadata.ContractReturnTypeNames.First(x => x.StatusCode == httpStatusCode);
            if (GenerateXunitTestPartsHelper.IsListKind(contractReturnTypeName.FullModelName))
            {
                AppendVarDataEqualNewListOfModel(
                    indentSpaces,
                    sb,
                    endpointMethodMetadata,
                    new KeyValuePair <string, OpenApiSchema>("data", contractReturnTypeName.Schema !),
                    trailingChar,
                    maxItemsForList,
                    depthHierarchy,
                    maxDepthHierarchy,
                    badPropertySchema,
                    asJsonBody);
            }
            else
            {
                GenerateXunitTestPartsHelper.AppendPartVarDataEqualNew(12, sb);
                AppendModel(
                    indentSpaces,
                    sb,
                    endpointMethodMetadata,
                    contractReturnTypeName.FullModelName,
                    contractReturnTypeName.Schema !,
                    trailingChar,
                    0,
                    maxItemsForList,
                    depthHierarchy,
                    maxDepthHierarchy,
                    badPropertySchema,
                    asJsonBody);
            }

            break;

        default:
            throw new ArgumentOutOfRangeException(nameof(locatedAreaType), locatedAreaType, message: null);
        }
    }
    public static void AppendModelComplexProperty(
        int indentSpaces,
        StringBuilder sb,
        EndpointMethodMetadata endpointMethodMetadata,
        KeyValuePair <string, OpenApiSchema> schemaProperty,
        string dataType,
        TrailingCharType trailingChar,
        int itemNumber,
        int maxItemsForList,
        int depthHierarchy,
        int maxDepthHierarchy,
        KeyValuePair <string, OpenApiSchema>?badPropertySchema,
        bool asJsonBody)
    {
        var propertyName = schemaProperty.Key.EnsureFirstCharacterToUpper();

        if (depthHierarchy > maxDepthHierarchy)
        {
            if (asJsonBody)
            {
                // TODO Missing Json support.
            }
            else
            {
                sb.AppendLine(
                    indentSpaces,
                    $"{propertyName} = null{GenerateCodeHelper.GetTrailingChar(trailingChar)}");
                return;
            }
        }

        if (!asJsonBody)
        {
            indentSpaces += 4;
            GenerateXunitTestPartsHelper.AppendPartDataEqualNew(
                indentSpaces,
                sb,
                propertyName);
        }

        var schemaPropertyValue = schemaProperty.Value;

        if (schemaProperty.Value.Properties.Count == 0 &&
            schemaProperty.Value.OneOf.Count > 0)
        {
            schemaPropertyValue = schemaProperty.Value.OneOf.First();
        }

        var modelName = schemaProperty.Key.EnsureFirstCharacterToUpper();

        AppendModel(
            indentSpaces,
            sb,
            endpointMethodMetadata,
            dataType,
            schemaPropertyValue,
            trailingChar,
            itemNumber,
            maxItemsForList,
            depthHierarchy,
            maxDepthHierarchy,
            badPropertySchema,
            asJsonBody,
            modelName);
    }
    public static void AppendVarDataEqualNewListOfModel(
        int indentSpaces,
        StringBuilder sb,
        EndpointMethodMetadata endpointMethodMetadata,
        KeyValuePair <string, OpenApiSchema> schemaProperty,
        TrailingCharType trailingChar,
        int maxItemsForList,
        int depthHierarchy,
        int maxDepthHierarchy,
        KeyValuePair <string, OpenApiSchema>?badPropertySchema,
        bool asJsonBody)
    {
        ArgumentNullException.ThrowIfNull(sb);

        var modelName       = schemaProperty.Value.GetModelName();
        var renderModelName = OpenApiDocumentSchemaModelNameHelper.EnsureModelNameWithNamespaceIfNeeded(endpointMethodMetadata, modelName);

        if (depthHierarchy > maxDepthHierarchy)
        {
            if (asJsonBody)
            {
                // TODO Missing Json support.
            }
            else
            {
                sb.AppendLine(
                    indentSpaces,
                    $"var {schemaProperty.Key} = new List<{renderModelName}>(){GenerateCodeHelper.GetTrailingChar(trailingChar)}");
                return;
            }
        }

        if (!asJsonBody)
        {
            GenerateXunitTestPartsHelper.AppendPartVarDataEqualNewListOf(
                indentSpaces,
                sb,
                schemaProperty.Key,
                renderModelName);
            sb.AppendLine();
            sb.AppendLine(indentSpaces, "{");
        }

        var modelSchema = endpointMethodMetadata.ComponentsSchemas.GetSchemaByModelName(modelName);

        for (var i = 0; i < maxItemsForList; i++)
        {
            var trailingCharForProperty = GenerateXunitTestPartsHelper.GetTrailingCharForProperty(asJsonBody, i, maxItemsForList);
            var indentSpacesForItem     = indentSpaces + 4;
            if (!asJsonBody)
            {
                indentSpacesForItem = indentSpaces + 4;
                GenerateXunitTestPartsHelper.AppendPartDataNew(indentSpacesForItem, sb);
            }

            AppendModel(
                indentSpacesForItem,
                sb,
                endpointMethodMetadata,
                modelName,
                modelSchema,
                trailingCharForProperty,
                i + 1,
                maxItemsForList,
                depthHierarchy,
                maxDepthHierarchy,
                badPropertySchema,
                asJsonBody);
        }

        if (!asJsonBody)
        {
            sb.AppendLine(
                indentSpaces,
                $"}}{GenerateCodeHelper.GetTrailingChar(trailingChar)}");
        }
    }
    private static void AppendContentForExecuteAsynchronous(
        StringBuilder sb,
        EndpointMethodMetadata endpointMethodMetadata,
        HttpStatusCode httpStatusCode)
    {
        var contractReturnTypeName = endpointMethodMetadata.ContractReturnTypeNames.First(x => x.StatusCode == httpStatusCode);
        var returnTypeName         = contractReturnTypeName.FullModelName;

        switch (returnTypeName)
        {
        case "string":
            sb.AppendLine(
                12,
                httpStatusCode == HttpStatusCode.Created
                        ? $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}());"
                        : $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(\"Hallo world\"));");
            break;

        case "bool":
            sb.AppendLine(
                12,
                httpStatusCode == HttpStatusCode.Created
                        ? $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}());"
                        : $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(true));");
            break;

        case "int":
        case "long":
            sb.AppendLine(
                12,
                httpStatusCode == HttpStatusCode.Created
                        ? $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}());"
                        : $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(42));");
            break;

        case "float":
        case "double":
            sb.AppendLine(
                12,
                httpStatusCode == HttpStatusCode.Created
                        ? $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}());"
                        : $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(42.2));");
            break;

        case "Guid":
            sb.AppendLine(
                12,
                httpStatusCode == HttpStatusCode.Created
                        ? $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}());"
                        : $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(System.Guid.NewGuid()));");
            break;

        case "byte[]":
            sb.AppendLine(12, "var bytes = System.Text.Encoding.UTF8.GetBytes(\"Hello World\");");
            sb.AppendLine(12, $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(bytes, \"dummy.txt\"));");
            break;

        default:
        {
            var singleReturnTypeName = OpenApiDocumentSchemaModelNameHelper.GetRawModelName(returnTypeName);
            var simpleTypePair       = SimpleTypeHelper.BeautifySimpleTypeLookup.FirstOrDefault(x => x.Value == singleReturnTypeName);

            if (simpleTypePair.Key is not null)
            {
                GenerateXunitTestHelper.AppendVarDataListSimpleType(
                    12,
                    sb,
                    simpleTypePair.Value);
                sb.AppendLine();
            }
            else
            {
                var modelSchema = endpointMethodMetadata.ComponentsSchemas.GetSchemaByModelName(singleReturnTypeName);

                GenerateXunitTestHelper.AppendVarDataModelOrListOfModel(
                    12,
                    sb,
                    endpointMethodMetadata,
                    modelSchema,
                    httpStatusCode,
                    SchemaMapLocatedAreaType.Response);
                sb.AppendLine();
            }

            if (contractReturnTypeName.Schema is null ||
                GenerateXunitTestPartsHelper.IsListKind(returnTypeName))
            {
                if (returnTypeName.StartsWith(Microsoft.OpenApi.Models.NameConstants.Pagination, StringComparison.Ordinal))
                {
                    if (endpointMethodMetadata.ContractParameter is not null)
                    {
                        var queryParameters = endpointMethodMetadata.ContractParameter.ApiOperation.Parameters.GetAllFromQuery();
                        var sPageSize       = "10";
                        if (queryParameters.Find(x => x.Name.Equals("PageSize", StringComparison.OrdinalIgnoreCase)) is not null)
                        {
                            sPageSize = "parameters.PageSize";
                        }

                        var sQueryString = "null";
                        if (queryParameters.Find(x => x.Name.Equals("QueryString", StringComparison.OrdinalIgnoreCase)) is not null)
                        {
                            sQueryString = "parameters.QueryString";
                        }

                        var sContinuationToken = "null";
                        if (queryParameters.Find(x => x.Name.Equals("ContinuationToken", StringComparison.OrdinalIgnoreCase)) is not null)
                        {
                            sContinuationToken = "parameters.ContinuationToken";
                        }

                        sb.AppendLine(12, $"var paginationData = new {contractReturnTypeName.FullModelName}(data, {sPageSize}, {sQueryString}, {sContinuationToken});");
                    }
                    else
                    {
                        sb.AppendLine(12, $"var paginationData = new {contractReturnTypeName.FullModelName}(data, 10, null, null);");
                    }

                    sb.AppendLine(12, $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(paginationData));");
                }
                else
                {
                    sb.AppendLine(12, $"return {OpenApiDocumentSchemaModelNameHelper.EnsureTaskNameWithNamespaceIfNeeded(contractReturnTypeName)}.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(data));");
                }
            }
    public static void AppendModel(
        int indentSpaces,
        StringBuilder sb,
        EndpointMethodMetadata endpointMethodMetadata,
        string modelName,
        OpenApiSchema schema,
        TrailingCharType trailingChar,
        int itemNumber,
        int maxItemsForList,
        int depthHierarchy,
        int maxDepthHierarchy,
        KeyValuePair <string, OpenApiSchema>?badPropertySchema,
        bool asJsonBody,
        string?parentModelNameToJsonBody = null)
    {
        ArgumentNullException.ThrowIfNull(sb);
        ArgumentNullException.ThrowIfNull(schema);

        var countString     = 1;
        var renderModelName = OpenApiDocumentSchemaModelNameHelper.EnsureModelNameWithNamespaceIfNeeded(endpointMethodMetadata, modelName);
        var jsonSpaces      = string.Empty.PadLeft(depthHierarchy * 2);

        if (asJsonBody)
        {
            sb.AppendLine(
                indentSpaces,
                string.IsNullOrEmpty(parentModelNameToJsonBody)
                    ? GenerateXunitTestPartsHelper.WrapInStringBuilderAppendLine($"{jsonSpaces}{{")
                    : GenerateXunitTestPartsHelper.WrapInStringBuilderAppendLine($"{jsonSpaces}\\\"{parentModelNameToJsonBody}\\\": {{"));
        }
        else
        {
            sb.AppendLine(renderModelName);
            sb.AppendLine(indentSpaces, "{");
        }

        foreach (var schemaProperty in schema.Properties)
        {
            var trailingCharForProperty = GenerateXunitTestPartsHelper.GetTrailingCharForProperty(asJsonBody, schemaProperty, schema.Properties);
            var useForBadRequest        = badPropertySchema is not null &&
                                          schemaProperty.Key.Equals(badPropertySchema.Value.Key, StringComparison.Ordinal);

            var dataType = schemaProperty.Value.GetDataType();

            var propertyValueGenerated = GenerateXunitTestPartsHelper.PropertyValueGenerator(
                schemaProperty,
                endpointMethodMetadata.ComponentsSchemas,
                useForBadRequest,
                itemNumber,
                customValue: null);

            if ("NEW-INSTANCE-LIST".Equals(propertyValueGenerated, StringComparison.Ordinal))
            {
                AppendDataEqualNewListOfModel(
                    indentSpaces + 4,
                    sb,
                    endpointMethodMetadata,
                    schemaProperty,
                    trailingCharForProperty,
                    maxItemsForList,
                    depthHierarchy + 1,
                    maxDepthHierarchy,
                    badPropertySchema,
                    asJsonBody);
            }
            else if ("NEW-INSTANCE".Equals(propertyValueGenerated, StringComparison.Ordinal))
            {
                AppendModelComplexProperty(
                    indentSpaces,
                    sb,
                    endpointMethodMetadata,
                    schemaProperty,
                    dataType,
                    trailingCharForProperty,
                    itemNumber,
                    maxItemsForList,
                    depthHierarchy + 1,
                    maxDepthHierarchy,
                    badPropertySchema,
                    asJsonBody);
            }
            else
            {
                var countResult = GenerateXunitTestPartsHelper.AppendModelSimpleProperty(
                    indentSpaces,
                    sb,
                    endpointMethodMetadata,
                    schemaProperty,
                    dataType,
                    schema.Required.Contains(schemaProperty.Key),
                    propertyValueGenerated,
                    countString,
                    asJsonBody,
                    depthHierarchy,
                    trailingCharForProperty);

                if (countResult > 1)
                {
                    countString += 1;
                }
            }
        }

        sb.AppendLine(
            indentSpaces,
            asJsonBody
                ? GenerateXunitTestPartsHelper.WrapInStringBuilderAppendLine($"{jsonSpaces}}}{GenerateCodeHelper.GetTrailingChar(trailingChar)}")
                : $"}}{GenerateCodeHelper.GetTrailingChar(trailingChar)}");
    }
Exemplo n.º 7
0
 public void WrapInStringBuilderAppendLineWithKeyAndValueQuotes(string expected, int depthHierarchy, string key, string value, TrailingCharType trailingChar)
 => Assert.Equal(
     expected,
     GenerateXunitTestPartsHelper.WrapInStringBuilderAppendLineWithKeyAndValueQuotes(depthHierarchy, key, value, trailingChar));
Exemplo n.º 8
0
 public void WrapInStringBuilderAppendLine(string expected, string value)
 => Assert.Equal(
     expected,
     GenerateXunitTestPartsHelper.WrapInStringBuilderAppendLine(value));
Exemplo n.º 9
0
 public void WrapInQuotes(string expected, string value)
 => Assert.Equal(
     expected,
     GenerateXunitTestPartsHelper.WrapInQuotes(value));
Exemplo n.º 10
0
 public void GetTrailingCharForProperty(TrailingCharType expected, bool asJsonBody, int currentItem, int totalItems)
 => Assert.Equal(
     expected,
     GenerateXunitTestPartsHelper.GetTrailingCharForProperty(asJsonBody, currentItem, totalItems));
Exemplo n.º 11
0
 public void IsListKind(bool expected, string value)
 => Assert.Equal(
     expected,
     GenerateXunitTestPartsHelper.IsListKind(value));