Search in sources :

Example 1 with StagingEntityDAO

use of com.quorum.tessera.data.staging.StagingEntityDAO in project tessera by ConsenSys.

the class StagingEntityDAOProviderTest method provider.

@Test
public void provider() {
    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));
        StagingEntityDAO result = StagingEntityDAOProvider.provider();
        assertThat(result).isNotNull().isExactlyInstanceOf(StagingEntityDAOImpl.class);
        mockedPersistence.verify(() -> Persistence.createEntityManagerFactory(anyString(), anyMap()));
        mockedPersistence.verifyNoMoreInteractions();
    }
}
Also used : DataSourceFactory(com.quorum.tessera.data.DataSourceFactory) JdbcConfig(com.quorum.tessera.config.JdbcConfig) Config(com.quorum.tessera.config.Config) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) StagingEntityDAO(com.quorum.tessera.data.staging.StagingEntityDAO) JdbcConfig(com.quorum.tessera.config.JdbcConfig) ConfigFactory(com.quorum.tessera.config.ConfigFactory) Test(org.junit.Test)

Example 2 with StagingEntityDAO

use of com.quorum.tessera.data.staging.StagingEntityDAO in project tessera by ConsenSys.

the class StagingEntityDAOProvider method provider.

public static StagingEntityDAO provider() {
    LOGGER.debug("Creating StagingEntityDAO");
    Config config = ConfigFactory.create().getConfig();
    final DataSource dataSource = DataSourceFactory.create().create(config.getJdbcConfig());
    Map properties = new HashMap();
    properties.put("jakarta.persistence.nonJtaDataSource", dataSource);
    properties.put("eclipselink.logging.logger", "org.eclipse.persistence.logging.slf4j.SLF4JLogger");
    properties.put("eclipselink.logging.level", "FINE");
    properties.put("eclipselink.logging.parameters", "true");
    properties.put("eclipselink.logging.level.sql", "FINE");
    properties.put("jakarta.persistence.schema-generation.database.action", config.getJdbcConfig().isAutoCreateTables() ? "drop-and-create" : "none");
    properties.put("eclipselink.session.customizer", "com.quorum.tessera.eclipselink.AtomicLongSequence");
    LOGGER.debug("Creating EntityManagerFactory from {}", properties);
    final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("tessera-recover", properties);
    LOGGER.debug("Created EntityManagerFactory from {}", properties);
    StagingEntityDAO stagingEntityDAO = new StagingEntityDAOImpl(entityManagerFactory);
    LOGGER.debug("Created StagingEntityDAO {}", stagingEntityDAO);
    return stagingEntityDAO;
}
Also used : HashMap(java.util.HashMap) Config(com.quorum.tessera.config.Config) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) StagingEntityDAO(com.quorum.tessera.data.staging.StagingEntityDAO) Map(java.util.Map) HashMap(java.util.HashMap) DataSource(javax.sql.DataSource)

Example 3 with StagingEntityDAO

use of com.quorum.tessera.data.staging.StagingEntityDAO in project tessera by ConsenSys.

the class BatchResendManagerImplTest method testStoreResendBatchMultipleVersions.

