This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 50f6fd51af9ed797cf92595f849a7bdf3c2841c0 Author: Tran Tien Duc <[email protected]> AuthorDate: Mon Apr 8 11:59:17 2019 +0700 JAMES-2708 Linshare Configuration & Token POJO --- .../apache/james/linshare/AuthorizationToken.java | 57 ++++++++++++ .../james/linshare/LinshareConfiguration.java | 100 +++++++++++++++++++++ .../james/linshare/AuthorizationTokenTest.java | 49 ++++++++++ .../james/linshare/LinshareConfigurationTest.java | 52 +++++++++++ 4 files changed, 258 insertions(+) diff --git a/third-party/linshare/src/main/java/org/apache/james/linshare/AuthorizationToken.java b/third-party/linshare/src/main/java/org/apache/james/linshare/AuthorizationToken.java new file mode 100644 index 0000000..34fe45f --- /dev/null +++ b/third-party/linshare/src/main/java/org/apache/james/linshare/AuthorizationToken.java @@ -0,0 +1,57 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.linshare; + +import java.util.Objects; + +import org.apache.commons.lang3.StringUtils; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; + +public class AuthorizationToken { + + private final String value; + + @VisibleForTesting + public AuthorizationToken(String value) { + Preconditions.checkArgument(StringUtils.isNotBlank(value), "token value cannot be null or blank"); + this.value = value; + } + + public String asBearerHeader() { + return "Bearer " + value; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof AuthorizationToken) { + AuthorizationToken that = (AuthorizationToken) o; + + return Objects.equals(this.value, that.value); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(value); + } +} diff --git a/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareConfiguration.java b/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareConfiguration.java new file mode 100644 index 0000000..d2ca31b --- /dev/null +++ b/third-party/linshare/src/main/java/org/apache/james/linshare/LinshareConfiguration.java @@ -0,0 +1,100 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.linshare; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Objects; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; + +public class LinshareConfiguration { + + public static class Builder { + + @FunctionalInterface + public interface RequireUrl { + RequireAuthorizationToken url(URL url); + + default RequireAuthorizationToken urlAsString(String url) throws MalformedURLException { + return url(new URL(url)); + } + } + + public interface RequireAuthorizationToken { + ReadyToBuild authorizationToken(AuthorizationToken token); + } + + public static class ReadyToBuild { + private final URL url; + private final AuthorizationToken token; + + ReadyToBuild(URL url, AuthorizationToken token) { + this.url = url; + this.token = token; + } + + public LinshareConfiguration build() { + return new LinshareConfiguration(url, token); + } + } + } + + public static Builder.RequireUrl builder() { + return url -> credential -> new Builder.ReadyToBuild(url, credential); + } + + private final URL url; + private final AuthorizationToken token; + + @VisibleForTesting + LinshareConfiguration(URL url, AuthorizationToken token) { + Preconditions.checkNotNull(url); + Preconditions.checkNotNull(token); + + this.url = url; + this.token = token; + } + + public URL getUrl() { + return url; + } + + public AuthorizationToken getToken() { + return token; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof LinshareConfiguration) { + LinshareConfiguration that = (LinshareConfiguration) o; + + return Objects.equals(this.url, that.url) + && Objects.equals(this.token, that.token); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(url, token); + } +} diff --git a/third-party/linshare/src/test/java/org/apache/james/linshare/AuthorizationTokenTest.java b/third-party/linshare/src/test/java/org/apache/james/linshare/AuthorizationTokenTest.java new file mode 100644 index 0000000..626bb1d --- /dev/null +++ b/third-party/linshare/src/test/java/org/apache/james/linshare/AuthorizationTokenTest.java @@ -0,0 +1,49 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.linshare; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.jupiter.api.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; + +class AuthorizationTokenTest { + + @Test + void shouldMatchBeanContract() { + EqualsVerifier.forClass(AuthorizationToken.class) + .verify(); + } + + @Test + void constructorShouldThrowWhenTokenIsNull() { + String nullToken = null; + assertThatThrownBy(() -> new AuthorizationToken(nullToken)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void constructorShouldThrowWhenTokenIsBlank() { + String blankToken = " "; + assertThatThrownBy(() -> new AuthorizationToken(blankToken)) + .isInstanceOf(IllegalArgumentException.class); + } +} diff --git a/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareConfigurationTest.java b/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareConfigurationTest.java new file mode 100644 index 0000000..8090d11 --- /dev/null +++ b/third-party/linshare/src/test/java/org/apache/james/linshare/LinshareConfigurationTest.java @@ -0,0 +1,52 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.linshare; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.net.URL; + +import org.junit.jupiter.api.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; + +class LinshareConfigurationTest { + + @Test + void shouldMatchBeanContract() { + EqualsVerifier.forClass(LinshareConfiguration.class) + .verify(); + } + + @Test + void constructorShouldThrowWhenPassingNullCredential() { + AuthorizationToken nullToken = null; + assertThatThrownBy(() -> new LinshareConfiguration(new URL("https://linshare.linagora.com"), nullToken)) + .isInstanceOf(NullPointerException.class); + } + + @Test + void constructorShouldThrowWhenPassingNullUrl() { + AuthorizationToken token = new AuthorizationToken("Bearer jwt-token-at-here"); + URL nullUrl = null; + assertThatThrownBy(() -> new LinshareConfiguration(nullUrl, token)) + .isInstanceOf(NullPointerException.class); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
