Skip to content

Commit 2aef356

Browse files
authored
Merge pull request #756 from bitwiseman/task/shorter
Shorten generated test resource paths
2 parents 4c30f94 + 7ddf1f5 commit 2aef356

File tree

4,203 files changed

+66036
-65960
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

4,203 files changed

+66036
-65960
lines changed

src/test/java/org/kohsuke/github/junit/GitHubWireMockRule.java

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.nio.file.Files;
1818
import java.nio.file.Path;
1919
import java.util.Collection;
20+
import java.util.HashMap;
2021
import java.util.Map;
2122

2223
import static com.github.tomakehurst.wiremock.client.WireMock.*;
@@ -120,7 +121,7 @@ protected void after() {
120121
.extractTextBodiesOver(255));
121122

122123
// After taking the snapshot, format the output
123-
formatJsonFiles(new File(this.apiServer().getOptions().filesRoot().getPath()).toPath());
124+
formatTestResources(new File(this.apiServer().getOptions().filesRoot().getPath()).toPath(), false);
124125

125126
if (this.rawServer() != null) {
126127
this.rawServer()
@@ -132,7 +133,7 @@ protected void after() {
132133
.extractTextBodiesOver(255));
133134

134135
// For raw server, only fix up mapping files
135-
formatJsonFiles(new File(this.rawServer().getOptions().filesRoot().child("mappings").getPath()).toPath());
136+
formatTestResources(new File(this.rawServer().getOptions().filesRoot().getPath()).toPath(), true);
136137
}
137138

138139
if (this.uploadsServer() != null) {
@@ -144,7 +145,7 @@ protected void after() {
144145
.captureHeader("Accept")
145146
.extractTextBodiesOver(255));
146147

147-
formatJsonFiles(new File(this.uploadsServer().getOptions().filesRoot().getPath()).toPath());
148+
formatTestResources(new File(this.uploadsServer().getOptions().filesRoot().getPath()).toPath(), false);
148149

149150
}
150151
}
@@ -157,7 +158,7 @@ public static int getRequestCount(WireMockServer server) {
157158
return server.countRequestsMatching(RequestPatternBuilder.allRequests().build()).getCount();
158159
}
159160

160-
private void formatJsonFiles(Path path) {
161+
private void formatTestResources(Path path, boolean isRawServer) {
161162
// The more consistent we can make the json output the more meaningful it will be.
162163
Gson g = new Gson().newBuilder()
163164
.serializeNulls()
@@ -176,8 +177,32 @@ public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContex
176177
.create();
177178

178179
try {
180+
Map<String, String> idToIndex = new HashMap<>();
181+
182+
// Match all the ids to request indexes
179183
Files.walk(path).forEach(filePath -> {
180184
try {
185+
if (filePath.toString().endsWith(".json") && filePath.toString().contains("/mappings/")) {
186+
String fileText = new String(Files.readAllBytes(filePath));
187+
Object parsedObject = g.fromJson(fileText, Object.class);
188+
addMappingId((Map<String, Object>) parsedObject, idToIndex);
189+
}
190+
} catch (Exception e) {
191+
throw new RuntimeException("Files could not be read: " + filePath.toString(), e);
192+
}
193+
});
194+
195+
// Update all
196+
Files.walk(path).forEach(filePath -> {
197+
try {
198+
Map.Entry<String, String> entry = getId(filePath, idToIndex);
199+
if (entry != null) {
200+
filePath = renameFileToIndex(filePath, entry);
201+
}
202+
// For raw server, only fix up mapping files
203+
if (isRawServer && !filePath.toString().contains("mappings")) {
204+
return;
205+
}
181206
if (filePath.toString().endsWith(".json")) {
182207
String fileText = new String(Files.readAllBytes(filePath));
183208
// while recording responses we replaced all github calls localhost
@@ -193,37 +218,48 @@ public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContex
193218
fileText = fileText.replace(this.uploadsServer().baseUrl(), "https://uploads.github.com");
194219
}
195220

221+
// point bodyFile in the mapping to the renamed body file
222+
if (entry != null && filePath.toString().contains("mappings")) {
223+
fileText = fileText.replace("-" + entry.getKey(), "-" + entry.getValue());
224+
}
225+
196226
// Can be Array or Map
197227
Object parsedObject = g.fromJson(fileText, Object.class);
198-
if (parsedObject instanceof Map && filePath.toString().contains("mappings")) {
199-
filePath = renameMappingFile(filePath, (Map<String, Object>) parsedObject);
200-
}
201228
fileText = g.toJson(parsedObject);
202229
Files.write(filePath, fileText.getBytes());
203230
}
204231
} catch (Exception e) {
205-
throw new RuntimeException("Files could not be written", e);
232+
throw new RuntimeException("Files could not be written: " + filePath.toString(), e);
206233
}
207234
});
208235
} catch (IOException e) {
209236
throw new RuntimeException("Files could not be written");
210237
}
211238
}
212239

