Skip to content

Commit fa07380

Browse files
committed
swagger processor updates, many new tests
1 parent a506e96 commit fa07380

21 files changed

+788
-95
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,10 @@ In case of relative paths you need to add an absolute base URL to `Swagger Proce
143143
This is an experimental feature. It will generate the missing data in the `List<HttpTestRequest>` object from the swagger models, uri and query parameters.
144144
`ReplcementValue[]` has precedence over data generation.
145145

146-
## TO-DO
146+
## Limitations
147147

148-
N/A
148+
- Swagger processor only returns `application/json` content type. Support for other might come later.
149+
- Data generation and replacement is only supported for Path, Url and Json Body properties.
149150

150151
## License
151152

src/QAToolKit.Source.Swagger.Test/Assets/swagger-test.json renamed to src/QAToolKit.Source.Swagger.Test/Assets/swagger-pets-test.json

File renamed without changes.
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
using QAToolKit.Core.Models;
2+
using System.Collections.Generic;
3+
4+
namespace QAToolKit.Source.Swagger.Test.Fixtures.PetApi
5+
{
6+
public static class PetObjectResponse
7+
{
8+
public static List<Property> GetProperties()
9+
{
10+
return new List<Property>()
11+
{
12+
new Property(){
13+
Name = "id",
14+
Description = null,
15+
Format = "int64",
16+
Required = false,
17+
Properties = null,
18+
Type = "integer",
19+
Value = "10"
20+
},
21+
new Property(){
22+
Name = "name",
23+
Description = null,
24+
Format = null,
25+
Required = false,
26+
Properties = null,
27+
Type = "string",
28+
Value = "doggie"
29+
},
30+
new Property(){
31+
Name = "Category",
32+
Description = null,
33+
Format = null,
34+
Required = false,
35+
Properties = new List<Property>(){
36+
new Property()
37+
{
38+
Name = "id",
39+
Description = null,
40+
Format = "int64",
41+
Required = false,
42+
Properties = null,
43+
Type = "integer",
44+
Value = "1"
45+
},
46+
new Property()
47+
{
48+
Name = "name",
49+
Description = null,
50+
Format = null,
51+
Required = false,
52+
Properties = null,
53+
Type = "string",
54+
Value = "Dogs"
55+
}
56+
},
57+
Type = "object",
58+
Value = null
59+
},
60+
new Property(){
61+
Name = "photoUrls",
62+
Description = null,
63+
Format = null,
64+
Required = false,
65+
Properties = new List<Property>(){
66+
new Property()
67+
{
68+
Name = null,
69+
Description = null,
70+
Format = null,
71+
Required = false,
72+
Properties = null,
73+
Type = "string",
74+
Value = null
75+
}
76+
},
77+
Type = "array",
78+
Value = null
79+
},
80+
new Property(){
81+
Name = "tags",
82+
Description = null,
83+
Format = null,
84+
Required = false,
85+
Properties = new List<Property>()
86+
{
87+
new Property()
88+
{
89+
Name = "Tag",
90+
Description = null,
91+
Format = null,
92+
Required = false,
93+
Properties = new List<Property>(){
94+
new Property()
95+
{
96+
Name = "id",
97+
Description = null,
98+
Format = "int64",
99+
Required = false,
100+
Properties = null,
101+
Type = "integer",
102+
Value = null
103+
},
104+
new Property()
105+
{
106+
Name = "name",
107+
Description = null,
108+
Format = null,
109+
Required = false,
110+
Properties = null,
111+
Type = "string",
112+
Value = null
113+
}
114+
},
115+
Type = "object",
116+
Value = null
117+
}
118+
},
119+
Type = "array",
120+
Value = null
121+
},
122+
new Property(){
123+
Name = "status",
124+
Description = "pet status in the store",
125+
Format = null,
126+
Required = false,
127+
Properties = null,
128+
Type = "string",
129+
Value = null
130+
}
131+
};
132+
}
133+
}
134+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using QAToolKit.Core.Models;
2+
using System.Net;
3+
4+
namespace QAToolKit.Source.Swagger.Test.Fixtures.PetApi
5+
{
6+
public static class PetResponse200
7+
{
8+
public static Response Get()
9+
{
10+
return new Response()
11+
{
12+
StatusCode = HttpStatusCode.OK,
13+
Type = ResponseType.Object,
14+
Properties = PetObjectResponse.GetProperties()
15+
};
16+
}
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using QAToolKit.Core.Models;
2+
using System.Net;
3+
4+
namespace QAToolKit.Source.Swagger.Test.Fixtures.PetApi
5+
{
6+
public static class PetResponse400
7+
{
8+
public static Response Get()
9+
{
10+
return new Response()
11+
{
12+
StatusCode = HttpStatusCode.BadRequest,
13+
Type = ResponseType.Undefined,
14+
Properties = null
15+
};
16+
}
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using QAToolKit.Core.Models;
2+
using System.Net;
3+
4+
namespace QAToolKit.Source.Swagger.Test.Fixtures.PetApi
5+
{
6+
public static class PetResponse404
7+
{
8+
public static Response Get()
9+
{
10+
return new Response()
11+
{
12+
StatusCode = HttpStatusCode.NotFound,
13+
Type = ResponseType.Undefined,
14+
Properties = null
15+
};
16+
}
17+
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using QAToolKit.Core.Models;
2+
using System.Collections.Generic;
3+
4+
namespace QAToolKit.Source.Swagger.Test.Fixtures.PetApi
5+
{
6+
public static class PetsFindByTagsResponses
7+
{
8+
public static List<Response> Get()
9+
{
10+
return new List<Response>()
11+
{
12+
PetsResponse200.Get(),
13+
PetResponse400.Get()
14+
};
15+
}
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using QAToolKit.Core.Models;
2+
using System.Collections.Generic;
3+
4+
namespace QAToolKit.Source.Swagger.Test.Fixtures.PetApi
5+
{
6+
public static class PetsGetByIdResponseFixtures
7+
{
8+
public static List<Response> Get()
9+
{
10+
return new List<Response>()
11+
{
12+
PetResponse200.Get(),
13+
PetResponse400.Get(),
14+
PetResponse404.Get()
15+
};
16+
}
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using QAToolKit.Core.Models;
2+
using System.Net;
3+
4+
namespace QAToolKit.Source.Swagger.Test.Fixtures.PetApi
5+
{
6+
public static class PetsResponse200
7+
{
8+
public static Response Get()
9+
{
10+
return new Response()
11+
{
12+
StatusCode = HttpStatusCode.OK,
13+
Type = ResponseType.Objects,
14+
Properties = PetObjectResponse.GetProperties()
15+
};
16+
}
17+
}
18+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
using ExpectedObjects;
2+
using Microsoft.Extensions.Logging;
3+
using Newtonsoft.Json;
4+
using QAToolKit.Core.Models;
5+
using QAToolKit.Source.Swagger.Test.Fixtures.PetApi;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.IO;
9+
using System.Linq;
10+
using System.Net.Http;
11+
using System.Threading.Tasks;
12+
using Xunit;
13+
using Xunit.Abstractions;
14+
15+
namespace QAToolKit.Source.Swagger.Test.HttpGetTests
16+
{
17+
public class SwaggerProcessorFindByTagsTests
18+
{
19+
private readonly ILogger<SwaggerProcessorFindByTagsTests> _logger;
20+
21+
public SwaggerProcessorFindByTagsTests(ITestOutputHelper testOutputHelper)
22+
{
23+
var loggerFactory = new LoggerFactory();
24+
loggerFactory.AddProvider(new XunitLoggerProvider(testOutputHelper));
25+
_logger = loggerFactory.CreateLogger<SwaggerProcessorFindByTagsTests>();
26+
}
27+
28+
[Fact]
29+
public async Task PetsSwaggerGetByPetByIdV3Test_Successfull()
30+
{
31+
var fileSource = new SwaggerFileSource(options =>
32+
{
33+
options.AddBaseUrl(new Uri("https://petstore3.swagger.io/"));
34+
options.AddRequestFilters(new RequestFilter()
35+
{
36+
EndpointNameWhitelist = new string[] { "findPetsByTags" }
37+
});
38+
});
39+
40+
var requests = await fileSource.Load(new List<FileInfo>() {
41+
new FileInfo("Assets/swagger-pets-test.json")
42+
});
43+
44+
_logger.LogInformation(JsonConvert.SerializeObject(requests, Formatting.Indented));
45+
46+
Assert.NotNull(requests);
47+
Assert.Equal(1, requests.Count);
48+
Assert.Empty(requests.FirstOrDefault().AuthenticationTypes);
49+
Assert.Equal("https://petstore3.swagger.io/api/v3", requests.FirstOrDefault().BasePath);
50+
Assert.Equal("Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.", requests.FirstOrDefault().Description);
51+
Assert.Equal(HttpMethod.Get, requests.FirstOrDefault().Method);
52+
Assert.Equal("findPetsByTags", requests.FirstOrDefault().OperationId);
53+
Assert.Single(requests.FirstOrDefault().Parameters);
54+
Assert.Equal("tags", requests.FirstOrDefault().Parameters.FirstOrDefault().Name);
55+
Assert.False(requests.FirstOrDefault().Parameters.FirstOrDefault().Nullable);
56+
Assert.False(requests.FirstOrDefault().Parameters.FirstOrDefault().Required);
57+
Assert.Equal(Location.Query, requests.FirstOrDefault().Parameters.FirstOrDefault().Location);
58+
Assert.Equal("array", requests.FirstOrDefault().Parameters.FirstOrDefault().Type);
59+
Assert.Null(requests.FirstOrDefault().Parameters.FirstOrDefault().Value);
60+
Assert.Equal("/pet/findByTags", requests.FirstOrDefault().Path);
61+
Assert.Empty(requests.FirstOrDefault().RequestBodies);
62+
Assert.Equal(2, requests.FirstOrDefault().Responses.Count);
63+
64+
var expectedPetsResponse = PetsFindByTagsResponses.Get().ToExpectedObject();
65+
expectedPetsResponse.ShouldEqual(requests.FirstOrDefault().Responses);
66+
67+
Assert.Equal("Finds Pets by tags", requests.FirstOrDefault().Summary);
68+
Assert.Collection(requests.FirstOrDefault().Tags, item =>
69+
{
70+
item = "pet";
71+
});
72+
Assert.Empty(requests.FirstOrDefault().TestTypes);
73+
}
74+
75+
[Fact]
76+
public async Task PetsSwaggerGetByPetByIdWithReplacableIdV3Test_Successfull()
77+
{
78+
var fileSource = new SwaggerFileSource(options =>
79+
{
80+
options.AddBaseUrl(new Uri("https://petstore3.swagger.io/"));
81+
options.AddRequestFilters(new RequestFilter()
82+
{
83+
EndpointNameWhitelist = new string[] { "findPetsByTags" }
84+
});
85+
options.AddReplacementValues(new ReplacementValue[]
86+
{
87+
new ReplacementValue() { Key = "tags", Value = "1,2,3" }
88+
});
89+
});
90+
91+
var requests = await fileSource.Load(new List<FileInfo>() {
92+
new FileInfo("Assets/swagger-pets-test.json")
93+
});
94+
95+
_logger.LogInformation(JsonConvert.SerializeObject(requests, Formatting.Indented));
96+
97+
Assert.NotNull(requests);
98+
Assert.Equal(1, requests.Count);
99+
Assert.Empty(requests.FirstOrDefault().AuthenticationTypes);
100+
Assert.Equal("https://petstore3.swagger.io/api/v3", requests.FirstOrDefault().BasePath);
101+
Assert.Equal("Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.", requests.FirstOrDefault().Description);
102+
Assert.Equal(HttpMethod.Get, requests.FirstOrDefault().Method);
103+
Assert.Equal("findPetsByTags", requests.FirstOrDefault().OperationId);
104+
Assert.Single(requests.FirstOrDefault().Parameters);
105+
Assert.Equal("tags", requests.FirstOrDefault().Parameters.FirstOrDefault().Name);
106+
Assert.False(requests.FirstOrDefault().Parameters.FirstOrDefault().Nullable);
107+
Assert.False(requests.FirstOrDefault().Parameters.FirstOrDefault().Required);
108+
Assert.Equal(Location.Query, requests.FirstOrDefault().Parameters.FirstOrDefault().Location);
109+
Assert.Equal("array", requests.FirstOrDefault().Parameters.FirstOrDefault().Type);
110+
Assert.Equal("1,2,3", requests.FirstOrDefault().Parameters.FirstOrDefault().Value);
111+
Assert.Equal("/pet/findByTags", requests.FirstOrDefault().Path);
112+
Assert.Empty(requests.FirstOrDefault().RequestBodies);
113+
Assert.Equal(2, requests.FirstOrDefault().Responses.Count);
114+
115+
var expectedPetsResponse = PetsFindByTagsResponses.Get().ToExpectedObject();
116+
expectedPetsResponse.ShouldEqual(requests.FirstOrDefault().Responses);
117+
118+
Assert.Equal("Finds Pets by tags", requests.FirstOrDefault().Summary);
119+
Assert.Collection(requests.FirstOrDefault().Tags, item =>
120+
{
121+
item = "pet";
122+
});
123+
Assert.Empty(requests.FirstOrDefault().TestTypes);
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)