Skip to content

Commit

Permalink
add test for CredentialsProviderFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry-024 committed Dec 6, 2024
1 parent 6e7f2cd commit 2a422b5
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.paimon.rest.auth;

import org.apache.paimon.options.Options;
import org.apache.paimon.utils.StringUtils;

/** factory for create {@link BearTokenCredentialsProvider}. */
public class BearTokenCredentialsProviderFactory implements CredentialsProviderFactory {
Expand All @@ -29,6 +30,12 @@ public String identifier() {

@Override
public CredentialsProvider create(Options options) {
if (options.getOptional(AuthOptions.TOKEN)
.map(StringUtils::isNullOrWhitespaceOnly)
.orElse(true)) {
throw new IllegalArgumentException(
AuthOptions.TOKEN.key() + " is required and not empty");
}
return new BearTokenCredentialsProvider(options.get(AuthOptions.TOKEN));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* 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.paimon.rest.auth;

import org.apache.paimon.options.Options;

import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.time.Duration;
import java.util.UUID;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

/** Test for {@link CredentialsProviderFactory}. */
public class CredentialsProviderFactoryTest {

@Rule public TemporaryFolder folder = new TemporaryFolder();

@Test
public void testCreateBearTokenCredentialsProviderSuccess() {
Options options = new Options();
String token = UUID.randomUUID().toString();
options.set(AuthOptions.TOKEN, token);
options.set(AuthOptions.CREDENTIALS_PROVIDER, CredentialsProviderType.BEAR_TOKEN.name());
BearTokenCredentialsProvider credentialsProvider =
(BearTokenCredentialsProvider)
CredentialsProviderFactory.createCredentialsProvider(
options, this.getClass().getClassLoader());
assertEquals(token, credentialsProvider.token());
}

@Test
public void testCreateBearTokenCredentialsProviderFail() {
Options options = new Options();
options.set(AuthOptions.CREDENTIALS_PROVIDER, CredentialsProviderType.BEAR_TOKEN.name());
assertThrows(
IllegalArgumentException.class,
() ->
CredentialsProviderFactory.createCredentialsProvider(
options, this.getClass().getClassLoader()));
}

@Test
public void testCreateBearTokenFileCredentialsProviderSuccess() throws Exception {
Options options = new Options();
String fileName = "token";
File tokenFile = folder.newFile(fileName);
String token = UUID.randomUUID().toString();
FileUtils.writeStringToFile(tokenFile, token);
options.set(AuthOptions.TOKEN_FILE_PATH, tokenFile.getPath());
options.set(
AuthOptions.CREDENTIALS_PROVIDER, CredentialsProviderType.BEAR_TOKEN_FILE.name());
BearTokenFileCredentialsProvider credentialsProvider =
(BearTokenFileCredentialsProvider)
CredentialsProviderFactory.createCredentialsProvider(
options, this.getClass().getClassLoader());
assertEquals(token, credentialsProvider.token());
}

@Test
public void testCreateBearTokenFileCredentialsProviderFail() throws Exception {
Options options = new Options();
options.set(
AuthOptions.CREDENTIALS_PROVIDER, CredentialsProviderType.BEAR_TOKEN_FILE.name());
assertThrows(
IllegalArgumentException.class,
() ->
CredentialsProviderFactory.createCredentialsProvider(
options, this.getClass().getClassLoader()));
}

@Test
public void testCreateRefreshBearTokenFileCredentialsProviderSuccess() throws Exception {
Options options = new Options();
String fileName = "token";
File tokenFile = folder.newFile(fileName);
String token = UUID.randomUUID().toString();
FileUtils.writeStringToFile(tokenFile, token);
options.set(
AuthOptions.CREDENTIALS_PROVIDER, CredentialsProviderType.BEAR_TOKEN_FILE.name());
options.set(AuthOptions.TOKEN_FILE_PATH, tokenFile.getPath());
options.set(AuthOptions.TOKEN_REFRESH_ENABLED, true);
options.set(AuthOptions.TOKEN_EXPIRES_IN, Duration.ofSeconds(10L));
BearTokenFileCredentialsProvider credentialsProvider =
(BearTokenFileCredentialsProvider)
CredentialsProviderFactory.createCredentialsProvider(
options, this.getClass().getClassLoader());
assertEquals(token, credentialsProvider.token());
}

@Test
public void testCreateRefreshBearTokenFileCredentialsProviderFail() throws Exception {
Options options = new Options();
String fileName = "token";
File tokenFile = folder.newFile(fileName);
String token = UUID.randomUUID().toString();
FileUtils.writeStringToFile(tokenFile, token);
options.set(
AuthOptions.CREDENTIALS_PROVIDER, CredentialsProviderType.BEAR_TOKEN_FILE.name());
options.set(AuthOptions.TOKEN_FILE_PATH, tokenFile.getPath());
options.set(AuthOptions.TOKEN_REFRESH_ENABLED, true);
options.set(
AuthOptions.CREDENTIALS_PROVIDER, CredentialsProviderType.BEAR_TOKEN_FILE.name());
assertThrows(
IllegalArgumentException.class,
() ->
CredentialsProviderFactory.createCredentialsProvider(
options, this.getClass().getClassLoader()));
}
}

0 comments on commit 2a422b5

Please sign in to comment.