diff --git a/backend/src/main/java/ch/puzzle/okr/multitenancy/AbstractSchemaMultiTenantConnectionProvider.java b/backend/src/main/java/ch/puzzle/okr/multitenancy/AbstractSchemaMultiTenantConnectionProvider.java index 4c2a1161b9..2a77061a7d 100644 --- a/backend/src/main/java/ch/puzzle/okr/multitenancy/AbstractSchemaMultiTenantConnectionProvider.java +++ b/backend/src/main/java/ch/puzzle/okr/multitenancy/AbstractSchemaMultiTenantConnectionProvider.java @@ -29,7 +29,10 @@ 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); @@ -37,7 +40,7 @@ public Connection getConnection(String tenantIdentifier) throws SQLException { 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); } @@ -91,7 +94,7 @@ private ConnectionProvider initConnectionProvider(Properties hibernateProperties return connectionProvider; } - private static Map convertPropertiesToMap(Properties properties) { + private Map convertPropertiesToMap(Properties properties) { Map configProperties = new HashMap<>(); for (String key : properties.stringPropertyNames()) { String value = properties.getProperty(key); diff --git a/backend/src/test/java/ch/puzzle/okr/multitenancy/SchemaMultiTenantConnectionProviderTest.java b/backend/src/test/java/ch/puzzle/okr/multitenancy/SchemaMultiTenantConnectionProviderTest.java new file mode 100644 index 0000000000..86c5033f5f --- /dev/null +++ b/backend/src/test/java/ch/puzzle/okr/multitenancy/SchemaMultiTenantConnectionProviderTest.java @@ -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 + "';"); + } + +}