From 3161c4424ca80e5a764b08a26e1722f9c8fa6160 Mon Sep 17 00:00:00 2001 From: Hynek Mlnarik Date: Sat, 11 Mar 2023 19:50:21 +0100 Subject: [PATCH] Fix export / import tests relict Closes: #19812 --- .../exportimport/ExportImportConfig.java | 5 ++++- .../storage/ComponentExportImportTest.java | 11 ++++++---- .../FederatedStorageExportImportTest.java | 21 ++++++++++++------- .../model/exportimport/ExportModelTest.java | 19 +++++++++++------ 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/services/src/main/java/org/keycloak/exportimport/ExportImportConfig.java b/services/src/main/java/org/keycloak/exportimport/ExportImportConfig.java index 0c69b61414..08c26909d3 100644 --- a/services/src/main/java/org/keycloak/exportimport/ExportImportConfig.java +++ b/services/src/main/java/org/keycloak/exportimport/ExportImportConfig.java @@ -17,6 +17,8 @@ package org.keycloak.exportimport; +import java.io.Closeable; + /** * @author Stian Thorgersen */ @@ -58,8 +60,9 @@ public class ExportImportConfig { return System.getProperty(ACTION); } - public static void setAction(String exportImportAction) { + public static Closeable setAction(String exportImportAction) { System.setProperty(ACTION, exportImportAction); + return () -> System.getProperties().remove(ACTION); } public static void setProvider(String exportImportProvider) { diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/federation/storage/ComponentExportImportTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/federation/storage/ComponentExportImportTest.java index 13fd9eb823..f65bc0f45d 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/federation/storage/ComponentExportImportTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/federation/storage/ComponentExportImportTest.java @@ -19,6 +19,7 @@ import org.keycloak.testsuite.client.KeycloakTestingClient; import org.keycloak.testsuite.federation.UserMapStorageFactory; import org.keycloak.testsuite.util.RealmBuilder; +import java.io.Closeable; import javax.ws.rs.NotFoundException; import java.io.File; import java.util.HashSet; @@ -123,8 +124,9 @@ public class ComponentExportImportTest extends AbstractAuthTest { ExportImportConfig.setProvider(SingleFileExportProviderFactory.PROVIDER_ID); ExportImportConfig.setFile(exportFilePath); ExportImportConfig.setRealmName(REALM_NAME); - ExportImportConfig.setAction(ExportImportConfig.ACTION_EXPORT); - new ExportImportManager(session).runExport(); + try (Closeable c = ExportImportConfig.setAction(ExportImportConfig.ACTION_EXPORT)) { + new ExportImportManager(session).runExport(); + } }); testRealmResource().remove(); @@ -139,8 +141,9 @@ public class ComponentExportImportTest extends AbstractAuthTest { // import testingClient.server().run(session -> { Assert.assertNull(session.realms().getRealmByName(REALM_NAME)); - ExportImportConfig.setAction(ExportImportConfig.ACTION_IMPORT); - new ExportImportManager(session).runImport(); + try (Closeable c = ExportImportConfig.setAction(ExportImportConfig.ACTION_IMPORT)) { + new ExportImportManager(session).runImport(); + } }); // Assert realm was imported diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/federation/storage/FederatedStorageExportImportTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/federation/storage/FederatedStorageExportImportTest.java index e4b54d74b9..46b66884f7 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/federation/storage/FederatedStorageExportImportTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/federation/storage/FederatedStorageExportImportTest.java @@ -39,6 +39,7 @@ import org.keycloak.services.managers.RealmManager; import org.keycloak.storage.UserStorageUtil; import org.keycloak.testsuite.AbstractAuthTest; +import java.io.Closeable; import javax.ws.rs.NotFoundException; import java.io.File; import java.util.LinkedList; @@ -125,15 +126,17 @@ public class FederatedStorageExportImportTest extends AbstractAuthTest { ExportImportConfig.setProvider(SingleFileExportProviderFactory.PROVIDER_ID); ExportImportConfig.setFile(exportFileAbsolutePath); ExportImportConfig.setRealmName(REALM_NAME); - ExportImportConfig.setAction(ExportImportConfig.ACTION_EXPORT); - new ExportImportManager(session).runExport(); + try (Closeable c = ExportImportConfig.setAction(ExportImportConfig.ACTION_EXPORT)) { + new ExportImportManager(session).runExport(); + } session.realms().removeRealm(realmId); }); testingClient.server().run(session -> { Assert.assertNull(session.realms().getRealmByName(REALM_NAME)); - ExportImportConfig.setAction(ExportImportConfig.ACTION_IMPORT); - new ExportImportManager(session).runImport(); + try (Closeable c = ExportImportConfig.setAction(ExportImportConfig.ACTION_IMPORT)) { + new ExportImportManager(session).runImport(); + } }); testingClient.server().run(session -> { @@ -193,16 +196,18 @@ public class FederatedStorageExportImportTest extends AbstractAuthTest { ExportImportConfig.setProvider(DirExportProviderFactory.PROVIDER_ID); ExportImportConfig.setDir(exportDirAbsolutePath); ExportImportConfig.setRealmName(REALM_NAME); - ExportImportConfig.setAction(ExportImportConfig.ACTION_EXPORT); - new ExportImportManager(session).runExport(); + try (Closeable c = ExportImportConfig.setAction(ExportImportConfig.ACTION_EXPORT)) { + new ExportImportManager(session).runExport(); + } session.realms().removeRealm(realmId); }); testingClient.server().run(session -> { Assert.assertNull(session.realms().getRealmByName(REALM_NAME)); - ExportImportConfig.setAction(ExportImportConfig.ACTION_IMPORT); - new ExportImportManager(session).runImport(); + try (Closeable c = ExportImportConfig.setAction(ExportImportConfig.ACTION_IMPORT)) { + new ExportImportManager(session).runImport(); + } }); testingClient.server().run(session -> { diff --git a/testsuite/model/src/test/java/org/keycloak/testsuite/model/exportimport/ExportModelTest.java b/testsuite/model/src/test/java/org/keycloak/testsuite/model/exportimport/ExportModelTest.java index eeaf25989b..4e46a96752 100644 --- a/testsuite/model/src/test/java/org/keycloak/testsuite/model/exportimport/ExportModelTest.java +++ b/testsuite/model/src/test/java/org/keycloak/testsuite/model/exportimport/ExportModelTest.java @@ -34,6 +34,7 @@ import org.keycloak.models.RoleModel; import org.keycloak.testsuite.model.KeycloakModelTest; import org.keycloak.testsuite.model.RequireProvider; +import java.io.Closeable; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -77,9 +78,12 @@ public class ExportModelTest extends KeycloakModelTest { .config(SingleFileExportProviderFactory.REALM_NAME, REALM_NAME); inComittedTransaction(session -> { - ExportImportConfig.setAction(ExportImportConfig.ACTION_EXPORT); - ExportImportManager exportImportManager = new ExportImportManager(session); - exportImportManager.runExport(); + try (Closeable c = ExportImportConfig.setAction(ExportImportConfig.ACTION_EXPORT)) { + ExportImportManager exportImportManager = new ExportImportManager(session); + exportImportManager.runExport(); + } catch (IOException ex) { + log.error(ex.getMessage(), ex); + } }); // file will exist if export was successful @@ -112,9 +116,12 @@ public class ExportModelTest extends KeycloakModelTest { .config(DirExportProviderFactory.REALM_NAME, REALM_NAME); inComittedTransaction(session -> { - ExportImportConfig.setAction(ExportImportConfig.ACTION_EXPORT); - ExportImportManager exportImportManager = new ExportImportManager(session); - exportImportManager.runExport(); + try (Closeable c = ExportImportConfig.setAction(ExportImportConfig.ACTION_EXPORT)) { + ExportImportManager exportImportManager = new ExportImportManager(session); + exportImportManager.runExport(); + } catch (IOException ex) { + log.error(ex.getMessage(), ex); + } }); // file will exist if export was successful