Fix testsuite with Mongo

This commit is contained in:
mposolda 2014-02-21 19:10:38 +01:00
parent 15968e9783
commit e85c2c9826
3 changed files with 14 additions and 23 deletions

View file

@ -126,7 +126,9 @@ public class ApplicationAdapter extends AbstractAdapter implements ApplicationMo
@Override @Override
public RoleModel getRoleById(String id) { public RoleModel getRoleById(String id) {
RoleEntity role = getMongoStore().loadEntity(RoleEntity.class, id, invocationContext); RoleEntity role = getMongoStore().loadEntity(RoleEntity.class, id, invocationContext);
if (role == null) {
// Check that role belongs to this application
if (role == null || !getId().equals(role.getApplicationId())) {
return null; return null;
} else { } else {
return new RoleAdapter(role, this, invocationContext); return new RoleAdapter(role, this, invocationContext);

View file

@ -316,13 +316,10 @@ public class RealmAdapter extends AbstractAdapter implements RealmModel {
@Override @Override
public UserModel getUserById(String id) { public UserModel getUserById(String id) {
DBObject query = new QueryBuilder() UserEntity user = getMongoStore().loadEntity(UserEntity.class, id, invocationContext);
.and("id").is(id)
.and("realmId").is(getId())
.get();
UserEntity user = getMongoStore().loadSingleEntity(UserEntity.class, query, invocationContext);
if (user == null) { // Check that it's user from this realm
if (user == null || !getId().equals(user.getRealmId())) {
return null; return null;
} else { } else {
return new UserAdapter(user, invocationContext); return new UserAdapter(user, invocationContext);
@ -426,7 +423,7 @@ public class RealmAdapter extends AbstractAdapter implements RealmModel {
@Override @Override
public RoleModel getRoleById(String id) { public RoleModel getRoleById(String id) {
RoleEntity role = getMongoStore().loadEntity(RoleEntity.class, id, invocationContext); RoleEntity role = getMongoStore().loadEntity(RoleEntity.class, id, invocationContext);
if (role == null) { if (role == null || !getId().equals(role.getRealmId())) {
return null; return null;
} else { } else {
return new RoleAdapter(role, this, invocationContext); return new RoleAdapter(role, this, invocationContext);
@ -660,7 +657,10 @@ public class RealmAdapter extends AbstractAdapter implements RealmModel {
@Override @Override
public OAuthClientModel getOAuthClientById(String id) { public OAuthClientModel getOAuthClientById(String id) {
OAuthClientEntity clientEntity = getMongoStore().loadEntity(OAuthClientEntity.class, id, invocationContext); OAuthClientEntity clientEntity = getMongoStore().loadEntity(OAuthClientEntity.class, id, invocationContext);
if (clientEntity == null) return null;
// Check if client belongs to this realm
if (clientEntity == null || !getId().equals(clientEntity.getRealmId())) return null;
return new OAuthClientAdapter(clientEntity, invocationContext); return new OAuthClientAdapter(clientEntity, invocationContext);
} }

View file

@ -3,13 +3,8 @@ package org.keycloak.model.test;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.keycloak.models.RealmModel; import org.keycloak.models.RealmModel;
import org.keycloak.models.RoleModel;
import org.keycloak.models.UserModel; import org.keycloak.models.UserModel;
import org.keycloak.models.UserModel.RequiredAction; import org.keycloak.models.UserModel.RequiredAction;
import org.keycloak.services.managers.RealmManager;
import java.util.Iterator;
import java.util.List;
/** /**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
@ -36,6 +31,9 @@ public class UserModelTest extends AbstractModelTest {
UserModel persisted = realmManager.getRealm(realm.getId()).getUser("user"); UserModel persisted = realmManager.getRealm(realm.getId()).getUser("user");
assertEquals(user, persisted); assertEquals(user, persisted);
UserModel persisted2 = realmManager.getRealm(realm.getId()).getUserById(user.getId());
assertEquals(user, persisted2);
} }
@Test @Test
@ -108,14 +106,5 @@ public class UserModelTest extends AbstractModelTest {
Assert.assertArrayEquals(expected.getWebOrigins().toArray(), actual.getWebOrigins().toArray()); Assert.assertArrayEquals(expected.getWebOrigins().toArray(), actual.getWebOrigins().toArray());
} }
public static void assertEquals(List<RoleModel> expected, List<RoleModel> actual) {
Assert.assertEquals(expected.size(), actual.size());
Iterator<RoleModel> exp = expected.iterator();
Iterator<RoleModel> act = actual.iterator();
while (exp.hasNext()) {
Assert.assertEquals(exp.next().getName(), act.next().getName());
}
}
} }