Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
clean-coder authored and peggimann committed Nov 6, 2024
1 parent 0044ecf commit 58b8c27
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public String getLog() {
NOT_USED, URL, NAME, PASSWORD, SCHEMA);

private final TenantConfigProvider.TenantConfig tenantConfig = new TenantConfigProvider.TenantConfig(NOT_USED,
new String[] { NOT_USED }, NOT_USED, NOT_USED, NOT_USED, dataSourceConfig);
new String[] { NOT_USED }, NOT_USED, NOT_USED, NOT_USED, dataSourceConfig, dataSourceConfig);

@Override
public List<TenantConfigProvider.TenantConfig> getTenantConfigs() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.puzzle.okr.multitenancy;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -23,6 +24,11 @@ void setUp() {
resetHibernateConfig();
}

@AfterEach
void tearDown() {
resetHibernateConfig();
}

@DisplayName("setHibernateConfig() should throw exception if db config is null")
@Test
void setHibernateConfigShouldThrowExceptionIfDbConfigIsNull() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class SchemaMultiTenantConnectionProviderInternalsTest {
private static class ConfigurableConnectionProviderMock extends SchemaMultiTenantConnectionProvider {

@Override
protected Properties getHibernateProperties() {
protected Properties getHibernateProperties(String tenantIdentifier) {
return new Properties();
}

Expand Down Expand Up @@ -78,6 +78,7 @@ void getAnyConnectionProviderReturnConnectionProviderForTenantIdPublic() {
Assertions.assertNotNull(foundConnectionProvider);
}

// @Disabled
@DisplayName("getConnectionProviderShouldThrowRuntimeExceptionWhenNoPropertiesAreFound")
@Test
void getConnectionProviderShouldThrowRuntimeExceptionWhenNoPropertiesAreFound() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.puzzle.okr.multitenancy;

import ch.puzzle.okr.test.SpringIntegrationTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -24,8 +25,10 @@ public class TenantConfigProviderTestIT {
private static final String FRONTEND_CLIENT_ISSUER_URL = "frontendClientIssuerUrl";
private static final String FRONTEND_CLIENT_ID = "frontendClientId";
private static final String DATASOURCE_URL = "datasourceUrl";
private static final String DATASOURCE_NAME = "datasourceName";
private static final String DATASOURCE_PASSWORD = "datasourcePassword";
private static final String DATASOURCE_NAME_FLY = "datasourceNameFly";
private static final String DATASOURCE_PASSWORD_FLY = "datasourcePasswordFly";
private static final String DATASOURCE_NAME_APP = "datasourceNameApp";
private static final String DATASOURCE_PASSWORD_APP = "datasourcePasswordApp";
private static final String DATASOURCE_SCHEMA = "datasourceSchema";
private static final String DRIVER_CLASS_NAME = "driverClassName";
private static final String CHAMPION_EMAILS_1 = "[email protected]";
Expand All @@ -37,20 +40,30 @@ public class TenantConfigProviderTestIT {
@Mock
private Environment env;

private TenantConfigProvider configProvider;

@BeforeEach
void setUp() {
for (String tenantId : tenantIds) {
setupPropertiesForTenantWithId(tenantId);
}
configProvider = new TenantConfigProvider(tenantIds, env);
}

@AfterEach
void tearDown() {
TenantConfigProvider.clearTenantConfigsCache();
}

private void setupPropertiesForTenantWithId(String id) {
mockProperty("okr.tenants.{0}.security.oauth2.resourceserver.jwt.jwk-set-uri", JWK_SET_URI, id);
mockProperty("okr.tenants.{0}.security.oauth2.frontend.issuer-url", FRONTEND_CLIENT_ISSUER_URL, id);
mockProperty("okr.tenants.{0}.security.oauth2.frontend.client-id", FRONTEND_CLIENT_ID, id);
mockProperty("okr.tenants.{0}.datasource.url", DATASOURCE_URL, id);
mockProperty("okr.tenants.{0}.datasource.username", DATASOURCE_NAME, id);
mockProperty("okr.tenants.{0}.datasource.password", DATASOURCE_PASSWORD, id);
mockProperty("okr.tenants.{0}.datasource.username.fly", DATASOURCE_NAME_FLY, id);
mockProperty("okr.tenants.{0}.datasource.password.fly", DATASOURCE_PASSWORD_FLY, id);
mockProperty("okr.tenants.{0}.datasource.username.app", DATASOURCE_NAME_APP, id);
mockProperty("okr.tenants.{0}.datasource.password.app", DATASOURCE_PASSWORD_APP, id);
mockProperty("okr.tenants.{0}.datasource.schema", DATASOURCE_SCHEMA, id);

mockProperty("okr.datasource.driver-class-name", DRIVER_CLASS_NAME);
Expand Down Expand Up @@ -78,7 +91,6 @@ private String prefix(String tenantId) {
@DisplayName("getTenantConfigs returns all TenantConfigs as List")
@Test
public void testGetTenantConfigs() {
TenantConfigProvider configProvider = new TenantConfigProvider(tenantIds, env);
List<TenantConfigProvider.TenantConfig> tenantConfigs = configProvider.getTenantConfigs();
for (TenantConfigProvider.TenantConfig config : tenantConfigs) {
assertTenantConfigProvider(config);
Expand All @@ -89,7 +101,6 @@ public void testGetTenantConfigs() {
@ParameterizedTest
@CsvSource({ "pitc, acme" })
void testGetTenantConfigByIdForExistingTenantId(String tenantId) {
TenantConfigProvider configProvider = new TenantConfigProvider(tenantIds, env);
Optional<TenantConfigProvider.TenantConfig> config = configProvider.getTenantConfigById(tenantId);
assertTrue(config.isPresent());
assertTenantConfigProvider(config.get());
Expand All @@ -99,7 +110,6 @@ void testGetTenantConfigByIdForExistingTenantId(String tenantId) {
@ParameterizedTest
@CsvSource({ "PITC-London" })
void testGetTenantConfigByIdForNonExistingTenantId(String nonExistingTenantId) {
TenantConfigProvider configProvider = new TenantConfigProvider(tenantIds, env);
Optional<TenantConfigProvider.TenantConfig> config = configProvider.getTenantConfigById(nonExistingTenantId);
assertTrue(config.isEmpty());
}
Expand All @@ -108,9 +118,6 @@ void testGetTenantConfigByIdForNonExistingTenantId(String nonExistingTenantId) {
@ParameterizedTest
@CsvSource({ "pitc", "acme" })
void testGetJwkSetUriForExistingTenantId(String tenantId) {
// arrange
TenantConfigProvider configProvider = new TenantConfigProvider(tenantIds, env);

// act
Optional<String> jwkSetUri = configProvider.getJwkSetUri(tenantId);

Expand All @@ -123,7 +130,6 @@ void testGetJwkSetUriForExistingTenantId(String tenantId) {
@ParameterizedTest
@CsvSource({ "PITC-London" })
void testGetJwkSetUriForNonExistingTenantId(String nonExistingTenantId) {
TenantConfigProvider configProvider = new TenantConfigProvider(tenantIds, env);
Optional<String> jwkSetUri = configProvider.getJwkSetUri(nonExistingTenantId);
assertTrue(jwkSetUri.isEmpty());
}
Expand All @@ -134,13 +140,15 @@ private void assertTenantConfigProvider(TenantConfigProvider.TenantConfig tenant
assertEquals(prefix(tenantId) + JWK_SET_URI, tenantConfig.jwkSetUri());
assertEquals(prefix(tenantId) + FRONTEND_CLIENT_ISSUER_URL, tenantConfig.issuerUrl());
assertEquals(prefix(tenantId) + FRONTEND_CLIENT_ID, tenantConfig.clientId());
assertEquals(prefix(tenantId) + DATASOURCE_URL, tenantConfig.dataSourceConfig().url());
assertEquals(prefix(tenantId) + DATASOURCE_NAME, tenantConfig.dataSourceConfig().name());
assertEquals(prefix(tenantId) + DATASOURCE_PASSWORD, tenantConfig.dataSourceConfig().password());
assertEquals(prefix(tenantId) + DATASOURCE_SCHEMA, tenantConfig.dataSourceConfig().schema());
assertEquals(prefix(tenantId) + DATASOURCE_URL, tenantConfig.dataSourceConfigFlyway().url());
assertEquals(prefix(tenantId) + DATASOURCE_NAME_FLY, tenantConfig.dataSourceConfigFlyway().name());
assertEquals(prefix(tenantId) + DATASOURCE_PASSWORD_FLY, tenantConfig.dataSourceConfigFlyway().password());
assertEquals(prefix(tenantId) + DATASOURCE_NAME_APP, tenantConfig.dataSourceConfigApp().name());
assertEquals(prefix(tenantId) + DATASOURCE_PASSWORD_APP, tenantConfig.dataSourceConfigApp().password());
assertEquals(prefix(tenantId) + DATASOURCE_SCHEMA, tenantConfig.dataSourceConfigApp().schema());

assertArrayEquals(new String[] { CHAMPION_EMAILS_1, CHAMPION_EMAILS_2 }, tenantConfig.okrChampionEmails());
assertEquals(DRIVER_CLASS_NAME, tenantConfig.dataSourceConfig().driverClassName());
assertEquals(DRIVER_CLASS_NAME, tenantConfig.dataSourceConfigFlyway().driverClassName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void getTenantFromTokenReturnsTenantIfTenantFoundInTenantConfigProvider() {
when(tenantConfigProviderMock.getTenantConfigById(PITC)).thenReturn(Optional.of( //
new TenantConfigProvider.TenantConfig(PITC, //
new String[] {}, "jwkSetUri", "issuerUrl", //
"clientId", null) //
"clientId", null, null) //
));

JwtHelper jwtHelper = new JwtHelper(tenantConfigProviderMock, null, null, null);
Expand Down Expand Up @@ -122,7 +122,7 @@ void getTenantFromJWTClaimsSetReturnsTenantIfTenantFoundInTenantConfigProvider()
when(tenantConfigProviderWithDataMock.getTenantConfigById(PITC)).thenReturn(Optional.of( //
new TenantConfigProvider.TenantConfig(PITC, //
new String[] {}, "jwkSetUri", "issuerUrl", //
"clientId", null) //
"clientId", null, null) //
));

JwtHelper jwtHelper = new JwtHelper(tenantConfigProviderWithDataMock, null, null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void validateReturnOAuth2TokenValidatorResultIfTenantConfigWithIssuerUrlIsFound(
when(tenantConfigProviderWithPitcConfig.getTenantConfigById(PITC)).thenReturn(Optional.of( //
new TenantConfigProvider.TenantConfig( //
PITC, new String[] {}, "jwkSetUri", //
ISSUER_URL, "clientId", null)));
ISSUER_URL, "clientId", null, null)));

TenantJwtIssuerValidator tenantJwtIssuerValidator = new TenantJwtIssuerValidator(
tenantConfigProviderWithPitcConfig, jwtHelper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void updateUserAsNoChampion() {
// arrange
User noChampionUser = User.Builder.builder().withEmail("[email protected]").build();
TenantConfigProvider.TenantConfig tenantConfig = new TenantConfigProvider.TenantConfig(null, //
new String[] { "[email protected]" }, null, null, null, null);
new String[] { "[email protected]" }, null, null, null, null, null);

// act
User updatedUser = helper.setOkrChampionFromProperties(noChampionUser, tenantConfig);
Expand All @@ -61,7 +61,7 @@ void updateUserAsChampion() {
// arrange
User championUser = User.Builder.builder().withEmail("[email protected]").build();
TenantConfigProvider.TenantConfig tenantConfig = new TenantConfigProvider.TenantConfig(null, //
new String[] { "[email protected]" }, null, null, null, null);
new String[] { "[email protected]" }, null, null, null, null, null);

// act
User updatedUser = helper.setOkrChampionFromProperties(championUser, tenantConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private TenantConfigProvider.TenantConfig getTenantConfig(String tenantId) {
prefix(tenantId) + "jwkSetUri", //
prefix(tenantId) + "issuerUrl", //
prefix(tenantId) + "clientId", //
null);
null, null);
}

private TenantClientCustomization getTenantClientCustomization(String tenantId) {
Expand Down

0 comments on commit 58b8c27

Please sign in to comment.