213-
private Path renameMappingFile(Path filePath, Map<String, Object> parsedObject) throws IOException {
214-
// Shorten the file names
215-
// For understandability, rename the files to include the response order
216-
Path targetPath = filePath;
240+
private void addMappingId(Map<String, Object> parsedObject, Map<String, String> idToIndex) {
217241
String id = (String) parsedObject.getOrDefault("id", null);
218-
Long insertionIndex = ((Double) parsedObject.getOrDefault("insertionIndex", 0.0)).longValue();
242+
long insertionIndex = ((Double) parsedObject.getOrDefault("insertionIndex", 0.0)).longValue();
219243
if (id != null && insertionIndex > 0) {
220-
String filePathString = filePath.toString();
221-
if (filePathString.contains(id)) {
222-
targetPath = new File(filePathString.replace(id, insertionIndex.toString() + "-" + id.substring(0, 6)))
223-
.toPath();
224-
Files.move(filePath, targetPath);
244+
idToIndex.put(id, Long.toString(insertionIndex));
245+
}
246+
}
247+
248+
private Map.Entry<String, String> getId(Path filePath, Map<String, String> idToIndex) throws IOException {
249+
Path targetPath = filePath;
250+
String filePathString = filePath.toString();
251+
for (Map.Entry<String, String> item : idToIndex.entrySet()) {
252+
if (filePathString.contains(item.getKey())) {
253+
return item;
225254
}
226255
}
256+
return null;
257+
}
258+
259+
private Path renameFileToIndex(Path filePath, Map.Entry<String, String> idToIndex) throws IOException {
260+
String filePathString = filePath.toString();
261+
Path targetPath = new File(filePathString.replace(idToIndex.getKey(), idToIndex.getValue())).toPath();
262+
Files.move(filePath, targetPath);
227263

228264
return targetPath;
229265
}

src/test/resources/org/kohsuke/github/AbuseLimitHandlerTest/wiremock/testHandler_Fail/mappings/repos_github-api-test-org_temp-testratelimithandler_fail-3-574da1.json

Lines changed: 0 additions & 50 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"id": "574da117-6845-46d8-b2c1-4415546ca670",
3+
"name": "repos_github-api-test-org_temp-testratelimithandler_fail",
4+
"request": {
5+
"url": "/repos/github-api-test-org/temp-testHandler_Fail",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "repos_github-api-test-org_temp-testratelimithandler_fail-3.json",
16+
"headers": {
17+
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
18+
"Content-Type": "application/json; charset=utf-8",
19+
"Server": "GitHub.com",
20+
"Status": "200 OK",
21+
"X-RateLimit-Limit": "5000",
22+
"X-RateLimit-Remaining": "4922",
23+
"X-RateLimit-Reset": "{{testStartDate offset='3 seconds' format='unix'}}",
24+
"Cache-Control": "private, max-age=60, s-maxage=60",
25+
"Vary": [
26+
"Accept, Authorization, Cookie, X-GitHub-OTP",
27+
"Accept-Encoding"
28+
],
29+
"ETag": "W/\"858224998ac7d1fd6dcd43f73d375297\"",
30+
"Last-Modified": "Thu, 06 Feb 2020 18:33:43 GMT",
31+
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
32+
"X-Accepted-OAuth-Scopes": "repo",
33+
"X-GitHub-Media-Type": "unknown, github.v3",
34+
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
35+
"Access-Control-Allow-Origin": "*",
36+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
37+
"X-Frame-Options": "deny",
38+
"X-Content-Type-Options": "nosniff",
39+
"X-XSS-Protection": "1; mode=block",
40+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
41+
"Content-Security-Policy": "default-src 'none'",
42+
"X-GitHub-Request-Id": "CC37:2605:3FADC:4EA8C:5E3C5C02"
43+
}
44+
},
45+
"uuid": "574da117-6845-46d8-b2c1-4415546ca670",
46+
"persistent": true,
47+
"scenarioName": "scenario-1-repos-github-api-test-org-temp-testHandler_Fail",
48+
"requiredScenarioState": "scenario-1-repos-github-api-test-org-temp-testHandler_Fail-2",
49+
"insertionIndex": 3
50+
}

src/test/resources/org/kohsuke/github/AbuseLimitHandlerTest/wiremock/testHandler_Fail/mappings/user-1-a60baf.json

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"id": "a60baf84-5b5c-4f86-af3d-cab0d609c7b2",
3+
"name": "user",
4+
"request": {
5+
"url": "/user",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "user-1.json",
16+
"headers": {
17+
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
18+
"Content-Type": "application/json; charset=utf-8",
19+
"Server": "GitHub.com",
20+
"Status": "200 OK",
21+
"X-RateLimit-Limit": "5000",
22+
"X-RateLimit-Remaining": "4930",
23+
"X-RateLimit-Reset": "{{now offset='3 seconds' format='unix'}}",
24+
"Cache-Control": "private, max-age=60, s-maxage=60",
25+
"Vary": [
26+
"Accept, Authorization, Cookie, X-GitHub-OTP",
27+
"Accept-Encoding"
28+
],
29+
"ETag": "W/\"1cb30f031c67c499473b3aad01c7f7a5\"",
30+
"Last-Modified": "Thu, 06 Feb 2020 17:29:39 GMT",
31+
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
32+
"X-Accepted-OAuth-Scopes": "",
33+
"X-GitHub-Media-Type": "unknown, github.v3",
34+
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
35+
"Access-Control-Allow-Origin": "*",
36+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
37+
"X-Frame-Options": "deny",
38+
"X-Content-Type-Options": "nosniff",
39+
"X-XSS-Protection": "1; mode=block",
40+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
41+
"Content-Security-Policy": "default-src 'none'",
42+
"X-GitHub-Request-Id": "CC37:2605:3F884:4E941:5E3C5BFC"
43+
}
44+
},
45+
"uuid": "a60baf84-5b5c-4f86-af3d-cab0d609c7b2",
46+
"persistent": true,
47+
"insertionIndex": 1
48+
}

0 commit comments

Comments
 (0)