diff --git a/src/main/java/ch/ethz/seb/sebserver/gbl/model/user/UserFilter.java b/src/main/java/ch/ethz/seb/sebserver/gbl/model/user/UserFilter.java index 6370220a..cc314dd6 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gbl/model/user/UserFilter.java +++ b/src/main/java/ch/ethz/seb/sebserver/gbl/model/user/UserFilter.java @@ -10,10 +10,16 @@ package ch.ethz.seb.sebserver.gbl.model.user; import javax.validation.constraints.NotNull; +import org.apache.commons.lang3.BooleanUtils; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonProperty; import ch.ethz.seb.sebserver.gbl.model.Domain.USER; +@JsonInclude(Include.NON_NULL) public final class UserFilter { @JsonProperty(USER.ATTR_ACTIVE) @@ -41,7 +47,7 @@ public final class UserFilter { this.name = name; this.userName = userName; this.email = email; - this.active = active; + this.active = BooleanUtils.isFalse(active); this.locale = locale; } @@ -69,6 +75,21 @@ public final class UserFilter { return this.locale; } + @JsonIgnore + public String getNameLike() { + return (this.name == null) ? null : "%" + this.name + "%"; + } + + @JsonIgnore + public String getUserNameLike() { + return (this.userName == null) ? null : "%" + this.userName + "%"; + } + + @JsonIgnore + public String getEmailLike() { + return (this.email == null) ? null : "%" + this.email + "%"; + } + @Override public String toString() { return "UserFilter [institutionId=" + this.institutionId + ", name=" + this.name + ", userName=" + this.userName @@ -80,6 +101,10 @@ public final class UserFilter { return new UserFilter(null, null, null, null, true, null); } + public static UserFilter ofInactive() { + return new UserFilter(null, null, null, null, false, null); + } + public static UserFilter ofInstitution(@NotNull final Long institutionId) { return new UserFilter(institutionId, null, null, null, true, null); } diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/UserDaoImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/UserDaoImpl.java index 9716bbbe..0e0cc04a 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/UserDaoImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/UserDaoImpl.java @@ -137,8 +137,9 @@ public class UserDaoImpl implements UserDAO { UserRecordDynamicSqlSupport.active, isEqualTo(BooleanUtils.toInteger(filter.active))) .and(UserRecordDynamicSqlSupport.institutionId, isEqualToWhenPresent(filter.institutionId)) - .and(UserRecordDynamicSqlSupport.name, isLikeWhenPresent(filter.name)) - .and(UserRecordDynamicSqlSupport.userName, isLikeWhenPresent(filter.userName)) + .and(UserRecordDynamicSqlSupport.name, isLikeWhenPresent(filter.getNameLike())) + .and(UserRecordDynamicSqlSupport.userName, isLikeWhenPresent(filter.getUserNameLike())) + .and(UserRecordDynamicSqlSupport.email, isLikeWhenPresent(filter.getEmailLike())) .and(UserRecordDynamicSqlSupport.locale, isLikeWhenPresent(filter.locale)) .build() .execute(); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/UserAccountController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/UserAccountController.java index da16d886..2ada3f9f 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/UserAccountController.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/UserAccountController.java @@ -48,6 +48,7 @@ public class UserAccountController { @RequestMapping(method = RequestMethod.GET) public Collection getAll( + //@RequestParam(required = false) final UserFilter filter, @RequestBody(required = false) final UserFilter filter, final Principal principal) { diff --git a/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/UserAPITest.java b/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/UserAPITest.java index 31cb206f..ecb2fa78 100644 --- a/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/UserAPITest.java +++ b/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/UserAPITest.java @@ -14,11 +14,14 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import java.util.Collection; import java.util.List; +import java.util.NoSuchElementException; import org.junit.Test; +import org.springframework.http.MediaType; import com.fasterxml.jackson.core.type.TypeReference; +import ch.ethz.seb.sebserver.gbl.model.user.UserFilter; import ch.ethz.seb.sebserver.gbl.model.user.UserInfo; public class UserAPITest extends AdministrationAPIIntegrationTest { @@ -63,7 +66,7 @@ public class UserAPITest extends AdministrationAPIIntegrationTest { } @Test - public void getUserInfo() throws Exception { + public void getUserInfoWithUUID() throws Exception { final String sebAdminAccessToken = getSebAdminAccess(); String contentAsString = this.mockMvc.perform(get(this.endpoint + "/useraccount/2") .header("Authorization", "Bearer " + sebAdminAccessToken)) @@ -136,12 +139,55 @@ public class UserAPITest extends AdministrationAPIIntegrationTest { // TODO more tests } + @Test + public void getAllUserInfoWithSearchInactive() throws Exception { + final UserFilter filter = UserFilter.ofInactive(); + final String filterJson = this.jsonMapper.writeValueAsString(filter); + + final String token = getSebAdminAccess(); + final List userInfos = this.jsonMapper.readValue( + this.mockMvc.perform(get(this.endpoint + "/useraccount") + .header("Authorization", "Bearer " + token) + .contentType(MediaType.APPLICATION_JSON_UTF8) + .content(filterJson)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(), + new TypeReference>() { + }); + + assertNotNull(userInfos); + assertTrue(userInfos.size() == 1); + assertNotNull(getUserInfo("deactivatedUser", userInfos)); + } + + @Test + public void getAllUserInfoWithSearchUsernameLike() throws Exception { + final UserFilter filter = new UserFilter(null, null, "exam", null, null, null); + final String filterJson = this.jsonMapper.writeValueAsString(filter); + + final String token = getSebAdminAccess(); + final List userInfos = this.jsonMapper.readValue( + this.mockMvc.perform(get(this.endpoint + "/useraccount") + .header("Authorization", "Bearer " + token) + .contentType(MediaType.APPLICATION_JSON_UTF8) + .content(filterJson)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(), + new TypeReference>() { + }); + + assertNotNull(userInfos); + assertTrue(userInfos.size() == 2); + assertNotNull(getUserInfo("examAdmin1", userInfos)); + assertNotNull(getUserInfo("examSupporter", userInfos)); + } + private UserInfo getUserInfo(final String name, final Collection infos) { return infos .stream() .filter(ui -> ui.userName.equals(name)) .findFirst() - .orElseThrow(); + .orElseThrow(NoSuchElementException::new); } }