Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shorten generated test resource paths #756

Merged
merged 1 commit into from
Mar 26, 2020
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
72 changes: 54 additions & 18 deletions src/test/java/org/kohsuke/github/junit/GitHubWireMockRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

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

// After taking the snapshot, format the output
formatJsonFiles(new File(this.apiServer().getOptions().filesRoot().getPath()).toPath());
formatTestResources(new File(this.apiServer().getOptions().filesRoot().getPath()).toPath(), false);

if (this.rawServer() != null) {
this.rawServer()
Expand All @@ -132,7 +133,7 @@ protected void after() {
.extractTextBodiesOver(255));

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

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

formatJsonFiles(new File(this.uploadsServer().getOptions().filesRoot().getPath()).toPath());
formatTestResources(new File(this.uploadsServer().getOptions().filesRoot().getPath()).toPath(), false);

}
}
Expand All @@ -157,7 +158,7 @@ public static int getRequestCount(WireMockServer server) {
return server.countRequestsMatching(RequestPatternBuilder.allRequests().build()).getCount();
}

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

try {
Map<String, String> idToIndex = new HashMap<>();

// Match all the ids to request indexes
Files.walk(path).forEach(filePath -> {
try {
if (filePath.toString().endsWith(".json") && filePath.toString().contains("/mappings/")) {
String fileText = new String(Files.readAllBytes(filePath));
Object parsedObject = g.fromJson(fileText, Object.class);
addMappingId((Map<String, Object>) parsedObject, idToIndex);
}
} catch (Exception e) {
throw new RuntimeException("Files could not be read: " + filePath.toString(), e);
}
});

// Update all
Files.walk(path).forEach(filePath -> {
try {
Map.Entry<String, String> entry = getId(filePath, idToIndex);
if (entry != null) {
filePath = renameFileToIndex(filePath, entry);
}
// For raw server, only fix up mapping files
if (isRawServer && !filePath.toString().contains("mappings")) {
return;
}
if (filePath.toString().endsWith(".json")) {
String fileText = new String(Files.readAllBytes(filePath));
// while recording responses we replaced all github calls localhost
Expand All @@ -193,37 +218,48 @@ public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContex
fileText = fileText.replace(this.uploadsServer().baseUrl(), "https://uploads.github.com");
}

// point bodyFile in the mapping to the renamed body file
if (entry != null && filePath.toString().contains("mappings")) {
fileText = fileText.replace("-" + entry.getKey(), "-" + entry.getValue());
}

// Can be Array or Map
Object parsedObject = g.fromJson(fileText, Object.class);
if (parsedObject instanceof Map && filePath.toString().contains("mappings")) {
filePath = renameMappingFile(filePath, (Map<String, Object>) parsedObject);
}
fileText = g.toJson(parsedObject);
Files.write(filePath, fileText.getBytes());
}
} catch (Exception e) {
throw new RuntimeException("Files could not be written", e);
throw new RuntimeException("Files could not be written: " + filePath.toString(), e);
}
});
} catch (IOException e) {
throw new RuntimeException("Files could not be written");
}
}

private Path renameMappingFile(Path filePath, Map<String, Object> parsedObject) throws IOException {
// Shorten the file names
// For understandability, rename the files to include the response order
Path targetPath = filePath;
private void addMappingId(Map<String, Object> parsedObject, Map<String, String> idToIndex) {
String id = (String) parsedObject.getOrDefault("id", null);
Long insertionIndex = ((Double) parsedObject.getOrDefault("insertionIndex", 0.0)).longValue();
long insertionIndex = ((Double) parsedObject.getOrDefault("insertionIndex", 0.0)).longValue();
if (id != null && insertionIndex > 0) {
String filePathString = filePath.toString();
if (filePathString.contains(id)) {
targetPath = new File(filePathString.replace(id, insertionIndex.toString() + "-" + id.substring(0, 6)))
.toPath();
Files.move(filePath, targetPath);
idToIndex.put(id, Long.toString(insertionIndex));
}
}

private Map.Entry<String, String> getId(Path filePath, Map<String, String> idToIndex) throws IOException {
Path targetPath = filePath;
String filePathString = filePath.toString();
for (Map.Entry<String, String> item : idToIndex.entrySet()) {
if (filePathString.contains(item.getKey())) {
return item;
}
}
return null;
}

private Path renameFileToIndex(Path filePath, Map.Entry<String, String> idToIndex) throws IOException {
String filePathString = filePath.toString();
Path targetPath = new File(filePathString.replace(idToIndex.getKey(), idToIndex.getValue())).toPath();
Files.move(filePath, targetPath);

return targetPath;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"id": "574da117-6845-46d8-b2c1-4415546ca670",
"name": "repos_github-api-test-org_temp-testratelimithandler_fail",
"request": {
"url": "/repos/github-api-test-org/temp-testHandler_Fail",
"method": "GET",
"headers": {
"Accept": {
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
}
}
},
"response": {
"status": 200,
"bodyFileName": "repos_github-api-test-org_temp-testratelimithandler_fail-3.json",
"headers": {
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4922",
"X-RateLimit-Reset": "{{testStartDate offset='3 seconds' format='unix'}}",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": [
"Accept, Authorization, Cookie, X-GitHub-OTP",
"Accept-Encoding"
],
"ETag": "W/\"858224998ac7d1fd6dcd43f73d375297\"",
"Last-Modified": "Thu, 06 Feb 2020 18:33:43 GMT",
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
"X-Accepted-OAuth-Scopes": "repo",
"X-GitHub-Media-Type": "unknown, github.v3",
"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",
"Access-Control-Allow-Origin": "*",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options": "deny",
"X-Content-Type-Options": "nosniff",
"X-XSS-Protection": "1; mode=block",
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy": "default-src 'none'",
"X-GitHub-Request-Id": "CC37:2605:3FADC:4EA8C:5E3C5C02"
}
},
"uuid": "574da117-6845-46d8-b2c1-4415546ca670",
"persistent": true,
"scenarioName": "scenario-1-repos-github-api-test-org-temp-testHandler_Fail",
"requiredScenarioState": "scenario-1-repos-github-api-test-org-temp-testHandler_Fail-2",
"insertionIndex": 3
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"id": "a60baf84-5b5c-4f86-af3d-cab0d609c7b2",
"name": "user",
"request": {
"url": "/user",
"method": "GET",
"headers": {
"Accept": {
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
}
}
},
"response": {
"status": 200,
"bodyFileName": "user-1.json",
"headers": {
"Date": "{{now timezone='GMT' format='EEE, dd MMM yyyy HH:mm:ss z'}}",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4930",
"X-RateLimit-Reset": "{{now offset='3 seconds' format='unix'}}",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": [
"Accept, Authorization, Cookie, X-GitHub-OTP",
"Accept-Encoding"
],
"ETag": "W/\"1cb30f031c67c499473b3aad01c7f7a5\"",
"Last-Modified": "Thu, 06 Feb 2020 17:29:39 GMT",
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
"X-Accepted-OAuth-Scopes": "",
"X-GitHub-Media-Type": "unknown, github.v3",
"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",
"Access-Control-Allow-Origin": "*",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options": "deny",
"X-Content-Type-Options": "nosniff",
"X-XSS-Protection": "1; mode=block",
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy": "default-src 'none'",
"X-GitHub-Request-Id": "CC37:2605:3F884:4E941:5E3C5BFC"
}
},
"uuid": "a60baf84-5b5c-4f86-af3d-cab0d609c7b2",
"persistent": true,
"insertionIndex": 1
}

This file was deleted.

Loading