Skip to content

Commit

Permalink
[Test framework MVP] RealmLocalizationResourceTest - refactor to use …
Browse files Browse the repository at this point in the history
…RealmConfig and realm.cleanup() in methods only (keycloak#35187)

Signed-off-by: Lukas Hanusovsky <[email protected]>
Signed-off-by: Lukas Hanusovsky <[email protected]>
  • Loading branch information
lhanusov authored Nov 26, 2024
1 parent 6bf7dad commit 0e7d9e8
Showing 1 changed file with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

import jakarta.ws.rs.NotFoundException;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.test.framework.annotations.InjectRealm;
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
import org.keycloak.test.framework.realm.ManagedRealm;
import org.keycloak.test.framework.realm.RealmConfig;
import org.keycloak.test.framework.realm.RealmConfigBuilder;

import java.util.HashMap;
import java.util.List;
Expand All @@ -39,30 +39,19 @@
@KeycloakIntegrationTest
public class RealmLocalizationResourceTest {

@InjectRealm
@InjectRealm(config = RealmLocaleConfig.class)
private ManagedRealm realm;

@BeforeEach
public void setupRealmLocale() {
RealmRepresentation rep = new RealmRepresentation();
rep.setDefaultLocale("en");
realm.admin().update(rep);
private static class RealmLocaleConfig implements RealmConfig {

realm.admin().localization().saveRealmLocalizationText("en", "key-a", "text-a_en");
realm.admin().localization().saveRealmLocalizationText("en", "key-b", "text-b_en");
realm.admin().localization().saveRealmLocalizationText("de", "key-a", "text-a_de");
}
@Override
public RealmConfigBuilder configure(RealmConfigBuilder realm) {
RealmRepresentation rep = realm.build();
rep.setDefaultLocale("en");
rep.setLocalizationTexts(Map.of("en", Map.of("key-a", "text-a_en", "key-b", "text-b_en"), "de", Map.of("key-a", "text-a_de")));
rep.setEnabled(true);

@AfterEach
public void cleanupRealmLocale() {
if (realm.admin().localization().getRealmLocalizationTexts("en").size() > 0) {
realm.cleanup().add(r -> r.localization().deleteRealmLocalizationTexts("en"));
}
if (realm.admin().localization().getRealmLocalizationTexts("de").size() > 0) {
realm.cleanup().add(r -> r.localization().deleteRealmLocalizationTexts("de"));
}
if (realm.admin().localization().getRealmLocalizationTexts("es").size() > 0) {
realm.cleanup().add(r -> r.localization().deleteRealmLocalizationTexts("es"));
return realm.update(rep);
}
}

Expand Down Expand Up @@ -116,6 +105,8 @@ public void getRealmLocalizationTextNotExists() {

@Test
public void addRealmLocalizationText() {
realm.cleanup().add(r -> r.localization().deleteRealmLocalizationText("en", "key-c"));

realm.admin().localization().saveRealmLocalizationText("en", "key-c", "text-c");

String localizationText = realm.admin().localization().getRealmLocalizationText("en", "key-c");
Expand All @@ -126,6 +117,8 @@ public void addRealmLocalizationText() {

@Test
public void updateRealmLocalizationText() {
realm.cleanup().add(r -> r.localization().saveRealmLocalizationText("en", "key-b", "text-b_en"));

realm.admin().localization().saveRealmLocalizationText("en", "key-b", "text-b-new");

String localizationText = realm.admin().localization().getRealmLocalizationText("en", "key-b");
Expand All @@ -136,6 +129,8 @@ public void updateRealmLocalizationText() {

@Test
public void deleteRealmLocalizationText() {
realm.cleanup().add(r -> r.localization().saveRealmLocalizationText("en", "key-a", "text-a_en"));

realm.admin().localization().deleteRealmLocalizationText("en", "key-a");

Map<String, String> localizations = realm.admin().localization().getRealmLocalizationTexts("en");
Expand All @@ -152,8 +147,11 @@ public void deleteRealmLocalizationTextNotExists() {

@Test
public void deleteRealmLocalizationTexts() {
realm.admin().localization().deleteRealmLocalizationTexts("en");
realm.cleanup().add(r -> r.localization().saveRealmLocalizationText("en", "key-a", "text-a_en"));
realm.cleanup().add(r -> r.localization().saveRealmLocalizationText("en", "key-b", "text-b_en"));

realm.admin().localization().deleteRealmLocalizationTexts("en");

List<String> localizations = realm.admin().localization().getRealmSpecificLocales();
assertEquals(1, localizations.size());

Expand All @@ -162,6 +160,8 @@ public void deleteRealmLocalizationTexts() {

@Test
public void createOrUpdateRealmLocalizationWhenLocaleDoesNotYetExist() {
realm.cleanup().add(r -> r.localization().deleteRealmLocalizationTexts("es"));

final Map<String, String> newLocalizationTexts = new HashMap<>();
newLocalizationTexts.put("key-a", "text-a_es");
newLocalizationTexts.put("key-b", "text-b_es");
Expand All @@ -174,6 +174,8 @@ public void createOrUpdateRealmLocalizationWhenLocaleDoesNotYetExist() {

@Test
public void createOrUpdateRealmLocalizationWhenLocaleAlreadyExists() {
realm.cleanup().add(r -> r.localization().saveRealmLocalizationText("en", "key-b", "text-b_en"));

final Map<String, String> newLocalizationTexts = new HashMap<>();
newLocalizationTexts.put("key-b", "text-b_changed_en");
newLocalizationTexts.put("key-c", "text-c_en");
Expand Down

0 comments on commit 0e7d9e8

Please sign in to comment.