|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.polaris.service.catalog.iceberg; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.lang.reflect.Field; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.stream.Stream; |
| 27 | +import org.apache.iceberg.Schema; |
| 28 | +import org.apache.iceberg.catalog.Namespace; |
| 29 | +import org.apache.iceberg.catalog.TableIdentifier; |
| 30 | +import org.apache.iceberg.inmemory.InMemoryCatalog; |
| 31 | +import org.apache.iceberg.rest.responses.ListNamespacesResponse; |
| 32 | +import org.apache.iceberg.rest.responses.ListTablesResponse; |
| 33 | +import org.apache.polaris.core.admin.model.AuthenticationParameters; |
| 34 | +import org.apache.polaris.core.admin.model.AwsStorageConfigInfo; |
| 35 | +import org.apache.polaris.core.admin.model.BearerAuthenticationParameters; |
| 36 | +import org.apache.polaris.core.admin.model.CatalogProperties; |
| 37 | +import org.apache.polaris.core.admin.model.ConnectionConfigInfo; |
| 38 | +import org.apache.polaris.core.admin.model.CreateCatalogRequest; |
| 39 | +import org.apache.polaris.core.admin.model.ExternalCatalog; |
| 40 | +import org.apache.polaris.core.admin.model.IcebergRestConnectionConfigInfo; |
| 41 | +import org.apache.polaris.core.admin.model.StorageConfigInfo; |
| 42 | +import org.apache.polaris.service.TestServices; |
| 43 | +import org.assertj.core.api.Assertions; |
| 44 | +import org.assertj.core.util.Strings; |
| 45 | +import org.junit.jupiter.api.BeforeEach; |
| 46 | +import org.junit.jupiter.params.ParameterizedTest; |
| 47 | +import org.junit.jupiter.params.provider.Arguments; |
| 48 | +import org.junit.jupiter.params.provider.MethodSource; |
| 49 | +import org.mockito.Mockito; |
| 50 | + |
| 51 | +public class IcebergCatalogAdapterTest { |
| 52 | + |
| 53 | + private static final String FEDERATED_CATALOG_NAME = "polaris-federated-catalog"; |
| 54 | + |
| 55 | + private TestServices testServices; |
| 56 | + private IcebergCatalogAdapter catalogAdapter; |
| 57 | + |
| 58 | + @BeforeEach |
| 59 | + public void setUp() { |
| 60 | + // Set up test services with catalog federation enabled |
| 61 | + testServices = |
| 62 | + TestServices.builder().config(Map.of("ENABLE_CATALOG_FEDERATION", "true")).build(); |
| 63 | + catalogAdapter = Mockito.spy(testServices.catalogAdapter()); |
| 64 | + |
| 65 | + // Prepare storage and connection configs for a federated Iceberg REST catalog |
| 66 | + String storageLocation = "s3://my-bucket/path/to/data"; |
| 67 | + AwsStorageConfigInfo storageConfig = |
| 68 | + AwsStorageConfigInfo.builder() |
| 69 | + .setRoleArn("arn:aws:iam::012345678901:role/polaris-user-role") |
| 70 | + .setExternalId("externalId") |
| 71 | + .setUserArn("aws::a:user:arn") |
| 72 | + .setStorageType(StorageConfigInfo.StorageTypeEnum.S3) |
| 73 | + .setAllowedLocations(List.of(storageLocation, "s3://externally-owned-bucket")) |
| 74 | + .build(); |
| 75 | + |
| 76 | + AuthenticationParameters authParams = |
| 77 | + BearerAuthenticationParameters.builder() |
| 78 | + .setAuthenticationType(AuthenticationParameters.AuthenticationTypeEnum.BEARER) |
| 79 | + .setBearerToken("xxx") |
| 80 | + .build(); |
| 81 | + |
| 82 | + IcebergRestConnectionConfigInfo connectionConfig = |
| 83 | + IcebergRestConnectionConfigInfo.builder() |
| 84 | + .setConnectionType(ConnectionConfigInfo.ConnectionTypeEnum.ICEBERG_REST) |
| 85 | + .setAuthenticationParameters(authParams) |
| 86 | + .setUri("http://localhost:8080/api/v1/catalogs") |
| 87 | + .setRemoteCatalogName("remote-catalog") |
| 88 | + .build(); |
| 89 | + |
| 90 | + // Register the catalog in the test environment |
| 91 | + testServices |
| 92 | + .catalogsApi() |
| 93 | + .createCatalog( |
| 94 | + new CreateCatalogRequest( |
| 95 | + ExternalCatalog.builder() |
| 96 | + .setName(FEDERATED_CATALOG_NAME) |
| 97 | + .setProperties( |
| 98 | + CatalogProperties.builder().setDefaultBaseLocation(storageLocation).build()) |
| 99 | + .setConnectionConfigInfo(connectionConfig) |
| 100 | + .setStorageConfigInfo(storageConfig) |
| 101 | + .build()), |
| 102 | + testServices.realmContext(), |
| 103 | + testServices.securityContext()); |
| 104 | + } |
| 105 | + |
| 106 | + @ParameterizedTest(name = "[{index}] initialPageToken={0}, pageSize={1}") |
| 107 | + @MethodSource("paginationTestCases") |
| 108 | + void testPaginationForNonIcebergCatalog(String initialPageToken, Integer pageSize) |
| 109 | + throws IOException { |
| 110 | + |
| 111 | + try (InMemoryCatalog inMemoryCatalog = new InMemoryCatalog()) { |
| 112 | + // Initialize and replace the default handler with one backed by in-memory catalog |
| 113 | + inMemoryCatalog.initialize("inMemory", Map.of()); |
| 114 | + mockCatalogAdapter(inMemoryCatalog); |
| 115 | + |
| 116 | + // Set up 10 entities in the catalog: 10 namespaces, 10 tables, 10 views |
| 117 | + int entityCount = 10; |
| 118 | + for (int i = 0; i < entityCount; ++i) { |
| 119 | + inMemoryCatalog.createNamespace(Namespace.of("ns" + i)); |
| 120 | + inMemoryCatalog.createTable(TableIdentifier.of("ns0", "table" + i), new Schema()); |
| 121 | + inMemoryCatalog |
| 122 | + .buildView(TableIdentifier.of("ns0", "view" + i)) |
| 123 | + .withSchema(new Schema()) |
| 124 | + .withDefaultNamespace(Namespace.of("ns0")) |
| 125 | + .withQuery("a", "SELECT * FROM ns0.table" + i) |
| 126 | + .create(); |
| 127 | + } |
| 128 | + |
| 129 | + // Determine starting index for pagination based on the initial page token |
| 130 | + int pageStart = |
| 131 | + Strings.isNullOrEmpty(initialPageToken) ? 0 : Integer.parseInt(initialPageToken); |
| 132 | + int remain = entityCount - pageStart; |
| 133 | + |
| 134 | + // Initial tokens for pagination |
| 135 | + String listNamespacePageToken = initialPageToken; |
| 136 | + String listTablesPageToken = initialPageToken; |
| 137 | + String listViewsPageToken = initialPageToken; |
| 138 | + |
| 139 | + // Simulate page-by-page fetching until all entities are consumed |
| 140 | + while (remain > 0) { |
| 141 | + int expectedSize = |
| 142 | + (pageSize != null && initialPageToken != null) ? Math.min(remain, pageSize) : remain; |
| 143 | + |
| 144 | + // Verify namespaces pagination |
| 145 | + ListNamespacesResponse namespacesResponse = |
| 146 | + (ListNamespacesResponse) |
| 147 | + catalogAdapter |
| 148 | + .listNamespaces( |
| 149 | + FEDERATED_CATALOG_NAME, |
| 150 | + listNamespacePageToken, |
| 151 | + pageSize, |
| 152 | + null, |
| 153 | + testServices.realmContext(), |
| 154 | + testServices.securityContext()) |
| 155 | + .getEntity(); |
| 156 | + Assertions.assertThat(namespacesResponse.namespaces()).hasSize(expectedSize); |
| 157 | + listNamespacePageToken = namespacesResponse.nextPageToken(); |
| 158 | + |
| 159 | + // Verify tables pagination |
| 160 | + ListTablesResponse tablesResponse = |
| 161 | + (ListTablesResponse) |
| 162 | + catalogAdapter |
| 163 | + .listTables( |
| 164 | + FEDERATED_CATALOG_NAME, |
| 165 | + "ns0", |
| 166 | + listTablesPageToken, |
| 167 | + pageSize, |
| 168 | + testServices.realmContext(), |
| 169 | + testServices.securityContext()) |
| 170 | + .getEntity(); |
| 171 | + Assertions.assertThat(tablesResponse.identifiers()).hasSize(expectedSize); |
| 172 | + listTablesPageToken = tablesResponse.nextPageToken(); |
| 173 | + |
| 174 | + // Verify views pagination |
| 175 | + ListTablesResponse viewsResponse = |
| 176 | + (ListTablesResponse) |
| 177 | + catalogAdapter |
| 178 | + .listViews( |
| 179 | + FEDERATED_CATALOG_NAME, |
| 180 | + "ns0", |
| 181 | + listViewsPageToken, |
| 182 | + pageSize, |
| 183 | + testServices.realmContext(), |
| 184 | + testServices.securityContext()) |
| 185 | + .getEntity(); |
| 186 | + Assertions.assertThat(viewsResponse.identifiers()).hasSize(expectedSize); |
| 187 | + listViewsPageToken = viewsResponse.nextPageToken(); |
| 188 | + |
| 189 | + remain -= expectedSize; |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + private void mockCatalogAdapter(org.apache.iceberg.catalog.Catalog catalog) { |
| 195 | + // Override handler creation to inject in-memory catalog and suppress actual close() |
| 196 | + Mockito.doAnswer( |
| 197 | + invocation -> { |
| 198 | + IcebergCatalogHandler realHandler = |
| 199 | + (IcebergCatalogHandler) invocation.callRealMethod(); |
| 200 | + IcebergCatalogHandler wrappedHandler = Mockito.spy(realHandler); |
| 201 | + |
| 202 | + // Override initializeCatalog to inject test catalog using reflection |
| 203 | + Mockito.doAnswer( |
| 204 | + innerInvocation -> { |
| 205 | + for (String fieldName : |
| 206 | + List.of("baseCatalog", "namespaceCatalog", "viewCatalog")) { |
| 207 | + Field field = IcebergCatalogHandler.class.getDeclaredField(fieldName); |
| 208 | + field.setAccessible(true); |
| 209 | + field.set(wrappedHandler, catalog); |
| 210 | + } |
| 211 | + return null; |
| 212 | + }) |
| 213 | + .when(wrappedHandler) |
| 214 | + .initializeCatalog(); |
| 215 | + |
| 216 | + // Prevent catalog from being closed during test lifecycle |
| 217 | + Mockito.doNothing().when(wrappedHandler).close(); |
| 218 | + |
| 219 | + return wrappedHandler; |
| 220 | + }) |
| 221 | + .when(catalogAdapter) |
| 222 | + .newHandlerWrapper(Mockito.any(), Mockito.any()); |
| 223 | + } |
| 224 | + |
| 225 | + private static Stream<Arguments> paginationTestCases() { |
| 226 | + return Stream.of( |
| 227 | + Arguments.of(null, null), |
| 228 | + Arguments.of(null, 1), |
| 229 | + Arguments.of(null, 3), |
| 230 | + Arguments.of(null, 5), |
| 231 | + Arguments.of(null, 10), |
| 232 | + Arguments.of(null, 20), |
| 233 | + Arguments.of("", null), |
| 234 | + Arguments.of("", 1), |
| 235 | + Arguments.of("", 3), |
| 236 | + Arguments.of("", 5), |
| 237 | + Arguments.of("", 10), |
| 238 | + Arguments.of("", 20), |
| 239 | + Arguments.of("5", null), |
| 240 | + Arguments.of("5", 1), |
| 241 | + Arguments.of("5", 3), |
| 242 | + Arguments.of("5", 5), |
| 243 | + Arguments.of("5", 10)); |
| 244 | + } |
| 245 | +} |
0 commit comments