Skip to content

Commit

Permalink
Testing core functionality "set schema" of AbstractSchemaMultiTenantC…
Browse files Browse the repository at this point in the history
…onnectionProvider
  • Loading branch information
clean-coder committed Apr 22, 2024
1 parent d63508b commit 7ff7cdf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ public AbstractSchemaMultiTenantConnectionProvider() {
@Override
public Connection getConnection(String tenantIdentifier) throws SQLException {
Connection connection = super.getConnection(tenantIdentifier);
return getConnection(tenantIdentifier, connection);
}

protected Connection getConnection(String tenantIdentifier, Connection connection) throws SQLException {
String schema = convertTenantIdToSchemaName(tenantIdentifier);
logger.debug("Setting schema to {}", schema);

connection.createStatement().execute(String.format("SET SCHEMA '%s';", schema));
return connection;
}

private static String convertTenantIdToSchemaName(String tenantIdentifier) {
private String convertTenantIdToSchemaName(String tenantIdentifier) {
return Objects.equals(tenantIdentifier, DEFAULT_TENANT_ID) ? tenantIdentifier
: MessageFormat.format("okr_{0}", tenantIdentifier);
}
Expand Down Expand Up @@ -91,7 +94,7 @@ private ConnectionProvider initConnectionProvider(Properties hibernateProperties
return connectionProvider;
}

private static Map<String, Object> convertPropertiesToMap(Properties properties) {
private Map<String, Object> convertPropertiesToMap(Properties properties) {
Map<String, Object> configProperties = new HashMap<>();
for (String key : properties.stringPropertyNames()) {
String value = properties.getProperty(key);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ch.puzzle.okr.multitenancy;

import ch.puzzle.okr.test.SpringIntegrationTest;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@SpringIntegrationTest
public class SchemaMultiTenantConnectionProviderTest {
@Mock
Connection connection;
@Mock
Statement statement;

private static final String TENANT_ID = "pitc";

@Test
void testSetSchemaOfTenant() throws SQLException {
when(connection.createStatement()).thenReturn(statement);

AbstractSchemaMultiTenantConnectionProvider provider = new AbstractSchemaMultiTenantConnectionProvider() {
@Override
protected String getHibernatePropertiesFilePaths() {
return null;
}
};
provider.getConnection(TENANT_ID, connection);

verify(statement).execute("SET SCHEMA 'okr_" + TENANT_ID + "';");
}

}

0 comments on commit 7ff7cdf

Please sign in to comment.