From 5009c0ff2bdb1ce0d25a126cb7a4c713f0b0f122 Mon Sep 17 00:00:00 2001 From: jschrepp-MSFT <41338290+jschrepp-MSFT@users.noreply.github.com> Date: Thu, 17 Jun 2021 15:27:54 -0700 Subject: [PATCH 1/2] HttpAuthorization added to azure-core-experimental --- .../experimental/http/HttpAuthorization.java | 52 +++++++++++++++++++ .../http/HttpAuthorizationTests.java | 29 +++++++++++ 2 files changed, 81 insertions(+) create mode 100644 sdk/core/azure-core-experimental/src/main/java/com/azure/core/experimental/http/HttpAuthorization.java create mode 100644 sdk/core/azure-core-experimental/src/test/java/com/azure/core/experimental/http/HttpAuthorizationTests.java diff --git a/sdk/core/azure-core-experimental/src/main/java/com/azure/core/experimental/http/HttpAuthorization.java b/sdk/core/azure-core-experimental/src/main/java/com/azure/core/experimental/http/HttpAuthorization.java new file mode 100644 index 0000000000000..98ec2b15b2d2a --- /dev/null +++ b/sdk/core/azure-core-experimental/src/main/java/com/azure/core/experimental/http/HttpAuthorization.java @@ -0,0 +1,52 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.core.experimental.http; + +import com.azure.core.util.CoreUtils; +import com.azure.core.util.logging.ClientLogger; + +/** + * Represents the value of an HTTP Authorization header. + */ +public class HttpAuthorization { + private final ClientLogger logger = new ClientLogger(DynamicRequest.class); + private final String scheme; + private final String parameter; + + /** + * Constructs a new HttpAuthorization instance. + * + * @param scheme Scheme component of an authorization header value. + * @param parameter The credentials used for the authorization header value. + */ + public HttpAuthorization(String scheme, String parameter) { + if (CoreUtils.isNullOrEmpty(scheme)) { + throw logger.logExceptionAsError(new IllegalArgumentException("scheme must be a nonempty string.")); + } + if (CoreUtils.isNullOrEmpty(parameter)) { + throw logger.logExceptionAsError(new NullPointerException("parameter must be a nonempty string.")); + } + this.scheme = scheme; + this.parameter = parameter; + } + + /** + * @return Scheme of the authorization header. + */ + public String getScheme() { + return scheme; + } + + /** + * @return Credential of the authorization header. + */ + public String getParameter() { + return parameter; + } + + @Override + public String toString() { + return String.format("%s %s", scheme, parameter); + } +} diff --git a/sdk/core/azure-core-experimental/src/test/java/com/azure/core/experimental/http/HttpAuthorizationTests.java b/sdk/core/azure-core-experimental/src/test/java/com/azure/core/experimental/http/HttpAuthorizationTests.java new file mode 100644 index 0000000000000..ba2c4179c9d43 --- /dev/null +++ b/sdk/core/azure-core-experimental/src/test/java/com/azure/core/experimental/http/HttpAuthorizationTests.java @@ -0,0 +1,29 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.core.experimental.http; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Unit tests for {@link DynamicRequest}. + */ +public class HttpAuthorizationTests { + @Test + public void NullOrWhiteSpaceParameters() + { + Assertions.assertThrows(IllegalArgumentException.class, () -> new HttpAuthorization(null, "parameter")); + Assertions.assertThrows(IllegalArgumentException.class, () -> new HttpAuthorization("scheme", null)); + } + + @Test + public void ToStringTest() + { + String scheme = "scheme"; + String parameter = "parameter"; + HttpAuthorization httpAuthorization = new HttpAuthorization(scheme, parameter); + + Assertions.assertEquals(String.format("%s %s", scheme, parameter), httpAuthorization.toString()); + } +} From 8d29af0fc068c5b707909ae586283fa7bbc60e06 Mon Sep 17 00:00:00 2001 From: jschrepp-MSFT <41338290+jschrepp-MSFT@users.noreply.github.com> Date: Thu, 17 Jun 2021 16:56:05 -0700 Subject: [PATCH 2/2] feedback and CI --- .../experimental/http/HttpAuthorization.java | 19 +++++++++++++------ .../http/HttpAuthorizationTests.java | 10 ++++++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/sdk/core/azure-core-experimental/src/main/java/com/azure/core/experimental/http/HttpAuthorization.java b/sdk/core/azure-core-experimental/src/main/java/com/azure/core/experimental/http/HttpAuthorization.java index 98ec2b15b2d2a..d3f914e327c1d 100644 --- a/sdk/core/azure-core-experimental/src/main/java/com/azure/core/experimental/http/HttpAuthorization.java +++ b/sdk/core/azure-core-experimental/src/main/java/com/azure/core/experimental/http/HttpAuthorization.java @@ -3,14 +3,17 @@ package com.azure.core.experimental.http; -import com.azure.core.util.CoreUtils; +import com.azure.core.annotation.Immutable; import com.azure.core.util.logging.ClientLogger; +import java.util.Objects; + /** * Represents the value of an HTTP Authorization header. */ -public class HttpAuthorization { - private final ClientLogger logger = new ClientLogger(DynamicRequest.class); +@Immutable +public final class HttpAuthorization { + private final ClientLogger logger = new ClientLogger(HttpAuthorization.class); private final String scheme; private final String parameter; @@ -19,13 +22,17 @@ public class HttpAuthorization { * * @param scheme Scheme component of an authorization header value. * @param parameter The credentials used for the authorization header value. + * @throws NullPointerException if any argument is null. + * @throws IllegalArgumentException if any argument is an empty string. */ public HttpAuthorization(String scheme, String parameter) { - if (CoreUtils.isNullOrEmpty(scheme)) { + Objects.requireNonNull(scheme); + Objects.requireNonNull(parameter); + if (scheme.isEmpty()) { throw logger.logExceptionAsError(new IllegalArgumentException("scheme must be a nonempty string.")); } - if (CoreUtils.isNullOrEmpty(parameter)) { - throw logger.logExceptionAsError(new NullPointerException("parameter must be a nonempty string.")); + if (parameter.isEmpty()) { + throw logger.logExceptionAsError(new IllegalArgumentException("parameter must be a nonempty string.")); } this.scheme = scheme; this.parameter = parameter; diff --git a/sdk/core/azure-core-experimental/src/test/java/com/azure/core/experimental/http/HttpAuthorizationTests.java b/sdk/core/azure-core-experimental/src/test/java/com/azure/core/experimental/http/HttpAuthorizationTests.java index ba2c4179c9d43..45d0be7e9ccc2 100644 --- a/sdk/core/azure-core-experimental/src/test/java/com/azure/core/experimental/http/HttpAuthorizationTests.java +++ b/sdk/core/azure-core-experimental/src/test/java/com/azure/core/experimental/http/HttpAuthorizationTests.java @@ -11,14 +11,16 @@ */ public class HttpAuthorizationTests { @Test - public void NullOrWhiteSpaceParameters() + public void nullOrWhiteSpaceParameters() { - Assertions.assertThrows(IllegalArgumentException.class, () -> new HttpAuthorization(null, "parameter")); - Assertions.assertThrows(IllegalArgumentException.class, () -> new HttpAuthorization("scheme", null)); + Assertions.assertThrows(NullPointerException.class, () -> new HttpAuthorization(null, "parameter")); + Assertions.assertThrows(NullPointerException.class, () -> new HttpAuthorization("scheme", null)); + Assertions.assertThrows(IllegalArgumentException.class, () -> new HttpAuthorization("", "parameter")); + Assertions.assertThrows(IllegalArgumentException.class, () -> new HttpAuthorization("scheme", "")); } @Test - public void ToStringTest() + public void toStringTest() { String scheme = "scheme"; String parameter = "parameter";