Repository: SAP/cloud-security-xsuaa-integration
Pull request #505 (2021-04-21 08:51:42)

OAuth2ServiceException provides getStatusCode()


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6e67aab8..e50e089e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,7 +3,7 @@ All notable changes to this project will be documented in this file.
 ## 2.8.11 and 0.1.5 [BETA]
 - The following dependencies were updated:
     - spring.security.version 5.4.5 --> 5.4.6
-- [token-client] ```OAuth2ServiceException``` provides ```getStatusCode()```. This allows applications to retry e.g. in case of ```429``` - when the request was rate limited.
+- [token-client] ```OAuth2ServiceException``` provides ```getHttpStatusCode()```. This allows applications to retry e.g. in case of ```429``` - when the request was rate limited.
     
 ## 2.8.10 and 0.1.4 [BETA]
 - [spring-xsuaa] introduced spring properties for IAS -> Xsuaa token exchange activation, as described [here](/spring-xsuaa/README.md#ias-to-xsuaa-token-exchange)
diff --git a/token-client/src/main/java/com/sap/cloud/security/xsuaa/client/OAuth2ServiceException.java b/token-client/src/main/java/com/sap/cloud/security/xsuaa/client/OAuth2ServiceException.java
index e82c6b4c..2c1ca0fd 100644
--- a/token-client/src/main/java/com/sap/cloud/security/xsuaa/client/OAuth2ServiceException.java
+++ b/token-client/src/main/java/com/sap/cloud/security/xsuaa/client/OAuth2ServiceException.java
@@ -12,18 +12,45 @@ import java.util.stream.Stream;
 public class OAuth2ServiceException extends IOException {
 
 	private static final long serialVersionUID = 1L;
+	private Integer httpStatusCode = 0;
 
 	public OAuth2ServiceException(String message) {
 		super(message);
 	}
 
+	/**
+	 * Creates an exception.
+	 *
+	 * @param message the error message
+	 * @param httpStatusCode the status code of the HTTP service request
+	 */
+	public OAuth2ServiceException(String message, Integer httpStatusCode) {
+		super(message);
+		this.httpStatusCode = httpStatusCode != null ?  httpStatusCode : 0;
+	}
+
+	/**
+	 * Creates an exception.
+	 *
+	 * @param message the error message
+	 */
 	public static OAuth2ServiceExceptionBuilder builder(String message) {
 		return new OAuth2ServiceExceptionBuilder(message);
 	}
 
+	/**
+	 * Returns the HTTP status code of the failed OAuth2 service request or {@code 0}
+	 * e.g. in case the service wasn't called at all.
+	 *
+	 * @return status code or 0
+	 */
+	public Integer getHttpStatusCode() {
+		return httpStatusCode;
+	}
+
 	public static class OAuth2ServiceExceptionBuilder {
 		private String message;
-		private Integer statusCode;
+		private Integer httpStatusCode;
 		private URI serverUri;
 		private String responseBody;
 
@@ -31,8 +58,13 @@ public class OAuth2ServiceException extends IOException {
 			this.message = message;
 		}
 
-		public OAuth2ServiceExceptionBuilder withStatusCode(int statusCode) {
-			this.statusCode = statusCode;
+		/**
+		 * Parameterizes the Exception with a HTTP status code.
+		 * @param httpStatusCode the http status code
+		 * @return the builder
+		 */
+		public OAuth2ServiceExceptionBuilder withStatusCode(int httpStatusCode) {
+			this.httpStatusCode = httpStatusCode;
 			return this;
 		}
 
@@ -51,7 +83,7 @@ public class OAuth2ServiceException extends IOException {
 					.of(this.message, createUriMessage(), createStatusCodeMessage(), createResponseBodyMessage())
 					.filter(Objects::nonNull)
 					.collect(Collectors.joining(". "));
-			return new OAuth2ServiceException(message);
+			return new OAuth2ServiceException(message, httpStatusCode);
 		}
 
 		private String createResponseBodyMessage() {
@@ -59,7 +91,7 @@ public class OAuth2ServiceException extends IOException {
 		}
 
 		private String createStatusCodeMessage() {
-			return statusCode == null ? null : "Status code " + statusCode;
+			return httpStatusCode == null ? null : "Http status code " + httpStatusCode;
 		}
 
 		private String createUriMessage() {


Test patch:
diff --git a/token-client/src/test/java/com/sap/cloud/security/xsuaa/client/DefaultOAuth2TokenServiceTest.java b/token-client/src/test/java/com/sap/cloud/security/xsuaa/client/DefaultOAuth2TokenServiceTest.java
index 45718a1d..5420dffc 100644
--- a/token-client/src/test/java/com/sap/cloud/security/xsuaa/client/DefaultOAuth2TokenServiceTest.java
+++ b/token-client/src/test/java/com/sap/cloud/security/xsuaa/client/DefaultOAuth2TokenServiceTest.java
@@ -9,6 +9,7 @@ import org.apache.http.HttpStatus;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.impl.client.CloseableHttpClient;
+import org.assertj.core.api.InstanceOfAssertFactories;
 import org.assertj.core.util.Maps;
 import org.junit.Before;
 import org.junit.Test;
@@ -96,8 +97,8 @@ public class DefaultOAuth2TokenServiceTest {
 		assertThatThrownBy(() -> requestAccessToken(emptyMap()))
 				.isInstanceOf(OAuth2ServiceException.class)
 				.hasMessageContaining(unauthorizedResponseText)
-				.hasMessageContaining(String.valueOf(HttpStatus.SC_UNAUTHORIZED))
-				.hasMessageContaining(TOKEN_ENDPOINT_URI.toString());
+				.hasMessageContaining(TOKEN_ENDPOINT_URI.toString())
+				.extracting("httpStatusCode").isEqualTo(HttpStatus.SC_UNAUTHORIZED);
 	}
 
 	private OAuth2TokenResponse requestAccessToken(Map<String, String> optionalParameters)
diff --git a/token-client/src/test/java/com/sap/cloud/security/xsuaa/client/DefaultOidcConfigurationServiceTest.java b/token-client/src/test/java/com/sap/cloud/security/xsuaa/client/DefaultOidcConfigurationServiceTest.java
index 633d8b99..a016087f 100644
--- a/token-client/src/test/java/com/sap/cloud/security/xsuaa/client/DefaultOidcConfigurationServiceTest.java
+++ b/token-client/src/test/java/com/sap/cloud/security/xsuaa/client/DefaultOidcConfigurationServiceTest.java
@@ -84,7 +84,8 @@ public class DefaultOidcConfigurationServiceTest {
 
 		assertThatThrownBy(() -> retrieveEndpoints())
 				.isInstanceOf(OAuth2ServiceException.class)
-				.hasMessageContaining(errorMessage);
+				.hasMessageContaining(errorMessage)
+				.extracting("httpStatusCode").isEqualTo(0);
 	}
 
 	@Test