From 58b8c27583b405a26671e658337ec93730b62c69 Mon Sep 17 00:00:00 2001 From: clean-coder Date: Mon, 4 Nov 2024 14:12:52 +0100 Subject: [PATCH] fix tests --- ...ayMultitenantMigrationInitializerTest.java | 2 +- .../multitenancy/HibernateContextTest.java | 6 +++ ...TenantConnectionProviderInternalsTest.java | 3 +- .../TenantConfigProviderTestIT.java | 40 +++++++++++-------- .../ch/puzzle/okr/security/JwtHelperTest.java | 4 +- .../TenantJwtIssuerValidatorTest.java | 2 +- .../authorization/UserUpdateHelperTest.java | 4 +- .../clientconfig/ClientConfigServiceTest.java | 2 +- 8 files changed, 39 insertions(+), 24 deletions(-) diff --git a/backend/src/test/java/ch/puzzle/okr/multitenancy/FlywayMultitenantMigrationInitializerTest.java b/backend/src/test/java/ch/puzzle/okr/multitenancy/FlywayMultitenantMigrationInitializerTest.java index 3ab1cb4eb4..dc8cafbdc1 100644 --- a/backend/src/test/java/ch/puzzle/okr/multitenancy/FlywayMultitenantMigrationInitializerTest.java +++ b/backend/src/test/java/ch/puzzle/okr/multitenancy/FlywayMultitenantMigrationInitializerTest.java @@ -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 getTenantConfigs() { diff --git a/backend/src/test/java/ch/puzzle/okr/multitenancy/HibernateContextTest.java b/backend/src/test/java/ch/puzzle/okr/multitenancy/HibernateContextTest.java index 7dbc3a5b16..4c6445b5aa 100644 --- a/backend/src/test/java/ch/puzzle/okr/multitenancy/HibernateContextTest.java +++ b/backend/src/test/java/ch/puzzle/okr/multitenancy/HibernateContextTest.java @@ -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; @@ -23,6 +24,11 @@ void setUp() { resetHibernateConfig(); } + @AfterEach + void tearDown() { + resetHibernateConfig(); + } + @DisplayName("setHibernateConfig() should throw exception if db config is null") @Test void setHibernateConfigShouldThrowExceptionIfDbConfigIsNull() { diff --git a/backend/src/test/java/ch/puzzle/okr/multitenancy/SchemaMultiTenantConnectionProviderInternalsTest.java b/backend/src/test/java/ch/puzzle/okr/multitenancy/SchemaMultiTenantConnectionProviderInternalsTest.java index e8bc0dc7b0..f8bdbfb258 100644 --- a/backend/src/test/java/ch/puzzle/okr/multitenancy/SchemaMultiTenantConnectionProviderInternalsTest.java +++ b/backend/src/test/java/ch/puzzle/okr/multitenancy/SchemaMultiTenantConnectionProviderInternalsTest.java @@ -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(); } @@ -78,6 +78,7 @@ void getAnyConnectionProviderReturnConnectionProviderForTenantIdPublic() { Assertions.assertNotNull(foundConnectionProvider); } + // @Disabled @DisplayName("getConnectionProviderShouldThrowRuntimeExceptionWhenNoPropertiesAreFound") @Test void getConnectionProviderShouldThrowRuntimeExceptionWhenNoPropertiesAreFound() { diff --git a/backend/src/test/java/ch/puzzle/okr/multitenancy/TenantConfigProviderTestIT.java b/backend/src/test/java/ch/puzzle/okr/multitenancy/TenantConfigProviderTestIT.java index 940473d020..aadcf79f24 100644 --- a/backend/src/test/java/ch/puzzle/okr/multitenancy/TenantConfigProviderTestIT.java +++ b/backend/src/test/java/ch/puzzle/okr/multitenancy/TenantConfigProviderTestIT.java @@ -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; @@ -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 = "a@pitc.ch"; @@ -37,11 +40,19 @@ 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) { @@ -49,8 +60,10 @@ private void setupPropertiesForTenantWithId(String 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); @@ -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 tenantConfigs = configProvider.getTenantConfigs(); for (TenantConfigProvider.TenantConfig config : tenantConfigs) { assertTenantConfigProvider(config); @@ -89,7 +101,6 @@ public void testGetTenantConfigs() { @ParameterizedTest @CsvSource({ "pitc, acme" }) void testGetTenantConfigByIdForExistingTenantId(String tenantId) { - TenantConfigProvider configProvider = new TenantConfigProvider(tenantIds, env); Optional config = configProvider.getTenantConfigById(tenantId); assertTrue(config.isPresent()); assertTenantConfigProvider(config.get()); @@ -99,7 +110,6 @@ void testGetTenantConfigByIdForExistingTenantId(String tenantId) { @ParameterizedTest @CsvSource({ "PITC-London" }) void testGetTenantConfigByIdForNonExistingTenantId(String nonExistingTenantId) { - TenantConfigProvider configProvider = new TenantConfigProvider(tenantIds, env); Optional config = configProvider.getTenantConfigById(nonExistingTenantId); assertTrue(config.isEmpty()); } @@ -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 jwkSetUri = configProvider.getJwkSetUri(tenantId); @@ -123,7 +130,6 @@ void testGetJwkSetUriForExistingTenantId(String tenantId) { @ParameterizedTest @CsvSource({ "PITC-London" }) void testGetJwkSetUriForNonExistingTenantId(String nonExistingTenantId) { - TenantConfigProvider configProvider = new TenantConfigProvider(tenantIds, env); Optional jwkSetUri = configProvider.getJwkSetUri(nonExistingTenantId); assertTrue(jwkSetUri.isEmpty()); } @@ -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()); } } diff --git a/backend/src/test/java/ch/puzzle/okr/security/JwtHelperTest.java b/backend/src/test/java/ch/puzzle/okr/security/JwtHelperTest.java index 35f0367478..e454e38aaf 100644 --- a/backend/src/test/java/ch/puzzle/okr/security/JwtHelperTest.java +++ b/backend/src/test/java/ch/puzzle/okr/security/JwtHelperTest.java @@ -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); @@ -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); diff --git a/backend/src/test/java/ch/puzzle/okr/security/TenantJwtIssuerValidatorTest.java b/backend/src/test/java/ch/puzzle/okr/security/TenantJwtIssuerValidatorTest.java index 7c675c4204..cb94be43c2 100644 --- a/backend/src/test/java/ch/puzzle/okr/security/TenantJwtIssuerValidatorTest.java +++ b/backend/src/test/java/ch/puzzle/okr/security/TenantJwtIssuerValidatorTest.java @@ -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) { diff --git a/backend/src/test/java/ch/puzzle/okr/service/authorization/UserUpdateHelperTest.java b/backend/src/test/java/ch/puzzle/okr/service/authorization/UserUpdateHelperTest.java index 958eaac311..32838f4882 100644 --- a/backend/src/test/java/ch/puzzle/okr/service/authorization/UserUpdateHelperTest.java +++ b/backend/src/test/java/ch/puzzle/okr/service/authorization/UserUpdateHelperTest.java @@ -47,7 +47,7 @@ void updateUserAsNoChampion() { // arrange User noChampionUser = User.Builder.builder().withEmail("no@champions.ch").build(); TenantConfigProvider.TenantConfig tenantConfig = new TenantConfigProvider.TenantConfig(null, // - new String[] { "yes@champions.ch" }, null, null, null, null); + new String[] { "yes@champions.ch" }, null, null, null, null, null); // act User updatedUser = helper.setOkrChampionFromProperties(noChampionUser, tenantConfig); @@ -61,7 +61,7 @@ void updateUserAsChampion() { // arrange User championUser = User.Builder.builder().withEmail("yes@champions.ch").build(); TenantConfigProvider.TenantConfig tenantConfig = new TenantConfigProvider.TenantConfig(null, // - new String[] { "yes@champions.ch" }, null, null, null, null); + new String[] { "yes@champions.ch" }, null, null, null, null, null); // act User updatedUser = helper.setOkrChampionFromProperties(championUser, tenantConfig); diff --git a/backend/src/test/java/ch/puzzle/okr/service/clientconfig/ClientConfigServiceTest.java b/backend/src/test/java/ch/puzzle/okr/service/clientconfig/ClientConfigServiceTest.java index aced05adae..3e5b180fe5 100644 --- a/backend/src/test/java/ch/puzzle/okr/service/clientconfig/ClientConfigServiceTest.java +++ b/backend/src/test/java/ch/puzzle/okr/service/clientconfig/ClientConfigServiceTest.java @@ -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) {