From dd7983cbb7f93a9b7129556f3b6f1e4e0e95c7b3 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 12 Feb 2021 09:51:26 -0500 Subject: [PATCH 1/5] - adds checkstyle configuration --- .vscode/settings.json | 4 +- build.gradle | 11 + config/checkstyle/checkstyle.xml | 367 +++++++++++++++++++++++++++++++ 3 files changed, 381 insertions(+), 1 deletion(-) create mode 100644 config/checkstyle/checkstyle.xml diff --git a/.vscode/settings.json b/.vscode/settings.json index e0f15db2e..7d41d95b1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,5 @@ { - "java.configuration.updateBuildConfiguration": "automatic" + "java.configuration.updateBuildConfiguration": "automatic", + "java.checkstyle.configuration": "${workspaceFolder}\\config\\checkstyle\\checkstyle.xml", + "java.checkstyle.version": "8.40" } \ No newline at end of file diff --git a/build.gradle b/build.gradle index 923ea3704..a6136c3fd 100644 --- a/build.gradle +++ b/build.gradle @@ -15,6 +15,7 @@ plugins { id 'signing' id 'jacoco' id 'com.github.spotbugs' version '4.6.0' + id 'checkstyle' } java { @@ -88,6 +89,16 @@ def pomConfig = { } } +checkstyle { + toolVersion "8.40" +} +tasks.withType(Checkstyle) { + reports { + xml.enabled false + html.enabled true + } +} + //Publishing tasks- //Maven Central Snapshot: publishSnapshotPublicationToMavenRepository //Maven Central Release: publishMavenCentralReleasePublicationToMaven2Repository diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml new file mode 100644 index 000000000..2326ff8c3 --- /dev/null +++ b/config/checkstyle/checkstyle.xml @@ -0,0 +1,367 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 3b3682ad5dbb6b09a7d10549393f64b7a23c8eb0 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 12 Feb 2021 09:52:07 -0500 Subject: [PATCH 2/5] - aligns indent size in editor config with checkstyle --- .editorconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 3bf6f4f2b..896da789b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,5 +1,5 @@ [*.java] indent_style = space -indent_size = 4 +indent_size = 2 insert_final_newline = true trim_trailing_whitespace = true \ No newline at end of file From 35b211bfd693b1b02021eb5742a6a82fa2a68dcf Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 12 Feb 2021 09:52:18 -0500 Subject: [PATCH 3/5] - adds recommended extensions --- .vscode/extensions.json | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .vscode/extensions.json diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 000000000..4baadcd74 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,6 @@ +{ + "recommendations": [ + "shengchen.vscode-checkstyle", + "vscjava.vscode-java-pack" + ] +} \ No newline at end of file From 21ab1c85b805cfceaa12a75e256c20126bfd5413 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 12 Feb 2021 09:52:41 -0500 Subject: [PATCH 4/5] - fixes the style of chaos handler as demonstration --- .../graph/httpcore/ChaosHttpHandler.java | 108 +++++++++--------- 1 file changed, 53 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/microsoft/graph/httpcore/ChaosHttpHandler.java b/src/main/java/com/microsoft/graph/httpcore/ChaosHttpHandler.java index e4eec8304..6a0e656d3 100644 --- a/src/main/java/com/microsoft/graph/httpcore/ChaosHttpHandler.java +++ b/src/main/java/com/microsoft/graph/httpcore/ChaosHttpHandler.java @@ -1,14 +1,10 @@ package com.microsoft.graph.httpcore; +import com.microsoft.graph.httpcore.middlewareoption.MiddlewareType; +import com.microsoft.graph.httpcore.middlewareoption.TelemetryOptions; import java.io.IOException; import java.util.concurrent.ThreadLocalRandom; - -import javax.annotation.Nullable; import javax.annotation.Nonnull; - -import com.microsoft.graph.httpcore.middlewareoption.MiddlewareType; -import com.microsoft.graph.httpcore.middlewareoption.TelemetryOptions; - import okhttp3.Interceptor; import okhttp3.MediaType; import okhttp3.Protocol; @@ -17,62 +13,64 @@ import okhttp3.ResponseBody; /** - * DO NOT USE IN PRODUCTION + * DO NOT USE IN PRODUCTION. * interceptor that randomly fails the responses for unit testing purposes */ public class ChaosHttpHandler implements Interceptor { - /** - * The current middleware type - */ - public final MiddlewareType MIDDLEWARE_TYPE = MiddlewareType.RETRY; - /** - * constant string being used - */ - private static final String RETRY_AFTER = "Retry-After"; - /** - * Denominator for the failure rate (i.e. 1/X) - */ - private static final int failureRate = 3; - /** - * default value to return on retry after - */ - private static final String retryAfterValue = "10"; - /** - * body to respond on failed requests - */ - private static final String responseBody = "{\"error\": {\"code\": \"TooManyRequests\",\"innerError\": {\"code\": \"429\",\"date\": \"2020-08-18T12:51:51\",\"message\": \"Please retry after\",\"request-id\": \"94fb3b52-452a-4535-a601-69e0a90e3aa2\",\"status\": \"429\"},\"message\": \"Please retry again later.\"}}"; - /** - * Too many requests status code - */ - public static final int MSClientErrorCodeTooManyRequests = 429; + /** + * The current middleware type. + */ + @SuppressWarnings({"checkstyle:MemberName", "checkstyle:AbbreviationAsWordInName"}) + public final MiddlewareType MIDDLEWARE_TYPE = MiddlewareType.RETRY; + /** + * constant string being used. + */ + private static final String retryAfter = "Retry-After"; + /** + * Denominator for the failure rate (i.e. 1/X) + */ + private static final int failureRate = 3; + /** + * default value to return on retry after. + */ + private static final String retryAfterValue = "10"; + /** + * body to respond on failed requests. + */ + @SuppressWarnings("checkstyle:LineLength") + private static final String responseBody = "{\"error\": {\"code\": \"TooManyRequests\",\"innerError\": {\"code\": \"429\",\"date\": \"2020-08-18T12:51:51\",\"message\": \"Please retry after\",\"request-id\": \"94fb3b52-452a-4535-a601-69e0a90e3aa2\",\"status\": \"429\"},\"message\": \"Please retry again later.\"}}"; + /** + * Too many requests status code. + */ + public static final int MSClientErrorCodeTooManyRequests = 429; - @Override - @Nonnull - public Response intercept(@Nonnull final Chain chain) throws IOException { - Request request = chain.request(); + @Override + @Nonnull + public Response intercept(@Nonnull final Chain chain) throws IOException { + Request request = chain.request(); - TelemetryOptions telemetryOptions = request.tag(TelemetryOptions.class); - if(telemetryOptions == null) { - telemetryOptions = new TelemetryOptions(); - request = request.newBuilder().tag(TelemetryOptions.class, telemetryOptions).build(); - } - telemetryOptions.setFeatureUsage(TelemetryOptions.RETRY_HANDLER_ENABLED_FLAG); + TelemetryOptions telemetryOptions = request.tag(TelemetryOptions.class); + if (telemetryOptions == null) { + telemetryOptions = new TelemetryOptions(); + request = request.newBuilder().tag(TelemetryOptions.class, telemetryOptions).build(); + } + telemetryOptions.setFeatureUsage(TelemetryOptions.RETRY_HANDLER_ENABLED_FLAG); - final int dice = ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE); + final int dice = ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE); - if(dice % failureRate == 0) { - return new Response - .Builder() - .request(request) - .protocol(Protocol.HTTP_1_1) - .code(MSClientErrorCodeTooManyRequests) - .message("Too Many Requests") - .addHeader(RETRY_AFTER, retryAfterValue) - .body(ResponseBody.create(responseBody, MediaType.get("application/json"))) - .build(); - } else { - return chain.proceed(request); - } + if (dice % failureRate == 0) { + return new Response + .Builder() + .request(request) + .protocol(Protocol.HTTP_1_1) + .code(MSClientErrorCodeTooManyRequests) + .message("Too Many Requests") + .addHeader(retryAfter, retryAfterValue) + .body(ResponseBody.create(responseBody, MediaType.get("application/json"))) + .build(); + } else { + return chain.proceed(request); } + } } From f03056ab6bdeebb6b7123cd4bdba686005c3c4a5 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 12 Feb 2021 10:18:31 -0500 Subject: [PATCH 5/5] - fixes samples build after setting up checkstyle --- build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/build.gradle b/build.gradle index a6136c3fd..d5ed1c9fd 100644 --- a/build.gradle +++ b/build.gradle @@ -91,6 +91,7 @@ def pomConfig = { checkstyle { toolVersion "8.40" + configFile = file("config/checkstyle/checkstyle.xml") //required for samples to build as this project is not root project when building samples } tasks.withType(Checkstyle) { reports {