Skip to content

Commit 67fbde4

Browse files
committed
Refactoring to remove replaceer and generator from the library, enum support, test refactoring, readme update
1 parent fa07380 commit 67fbde4

28 files changed

+597
-423
lines changed

README.md

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
# QAToolKit.Source.Swagger
22
![.NET Core](https://github.com/qatoolkit/qatoolkit-source-swagger-net/workflows/.NET%20Core/badge.svg)
33

4-
`QAToolKit.Source.Swagger` is a .NET standard library, which generates `IList<HttpTestRequest>` object that is the input for other components.
4+
`QAToolKit.Source.Swagger` is a .NET standard library, which generates `IList<HttpRequest>` object that is the input for other components.
55

66
Major features:
77

88
- Parses `OpenAPI v3.0` Swagger files,
99
- swagger.json can be loaded from `disk` or from `URL`,
10-
- path parameters, URL paramters and JSON body models are replaced with custom values,
1110
- access swagger.json from URL, which is protected by `basic authentication`,
1211
- control which swagger endpoints are called by specifying `request filters` (check below)
13-
- data generation for missing values (Experimental)
1412

1513
## Sample
1614

@@ -21,17 +19,6 @@ This is a sample of instantiating a new Swagger Source object from URL.
2119
SwaggerUrlSource swaggerSource = new SwaggerUrlSource(
2220
options =>
2321
{
24-
options.AddReplacementValues(new ReplacementValue[] {
25-
new ReplacementValue()
26-
{
27-
Key = "version",
28-
Value = "1"
29-
},
30-
{
31-
Key = "parentId",
32-
Value = "4"
33-
}
34-
});
3522
options.AddBasicAuthentication("myuser", "mypassword");
3623
options.AddRequestFilters(new RequestFilter()
3724
{
@@ -40,11 +27,11 @@ SwaggerUrlSource swaggerSource = new SwaggerUrlSource(
4027
EndpointNameWhitelist = new string[] { "GetCategories" }
4128
});
4229
options.AddBaseUrl(new Uri("https://dev.myapi.com"));
43-
options.AddDataGeneration();
30+
options.UseSwaggerExampleValues = true;
4431
});
4532

4633
//To run the Swagger parser we need to pass an array of URLs
47-
IList<HttpTestRequest> requests = await swaggerSource.Load(new Uri[] {
34+
IList<HttpRequest> requests = await swaggerSource.Load(new Uri[] {
4835
new Uri("https://api.demo.com/swagger/v1/swagger.json"),
4936
new Uri("https://api2.demo.com/swagger/v1/swagger.json")
5037
});
@@ -54,26 +41,13 @@ The above code is quite simple, but it needs some explanation.
5441

5542
## Description
5643

57-
#### 1. AddReplacementValues
58-
This is a method that will add key/value pairs for replacement values you need to replace in the Swagger requests.
59-
60-
In the example above we say: "Replace `{version}` placeholder in Path and URL parameters and JSON body models."
61-
62-
In other words, if you have a test API endpoint like this: https://api.demo.com/v{version}/categories?parent={parentId} that will be set to https://api.demo.com/v1/categories?parent=4.
63-
64-
That, does not stop there, you can also populate JSON request bodies in this way:
65-
66-
[TODO sample JSON Body]
67-
68-
`ReplcementValue[]` has precedence over data generation (check below chapter 5).
69-
70-
#### 2. AddBasicAuthentication
44+
#### 1. AddBasicAuthentication
7145
If your Swagger.json files are protected by basic authentication, you can set those with `AddBasicAuthentication`.
7246

73-
#### 3, AddRequestFilters
47+
#### 2. AddRequestFilters
7448
Filters comprise of different types. Those are `AuthenticationTypes`, `TestTypes` and `EndpointNameWhitelist`.
7549

76-
##### 3.1. AuthenticationTypes
50+
##### 2.1. AuthenticationTypes
7751
Here we specify a list of Authentication types, that will be filtered out from the whole swagger file. This is where QA Tool Kit presents a convention.
7852
The built-in types are:
7953
- `AuthenticationType.Customer` which specifies a string `"@customer"`,
@@ -101,7 +75,7 @@ This is an example from swagger.json excerpt:
10175

10276
Parser then finds those string in the description field and populates the `RequestFilter` property.
10377

104-
##### 3.2 TestTypes
78+
##### 2.2 TestTypes
10579
Similarly as in the `AuthenticationTypes` you can filter out certain endpoints to be used in certain test scenarios. Currently libraray supports:
10680

10781
- TestType.LoadTest which specifies a string `"@loadtest"`,
@@ -121,13 +95,13 @@ The same swagger-json excerpt which allows load and integration tests.
12195
"operationId": "GetCategories",
12296
```
12397

124-
##### 3.3 EndpointNameWhitelist
98+
##### 2.3 EndpointNameWhitelist
12599
Final `RequestFilter` option is `EndpointNameWhitelist`. With it you can specify a list of endpoints that will be included in the results.
126100

127101
Every other endpoint will be excluded. In the sample above we have set the result to include only `GetCategories` endpoint.
128102
That corresponds to the `operationId` in the swagger file above.
129103

130-
#### 4. AddBaseUrl
104+
#### 3. AddBaseUrl
131105
Your swagger file has a `Server section`, where you can specify an server URI and can be absolute or relative. An example of relative server section is:
132106
```json
133107
"servers": [
@@ -138,10 +112,11 @@ Your swagger file has a `Server section`, where you can specify an server URI an
138112
```
139113
In case of relative paths you need to add an absolute base URL to `Swagger Processor` with `AddBaseUrl`, otherwise the one from the `Servers section` will be used.
140114

141-
#### 5. AddDataGeneration
142-
##### !! EXPERIMENTAL !!
143-
This is an experimental feature. It will generate the missing data in the `List<HttpTestRequest>` object from the swagger models, uri and query parameters.
144-
`ReplcementValue[]` has precedence over data generation.
115+
#### 4. UseSwaggerExampleValues
116+
You can set `UseSwaggerExampleValues = true` in the SwaggerOptions when creating new Swagger source object. This will
117+
check Swagger for example files and populate those.
118+
119+
By default this option is set to false.
145120

146121
## Limitations
147122

src/QAToolKit.Source.Swagger.Test/Fixtures/PetApi/PetObjectResponse.cs renamed to src/QAToolKit.Source.Swagger.Test/Fixtures/PetApi/Get/Helpers/PetObjectWithExampleValues.cs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using QAToolKit.Core.Models;
22
using System.Collections.Generic;
33

4-
namespace QAToolKit.Source.Swagger.Test.Fixtures.PetApi
4+
namespace QAToolKit.Source.Swagger.Test.Fixtures.PetApi.Get.Helpers
55
{
6-
public static class PetObjectResponse
6+
public static class PetObjectWithExampleValues
77
{
88
public static List<Property> GetProperties()
99
{
@@ -124,8 +124,39 @@ public static List<Property> GetProperties()
124124
Description = "pet status in the store",
125125
Format = null,
126126
Required = false,
127-
Properties = null,
128-
Type = "string",
127+
Properties = new List<Property>(){
128+
new Property()
129+
{
130+
Name = null,
131+
Description = null,
132+
Format = null,
133+
Required = false,
134+
Properties = null,
135+
Type = "string",
136+
Value = "available"
137+
},
138+
new Property()
139+
{
140+
Name = null,
141+
Description = null,
142+
Format = null,
143+
Required = false,
144+
Properties = null,
145+
Type = "string",
146+
Value = "pending"
147+
},
148+
new Property()
149+
{
150+
Name = null,
151+
Description = null,
152+
Format = null,
153+
Required = false,
154+
Properties = null,
155+
Type = "string",
156+
Value = "sold"
157+
}
158+
},
159+
Type = "enum",
129160
Value = null
130161
}
131162
};
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
using QAToolKit.Core.Models;
2+
using System.Collections.Generic;
3+
4+
namespace QAToolKit.Source.Swagger.Test.Fixtures.PetApi.Get.Helpers
5+
{
6+
public static class PetObjectWithoutExampleValues
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 = null
20+
},
21+
new Property(){
22+
Name = "name",
23+
Description = null,
24+
Format = null,
25+
Required = false,
26+
Properties = null,
27+
Type = "string",
28+
Value = null
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 = null
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 = null
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 = new List<Property>(){
128+
new Property()
129+
{
130+
Name = null,
131+
Description = null,
132+
Format = null,
133+
Required = false,
134+
Properties = null,
135+
Type = "string",
136+
Value = "available"
137+
},
138+
new Property()
139+
{
140+
Name = null,
141+
Description = null,
142+
Format = null,
143+
Required = false,
144+
Properties = null,
145+
Type = "string",
146+
Value = "pending"
147+
},
148+
new Property()
149+
{
150+
Name = null,
151+
Description = null,
152+
Format = null,
153+
Required = false,
154+
Properties = null,
155+
Type = "string",
156+
Value = "sold"
157+
}
158+
},
159+
Type = "enum",
160+
Value = null
161+
}
162+
};
163+
}
164+
}
165+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using QAToolKit.Core.Models;
2+
using System.Net;
3+
4+
namespace QAToolKit.Source.Swagger.Test.Fixtures.PetApi.Get.Helpers
5+
{
6+
public static class PetResponse200
7+
{
8+
public static Response Get(bool exampleValues)
9+
{
10+
if (exampleValues)
11+
{
12+
return new Response()
13+
{
14+
StatusCode = HttpStatusCode.OK,
15+
Type = ResponseType.Object,
16+
Properties = PetObjectWithExampleValues.GetProperties()
17+
};
18+
}
19+
else
20+
{
21+
return new Response()
22+
{
23+
StatusCode = HttpStatusCode.OK,
24+
Type = ResponseType.Object,
25+
Properties = PetObjectWithoutExampleValues.GetProperties()
26+
};
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)