Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Newtonsoft.Json;
using QAToolKit.Core.Models;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down
28 changes: 26 additions & 2 deletions src/QAToolKit.Source.Swagger.Test/SwaggerOptionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;

namespace QAToolKit.Source.Swagger.Test
Expand Down Expand Up @@ -88,12 +87,37 @@ public void SwaggerTestTypeRequestFiltersTest_Successful()
}

[Fact]
public void SwaggerAddBaseUrlRequestFiltersTest_Successful()
public void SwaggerAddBaseUrlOnRequestFiltersTest_Successful()
{
var options = new SwaggerOptions();
options.AddBaseUrl(new Uri("https://petstore3.swagger.io/"));

Assert.Equal("https://petstore3.swagger.io/", options.BaseUrl.ToString());
}

[Fact]
public void SwaggerAddBaseUrlOffRequestFiltersTest_Successful()
{
var options = new SwaggerOptions();

Assert.Null(options.BaseUrl);
}

[Fact]
public void SwaggerAddDataGenerationOnRequestFiltersTest_Successful()
{
var options = new SwaggerOptions();
options.AddDataGeneration();

Assert.True(options.UseDataGeneration);
}

[Fact]
public void SwaggerAddDataGenerationOffRequestFiltersTest_Successful()
{
var options = new SwaggerOptions();

Assert.False(options.UseDataGeneration);
}
}
}
3 changes: 2 additions & 1 deletion src/QAToolKit.Source.Swagger/QAToolKit.Source.Swagger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Faker.Net" Version="1.3.82" />
<PackageReference Include="Microsoft.OpenApi" Version="1.2.3" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.2.3" />
<PackageReference Include="QAToolKit.Core" Version="0.1.2" />
<PackageReference Include="QAToolKit.Core" Version="0.1.3" />
</ItemGroup>

</Project>
83 changes: 83 additions & 0 deletions src/QAToolKit.Source.Swagger/SwaggerDataGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using QAToolKit.Core.Models;
using System.Collections.Generic;

namespace QAToolKit.Source.Swagger
{
/// <summary>
/// Data generator for Swagger that will generate the data for the models or URLs. It is applied after the user defined replaced values.
/// </summary>
public class SwaggerDataGenerator
{
private readonly IList<HttpTestRequest> _requests;

/// <summary>
/// Swagger data generator
/// </summary>
/// <param name="requests"></param>
public SwaggerDataGenerator(IList<HttpTestRequest> requests)
{
_requests = requests;
}

/// <summary>
/// Generate HTTP request model data
/// </summary>
/// <returns></returns>
public IList<HttpTestRequest> GenerateModelValues()
{
foreach (var request in _requests)
{
foreach (var body in request.RequestBodies)
{
if (body.ContentType == ContentType.Json)
{
var propsTemp = new List<Property>();
foreach (var property in body.Properties)
{
propsTemp.Add(GeneratePropertyValue(property));
}

body.Properties = propsTemp;
}
}
}

return _requests;
}

private Property GeneratePropertyValue(Property property)
{
switch (property.Type)
{
case "integer":
if (string.IsNullOrEmpty(property.Value))
{
property.Value = Faker.RandomNumber.Next(0, 1).ToString();
}

break;
case "string":
property.Value = Faker.Lorem.Sentence(1);
break;
case "object":
property.Value = Faker.Lorem.Sentence(1);
break;
case "array":
foreach (var prop in property.Items.Properties)
{
prop.Value = Faker.Lorem.Sentence(1);
}
break;
default:
break;
}

return property;
}

/* public IList<HttpTestRequestV2> GenerateUrlValues()
{

}*/
}
}
22 changes: 21 additions & 1 deletion src/QAToolKit.Source.Swagger/SwaggerFileSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,27 @@ public async Task<IList<HttpTestRequest>> Load(IList<FileInfo> source)
var textWritter = new OpenApiJsonWriter(new StringWriter());
openApiDocument.SerializeAsV3(textWritter);

restRequests.AddRange(processor.MapFromOpenApiDocument(_swaggerOptions.BaseUrl, openApiDocument, _swaggerOptions.ReplacementValues));
var requests = processor.MapFromOpenApiDocument(_swaggerOptions.BaseUrl, openApiDocument);

if (_swaggerOptions.UseRequestFilter)
{
var filters = new SwaggerRequestFilter(requests);
requests = filters.FilterRequests(_swaggerOptions.RequestFilter);
}

if (_swaggerOptions.UseDataGeneration)
{
var generator = new SwaggerDataGenerator(requests);
requests = generator.GenerateModelValues();
}

if (_swaggerOptions.ReplacementValues != null)
{
var generator = new SwaggerValueReplacement(requests, _swaggerOptions.ReplacementValues);
requests = generator.ReplaceAll();
}

restRequests.AddRange(requests);
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/QAToolKit.Source.Swagger/SwaggerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public class SwaggerOptions
/// Set custom base API URL
/// </summary>
internal Uri BaseUrl { get; private set; }
/// <summary>
/// Should data be automatically generated
/// </summary>
internal bool UseDataGeneration { get; private set; } = false;

/// <summary>
/// Add basic authentication
Expand Down Expand Up @@ -86,5 +90,15 @@ public SwaggerOptions AddBaseUrl(Uri baseUrl)
BaseUrl = baseUrl;
return this;
}

/// <summary>
/// Add data generation to the Swagger processor
/// </summary>
/// <returns></returns>
public SwaggerOptions AddDataGeneration()
{
UseDataGeneration = true;
return this;
}
}
}
Loading