4242import org .junit .jupiter .api .Test ;
4343
4444import java .io .File ;
45+ import java .io .IOException ;
4546import java .util .HashMap ;
4647import java .util .Map ;
4748import java .util .UUID ;
5051import static org .junit .jupiter .api .Assertions .assertEquals ;
5152
5253/** Test REST Catalog on Mocked REST server. */
53- class MockRESTCatalogTest extends RESTCatalogTestBase {
54+ class RESTCatalogMockTest extends RESTCatalogTestBase {
5455
5556 private RESTCatalogServer restCatalogServer ;
5657 private final String initToken = "init_token" ;
5758 private final String serverDefineHeaderName = "test-header" ;
5859 private final String serverDefineHeaderValue = "test-value" ;
5960 private String dataPath ;
6061 private AuthProvider authProvider ;
62+ private Map <String , String > authMap ;
6163
6264 @ BeforeEach
6365 @ Override
6466 public void setUp () throws Exception {
6567 super .setUp ();
6668 dataPath = warehouse ;
67- String restWarehouse = UUID .randomUUID ().toString ();
68- this .config =
69- new ConfigResponse (
70- ImmutableMap .of (
71- RESTCatalogInternalOptions .PREFIX .key (),
72- "paimon" ,
73- "header." + serverDefineHeaderName ,
74- serverDefineHeaderValue ,
75- CatalogOptions .WAREHOUSE .key (),
76- restWarehouse ),
77- ImmutableMap .of ());
7869 this .authProvider = new BearTokenAuthProvider (initToken );
79- restCatalogServer =
80- new RESTCatalogServer (dataPath , authProvider , this .config , restWarehouse );
81- restCatalogServer .start ();
82- options .set (CatalogOptions .WAREHOUSE .key (), restWarehouse );
83- options .set (RESTCatalogOptions .URI , restCatalogServer .getUrl ());
84- options .set (RESTCatalogOptions .TOKEN , initToken );
85- options .set (RESTCatalogOptions .TOKEN_PROVIDER , AuthProviderEnum .BEAR .identifier ());
86- this .restCatalog = new RESTCatalog (CatalogContext .create (options ));
70+ this .authMap =
71+ ImmutableMap .of (
72+ RESTCatalogOptions .TOKEN .key (),
73+ initToken ,
74+ RESTCatalogOptions .TOKEN_PROVIDER .key (),
75+ AuthProviderEnum .BEAR .identifier ());
76+ this .restCatalog = initCatalog (false );
8777 this .catalog = restCatalog ;
8878 }
8979
@@ -105,30 +95,24 @@ void testAuthFail() {
10595
10696 @ Test
10797 void testDlfStSTokenAuth () throws Exception {
108- String restWarehouse = UUID .randomUUID ().toString ();
10998 String akId = "akId" + UUID .randomUUID ();
11099 String akSecret = "akSecret" + UUID .randomUUID ();
111100 String securityToken = "securityToken" + UUID .randomUUID ();
112101 String region = "cn-hangzhou" ;
113- DLFAuthProvider authProvider =
114- DLFAuthProvider .buildAKToken (akId , akSecret , securityToken , region );
115- restCatalogServer =
116- new RESTCatalogServer (dataPath , authProvider , this .config , restWarehouse );
117- restCatalogServer .start ();
118- options .set (CatalogOptions .WAREHOUSE .key (), restWarehouse );
119- options .set (RESTCatalogOptions .URI , restCatalogServer .getUrl ());
120- options .set (RESTCatalogOptions .TOKEN_PROVIDER , AuthProviderEnum .DLF .identifier ());
121- options .set (RESTCatalogOptions .DLF_REGION , region );
122- options .set (RESTCatalogOptions .DLF_ACCESS_KEY_ID , akId );
123- options .set (RESTCatalogOptions .DLF_ACCESS_KEY_SECRET , akSecret );
124- options .set (RESTCatalogOptions .DLF_SECURITY_TOKEN , securityToken );
125- RESTCatalog restCatalog = new RESTCatalog (CatalogContext .create (options ));
102+ this .authProvider = DLFAuthProvider .buildAKToken (akId , akSecret , securityToken , region );
103+ this .authMap =
104+ ImmutableMap .of (
105+ RESTCatalogOptions .TOKEN_PROVIDER .key (), AuthProviderEnum .DLF .identifier (),
106+ RESTCatalogOptions .DLF_REGION .key (), region ,
107+ RESTCatalogOptions .DLF_ACCESS_KEY_ID .key (), akId ,
108+ RESTCatalogOptions .DLF_ACCESS_KEY_SECRET .key (), akSecret ,
109+ RESTCatalogOptions .DLF_SECURITY_TOKEN .key (), securityToken );
110+ RESTCatalog restCatalog = initCatalog (false );
126111 testDlfAuth (restCatalog );
127112 }
128113
129114 @ Test
130115 void testDlfStSTokenPathAuth () throws Exception {
131- String restWarehouse = UUID .randomUUID ().toString ();
132116 String region = "cn-hangzhou" ;
133117 String tokenPath = dataPath + UUID .randomUUID ();
134118 generateTokenAndWriteToFile (tokenPath );
@@ -138,17 +122,13 @@ void testDlfStSTokenPathAuth() throws Exception {
138122 new Options (
139123 ImmutableMap .of (
140124 RESTCatalogOptions .DLF_TOKEN_PATH .key (), tokenPath )));
141- DLFAuthProvider authProvider =
142- DLFAuthProvider .buildRefreshToken (tokenLoader , 1000_000L , region );
143- restCatalogServer =
144- new RESTCatalogServer (dataPath , authProvider , this .config , restWarehouse );
145- restCatalogServer .start ();
146- options .set (CatalogOptions .WAREHOUSE .key (), restWarehouse );
147- options .set (RESTCatalogOptions .URI , restCatalogServer .getUrl ());
148- options .set (RESTCatalogOptions .TOKEN_PROVIDER , AuthProviderEnum .DLF .identifier ());
149- options .set (RESTCatalogOptions .DLF_REGION , region );
150- options .set (RESTCatalogOptions .DLF_TOKEN_PATH , tokenPath );
151- RESTCatalog restCatalog = new RESTCatalog (CatalogContext .create (options ));
125+ this .authProvider = DLFAuthProvider .buildRefreshToken (tokenLoader , 1000_000L , region );
126+ this .authMap =
127+ ImmutableMap .of (
128+ RESTCatalogOptions .TOKEN_PROVIDER .key (), AuthProviderEnum .DLF .identifier (),
129+ RESTCatalogOptions .DLF_REGION .key (), region ,
130+ RESTCatalogOptions .DLF_TOKEN_PATH .key (), tokenPath );
131+ RESTCatalog restCatalog = initCatalog (false );
152132 testDlfAuth (restCatalog );
153133 File file = new File (tokenPath );
154134 file .delete ();
@@ -184,12 +164,8 @@ private void testDlfAuth(RESTCatalog restCatalog) throws Exception {
184164 }
185165
186166 @ Override
187- protected Catalog newRestCatalogWithDataToken () {
188- options .set (RESTTokenFileIO .DATA_TOKEN_ENABLED , true );
189- options .set (
190- RESTTestFileIO .DATA_PATH_CONF_KEY ,
191- dataPath .replaceFirst ("file" , RESTFileIOTestLoader .SCHEME ));
192- return new RESTCatalog (CatalogContext .create (options ));
167+ protected Catalog newRestCatalogWithDataToken () throws IOException {
168+ return initCatalog (true );
193169 }
194170
195171 @ Override
@@ -234,4 +210,34 @@ protected void updateSnapshotOnRestServer(
234210 fileCount ,
235211 lastFileCreationTime );
236212 }
213+
214+ private RESTCatalog initCatalog (boolean enableDataToken ) throws IOException {
215+ String restWarehouse = UUID .randomUUID ().toString ();
216+ this .config =
217+ new ConfigResponse (
218+ ImmutableMap .of (
219+ RESTCatalogInternalOptions .PREFIX .key (),
220+ "paimon" ,
221+ "header." + serverDefineHeaderName ,
222+ serverDefineHeaderValue ,
223+ RESTTokenFileIO .DATA_TOKEN_ENABLED .key (),
224+ enableDataToken + "" ,
225+ CatalogOptions .WAREHOUSE .key (),
226+ restWarehouse ),
227+ ImmutableMap .of ());
228+ restCatalogServer =
229+ new RESTCatalogServer (dataPath , this .authProvider , this .config , restWarehouse );
230+ restCatalogServer .start ();
231+ for (Map .Entry <String , String > entry : this .authMap .entrySet ()) {
232+ options .set (entry .getKey (), entry .getValue ());
233+ }
234+ options .set (CatalogOptions .WAREHOUSE .key (), restWarehouse );
235+ options .set (RESTCatalogOptions .URI , restCatalogServer .getUrl ());
236+ String path =
237+ enableDataToken
238+ ? dataPath .replaceFirst ("file" , RESTFileIOTestLoader .SCHEME )
239+ : dataPath ;
240+ options .set (RESTTestFileIO .DATA_PATH_CONF_KEY , path );
241+ return new RESTCatalog (CatalogContext .create (options ));
242+ }
237243}
0 commit comments