Fineract with ReadOnly tenant database connection

This commit is contained in:
Jose Alberto Hernandez
2022-06-14 09:15:12 +02:00
committed by Arnold Gálovics
parent 7cd9ff879a
commit 2a7d8b9496
14 changed files with 640 additions and 215 deletions
@@ -0,0 +1,147 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.core;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.fineract.infrastructure.core.config.FineractProperties;
import org.apache.fineract.infrastructure.core.db.DatabaseConnectionProperties;
import org.apache.fineract.infrastructure.core.domain.FineractPlatformTenant;
import org.apache.fineract.infrastructure.core.domain.FineractPlatformTenantConnection;
import org.apache.fineract.infrastructure.core.service.DataSourcePerTenantServiceFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.test.context.TestPropertySource;
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@TestPropertySource("classpath:application-test.properties")
public class TomcatJdbcDataSourcePerTenantServiceTest {
@Mock
private FineractProperties fineractProperties;
@Mock
private HikariConfig hikariConfig;
@Mock
private Connection connection;
@Mock
private FineractPlatformTenant defaultTenant;
@Mock
private FineractPlatformTenantConnection tenantConnection;
@InjectMocks
private DataSourcePerTenantServiceFactory underTest;
private DataSource dataSource;
private DataSource tenantDataSource;
private DatabaseConnectionProperties databaseConnectionProperties;
@BeforeEach
void setUp() throws SQLException {
this.databaseConnectionProperties = DatabaseConnectionProperties.instance(System.getProperty("dbType"));
tenantDataSource = mock(HikariDataSource.class);
given(tenantDataSource.getConnection()).willReturn(connection);
given(connection.getMetaData()).willReturn(mock(DatabaseMetaData.class));
given(connection.getMetaData().getURL()).willReturn(databaseConnectionProperties.getJdbcUrl());
given(tenantConnection.getSchemaServer()).willReturn(databaseConnectionProperties.getSchemaServer());
given(tenantConnection.getSchemaServerPort()).willReturn(databaseConnectionProperties.getSchemaPort());
given(tenantConnection.getSchemaUsername()).willReturn(databaseConnectionProperties.getSchemaUsername());
given(tenantConnection.getSchemaPassword()).willReturn(databaseConnectionProperties.getSchemaPassword());
given(tenantConnection.getSchemaName()).willReturn(databaseConnectionProperties.getSchemaName());
given(defaultTenant.getConnection()).willReturn(tenantConnection);
given(defaultTenant.getConnection().getMaxActive()).willReturn(5);
given(defaultTenant.getConnection().getValidationInterval()).willReturn(500L);
given(hikariConfig.getInitializationFailTimeout()).willReturn(0L);
given(hikariConfig.getDriverClassName()).willReturn(databaseConnectionProperties.getDriverClassName());
given(hikariConfig.getConnectionTestQuery()).willReturn(databaseConnectionProperties.getConnectionTestQuery());
given(hikariConfig.getDataSourceProperties()).willReturn(mock(Properties.class));
given(hikariConfig.isAutoCommit()).willReturn(true);
}
@Test
void testRetrieveDataSource_ShouldCreateDataSource_WhenFineractIsInAllMode() {
// given
FineractProperties.FineractModeProperties modeProperties = createModeProps(true, true, true);
given(fineractProperties.getMode()).willReturn(modeProperties);
// when
dataSource = underTest.createNewDataSourceFor(tenantDataSource, defaultTenant.getConnection());
// then
assertNotNull(dataSource);
}
@Test
void testRetrieveDataSource_ShouldCreateDataSource_WhenFineractIsInReadOnlyMode() {
// given
FineractProperties.FineractModeProperties modeProperties = createModeProps(true, false, false);
given(fineractProperties.getMode()).willReturn(modeProperties);
// when
dataSource = underTest.createNewDataSourceFor(tenantDataSource, defaultTenant.getConnection());
// then
assertNotNull(dataSource);
}
@Test
void testRetrieveDataSource_ShouldCreateDataSource_WhenFineractIsInBatchMode() {
// given
FineractProperties.FineractModeProperties modeProperties = createModeProps(false, false, true);
given(fineractProperties.getMode()).willReturn(modeProperties);
// when
dataSource = underTest.createNewDataSourceFor(tenantDataSource, defaultTenant.getConnection());
// then
assertNotNull(dataSource);
}
private FineractProperties.FineractModeProperties createModeProps(boolean readEnabled, boolean writeEnabled, boolean batchEnabled) {
FineractProperties.FineractModeProperties modeProperties = new FineractProperties.FineractModeProperties();
modeProperties.setReadEnabled(readEnabled);
modeProperties.setWriteEnabled(writeEnabled);
modeProperties.setBatchEnabled(batchEnabled);
return modeProperties;
}
}
@@ -0,0 +1,111 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.core.db;
public class DatabaseConnectionProperties {
private String driverClassName;
private String jdbcUrl;
private String connectionTestQuery;
private String schemaServer;
private String schemaName;
private String schemaPort;
private String schemaUsername;
private String schemaPassword;
protected DatabaseConnectionProperties(final String driverClassName, final String jdbcUrl, final String schemaPort,
final String schemaPassword) {
this.driverClassName = driverClassName;
this.jdbcUrl = jdbcUrl;
this.schemaPort = schemaPort;
this.schemaPassword = schemaPassword;
this.schemaServer = "localhost";
this.schemaUsername = "root";
this.schemaName = "fineract_tenants";
this.connectionTestQuery = "SELECT 1";
}
public static DatabaseConnectionProperties instance(final String dbType) {
return new DatabaseConnectionProperties(getDbDriverClassName(dbType), buildJdbcUrl(dbType), getDbPort(dbType),
getDbPassword(dbType));
}
private static String getDbDriverClassName(final String dbType) {
if (dbType.equals("mariadb")) {
return "org.mariadb.jdbc.Driver";
} else if (dbType.equals("mysql")) {
return "com.mysql.cj.jdbc.Driver";
} else {
return "org.postgresql.Driver";
}
}
private static String buildJdbcUrl(final String dbType) {
return "jdbc:" + dbType + "://localhost:" + getDbPort(dbType) + "/fineract_tenants";
}
private static String getDbPort(final String dbType) {
if (dbType.equals("postgresql")) {
return "5432";
}
return "3306";
}
private static String getDbPassword(final String dbType) {
if (dbType.equals("postgresql")) {
return "postgres";
}
return "mysql";
}
public String getDriverClassName() {
return driverClassName;
}
public String getJdbcUrl() {
return jdbcUrl;
}
public String getConnectionTestQuery() {
return connectionTestQuery;
}
public String getSchemaServer() {
return schemaServer;
}
public String getSchemaName() {
return schemaName;
}
public String getSchemaPort() {
return schemaPort;
}
public String getSchemaUsername() {
return schemaUsername;
}
public String getSchemaPassword() {
return schemaPassword;
}
}