diff --git a/src/QAToolKit.Source.Swagger.Test/SwaggerFileSourceIntegrationTest.cs b/src/QAToolKit.Source.Swagger.Test/SwaggerFileSourceIntegrationTest.cs
index 221c723..30cd674 100644
--- a/src/QAToolKit.Source.Swagger.Test/SwaggerFileSourceIntegrationTest.cs
+++ b/src/QAToolKit.Source.Swagger.Test/SwaggerFileSourceIntegrationTest.cs
@@ -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;
diff --git a/src/QAToolKit.Source.Swagger.Test/SwaggerOptionsTest.cs b/src/QAToolKit.Source.Swagger.Test/SwaggerOptionsTest.cs
index ebb0749..59a5dd9 100644
--- a/src/QAToolKit.Source.Swagger.Test/SwaggerOptionsTest.cs
+++ b/src/QAToolKit.Source.Swagger.Test/SwaggerOptionsTest.cs
@@ -2,7 +2,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
-using System.Text;
using Xunit;
namespace QAToolKit.Source.Swagger.Test
@@ -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);
+ }
}
}
diff --git a/src/QAToolKit.Source.Swagger/QAToolKit.Source.Swagger.csproj b/src/QAToolKit.Source.Swagger/QAToolKit.Source.Swagger.csproj
index de3b6be..5aec772 100644
--- a/src/QAToolKit.Source.Swagger/QAToolKit.Source.Swagger.csproj
+++ b/src/QAToolKit.Source.Swagger/QAToolKit.Source.Swagger.csproj
@@ -32,9 +32,10 @@
+
-
+
diff --git a/src/QAToolKit.Source.Swagger/SwaggerDataGenerator.cs b/src/QAToolKit.Source.Swagger/SwaggerDataGenerator.cs
new file mode 100644
index 0000000..bba560a
--- /dev/null
+++ b/src/QAToolKit.Source.Swagger/SwaggerDataGenerator.cs
@@ -0,0 +1,83 @@
+using QAToolKit.Core.Models;
+using System.Collections.Generic;
+
+namespace QAToolKit.Source.Swagger
+{
+ ///
+ /// Data generator for Swagger that will generate the data for the models or URLs. It is applied after the user defined replaced values.
+ ///
+ public class SwaggerDataGenerator
+ {
+ private readonly IList _requests;
+
+ ///
+ /// Swagger data generator
+ ///
+ ///
+ public SwaggerDataGenerator(IList requests)
+ {
+ _requests = requests;
+ }
+
+ ///
+ /// Generate HTTP request model data
+ ///
+ ///
+ public IList GenerateModelValues()
+ {
+ foreach (var request in _requests)
+ {
+ foreach (var body in request.RequestBodies)
+ {
+ if (body.ContentType == ContentType.Json)
+ {
+ var propsTemp = new List();
+ 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 GenerateUrlValues()
+ {
+
+ }*/
+ }
+}
diff --git a/src/QAToolKit.Source.Swagger/SwaggerFileSource.cs b/src/QAToolKit.Source.Swagger/SwaggerFileSource.cs
index 07a7822..0933daa 100644
--- a/src/QAToolKit.Source.Swagger/SwaggerFileSource.cs
+++ b/src/QAToolKit.Source.Swagger/SwaggerFileSource.cs
@@ -49,7 +49,27 @@ public async Task> Load(IList 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);
}
}
diff --git a/src/QAToolKit.Source.Swagger/SwaggerOptions.cs b/src/QAToolKit.Source.Swagger/SwaggerOptions.cs
index cdf8eaa..89e20a3 100644
--- a/src/QAToolKit.Source.Swagger/SwaggerOptions.cs
+++ b/src/QAToolKit.Source.Swagger/SwaggerOptions.cs
@@ -38,6 +38,10 @@ public class SwaggerOptions
/// Set custom base API URL
///
internal Uri BaseUrl { get; private set; }
+ ///
+ /// Should data be automatically generated
+ ///
+ internal bool UseDataGeneration { get; private set; } = false;
///
/// Add basic authentication
@@ -86,5 +90,15 @@ public SwaggerOptions AddBaseUrl(Uri baseUrl)
BaseUrl = baseUrl;
return this;
}
+
+ ///
+ /// Add data generation to the Swagger processor
+ ///
+ ///
+ public SwaggerOptions AddDataGeneration()
+ {
+ UseDataGeneration = true;
+ return this;
+ }
}
}
diff --git a/src/QAToolKit.Source.Swagger/SwaggerProcessor.cs b/src/QAToolKit.Source.Swagger/SwaggerProcessor.cs
index 1738aee..7243c9b 100644
--- a/src/QAToolKit.Source.Swagger/SwaggerProcessor.cs
+++ b/src/QAToolKit.Source.Swagger/SwaggerProcessor.cs
@@ -1,7 +1,11 @@
-using Microsoft.OpenApi.Models;
+using Microsoft.OpenApi.Any;
+using Microsoft.OpenApi.Models;
+using Microsoft.OpenApi.Writers;
using QAToolKit.Core.Models;
using System;
using System.Collections.Generic;
+using System.Data;
+using System.IO;
using System.Linq;
using System.Net.Http;
@@ -17,9 +21,8 @@ public class SwaggerProcessor
///
///
///
- ///
///
- public IList MapFromOpenApiDocument(Uri baseUri, OpenApiDocument openApiDocument, ReplacementValue[] replacementValues)
+ public IList MapFromOpenApiDocument(Uri baseUri, OpenApiDocument openApiDocument)
{
var requests = new List();
@@ -39,13 +42,13 @@ public IList MapFromOpenApiDocument(Uri baseUri, OpenApiDocumen
foreach (var path in openApiDocument.Paths)
{
- requests.AddRange(GetRestRequestsForPath(baseUri, path, replacementValues));
+ requests.AddRange(GetRestRequestsForPath(baseUri, path));
}
return requests;
}
- private IList GetRestRequestsForPath(Uri baseUri, KeyValuePair path, ReplacementValue[] replacementValues)
+ private IList GetRestRequestsForPath(Uri baseUri, KeyValuePair path)
{
var requests = new List();
@@ -54,13 +57,13 @@ private IList GetRestRequestsForPath(Uri baseUri, KeyValuePair<
requests.Add(new HttpTestRequest()
{
BasePath = baseUri.ToString(),
- Path = ReplacePathParameters(GetPath(path.Key), replacementValues),
+ Path = GetPath(path.Key),
Method = GetHttpMethod(operation),
Summary = GetSummary(operation),
Description = GetDescription(operation),
OperationId = GetOperationId(operation),
- Parameters = ReplaceUrlParameters(GetParameters(operation).ToList(), replacementValues).ToList(),
- RequestBodies = ReplaceRequestBodyModel(GetRequestBodies(operation), replacementValues),
+ Parameters = GetParameters(operation).ToList().ToList(),
+ RequestBodies = GetRequestBodies(operation),
Responses = GetResponses(operation),
Tags = GetTags(operation),
AuthenticationTypes = GetAuthenticationTypes(operation),
@@ -71,7 +74,7 @@ private IList GetRestRequestsForPath(Uri baseUri, KeyValuePair<
return requests;
}
- private IEnumerable GetTestTypes(KeyValuePair operation)
+ private List GetTestTypes(KeyValuePair operation)
{
var testType = new List();
@@ -98,7 +101,7 @@ private IEnumerable GetTestTypes(KeyValuePair GetAuthenticationTypes(KeyValuePair operation)
+ private List GetAuthenticationTypes(KeyValuePair operation)
{
var authenticationTypes = new List();
@@ -140,19 +143,6 @@ private string GetPath(string path)
return path;
}
- private string ReplacePathParameters(string path, ReplacementValue[] replacementValues)
- {
- if (replacementValues != null)
- {
- foreach (var replacementValue in replacementValues)
- {
- path = path.Replace("{" + replacementValue.Key + "}", replacementValue.Value);
- }
- }
-
- return path;
- }
-
private HttpMethod GetHttpMethod(KeyValuePair openApiOperation)
{
var httpMethodString = openApiOperation.Key.ToString().ToLower();
@@ -210,25 +200,6 @@ private IList GetParameters(KeyValuePair ReplaceUrlParameters(List urlParameters, ReplacementValue[] replacementValues)
- {
- if (replacementValues != null)
- {
- foreach (var replacementValue in replacementValues)
- {
- foreach (var parameter in urlParameters)
- {
- if (parameter.Name == replacementValue.Key)
- {
- parameter.Value = replacementValue.Value;
- }
- }
- }
- }
-
- return urlParameters;
- }
-
private List GetRequestBodies(KeyValuePair openApiOperation)
{
try
@@ -247,24 +218,19 @@ private List GetRequestBodies(KeyValuePair()
};
- foreach (var property in contentType.Value.Schema.Properties)
+ foreach (KeyValuePair property in contentType.Value.Schema.Properties)
{
- requestBody.Properties.Add(new Property()
- {
- Name = property.Key,
- Description = property.Value.Description,
- Type = property.Value.Type,
- });
+ RequestBody.Properties.AddRange(GetPropertiesRecursively(property));
}
- requests.Add(requestBody);
+ requests.Add(RequestBody);
}
return requests;
@@ -276,43 +242,78 @@ private List GetRequestBodies(KeyValuePair ReplaceRequestBodyModel(List requestBodies, ReplacementValue[] replacementValues)
+
+ private static List GetPropertiesRecursively(KeyValuePair source)
{
+ var properties = new List();
+ Property itemsProperty = null;
- foreach (var requestBody in requestBodies)
+ if (source.Value.Items != null)
{
- if (requestBody.Properties == null)
+ itemsProperty = new Property
{
- continue;
- }
+ Description = source.Value.Items.Description,
+ Format = source.Value.Items.Description,
+ Type = source.Value.Items.Type,
+ Properties = new List(),
+ Name = source.Value.Items.Reference != null ? source.Value.Items.Reference.Id : null
+ };
+ itemsProperty.Value = SetValue(source.Value.Items.Example);
- var requestBodyResult = new RequestBody
+ foreach (var property in source.Value.Items.Properties)
{
- Name = requestBody.Name,
- Properties = new List()
- };
+ var recursiveProperties = GetPropertiesRecursively(property);
+
+ if (recursiveProperties != null)
+ itemsProperty.Properties.AddRange(recursiveProperties);
+ }
+ }
- if (replacementValues != null)
+ var prop = new Property
+ {
+ Name = source.Value.Reference != null ? source.Value.Reference.Id : source.Key,
+ Description = source.Value.Description,
+ Type = source.Value.Type,
+ Format = source.Value.Format,
+ Properties = new List(),
+ Items = itemsProperty
+ };
+ prop.Value = SetValue(source.Value.Example);
+
+ foreach (var property in source.Value.Properties)
+ {
+ var propsTem = GetPropertiesRecursively(property);
+
+ if (propsTem != null)
+ prop.Properties.AddRange(propsTem);
+ }
+
+ properties.Add(prop);
+
+ return properties;
+ }
+
+ private static string SetValue(IOpenApiAny value)
+ {
+ if (value == null)
+ {
+ return null;
+ }
+
+ using (var outputString = new StringWriter())
+ {
+ var writer = new OpenApiJsonWriter(outputString);
+ value.Write(writer, Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0);
+
+ string exampleString = outputString.ToString();
+
+ if (exampleString != null)
{
- foreach (var replacementValue in replacementValues)
- {
- var prop = requestBody.Properties.FirstOrDefault(p => p.Name == replacementValue.Key);
-
- if (prop != null)
- {
- requestBodyResult.Properties.Add(new Property()
- {
- Description = prop.Description,
- Name = prop.Name,
- Type = prop.Type,
- Value = replacementValue.Value
- });
- }
- }
+ return exampleString;
}
}
- return requestBodies;
+ return null;
}
private List GetResponses(KeyValuePair openApiOperation)
diff --git a/src/QAToolKit.Source.Swagger/SwaggerRequestFilter.cs b/src/QAToolKit.Source.Swagger/SwaggerRequestFilter.cs
new file mode 100644
index 0000000..8cf77e1
--- /dev/null
+++ b/src/QAToolKit.Source.Swagger/SwaggerRequestFilter.cs
@@ -0,0 +1,49 @@
+using QAToolKit.Core.Models;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace QAToolKit.Source.Swagger
+{
+ ///
+ /// Swagger request filtering
+ ///
+ public class SwaggerRequestFilter
+ {
+ private readonly IList _requests;
+
+ ///
+ /// Swagger Request filter
+ ///
+ ///
+ public SwaggerRequestFilter(IList requests)
+ {
+ _requests = requests;
+ }
+ ///
+ /// Filter out the requests by the specified filters
+ ///
+ ///
+ ///
+ public IList FilterRequests(RequestFilter requestFilter)
+ {
+ var requestsLocal = new List();
+
+ if (requestFilter.AuthenticationTypes != null)
+ {
+ requestsLocal.AddRange(_requests.Where(request => requestFilter.AuthenticationTypes.ToList().Any(x => request.AuthenticationTypes.Contains(x))));
+ }
+
+ if (requestFilter.TestTypes != null)
+ {
+ requestsLocal.AddRange(_requests.Where(request => requestFilter.TestTypes.ToList().Any(x => request.TestTypes.Contains(x))));
+ }
+
+ if (requestFilter.EndpointNameWhitelist != null)
+ {
+ requestsLocal.AddRange(_requests.Where(request => requestFilter.EndpointNameWhitelist.Any(x => x == request.OperationId)));
+ }
+
+ return requestsLocal.ToList();
+ }
+ }
+}
diff --git a/src/QAToolKit.Source.Swagger/SwaggerUrlSource.cs b/src/QAToolKit.Source.Swagger/SwaggerUrlSource.cs
index 2939b12..7e686d2 100644
--- a/src/QAToolKit.Source.Swagger/SwaggerUrlSource.cs
+++ b/src/QAToolKit.Source.Swagger/SwaggerUrlSource.cs
@@ -5,7 +5,6 @@
using System;
using System.Collections.Generic;
using System.IO;
-using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
@@ -46,7 +45,6 @@ public async Task> Load(Uri[] source)
{
using (var httpClient = new HttpClient())
{
-
if (_swaggerOptions.UseBasicAuth)
{
var authenticationString = $"{_swaggerOptions.UserName}:{_swaggerOptions.Password}";
@@ -60,42 +58,31 @@ public async Task> Load(Uri[] source)
var textWritter = new OpenApiJsonWriter(new StringWriter());
openApiDocument.SerializeAsV3(textWritter);
- var requests = processor.MapFromOpenApiDocument(new Uri($"{uri.Scheme}://{uri.Host}"), openApiDocument, _swaggerOptions.ReplacementValues);
+ var requests = processor.MapFromOpenApiDocument(new Uri($"{uri.Scheme}://{uri.Host}"), openApiDocument);
if (_swaggerOptions.UseRequestFilter)
{
- restRequests.AddRange(FilterRequests(requests, _swaggerOptions.RequestFilter));
+ var filters = new SwaggerRequestFilter(requests);
+ requests = filters.FilterRequests(_swaggerOptions.RequestFilter);
}
- else
+
+ if (_swaggerOptions.UseDataGeneration)
{
- restRequests.AddRange(requests);
+ var generator = new SwaggerDataGenerator(requests);
+ requests = generator.GenerateModelValues();
}
- }
- }
-
- return restRequests;
- }
- private IList FilterRequests(IList requests, RequestFilter requestFilter)
- {
- var requestsLocal = new List();
-
- if (requestFilter.AuthenticationTypes != null)
- {
- requestsLocal.AddRange(requests.Where(request => requestFilter.AuthenticationTypes.ToList().Any(x => x == request.AuthenticationTypes)));
- }
-
- if (requestFilter.TestTypes != null)
- {
- requestsLocal.AddRange(requests.Where(request => requestFilter.TestTypes.ToList().Any(x => x == request.TestTypes)));
- }
+ if (_swaggerOptions.ReplacementValues != null)
+ {
+ var generator = new SwaggerValueReplacement(requests, _swaggerOptions.ReplacementValues);
+ requests = generator.ReplaceAll();
+ }
- if (requestFilter.EndpointNameWhitelist != null)
- {
- requestsLocal.AddRange(requests.Where(request => requestFilter.EndpointNameWhitelist.Any(x => x == request.OperationId)));
+ restRequests.AddRange(requests);
+ }
}
- return requestsLocal.ToList();
+ return restRequests;
}
}
}
diff --git a/src/QAToolKit.Source.Swagger/SwaggerValueReplacement.cs b/src/QAToolKit.Source.Swagger/SwaggerValueReplacement.cs
new file mode 100644
index 0000000..eba187b
--- /dev/null
+++ b/src/QAToolKit.Source.Swagger/SwaggerValueReplacement.cs
@@ -0,0 +1,100 @@
+using QAToolKit.Core.Models;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace QAToolKit.Source.Swagger
+{
+ ///
+ /// Replace swagger request body, url path and parameters with values from ReplacementValue[]
+ ///
+ public class SwaggerValueReplacement
+ {
+ private readonly IList _requests;
+ private readonly ReplacementValue[] _replacementValues;
+
+ ///
+ /// Swagger value replacement
+ ///
+ ///
+ ///
+ public SwaggerValueReplacement(IList requests, ReplacementValue[] replacementValues)
+ {
+ _requests = requests;
+ _replacementValues = replacementValues;
+ }
+
+ ///
+ /// Replace request body, url path and parameters with values from ReplacementValue[]
+ ///
+ ///
+ public List ReplaceAll()
+ {
+ ReplaceRequestBodyModel();
+ ReplaceUrlParameters();
+ ReplacePathParameters();
+
+ return _requests.ToList();
+ }
+
+ private void ReplaceRequestBodyModel()
+ {
+ foreach (var request in _requests)
+ {
+ foreach (var requestBody in request.RequestBodies)
+ {
+ if (requestBody.Properties == null)
+ {
+ continue;
+ }
+
+ if (_replacementValues != null)
+ {
+ foreach (var replacementValue in _replacementValues)
+ {
+ var prop = requestBody.Properties.FirstOrDefault(p => p.Name == replacementValue.Key);
+
+ if (prop != null)
+ {
+ prop.Value = replacementValue.Value;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ private void ReplaceUrlParameters()
+ {
+ if (_replacementValues != null)
+ {
+ foreach (var replacementValue in _replacementValues)
+ {
+ foreach (var request in _requests)
+ {
+ foreach (var parameter in request.Parameters)
+ {
+ if (parameter.Name == replacementValue.Key)
+ {
+ parameter.Value = replacementValue.Value;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ private void ReplacePathParameters()
+ {
+ if (_replacementValues != null)
+ {
+ foreach (var replacementValue in _replacementValues)
+ {
+ foreach (var request in _requests)
+ {
+ request.Path = request.Path.Replace("{" + replacementValue.Key + "}", replacementValue.Value);
+ }
+ }
+ }
+ }
+ }
+}