Fix export / import tests relict

Closes: #19812
This commit is contained in:
Hynek Mlnarik 2023-03-11 19:50:21 +01:00 committed by Hynek Mlnařík
parent 3b65f1163d
commit 3161c4424c
4 changed files with 37 additions and 19 deletions

View file

@ -17,6 +17,8 @@
package org.keycloak.exportimport;
import java.io.Closeable;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
@ -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) {

View file

@ -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

View file

@ -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 -> {

View file

@ -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