forked from opensearch-project/data-prepper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add model for table column metadata for Global state (opensearch-proj…
…ect#5136) Signed-off-by: Dinu John <[email protected]>
- Loading branch information
Showing
4 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...ce/src/main/java/org/opensearch/dataprepper/plugins/source/rds/model/DbTableMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.source.rds.model; | ||
|
||
import java.util.Map; | ||
|
||
public class DbTableMetadata { | ||
|
||
private static final String DB_METADATA_KEY = "dbMetadata"; | ||
private static final String TABLE_COLUMN_METADATA_KEY = "tableColumnDataTypeMap"; | ||
private final DbMetadata dbMetadata; | ||
private final Map<String, Map<String, String>> tableColumnDataTypeMap; | ||
|
||
public DbTableMetadata(final DbMetadata dbMetadata, final Map<String, Map<String, String>> tableColumnDataTypeMap) { | ||
this.dbMetadata = dbMetadata; | ||
this.tableColumnDataTypeMap = tableColumnDataTypeMap; | ||
} | ||
|
||
public DbMetadata getDbMetadata() { | ||
return dbMetadata; | ||
} | ||
|
||
public Map<String, Map<String, String>> getTableColumnDataTypeMap() { | ||
return tableColumnDataTypeMap; | ||
} | ||
|
||
public Map<String, Object> toMap() { | ||
return Map.of( | ||
DB_METADATA_KEY, dbMetadata.toMap(), | ||
TABLE_COLUMN_METADATA_KEY, tableColumnDataTypeMap | ||
); | ||
} | ||
|
||
public static DbTableMetadata fromMap(Map<String, Object> map) { | ||
return new DbTableMetadata( | ||
DbMetadata.fromMap((Map<String, Object>)map.get(DB_METADATA_KEY)), | ||
(Map<String, Map<String, String>>) map.get(TABLE_COLUMN_METADATA_KEY) | ||
); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...rce/src/test/java/org/opensearch/dataprepper/plugins/source/rds/model/DbMetadataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.opensearch.dataprepper.plugins.source.rds.model; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import java.util.UUID; | ||
|
||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.CoreMatchers.is; | ||
|
||
public class DbMetadataTest { | ||
|
||
@Test | ||
public void test_fromMap_success() { | ||
final String dbIdentifier = UUID.randomUUID().toString(); | ||
final String hostName = UUID.randomUUID().toString(); | ||
final int port = new Random().nextInt(); | ||
final Map<String, Object> map = new HashMap<>(); | ||
map.put("dbIdentifier", dbIdentifier); | ||
map.put("hostName", hostName); | ||
map.put("port", port); | ||
|
||
final DbMetadata result = DbMetadata.fromMap(map); | ||
|
||
assertThat(result.getDbIdentifier(), is(dbIdentifier)); | ||
assertThat(result.getHostName(), is(hostName)); | ||
assertThat(result.getPort(), is(port)); | ||
} | ||
|
||
@Test | ||
public void test_toMap_success() { | ||
final String dbIdentifier = UUID.randomUUID().toString(); | ||
final String hostName = UUID.randomUUID().toString(); | ||
final int port = new Random().nextInt(); | ||
final DbMetadata dbMetadata = new DbMetadata(dbIdentifier, hostName, port); | ||
|
||
final Map<String, Object> result = dbMetadata.toMap(); | ||
|
||
assertThat(result, is(notNullValue())); | ||
assertThat(result.size(), is(3)); | ||
assertThat(result.get("dbIdentifier"), is(dbIdentifier)); | ||
assertThat(result.get("hostName"), is(hostName)); | ||
assertThat(result.get("port"), is(port)); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...rc/test/java/org/opensearch/dataprepper/plugins/source/rds/model/DbTableMetadataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.opensearch.dataprepper.plugins.source.rds.model; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import java.util.UUID; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
public class DbTableMetadataTest { | ||
|
||
@Test | ||
public void test_fromMap_success() { | ||
final String dbIdentifier = UUID.randomUUID().toString(); | ||
final String hostName = UUID.randomUUID().toString(); | ||
final int port = new Random().nextInt(); | ||
final String tableName = UUID.randomUUID().toString(); | ||
|
||
final DbMetadata dbMetadata = new DbMetadata(dbIdentifier, hostName, port); | ||
final Map<String, Map<String, String>> tableColumnDataTypeMap = new HashMap<>(); | ||
final Map<String, String> columnDataTypeMap = new HashMap<>(); | ||
columnDataTypeMap.put("int_column", "INTEGER"); | ||
tableColumnDataTypeMap.put(tableName, columnDataTypeMap); | ||
|
||
|
||
final Map<String, Object> map = new HashMap<>(); | ||
map.put("dbMetadata", dbMetadata.toMap()); | ||
map.put("tableColumnDataTypeMap", tableColumnDataTypeMap); | ||
|
||
final DbTableMetadata result = DbTableMetadata.fromMap(map); | ||
|
||
assertThat(result.getDbMetadata().getDbIdentifier(), is(dbIdentifier)); | ||
assertThat(result.getDbMetadata().getHostName(), is(hostName)); | ||
assertThat(result.getDbMetadata().getPort(), is(port)); | ||
assertThat(result.getTableColumnDataTypeMap(), is(tableColumnDataTypeMap)); | ||
} | ||
|
||
@Test | ||
public void test_toMap_success() { | ||
final String dbIdentifier = UUID.randomUUID().toString(); | ||
final String hostName = UUID.randomUUID().toString(); | ||
final int port = new Random().nextInt(); | ||
final String tableName = UUID.randomUUID().toString(); | ||
|
||
final DbMetadata dbMetadata = new DbMetadata(dbIdentifier, hostName, port); | ||
final Map<String, Map<String, String>> tableColumnDataTypeMap = new HashMap<>(); | ||
final Map<String, String> columnDataTypeMap = new HashMap<>(); | ||
columnDataTypeMap.put("int_column", "INTEGER"); | ||
tableColumnDataTypeMap.put(tableName, columnDataTypeMap); | ||
final DbTableMetadata dbTableMetadata = new DbTableMetadata(dbMetadata, tableColumnDataTypeMap); | ||
|
||
final Map<String, Object> result = dbTableMetadata.toMap(); | ||
|
||
assertThat(result, is(notNullValue())); | ||
assertThat(result.size(), is(2)); | ||
assertThat(result.get("dbMetadata"), is(dbMetadata.toMap())); | ||
assertThat(result.get("tableColumnDataTypeMap"), is(tableColumnDataTypeMap)); | ||
} | ||
} |