コード例 #1
0
        private static void AppendNewRequestModel(
            int indentSpaces,
            StringBuilder sb,
            EndpointMethodMetadata endpointMethodMetadata,
            HttpStatusCode httpStatusCode,
            string variableName = "data")
        {
            var schema = endpointMethodMetadata.ContractParameter?.ApiOperation.RequestBody?.Content.GetSchema();

            if (schema == null)
            {
                return;
            }

            GenerateXunitTestHelper.AppendNewModelOrListOfModel(indentSpaces, sb, endpointMethodMetadata, schema, httpStatusCode, variableName);
        }
コード例 #2
0
        private static void AppendNewRequestModel(
            int indentSpaces,
            StringBuilder sb,
            EndpointMethodMetadata endpointMethodMetadata,
            HttpStatusCode httpStatusCode)
        {
            var schema = endpointMethodMetadata.ContractParameter?.ApiOperation.RequestBody?.Content.GetSchema();

            if (schema == null)
            {
                return;
            }

            GenerateXunitTestHelper.AppendVarDataModelOrListOfModel(
                indentSpaces,
                sb,
                endpointMethodMetadata,
                schema,
                httpStatusCode,
                SchemaMapLocatedAreaType.RequestBody);
        }
コード例 #3
0
        private static void AppendNewRequestModelForBadRequest(
            int indentSpaces,
            StringBuilder sb,
            EndpointMethodMetadata endpointMethodMetadata,
            HttpStatusCode httpStatusCode,
            KeyValuePair <string, OpenApiSchema> badPropertySchema)
        {
            var schema = endpointMethodMetadata.ContractParameter?.ApiOperation.RequestBody?.Content.GetSchema();

            if (schema == null)
            {
                return;
            }

            GenerateXunitTestHelper.AppendVarDataModelOrListOfModel(
                indentSpaces,
                sb,
                endpointMethodMetadata,
                schema,
                httpStatusCode,
                SchemaMapLocatedAreaType.RequestBody,
                badPropertySchema,
                asJsonBody: true);
        }
        private static void AppendContentForExecuteAsync(StringBuilder sb, EndpointMethodMetadata endpointMethodMetadata, HttpStatusCode httpStatusCode)
        {
            var contractReturnTypeName = endpointMethodMetadata.ContractReturnTypeNames.First(x => x.Item1 == httpStatusCode);
            var returnTypeName         = contractReturnTypeName.Item2;

            if (returnTypeName == "string")
            {
                sb.AppendLine(
                    12,
                    httpStatusCode == HttpStatusCode.Created
                        ? $"return Task.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}());"
                        : $"return Task.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(\"Hallo world\"));");
            }
            else
            {
                if (contractReturnTypeName.Item3 == null)
                {
                    if (returnTypeName.StartsWith(Microsoft.OpenApi.Models.NameConstants.Pagination, StringComparison.Ordinal) ||
                        returnTypeName.StartsWith(Microsoft.OpenApi.Models.NameConstants.List, StringComparison.Ordinal))
                    {
                        var singleReturnTypeName = returnTypeName
                                                   .Replace(Microsoft.OpenApi.Models.NameConstants.Pagination, string.Empty, StringComparison.Ordinal)
                                                   .Replace(Microsoft.OpenApi.Models.NameConstants.List, string.Empty, StringComparison.Ordinal)
                                                   .Replace("<", string.Empty, StringComparison.Ordinal)
                                                   .Replace(">", string.Empty, StringComparison.Ordinal);
                        var schemaPair = endpointMethodMetadata.ComponentsSchemas.First(x => x.Key == singleReturnTypeName);
                        GenerateXunitTestHelper.AppendNewModelOrListOfModel(12, sb, endpointMethodMetadata, schemaPair.Value, httpStatusCode);
                        sb.AppendLine();
                        if (contractReturnTypeName.Item2.StartsWith(Microsoft.OpenApi.Models.NameConstants.Pagination, StringComparison.Ordinal))
                        {
                            if (endpointMethodMetadata.ContractParameter != null)
                            {
                                var queryParameters = endpointMethodMetadata.ContractParameter.ApiOperation.Parameters.GetAllFromQuery();
                                var sPageSize       = "null";
                                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();
                            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()}());");
                    }
                }
                else
                {
                    GenerateXunitTestHelper.AppendNewModelOrListOfModel(12, sb, endpointMethodMetadata, contractReturnTypeName.Item3, httpStatusCode);
                    sb.AppendLine();
                    sb.AppendLine(12, $"return Task.FromResult({endpointMethodMetadata.ContractResultTypeName}.{httpStatusCode.ToNormalizedString()}(data));");
                }
            }
        }
コード例 #5
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;
            }
            }
        }