SEBSERV-8 #fix compile and user search bugs, user API added tests
This commit is contained in:
		
							parent
							
								
									4d61b9ed3d
								
							
						
					
					
						commit
						58881bf763
					
				
					 4 changed files with 78 additions and 5 deletions
				
			
		|  | @ -10,10 +10,16 @@ package ch.ethz.seb.sebserver.gbl.model.user; | ||||||
| 
 | 
 | ||||||
| import javax.validation.constraints.NotNull; | 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 com.fasterxml.jackson.annotation.JsonProperty; | ||||||
| 
 | 
 | ||||||
| import ch.ethz.seb.sebserver.gbl.model.Domain.USER; | import ch.ethz.seb.sebserver.gbl.model.Domain.USER; | ||||||
| 
 | 
 | ||||||
|  | @JsonInclude(Include.NON_NULL) | ||||||
| public final class UserFilter { | public final class UserFilter { | ||||||
| 
 | 
 | ||||||
|     @JsonProperty(USER.ATTR_ACTIVE) |     @JsonProperty(USER.ATTR_ACTIVE) | ||||||
|  | @ -41,7 +47,7 @@ public final class UserFilter { | ||||||
|         this.name = name; |         this.name = name; | ||||||
|         this.userName = userName; |         this.userName = userName; | ||||||
|         this.email = email; |         this.email = email; | ||||||
|         this.active = active; |         this.active = BooleanUtils.isFalse(active); | ||||||
|         this.locale = locale; |         this.locale = locale; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | @ -69,6 +75,21 @@ public final class UserFilter { | ||||||
|         return this.locale; |         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 |     @Override | ||||||
|     public String toString() { |     public String toString() { | ||||||
|         return "UserFilter [institutionId=" + this.institutionId + ", name=" + this.name + ", userName=" + this.userName |         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); |         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) { |     public static UserFilter ofInstitution(@NotNull final Long institutionId) { | ||||||
|         return new UserFilter(institutionId, null, null, null, true, null); |         return new UserFilter(institutionId, null, null, null, true, null); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  | @ -137,8 +137,9 @@ public class UserDaoImpl implements UserDAO { | ||||||
|                     UserRecordDynamicSqlSupport.active, |                     UserRecordDynamicSqlSupport.active, | ||||||
|                     isEqualTo(BooleanUtils.toInteger(filter.active))) |                     isEqualTo(BooleanUtils.toInteger(filter.active))) | ||||||
|                     .and(UserRecordDynamicSqlSupport.institutionId, isEqualToWhenPresent(filter.institutionId)) |                     .and(UserRecordDynamicSqlSupport.institutionId, isEqualToWhenPresent(filter.institutionId)) | ||||||
|                     .and(UserRecordDynamicSqlSupport.name, isLikeWhenPresent(filter.name)) |                     .and(UserRecordDynamicSqlSupport.name, isLikeWhenPresent(filter.getNameLike())) | ||||||
|                     .and(UserRecordDynamicSqlSupport.userName, isLikeWhenPresent(filter.userName)) |                     .and(UserRecordDynamicSqlSupport.userName, isLikeWhenPresent(filter.getUserNameLike())) | ||||||
|  |                     .and(UserRecordDynamicSqlSupport.email, isLikeWhenPresent(filter.getEmailLike())) | ||||||
|                     .and(UserRecordDynamicSqlSupport.locale, isLikeWhenPresent(filter.locale)) |                     .and(UserRecordDynamicSqlSupport.locale, isLikeWhenPresent(filter.locale)) | ||||||
|                     .build() |                     .build() | ||||||
|                     .execute(); |                     .execute(); | ||||||
|  |  | ||||||
|  | @ -48,6 +48,7 @@ public class UserAccountController { | ||||||
| 
 | 
 | ||||||
|     @RequestMapping(method = RequestMethod.GET) |     @RequestMapping(method = RequestMethod.GET) | ||||||
|     public Collection<UserInfo> getAll( |     public Collection<UserInfo> getAll( | ||||||
|  |             //@RequestParam(required = false) final UserFilter filter, | ||||||
|             @RequestBody(required = false) final UserFilter filter, |             @RequestBody(required = false) final UserFilter filter, | ||||||
|             final Principal principal) { |             final Principal principal) { | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -14,11 +14,14 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. | ||||||
| 
 | 
 | ||||||
| import java.util.Collection; | import java.util.Collection; | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  | import java.util.NoSuchElementException; | ||||||
| 
 | 
 | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
|  | import org.springframework.http.MediaType; | ||||||
| 
 | 
 | ||||||
| import com.fasterxml.jackson.core.type.TypeReference; | 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; | import ch.ethz.seb.sebserver.gbl.model.user.UserInfo; | ||||||
| 
 | 
 | ||||||
| public class UserAPITest extends AdministrationAPIIntegrationTest { | public class UserAPITest extends AdministrationAPIIntegrationTest { | ||||||
|  | @ -63,7 +66,7 @@ public class UserAPITest extends AdministrationAPIIntegrationTest { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void getUserInfo() throws Exception { |     public void getUserInfoWithUUID() throws Exception { | ||||||
|         final String sebAdminAccessToken = getSebAdminAccess(); |         final String sebAdminAccessToken = getSebAdminAccess(); | ||||||
|         String contentAsString = this.mockMvc.perform(get(this.endpoint + "/useraccount/2") |         String contentAsString = this.mockMvc.perform(get(this.endpoint + "/useraccount/2") | ||||||
|                 .header("Authorization", "Bearer " + sebAdminAccessToken)) |                 .header("Authorization", "Bearer " + sebAdminAccessToken)) | ||||||
|  | @ -136,12 +139,55 @@ public class UserAPITest extends AdministrationAPIIntegrationTest { | ||||||
|         // TODO more tests |         // 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<UserInfo> 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<List<UserInfo>>() { | ||||||
|  |                 }); | ||||||
|  | 
 | ||||||
|  |         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<UserInfo> 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<List<UserInfo>>() { | ||||||
|  |                 }); | ||||||
|  | 
 | ||||||
|  |         assertNotNull(userInfos); | ||||||
|  |         assertTrue(userInfos.size() == 2); | ||||||
|  |         assertNotNull(getUserInfo("examAdmin1", userInfos)); | ||||||
|  |         assertNotNull(getUserInfo("examSupporter", userInfos)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     private UserInfo getUserInfo(final String name, final Collection<UserInfo> infos) { |     private UserInfo getUserInfo(final String name, final Collection<UserInfo> infos) { | ||||||
|         return infos |         return infos | ||||||
|                 .stream() |                 .stream() | ||||||
|                 .filter(ui -> ui.userName.equals(name)) |                 .filter(ui -> ui.userName.equals(name)) | ||||||
|                 .findFirst() |                 .findFirst() | ||||||
|                 .orElseThrow(); |                 .orElseThrow(NoSuchElementException::new); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 anhefti
						anhefti