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 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 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..d5ed1c9fd 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,17 @@ 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 { + 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 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); } + } }