From 97dd10883c5bb8bc4c5b0ceda135028334affdee Mon Sep 17 00:00:00 2001 From: Bill Burke Date: Wed, 19 Oct 2016 16:20:59 -0400 Subject: [PATCH] test component import/export --- .../models/utils/KeycloakModelUtils.java | 4 +- .../storage/ComponentExportImportTest.java | 163 ++++++++++++++++++ .../FederatedStorageExportImportTest.java | 2 - 3 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 testsuite/integration/src/test/java/org/keycloak/testsuite/federation/storage/ComponentExportImportTest.java diff --git a/server-spi/src/main/java/org/keycloak/models/utils/KeycloakModelUtils.java b/server-spi/src/main/java/org/keycloak/models/utils/KeycloakModelUtils.java index 1263dc893e..8ef188ccaf 100755 --- a/server-spi/src/main/java/org/keycloak/models/utils/KeycloakModelUtils.java +++ b/server-spi/src/main/java/org/keycloak/models/utils/KeycloakModelUtils.java @@ -688,10 +688,10 @@ public final class KeycloakModelUtils { try { providerClass = (Class)Class.forName(model.getProviderType()); } catch (ClassNotFoundException e) { - throw new RuntimeException(e); + return; } ProviderFactory factory = session.getKeycloakSessionFactory().getProviderFactory(providerClass, model.getProviderId()); - if (factory instanceof ComponentFactory) { + if (factory instanceof ComponentFactory && factory != null) { ((ComponentFactory)factory).onCreate(session, realm, model); } } diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/storage/ComponentExportImportTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/storage/ComponentExportImportTest.java new file mode 100644 index 0000000000..ef4ddba61b --- /dev/null +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/storage/ComponentExportImportTest.java @@ -0,0 +1,163 @@ +/* + * Copyright 2016 Red Hat, Inc. and/or its affiliates + * and other contributors as indicated by the @author tags. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.keycloak.testsuite.federation.storage; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.keycloak.common.util.MultivaluedHashMap; +import org.keycloak.component.ComponentModel; +import org.keycloak.credential.CredentialModel; +import org.keycloak.credential.hash.PasswordHashProvider; +import org.keycloak.exportimport.ExportImportConfig; +import org.keycloak.exportimport.ExportImportManager; +import org.keycloak.exportimport.dir.DirExportProviderFactory; +import org.keycloak.exportimport.singlefile.SingleFileExportProviderFactory; +import org.keycloak.models.GroupModel; +import org.keycloak.models.KeycloakSession; +import org.keycloak.models.PasswordPolicy; +import org.keycloak.models.RealmModel; +import org.keycloak.models.RoleModel; +import org.keycloak.policy.HashAlgorithmPasswordPolicyProviderFactory; +import org.keycloak.services.managers.RealmManager; +import org.keycloak.storage.UserStorageProvider; +import org.keycloak.testsuite.rule.KeycloakRule; + +import java.io.File; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Properties; +import java.util.Set; + +/** + * @author Bill Burke + * @version $Revision: 1 $ + */ +public class ComponentExportImportTest { + @ClassRule + public static KeycloakRule keycloakRule = new KeycloakRule(new KeycloakRule.KeycloakSetup() { + + @Override + public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) { + + } + }); + + public static String basePath = null; + + @BeforeClass + public static void setDirs() { + basePath = new File(System.getProperty("project.build.directory", "target")).getAbsolutePath(); + + } + + protected PasswordHashProvider getHashProvider(KeycloakSession session, PasswordPolicy policy) { + PasswordHashProvider hash = session.getProvider(PasswordHashProvider.class, policy.getHashAlgorithm()); + if (hash == null) { + return session.getProvider(PasswordHashProvider.class, HashAlgorithmPasswordPolicyProviderFactory.DEFAULT_VALUE); + } + return hash; + } + + + @Test + public void testSingleFile() throws Exception { + clearExportImportProperties(); + KeycloakSession session = keycloakRule.startSession(); + RealmModel realm = new RealmManager(session).createRealm("exported-component"); + String realmId = realm.getId(); + ComponentModel component = new ComponentModel(); + component.setParentId(realm.getId()); + component.setProviderId(UserMapStorageFactory.PROVIDER_ID); + component.setProviderType(UserStorageProvider.class.getName()); + component.setName("parent"); + component.setSubType("subtype"); + component.put("attr", "value"); + component = realm.addComponentModel(component); + ComponentModel subComponent = new ComponentModel(); + subComponent.setParentId(component.getId()); + subComponent.setProviderId(UserMapStorageFactory.PROVIDER_ID); + subComponent.setProviderType(UserStorageProvider.class.getName()); + subComponent.setName("child"); + subComponent.setSubType("subtype2"); + subComponent.put("attr", "value2"); + subComponent = realm.addComponentModel(subComponent); + keycloakRule.stopSession(session, true); + + + + + String targetFilePath = basePath + File.separator + "singleFile-full.json"; + System.out.println("export file: " + targetFilePath); + session = keycloakRule.startSession(); + ExportImportConfig.setProvider(SingleFileExportProviderFactory.PROVIDER_ID); + ExportImportConfig.setFile(targetFilePath); + ExportImportConfig.setRealmName("exported-component"); + ExportImportConfig.setAction(ExportImportConfig.ACTION_EXPORT); + new ExportImportManager(session).runExport(); + session.realms().removeRealm(realmId); + keycloakRule.stopSession(session, true); + + session = keycloakRule.startSession(); + Assert.assertNull(session.realms().getRealmByName("exported-component")); + ExportImportConfig.setAction(ExportImportConfig.ACTION_IMPORT); + new ExportImportManager(session).runImport(); + realm = session.realms().getRealmByName("exported-component"); + Assert.assertNotNull(realm); + component = realm.getComponent(component.getId()); + Assert.assertNotNull(component); + Assert.assertEquals(component.getParentId(), realm.getId()); + Assert.assertEquals(component.getName(), "parent"); + Assert.assertEquals(component.getSubType(), "subtype"); + Assert.assertEquals(component.getProviderId(), UserMapStorageFactory.PROVIDER_ID); + Assert.assertEquals(component.getProviderType(), UserStorageProvider.class.getName()); + Assert.assertEquals(component.getConfig().getFirst("attr"), "value"); + subComponent = realm.getComponents(component.getId()).get(0); + Assert.assertEquals(subComponent.getParentId(), component.getId()); + Assert.assertEquals(subComponent.getName(), "child"); + Assert.assertEquals(subComponent.getSubType(), "subtype2"); + Assert.assertEquals(subComponent.getProviderId(), UserMapStorageFactory.PROVIDER_ID); + Assert.assertEquals(subComponent.getProviderType(), UserStorageProvider.class.getName()); + Assert.assertEquals(subComponent.getConfig().getFirst("attr"), "value2"); + session.realms().removeRealm(realmId); + keycloakRule.stopSession(session, true); + + } + + + public void clearExportImportProperties() { + // Clear export/import properties after test + Properties systemProps = System.getProperties(); + Set propsToRemove = new HashSet(); + + for (Object key : systemProps.keySet()) { + if (key.toString().startsWith(ExportImportConfig.PREFIX)) { + propsToRemove.add(key.toString()); + } + } + + for (String propToRemove : propsToRemove) { + systemProps.remove(propToRemove); + } + } + + + + +} diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/storage/FederatedStorageExportImportTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/storage/FederatedStorageExportImportTest.java index 2591c77197..420ca7a16d 100644 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/storage/FederatedStorageExportImportTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/federation/storage/FederatedStorageExportImportTest.java @@ -64,8 +64,6 @@ import java.util.Set; * @version $Revision: 1 $ */ public class FederatedStorageExportImportTest { - public static ComponentModel memoryProvider = null; - public static ComponentModel writableProvider = null; @ClassRule public static KeycloakRule keycloakRule = new KeycloakRule(new KeycloakRule.KeycloakSetup() {