@Test
public void testStoreResendBatchMultipleVersions() {
    try (var payloadDigestMockedStatic = mockStatic(PayloadDigest.class);
        var payloadEncoderMockedStatic = mockStatic(PayloadEncoder.class)) {
        payloadDigestMockedStatic.when(PayloadDigest::create).thenReturn((PayloadDigest) cipherText -> cipherText);
        payloadEncoderMockedStatic.when(() -> PayloadEncoder.create(any())).thenReturn(payloadEncoder);
        final EncodedPayload encodedPayload = EncodedPayload.Builder.create().withSenderKey(publicKey).withCipherText("cipherText".getBytes()).withCipherTextNonce(new Nonce("nonce".getBytes())).withRecipientBoxes(singletonList("box".getBytes())).withRecipientNonce(new Nonce("recipientNonce".getBytes())).withRecipientKeys(singletonList(PublicKey.from("receiverKey".getBytes()))).withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).withAffectedContractTransactions(emptyMap()).withExecHash(new byte[0]).build();
        when(payloadEncoder.decode(any())).thenReturn(encodedPayload);
        final byte[] raw = new PayloadEncoderImpl().encode(encodedPayload);
        PushBatchRequest request = PushBatchRequest.from(List.of(raw), EncodedPayloadCodec.LEGACY);
        StagingTransaction existing = new StagingTransaction();
        when(stagingEntityDAO.retrieveByHash(any())).thenReturn(Optional.of(existing));
        when(stagingEntityDAO.update(any(StagingTransaction.class))).thenReturn(new StagingTransaction());
        manager.storeResendBatch(request);
        verify(stagingEntityDAO).save(any(StagingTransaction.class));
        verify(payloadEncoder).decode(any());
        verify(payloadEncoder).encodedPayloadCodec();
        payloadDigestMockedStatic.verify(PayloadDigest::create);
        payloadDigestMockedStatic.verifyNoMoreInteractions();
    }
}
Also used : AdditionalMatchers.lt(org.mockito.AdditionalMatchers.lt) ResendBatchRequest(com.quorum.tessera.recovery.resend.ResendBatchRequest) IntStream(java.util.stream.IntStream) PublicKey(com.quorum.tessera.encryption.PublicKey) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BatchWorkflowFactory(com.quorum.tessera.recovery.workflow.BatchWorkflowFactory) BatchResendManager(com.quorum.tessera.recovery.workflow.BatchResendManager) Base64Codec(com.quorum.tessera.base64.Base64Codec) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) BatchWorkflow(com.quorum.tessera.recovery.workflow.BatchWorkflow) Collections.singletonList(java.util.Collections.singletonList) PushBatchRequest(com.quorum.tessera.recovery.resend.PushBatchRequest) BatchWorkflowContext(com.quorum.tessera.recovery.workflow.BatchWorkflowContext) StagingEntityDAO(com.quorum.tessera.data.staging.StagingEntityDAO) EncryptedTransactionDAO(com.quorum.tessera.data.EncryptedTransactionDAO) After(org.junit.After) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) AdditionalMatchers.gt(org.mockito.AdditionalMatchers.gt) com.quorum.tessera.enclave(com.quorum.tessera.enclave) Nonce(com.quorum.tessera.encryption.Nonce) ServiceLoader(java.util.ServiceLoader) Test(org.junit.Test) StagingTransaction(com.quorum.tessera.data.staging.StagingTransaction) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) List(java.util.List) ResendBatchResponse(com.quorum.tessera.recovery.resend.ResendBatchResponse) Optional(java.util.Optional) Nonce(com.quorum.tessera.encryption.Nonce) PushBatchRequest(com.quorum.tessera.recovery.resend.PushBatchRequest) StagingTransaction(com.quorum.tessera.data.staging.StagingTransaction) Test(org.junit.Test)

Example 4 with StagingEntityDAO

use of com.quorum.tessera.data.staging.StagingEntityDAO in project tessera by ConsenSys.

the class BatchResendManagerProviderTest method provider.

@Test
public void provider() {
    try (var staticEncryptedTransactionDAO = mockStatic(EncryptedTransactionDAO.class);
        var staticStagingEntityDAO = mockStatic(StagingEntityDAO.class);
        var staticBatchWorkflowFactory = mockStatic(BatchWorkflowFactory.class)) {
        staticEncryptedTransactionDAO.when(EncryptedTransactionDAO::create).thenReturn(mock(EncryptedTransactionDAO.class));
        staticStagingEntityDAO.when(StagingEntityDAO::create).thenReturn(mock(StagingEntityDAO.class));
        staticBatchWorkflowFactory.when(BatchWorkflowFactory::create).thenReturn(mock(BatchWorkflowFactory.class));
        BatchResendManager batchResendManager = BatchResendManagerProvider.provider();
        assertThat(batchResendManager).isNotNull().isExactlyInstanceOf(BatchResendManagerImpl.class);
        staticEncryptedTransactionDAO.verify(EncryptedTransactionDAO::create);
        staticStagingEntityDAO.verify(StagingEntityDAO::create);
        staticBatchWorkflowFactory.verify(BatchWorkflowFactory::create);
        staticEncryptedTransactionDAO.verifyNoMoreInteractions();
        staticStagingEntityDAO.verifyNoMoreInteractions();
        staticBatchWorkflowFactory.verifyNoMoreInteractions();
        assertThat(BatchResendManagerHolder.INSTANCE.getBatchResendManager()).isPresent().containsSame(batchResendManager);
        assertThat(BatchResendManagerProvider.provider()).isSameAs(batchResendManager);
    }
}
Also used : BatchWorkflowFactory(com.quorum.tessera.recovery.workflow.BatchWorkflowFactory) BatchResendManager(com.quorum.tessera.recovery.workflow.BatchResendManager) StagingEntityDAO(com.quorum.tessera.data.staging.StagingEntityDAO) EncryptedTransactionDAO(com.quorum.tessera.data.EncryptedTransactionDAO) Test(org.junit.Test)

