Fixing mongo

This commit is contained in:
mposolda 2016-08-02 08:13:15 +02:00
parent 7ec3813a07
commit b3a55df1e7
3 changed files with 61 additions and 3 deletions

View file

@ -78,7 +78,7 @@ public class DefaultMongoConnectionFactoryProvider implements MongoConnectionPro
"org.keycloak.models.entities.RequiredActionProviderEntity", "org.keycloak.models.entities.RequiredActionProviderEntity",
"org.keycloak.models.entities.PersistentUserSessionEntity", "org.keycloak.models.entities.PersistentUserSessionEntity",
"org.keycloak.models.entities.PersistentClientSessionEntity", "org.keycloak.models.entities.PersistentClientSessionEntity",
"org.keycloak.models.entities.StorageProviderEntity", "org.keycloak.models.entities.ComponentEntity",
"org.keycloak.authorization.mongo.entities.PolicyEntity", "org.keycloak.authorization.mongo.entities.PolicyEntity",
"org.keycloak.authorization.mongo.entities.ResourceEntity", "org.keycloak.authorization.mongo.entities.ResourceEntity",
"org.keycloak.authorization.mongo.entities.ResourceServerEntity", "org.keycloak.authorization.mongo.entities.ResourceServerEntity",

View file

@ -1841,6 +1841,7 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
RequiredActionProviderEntity entity = getRequiredActionProviderEntity(model.getId()); RequiredActionProviderEntity entity = getRequiredActionProviderEntity(model.getId());
if (entity == null) return; if (entity == null) return;
entity.setAlias(model.getAlias()); entity.setAlias(model.getAlias());
entity.setName(model.getName());
entity.setProviderId(model.getProviderId()); entity.setProviderId(model.getProviderId());
entity.setEnabled(model.isEnabled()); entity.setEnabled(model.isEnabled());
entity.setDefaultAction(model.isDefaultAction()); entity.setDefaultAction(model.isDefaultAction());
@ -2066,7 +2067,6 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
entity.setId(model.getId()); entity.setId(model.getId());
} }
entity.setConfig(model.getConfig()); entity.setConfig(model.getConfig());
entity.setId(model.getId());
entity.setParentId(model.getParentId()); entity.setParentId(model.getParentId());
entity.setProviderType(model.getProviderType()); entity.setProviderType(model.getProviderType());
entity.setProviderId(model.getProviderId()); entity.setProviderId(model.getProviderId());
@ -2083,7 +2083,6 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
for (ComponentEntity entity : realm.getComponentEntities()) { for (ComponentEntity entity : realm.getComponentEntities()) {
if (entity.getId().equals(model.getId())) { if (entity.getId().equals(model.getId())) {
entity.setConfig(model.getConfig()); entity.setConfig(model.getConfig());
entity.setId(model.getId());
entity.setParentId(model.getParentId()); entity.setParentId(model.getParentId());
entity.setProviderType(model.getProviderType()); entity.setProviderType(model.getProviderType());
entity.setProviderId(model.getProviderId()); entity.setProviderId(model.getProviderId());

View file

@ -22,6 +22,7 @@ import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import org.keycloak.Config; import org.keycloak.Config;
import org.keycloak.component.ComponentModel;
import org.keycloak.models.ClientModel; import org.keycloak.models.ClientModel;
import org.keycloak.models.FederatedIdentityModel; import org.keycloak.models.FederatedIdentityModel;
import org.keycloak.models.ModelDuplicateException; import org.keycloak.models.ModelDuplicateException;
@ -36,6 +37,9 @@ import org.keycloak.models.UserModel;
import org.keycloak.models.UserProvider; import org.keycloak.models.UserProvider;
import org.keycloak.representations.idm.CredentialRepresentation; import org.keycloak.representations.idm.CredentialRepresentation;
import org.keycloak.services.managers.RealmManager; import org.keycloak.services.managers.RealmManager;
import org.keycloak.storage.UserStorageProvider;
import org.keycloak.storage.UserStorageProviderModel;
import org.keycloak.testsuite.federation.storage.UserMapStorageFactory;
import java.security.KeyPair; import java.security.KeyPair;
import java.security.KeyPairGenerator; import java.security.KeyPairGenerator;
@ -831,6 +835,61 @@ public class AdapterTest extends AbstractModelTest {
realmManager.removeRealm(realmModel); realmManager.removeRealm(realmModel);
} }
@Test
public void testComponentModelCRUD() {
// Add
realmModel = realmManager.createRealm("foo-realm");
UserStorageProviderModel model = new UserStorageProviderModel();
model.setName("memory");
model.setPriority(0);
model.setProviderId(UserMapStorageFactory.PROVIDER_ID);
model.setParentId(realmModel.getId());
ComponentModel createdModel = realmModel.addComponentModel(model);
String id = createdModel.getId();
Assert.assertNotNull(id);
commit();
realmModel = realmManager.getRealmByName("foo-realm");
ComponentModel foundModel = realmModel.getComponent(id);
assertComponentModel(foundModel, id, UserMapStorageFactory.PROVIDER_ID, realmModel.getId(), "memory");
List<ComponentModel> components = realmModel.getComponents();
Assert.assertEquals(components.size(), 1);
assertComponentModel(components.get(0), id, UserMapStorageFactory.PROVIDER_ID, realmModel.getId(), "memory");
components = realmModel.getComponents(realmModel.getId(), UserStorageProvider.class.getName());
Assert.assertEquals(components.size(), 1);
assertComponentModel(components.get(0), id, UserMapStorageFactory.PROVIDER_ID, realmModel.getId(), "memory");
// Update
foundModel.getConfig().putSingle("foo", "bar");
realmModel.updateComponent(foundModel);
commit();
realmModel = realmManager.getRealmByName("foo-realm");
foundModel = realmModel.getComponent(id);
assertComponentModel(foundModel, id, UserMapStorageFactory.PROVIDER_ID, realmModel.getId(), "memory");
Assert.assertEquals("bar", foundModel.getConfig().getFirst("foo"));
// Remove
realmModel.removeComponent(foundModel);
commit();
realmModel = realmManager.getRealmByName("foo-realm");
foundModel = realmModel.getComponent(id);
Assert.assertNull(foundModel);
}
private void assertComponentModel(ComponentModel componentModel, String expectedId, String expectedProviderId, String expectedParentId, String expectedName) {
Assert.assertEquals(expectedId, componentModel.getId());
Assert.assertEquals(expectedProviderId, componentModel.getProviderId());
Assert.assertEquals(expectedParentId, componentModel.getParentId());
Assert.assertEquals(expectedName, componentModel.getName());
}
private KeyPair generateKeypair() throws NoSuchAlgorithmException { private KeyPair generateKeypair() throws NoSuchAlgorithmException {
return KeyPairGenerator.getInstance("RSA").generateKeyPair(); return KeyPairGenerator.getInstance("RSA").generateKeyPair();
} }