KEYCLOAK-11129 coalesce possible null values

This commit is contained in:
Erik Jan de Wit 2020-02-12 14:06:55 +01:00 committed by Stian Thorgersen
parent 469bca624b
commit 93a1374558
2 changed files with 30 additions and 2 deletions

View file

@ -45,9 +45,9 @@ import java.util.Collection;
@NamedQuery(name="getAllUsersByRealm", query="select u from UserEntity u where u.realmId = :realmId order by u.username"),
@NamedQuery(name="getAllUsersByRealmExcludeServiceAccount", query="select u from UserEntity u where u.realmId = :realmId and (u.serviceAccountClientLink is null) order by u.username"),
@NamedQuery(name="searchForUser", query="select u from UserEntity u where u.realmId = :realmId and (u.serviceAccountClientLink is null) and " +
"( lower(u.username) like :search or lower(concat(u.firstName, ' ', u.lastName)) like :search or u.email like :search ) order by u.username"),
"( lower(u.username) like :search or lower(concat(coalesce(u.firstName, ''), ' ', coalesce(u.lastName, ''))) like :search or u.email like :search ) order by u.username"),
@NamedQuery(name="searchForUserCount", query="select count(u) from UserEntity u where u.realmId = :realmId and (u.serviceAccountClientLink is null) and " +
"( lower(u.username) like :search or lower(concat(u.firstName, ' ', u.lastName)) like :search or u.email like :search )"),
"( lower(u.username) like :search or lower(concat(coalesce(u.firstName, ''), ' ', coalesce(u.lastName, ''))) like :search or u.email like :search )"),
@NamedQuery(name="getRealmUserByUsername", query="select u from UserEntity u where u.username = :username and u.realmId = :realmId"),
@NamedQuery(name="getRealmUserByEmail", query="select u from UserEntity u where u.email = :email and u.realmId = :realmId"),
@NamedQuery(name="getRealmUserByLastName", query="select u from UserEntity u where u.lastName = :lastName and u.realmId = :realmId"),

View file

@ -530,6 +530,34 @@ public class UserTest extends AbstractAdminTest {
assertEquals(9, users.size());
}
@Test
public void searchByFirstNameNullForLastName() {
UserRepresentation user = new UserRepresentation();
user.setUsername("user1");
user.setFirstName("Erik");
user.setRequiredActions(Collections.emptyList());
user.setEnabled(true);
createUser(user);
List<UserRepresentation> users = realm.users().search("Erik", 0, 50);
assertEquals(1, users.size());
}
@Test
public void searchByLastNameNullForFirstName() {
UserRepresentation user = new UserRepresentation();
user.setUsername("user1");
user.setLastName("de Wit");
user.setRequiredActions(Collections.emptyList());
user.setEnabled(true);
createUser(user);
List<UserRepresentation> users = realm.users().search("wit", null, null);
assertEquals(1, users.size());
}
@Test
public void searchById() {
String expectedUserId = createUsers().get(0);