Exemplo n.º 1
0
        private static void AppendContentForExecuteAsynchronous(
            StringBuilder sb,
            EndpointMethodMetadata endpointMethodMetadata,
            HttpStatusCode httpStatusCode)
        {
            var contractReturnTypeName = endpointMethodMetadata.ContractReturnTypeNames.First(x => x.Item1 == httpStatusCode);
            var returnTypeName         = contractReturnTypeName.Item2;

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

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

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

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

            default:
            {
                var singleReturnTypeName = OpenApiDocumentSchemaModelNameHelper.GetRawModelName(returnTypeName);
                var modelSchema          = endpointMethodMetadata.ComponentsSchemas.GetSchemaByModelName(singleReturnTypeName);

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

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

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

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

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

                        sb.AppendLine(12, $"return Task.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(paginationData));");
                    }
                    else
                    {
                        sb.AppendLine(12, $"return Task.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(data));");
                    }
                }
                else
                {
                    sb.AppendLine(12, $"return Task.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(data));");
                }

                break;
            }
            }
        }
        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)
        {
            var trailingChar = TrailingCharType.SemiColon;
            if (asJsonBody)
            {
                trailingChar = TrailingCharType.None;
            }

            switch (locatedAreaType)
            {
                case SchemaMapLocatedAreaType.Parameter:
                    break;
                case SchemaMapLocatedAreaType.RequestBody:
                    if (schema.Type == OpenApiDataTypeConstants.Array)
                    {
                        int indentSpacesForData = indentSpaces;
                        if (asJsonBody)
                        {
                            sb.AppendLine(indentSpaces, "var sb = new StringBuilder();");
                            indentSpacesForData = indentSpacesForData - 4;
                        }

                        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.Item1 == httpStatusCode);
                    if (GenerateXunitTestPartsHelper.IsListKind(contractReturnTypeName.Item2))
                    {
                        AppendVarDataEqualNewListOfModel(
                            indentSpaces,
                            sb,
                            endpointMethodMetadata,
                            new KeyValuePair<string, OpenApiSchema>("data", contractReturnTypeName.Item3!),
                            trailingChar,
                            maxItemsForList,
                            depthHierarchy,
                            maxDepthHierarchy,
                            badPropertySchema,
                            asJsonBody);
                    }
                    else
                    {
                        GenerateXunitTestPartsHelper.AppendPartVarDataEqualNew(12, sb);
                        AppendModel(
                            indentSpaces,
                            sb,
                            endpointMethodMetadata,
                            contractReturnTypeName.Item2,
                            contractReturnTypeName.Item3!,
                            trailingChar,
                            0,
                            maxItemsForList,
                            depthHierarchy,
                            maxDepthHierarchy,
                            badPropertySchema,
                            asJsonBody);
                    }

                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(locatedAreaType), locatedAreaType, null);
            }
        }