Skip to content

Commit b65f6c9

Browse files
committed
test: add tests for JdbcCatalogMetadataRepository
1 parent fc12f7f commit b65f6c9

14 files changed

Lines changed: 304 additions & 288 deletions

File tree

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
package kasanari.catalog.management.dto;
22

33

4+
import java.util.HashMap;
5+
import java.util.Map;
6+
47
public class CatalogSpecDto {
5-
private CatalogTypeDto type;
6-
private IcebergCatalogSpecModeConfigDto modeConfig;
8+
private Map<String, String> fileIoProperties = new HashMap<>();
9+
private Map<String, String> catalogProperties = new HashMap<>();
10+
private String endpoint;
11+
12+
public Map<String, String> getFileIoProperties() {
13+
return fileIoProperties;
14+
}
15+
16+
public void setFileIoProperties(Map<String, String> fileIoProperties) {
17+
this.fileIoProperties = fileIoProperties;
18+
}
719

8-
public CatalogTypeDto getType() {
9-
return type;
20+
public Map<String, String> getCatalogProperties() {
21+
return catalogProperties;
1022
}
1123

12-
public void setType(CatalogTypeDto type) {
13-
this.type = type;
24+
public void setCatalogProperties(Map<String, String> catalogProperties) {
25+
this.catalogProperties = catalogProperties;
1426
}
1527

16-
public IcebergCatalogSpecModeConfigDto getModeConfig() {
17-
return modeConfig;
28+
public String getEndpoint() {
29+
return endpoint;
1830
}
1931

20-
public void setModeConfig(IcebergCatalogSpecModeConfigDto modeConfig) {
21-
this.modeConfig = modeConfig;
32+
public void setEndpoint(String endpoint) {
33+
this.endpoint = endpoint;
2234
}
2335
}

modules/api/api-management/src/main/java/kasanari/catalog/management/dto/IcebergCatalogSpecModeConfigDto.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

modules/fixtures/fixtures-postgres/src/testFixtures/java/kasanari/fixtures/postgres/PostgresFixtureContainer.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package kasanari.fixtures.postgres;
22

3+
import com.github.dockerjava.api.model.HealthCheck;
34
import org.testcontainers.containers.Network;
45
import org.testcontainers.containers.PostgreSQLContainer;
6+
import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy;
7+
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;
8+
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
59
import org.testcontainers.utility.DockerImageName;
610

11+
import java.time.Duration;
12+
import java.time.temporal.ChronoUnit;
13+
714
public class PostgresFixtureContainer {
815
private static final int POSTGRES_INTERNAL_PORT = 5432;
916
private static final String NETWORK_ALIAS = "postgres";
@@ -19,6 +26,13 @@ public PostgresFixtureContainer(Network network) {
1926
DockerImageName
2027
.parse("postgres:17")
2128
.asCompatibleSubstituteFor("postgres")
29+
).waitingFor(
30+
new WaitAllStrategy()
31+
.withStrategy(new LogMessageWaitStrategy()
32+
.withRegEx(".*database system is ready to accept connections.*\\s")
33+
.withTimes(2)
34+
.withStartupTimeout(Duration.of(60, ChronoUnit.SECONDS)))
35+
.withStrategy(new HostPortWaitStrategy().forPorts(POSTGRES_INTERNAL_PORT))
2236
);
2337

2438
if (network != null) {
@@ -43,7 +57,9 @@ public void stop() {
4357
postgres.stop();
4458
}
4559

46-
/** JDBC URL reachable from the host (mapped port). */
60+
/**
61+
* JDBC URL reachable from the host (mapped port).
62+
*/
4763
public String jdbcUrl() {
4864
return postgres.getJdbcUrl();
4965
}

modules/management/management-catalog/src/main/java/kasanari/management/catalog/ManagementCatalogService.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import kasanari.repository.management.catalog.model.CatalogSpec;
1010
import kasanari.repository.management.catalog.postgres.JdbcCatalogMetadataRepository;
1111
import kasanari.repository.management.catalog.postgres.JdbcManagementCatalogQueries;
12+
import kasanari.repository.management.common.model.CatalogType;
1213
import org.jdbi.v3.core.Handle;
1314

1415
import java.util.Optional;
@@ -31,15 +32,15 @@ public boolean create(CatalogMetadata metadata) {
3132
return txManager.inTransactionR(tx -> catalogRepository.create(tx, metadata));
3233
}
3334

34-
public boolean delete(String catalogId) {
35-
return txManager.inTransactionR(tx -> catalogRepository.delete(tx, catalogId));
35+
public boolean delete(CatalogType catalogType, String catalogName) {
36+
return txManager.inTransactionR(tx -> catalogRepository.delete(tx, catalogType, catalogName));
3637
}
3738

38-
public Optional<CatalogMetadata> get(String catalogId) {
39-
return txManager.inTransactionR(tx -> catalogRepository.getById(tx, catalogId));
39+
public Optional<CatalogMetadata> get(CatalogType catalogType, String catalogName) {
40+
return txManager.inTransactionR(tx -> catalogRepository.getByName(tx, catalogType, catalogName));
4041
}
4142

42-
public Optional<CatalogMetadata> update(String catalogId, CatalogSpec spec, Long expectedVersion) {
43-
return txManager.inTransactionR(tx -> catalogRepository.update(tx, catalogId, spec, expectedVersion));
43+
public Optional<CatalogMetadata> update(CatalogType catalogType, String catalogName, CatalogSpec spec, Long expectedVersion) {
44+
return txManager.inTransactionR(tx -> catalogRepository.update(tx, catalogType, catalogName, spec, expectedVersion));
4445
}
4546
}

modules/repository/repository-management/repository-management-catalog/repository-management-catalog-postgres/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ dependencies {
77
implementation(project(":modules:repository:repository-jdbc"))
88
api(project(":modules:repository:repository-management:repository-management-catalog"))
99
implementation(project(":modules:platform:platform-jackson"))
10+
11+
testImplementation(platform(libs.junit.bom))
12+
testImplementation("org.junit.jupiter:junit-jupiter")
13+
testImplementation(testFixtures(project(":modules:fixtures:fixtures-common")))
14+
testImplementation(testFixtures(project(":modules:fixtures:fixtures-postgres")))
1015
}

modules/repository/repository-management/repository-management-catalog/repository-management-catalog-postgres/src/main/java/kasanari/repository/management/catalog/postgres/JdbcCatalogMetadataRepository.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ public JdbcCatalogMetadataRepository(ObjectMapper objectMapper) {
1919
}
2020

2121
@Override
22-
public Optional<CatalogMetadata> getById(Handle tx, String catalogId) {
22+
public Optional<CatalogMetadata> getByName(Handle tx, CatalogType catalogType, String catalogName) {
2323
var query = tx.createQuery(JdbcManagementCatalogQueries.SELECT_CATALOG);
24-
query.bind(0, catalogId);
24+
query.bind(0, catalogType.toString());
25+
query.bind(1, catalogName);
2526

2627
return query.map((rs, ctx) -> {
2728
try {
2829
var spec = objectMapper.readValue(rs.getString("spec_json"), CatalogSpec.class);
2930
return new CatalogMetadata(
30-
rs.getString("catalog_id"),
31+
rs.getString("catalog_name"),
3132
CatalogType.fromValue(rs.getString("catalog_type")),
3233
CatalogMode.fromValue(rs.getString("catalog_mode")),
3334
spec,
@@ -41,13 +42,13 @@ public Optional<CatalogMetadata> getById(Handle tx, String catalogId) {
4142

4243
@Override
4344
public boolean create(Handle tx, CatalogMetadata metadata) {
44-
if (getById(tx, metadata.catalogId()).isPresent()) {
45+
if (getByName(tx, metadata.catalogType(), metadata.catalogId()).isPresent()) {
4546
return false;
4647
}
4748

4849
var insert = tx.createUpdate(JdbcManagementCatalogQueries.INSERT_CATALOG);
49-
insert.bind(0, metadata.catalogId());
50-
insert.bind(1, metadata.catalogType().toString());
50+
insert.bind(0, metadata.catalogType().toString());
51+
insert.bind(1, metadata.catalogId());
5152
insert.bind(2, metadata.catalogMode().toString());
5253
insert.bind(3, serialize(metadata.spec()));
5354
insert.bind(4, metadata.version());
@@ -56,8 +57,8 @@ public boolean create(Handle tx, CatalogMetadata metadata) {
5657
}
5758

5859
@Override
59-
public Optional<CatalogMetadata> update(Handle tx, String catalogId, CatalogSpec spec, Long expectedVersion) {
60-
var current = getById(tx, catalogId);
60+
public Optional<CatalogMetadata> update(Handle tx, CatalogType catalogType, String catalogName, CatalogSpec spec, Long expectedVersion) {
61+
var current = getByName(tx, catalogType, catalogName);
6162

6263
if (current.isEmpty()) {
6364
return Optional.empty();
@@ -72,9 +73,13 @@ public Optional<CatalogMetadata> update(Handle tx, String catalogId, CatalogSpec
7273
var update = tx.createUpdate(JdbcManagementCatalogQueries.UPDATE_CATALOG);
7374
update.bind(0, serialize(spec));
7475
update.bind(1, nextVersion);
75-
update.bind(2, catalogId);
76-
update.bind(3, expectedVersion);
77-
update.execute();
76+
update.bind(2, catalogType.toString());
77+
update.bind(3, catalogName);
78+
update.bind(4, existing.version());
79+
var rows = update.execute();
80+
if (rows == 0) {
81+
throw new IllegalStateException("Catalog version does not match expected value");
82+
}
7883

7984
return Optional.of(new CatalogMetadata(
8085
existing.catalogId(),
@@ -86,9 +91,10 @@ public Optional<CatalogMetadata> update(Handle tx, String catalogId, CatalogSpec
8691
}
8792

8893
@Override
89-
public boolean delete(Handle tx, String catalogId) {
94+
public boolean delete(Handle tx, CatalogType catalogType, String catalogName) {
9095
var delete = tx.createUpdate(JdbcManagementCatalogQueries.DELETE_CATALOG);
91-
delete.bind(0, catalogId);
96+
delete.bind(0, catalogType.toString());
97+
delete.bind(1, catalogName);
9298
return delete.execute() > 0;
9399
}
94100

modules/repository/repository-management/repository-management-catalog/repository-management-catalog-postgres/src/main/java/kasanari/repository/management/catalog/postgres/JdbcManagementCatalogQueries.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,38 @@ private JdbcManagementCatalogQueries() {
55
}
66

77
public static final String CREATE_CATALOG_REGISTRY_DDL = """
8-
CREATE TABLE IF NOT EXISTS kasanari_catalog_registry
8+
CREATE TABLE IF NOT EXISTS kasanari_catalogs
99
(
10-
catalog_id TEXT PRIMARY KEY,
1110
catalog_type TEXT NOT NULL,
11+
catalog_name TEXT NOT NULL,
1212
catalog_mode TEXT NOT NULL,
13-
spec_json TEXT NOT NULL,
13+
spec_json JSON NOT NULL,
1414
version BIGINT NOT NULL DEFAULT 1,
1515
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
16-
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
16+
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
17+
CONSTRAINT kasanari_catalogs_pk PRIMARY KEY (catalog_type, catalog_name)
1718
)
1819
""";
1920

2021
public static final String INSERT_CATALOG = """
21-
INSERT INTO kasanari_catalog_registry(catalog_id, catalog_type, catalog_mode, spec_json, version)
22-
VALUES (?, ?, ?, ?, ?)
22+
INSERT INTO kasanari_catalogs(catalog_type, catalog_name, catalog_mode, spec_json, version)
23+
VALUES (?, ?, ?, ?::json, ?)
2324
""";
2425

25-
// todo: use CAS logic (version check)
2626
public static final String UPDATE_CATALOG = """
27-
UPDATE kasanari_catalog_registry
28-
SET spec_json = ?, version = ?, updated_at = CURRENT_TIMESTAMP
29-
WHERE catalog_id = ?
27+
UPDATE kasanari_catalogs
28+
SET spec_json = ?::json, version = ?, updated_at = CURRENT_TIMESTAMP
29+
WHERE catalog_type = ? AND catalog_name = ? AND version = ?
3030
""";
3131

32-
public static final String DELETE_CATALOG = "DELETE FROM kasanari_catalog_registry WHERE catalog_id = ?";
32+
public static final String DELETE_CATALOG = """
33+
DELETE FROM kasanari_catalogs
34+
WHERE catalog_type = ? AND catalog_name = ?
35+
""";
3336

3437
public static final String SELECT_CATALOG = """
35-
SELECT catalog_id, catalog_type, catalog_mode, spec_json, version
36-
FROM kasanari_catalog_registry
37-
WHERE catalog_id = ?
38+
SELECT catalog_type, catalog_name, catalog_mode, spec_json, version
39+
FROM kasanari_catalogs
40+
WHERE catalog_type = ? AND catalog_name = ?
3841
""";
3942
}

0 commit comments

Comments
 (0)