EncryptMetaDataLoader implements RuleMetaDataLoader<EncryptRule> { @Override public SchemaMetaData load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final DataNodes dataNodes, final EncryptRule encryptRule, final ConfigurationProperties props, final Collection<String> excludedTableNames) throws SQLException { DataSource dataSource = dataSourceMap.values().iterator().next(); Collection<String> encryptTableNames = encryptRule.getEncryptTableNames(); Map<String, TableMetaData> result = new HashMap<>(encryptTableNames.size(), 1); for (String each : encryptTableNames) { if (!excludedTableNames.contains(each)) { TableMetaDataLoader.load(dataSource, each, databaseType.getName()).ifPresent(tableMetaData -> result.put(each, tableMetaData)); } } return new SchemaMetaData(result); }  @Override SchemaMetaData load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final DataNodes dataNodes,
                               final EncryptRule encryptRule, final ConfigurationProperties props, final Collection<String> excludedTableNames); @Override Optional<TableMetaData> load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final DataNodes dataNodes, 
                                        final String tableName, final EncryptRule encryptRule, final ConfigurationProperties props); @Override int getOrder(); @Override Class<EncryptRule> getTypeClass();  }
@Test public void assertLoad() throws SQLException { EncryptRule rule = createEncryptRule(); EncryptMetaDataLoader loader = (EncryptMetaDataLoader) OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(rule), RuleMetaDataLoader.class).get(rule); SchemaMetaData actual = loader.load(databaseType, Collections.singletonMap("logic_db", dataSource), new DataNodes(Collections.singletonList(rule)), rule, props, Collections.emptyList()); assertThat(actual.get(TABLE_NAME).getColumnMetaData(0).getName(), is("id")); assertThat(actual.get(TABLE_NAME).getColumnMetaData(1).getName(), is("pwd_cipher")); assertThat(actual.get(TABLE_NAME).getColumnMetaData(2).getName(), is("pwd_plain")); }
@Test public void assertLoadByExistedTable() throws SQLException { EncryptRule rule = createEncryptRule(); EncryptMetaDataLoader loader = (EncryptMetaDataLoader) OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(rule), RuleMetaDataLoader.class).get(rule); Optional<TableMetaData> actual = loader.load(databaseType, Collections.singletonMap("logic_db", dataSource), new DataNodes(Collections.singletonList(rule)), TABLE_NAME, rule, props); assertTrue(actual.isPresent()); assertThat(actual.get().getColumnMetaData(0).getName(), is("id")); assertThat(actual.get().getColumnMetaData(1).getName(), is("pwd_cipher")); assertThat(actual.get().getColumnMetaData(2).getName(), is("pwd_plain")); }
@Test public void assertLoadByNotExistedTable() throws SQLException { EncryptRule rule = createEncryptRule(); EncryptMetaDataLoader loader = new EncryptMetaDataLoader(); Optional<TableMetaData> actual = loader.load(databaseType, Collections.singletonMap("logic_db", dataSource), new DataNodes(Collections.singletonList(rule)), "not_existed_table", rule, props); assertFalse(actual.isPresent()); }