Example 5 with StagingEntityDAO

use of com.quorum.tessera.data.staging.StagingEntityDAO in project tessera by ConsenSys.

the class BatchResendManagerProvider method provider.

public static BatchResendManager provider() {
    if (BatchResendManagerHolder.INSTANCE.getBatchResendManager().isPresent()) {
        return BatchResendManagerHolder.INSTANCE.getBatchResendManager().get();
    }
    LOGGER.debug("Creating EncryptedTransactionDAO");
    final EncryptedTransactionDAO encryptedTransactionDAO = EncryptedTransactionDAO.create();
    LOGGER.debug("Created EncryptedTransactionDAO {}", encryptedTransactionDAO);
    LOGGER.debug("Creating StagingEntityDAO");
    final StagingEntityDAO stagingEntityDAO = StagingEntityDAO.create();
    LOGGER.debug("Created StagingEntityDAO");
    final int defaultMaxResults = 10000;
    BatchWorkflowFactory batchWorkflowFactory = BatchWorkflowFactory.create();
    BatchResendManager batchResendManager = new BatchResendManagerImpl(stagingEntityDAO, encryptedTransactionDAO, defaultMaxResults, batchWorkflowFactory);
    return BatchResendManagerHolder.INSTANCE.setBatchResendManager(batchResendManager);
}
Also used : BatchWorkflowFactory(com.quorum.tessera.recovery.workflow.BatchWorkflowFactory) BatchResendManager(com.quorum.tessera.recovery.workflow.BatchResendManager) StagingEntityDAO(com.quorum.tessera.data.staging.StagingEntityDAO) EncryptedTransactionDAO(com.quorum.tessera.data.EncryptedTransactionDAO)

Aggregations

StagingEntityDAO (com.quorum.tessera.data.staging.StagingEntityDAO)7 Test (org.junit.Test)4 EncryptedTransactionDAO (com.quorum.tessera.data.EncryptedTransactionDAO)3 BatchResendManager (com.quorum.tessera.recovery.workflow.BatchResendManager)3 BatchWorkflowFactory (com.quorum.tessera.recovery.workflow.BatchWorkflowFactory)3 Config (com.quorum.tessera.config.Config)2 Discovery (com.quorum.tessera.discovery.Discovery)2 BatchTransactionRequester (com.quorum.tessera.recovery.resend.BatchTransactionRequester)2 TransactionManager (com.quorum.tessera.transaction.TransactionManager)2 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)2 Base64Codec (com.quorum.tessera.base64.Base64Codec)1 ConfigFactory (com.quorum.tessera.config.ConfigFactory)1 JdbcConfig (com.quorum.tessera.config.JdbcConfig)1 DataSourceFactory (com.quorum.tessera.data.DataSourceFactory)1 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)1 StagingTransaction (com.quorum.tessera.data.staging.StagingTransaction)1 com.quorum.tessera.enclave (com.quorum.tessera.enclave)1 Nonce (com.quorum.tessera.encryption.Nonce)1 PublicKey (com.quorum.tessera.encryption.PublicKey)1 Recovery (com.quorum.tessera.recovery.Recovery)1