use of com.quorum.tessera.config.JdbcConfig in project tessera by ConsenSys.
the class HikariDataSourceFactoryTest method createWithEncryptedDatabasePassword.
@Test
public void createWithEncryptedDatabasePassword() {
String username = "junit";
String encryptedPassword = // unencrypted value = "dbpassword"
"ENC(rJ70hNidkrpkTwHoVn2sGSp3h3uBWxjb)";
String url = "jdbc:h2:mem:";
JdbcConfig jdbcConfig = mock(JdbcConfig.class);
when(jdbcConfig.getUsername()).thenReturn(username);
when(jdbcConfig.getPassword()).thenReturn(encryptedPassword);
when(jdbcConfig.getUrl()).thenReturn(url);
InputStream sysInReset = System.in;
ByteArrayInputStream in = new ByteArrayInputStream("quorum".getBytes());
System.setIn(in);
DataSource dataSource = dataSourceFactory.create(jdbcConfig);
System.setIn(sysInReset);
assertThat(dataSource).isNotNull().isExactlyInstanceOf(HikariDataSource.class);
HikariDataSource hikariDataSource = HikariDataSource.class.cast(dataSource);
assertThat(hikariDataSource.getJdbcUrl()).isEqualTo(url);
assertThat(hikariDataSource.getUsername()).isEqualTo(username);
assertThat(hikariDataSource.getPassword()).isEqualTo("dbpassword");
}
use of com.quorum.tessera.config.JdbcConfig in project tessera by ConsenSys.
the class EncryptedRawTransactionDAOProviderTest method provides.
@Test
public void provides() {
try (var mockedConfigFactory = mockStatic(ConfigFactory.class);
var mockedDataSourceFactory = mockStatic(DataSourceFactory.class);
var mockedPersistence = mockStatic(Persistence.class)) {
mockedPersistence.when(() -> Persistence.createEntityManagerFactory(anyString(), anyMap())).thenReturn(mock(EntityManagerFactory.class));
Config config = mock(Config.class);
JdbcConfig jdbcConfig = mock(JdbcConfig.class);
when(jdbcConfig.isAutoCreateTables()).thenReturn(autocreateTables);
when(config.getJdbcConfig()).thenReturn(jdbcConfig);
ConfigFactory configFactory = mock(ConfigFactory.class);
when(configFactory.getConfig()).thenReturn(config);
mockedConfigFactory.when(ConfigFactory::create).thenReturn(configFactory);
mockedDataSourceFactory.when(DataSourceFactory::create).thenReturn(mock(DataSourceFactory.class));
EncryptedRawTransactionDAO result = EncryptedRawTransactionDAOProvider.provider();
assertThat(result).isNotNull().isExactlyInstanceOf(EncryptedRawTransactionDAOImpl.class);
mockedPersistence.verify(() -> Persistence.createEntityManagerFactory(anyString(), anyMap()));
mockedPersistence.verifyNoMoreInteractions();
EncryptedTransactionDAOProvider.provider();
}
}
use of com.quorum.tessera.config.JdbcConfig in project tessera by ConsenSys.
the class MigrationTest method beforeTest.
@Before
public void beforeTest() throws IOException {
Config primaryConfig = new Config();
primaryConfig.setJdbcConfig(new JdbcConfig());
primaryConfig.getJdbcConfig().setUsername("junit");
primaryConfig.getJdbcConfig().setPassword("junit");
String primaryJdbcUrl = "jdbc:h2:" + workDir.getRoot().toPath().resolve("primary.db").toString();
primaryConfig.getJdbcConfig().setUrl(primaryJdbcUrl);
Config secondaryConfig = new Config();
secondaryConfig.setJdbcConfig(new JdbcConfig());
secondaryConfig.getJdbcConfig().setUsername("junit");
secondaryConfig.getJdbcConfig().setPassword("junit");
String secondaryJdbcUrl = "jdbc:h2:" + workDir.getRoot().toPath().resolve("secondary.db").toString();
secondaryConfig.getJdbcConfig().setUrl(secondaryJdbcUrl);
primaryConfigPath = workDir.getRoot().toPath().toAbsolutePath().resolve("primary-confg.json");
try (OutputStream outputStream = Files.newOutputStream(primaryConfigPath)) {
JaxbUtil.marshalWithNoValidation(primaryConfig, outputStream);
}
secondaryConfigPath = workDir.getRoot().toPath().toAbsolutePath().resolve("secondary-confg.json");
try (OutputStream outputStream = Files.newOutputStream(secondaryConfigPath)) {
JaxbUtil.marshalWithNoValidation(secondaryConfig, outputStream);
}
args = List.of("--primary", primaryConfigPath.toString(), "--secondary", secondaryConfigPath.toString());
primaryEntityManagerFactory = Optional.of(primaryConfig).map(Config::getJdbcConfig).map(JdbcConfigUtil::toMap).map(m -> new HashMap(m)).map(p -> {
p.put("jakarta.persistence.schema-generation.database.action", "drop-and-create");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("tessera", p);
emf.createEntityManager();
return emf;
}).get();
secondaryEntityManagerFactory = Optional.of(secondaryConfig).map(Config::getJdbcConfig).map(JdbcConfigUtil::toMap).map(m -> new HashMap(m)).map(p -> {
p.put("jakarta.persistence.schema-generation.database.action", "create");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("tessera", p);
return emf;
}).get();
EntityManager secondaryEntityManager = secondaryEntityManagerFactory.createEntityManager();
secondaryEntityManager.getTransaction().begin();
IntStream.range(0, encryptedTransactionCount).forEach(i -> {
EncryptedTransaction encryptedTransaction = generateEncryptedTransaction();
secondaryEntityManager.persist(encryptedTransaction);
});
secondaryEntityManager.getTransaction().commit();
secondaryEntityManager.getTransaction().begin();
IntStream.range(0, encryptedRawTransactionCount).forEach(i -> {
EncryptedRawTransaction encryptedRawTransaction = generateEncryptedRawTransaction();
secondaryEntityManager.persist(encryptedRawTransaction);
});
secondaryEntityManager.getTransaction().commit();
}
Aggregations