#18: GET /group + fix JUnit

This commit is contained in:
jendib 2016-03-19 23:42:36 +01:00
parent 3b9a66d1d8
commit c1c2228937
8 changed files with 78 additions and 12 deletions

View File

@ -68,7 +68,8 @@ public class AclDao {
@SuppressWarnings("unchecked")
public List<AclDto> getBySourceId(String sourceId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
StringBuilder sb = new StringBuilder("select a.ACL_ID_C, a.ACL_PERM_C, a.ACL_TARGETID_C, u.USE_USERNAME_C, s.SHA_NAME_C, g.GRP_NAME_C ");
StringBuilder sb = new StringBuilder("select a.ACL_ID_C, a.ACL_PERM_C, a.ACL_TARGETID_C, ");
sb.append(" u.USE_USERNAME_C, s.SHA_ID_C, s.SHA_NAME_C, g.GRP_NAME_C ");
sb.append(" from T_ACL a ");
sb.append(" left join T_USER u on u.USE_ID_C = a.ACL_TARGETID_C ");
sb.append(" left join T_SHARE s on s.SHA_ID_C = a.ACL_TARGETID_C ");
@ -89,13 +90,14 @@ public class AclDao {
aclDto.setPerm(PermType.valueOf((String) o[i++]));
aclDto.setTargetId((String) o[i++]);
String userName = (String) o[i++];
String shareId = (String) o[i++];
String shareName = (String) o[i++];
String groupName = (String) o[i++];
if (userName != null) {
aclDto.setTargetName(userName);
aclDto.setTargetType(AclTargetType.USER.name());
}
if (shareName != null) {
if (shareId != null) { // Use ID because share name is nullable
aclDto.setTargetName(shareName);
aclDto.setTargetType(AclTargetType.SHARE.name());
}

View File

@ -152,11 +152,12 @@ public class GroupDao {
Map<String, Object> parameterMap = new HashMap<String, Object>();
List<String> criteriaList = new ArrayList<String>();
StringBuilder sb = new StringBuilder("select g.GRP_ID_C as c0, g.GRP_NAME_C as c1, g.GRP_IDPARENT_C as c2 ");
StringBuilder sb = new StringBuilder("select g.GRP_ID_C as c0, g.GRP_NAME_C as c1, g.GRP_IDPARENT_C as c2, gp.GRP_NAME_C as c3 ");
if (criteria.getUserId() != null) {
sb.append(" , ug.UGP_ID_C ");
}
sb.append(" from T_GROUP g ");
sb.append(" left join T_GROUP gp on g.GRP_IDPARENT_C = gp.GRP_ID_C ");
// Add search criterias
if (criteria.getSearch() != null) {
@ -190,7 +191,8 @@ public class GroupDao {
GroupDto groupDto = new GroupDto()
.setId((String) o[i++])
.setName((String) o[i++])
.setParentId((String) o[i++]);
.setParentId((String) o[i++])
.setParentName((String) o[i++]);
groupDtoList.add(groupDto);
if (criteria.getUserId() != null && o[i++] != null) {
userGroupDtoList.add(groupDto);

View File

@ -21,6 +21,11 @@ public class GroupDto {
*/
private String parentId;
/**
* Parent name.
*/
private String parentName;
public String getId() {
return id;
}
@ -48,6 +53,15 @@ public class GroupDto {
return this;
}
public String getParentName() {
return parentName;
}
public GroupDto setParentName(String parentName) {
this.parentName = parentName;
return this;
}
@Override
public boolean equals(Object obj) {
return id.equals(((GroupDto) obj).getId());

View File

@ -30,20 +30,20 @@ public class Acl implements Loggable {
/**
* ACL permission.
*/
@Column(name = "ACL_PERM_C", length = 30)
@Column(name = "ACL_PERM_C", length = 30, nullable = false)
@Enumerated(EnumType.STRING)
private PermType perm;
/**
* ACL source ID.
*/
@Column(name = "ACL_SOURCEID_C", length = 36)
@Column(name = "ACL_SOURCEID_C", length = 36, nullable = false)
private String sourceId;
/**
* ACL target ID.
*/
@Column(name = "ACL_TARGETID_C", length = 36)
@Column(name = "ACL_TARGETID_C", length = 36, nullable = false)
private String targetId;
/**

View File

@ -4,12 +4,15 @@ import java.text.MessageFormat;
import java.util.List;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
@ -21,9 +24,11 @@ import com.sismics.docs.core.dao.jpa.dto.GroupDto;
import com.sismics.docs.core.model.jpa.Group;
import com.sismics.docs.core.model.jpa.User;
import com.sismics.docs.core.model.jpa.UserGroup;
import com.sismics.docs.core.util.jpa.SortCriteria;
import com.sismics.docs.rest.constant.BaseFunction;
import com.sismics.rest.exception.ClientException;
import com.sismics.rest.exception.ForbiddenClientException;
import com.sismics.rest.util.JsonUtil;
import com.sismics.rest.util.ValidationUtil;
/**
@ -177,4 +182,35 @@ public class GroupResource extends BaseResource {
.add("status", "ok");
return Response.ok().entity(response.build()).build();
}
/**
* Returns all active groups.
*
* @param sortColumn Sort index
* @param asc If true, ascending sorting, else descending
* @return Response
*/
@GET
public Response list(
@QueryParam("sort_column") Integer sortColumn,
@QueryParam("asc") Boolean asc) {
if (!authenticate()) {
throw new ForbiddenClientException();
}
JsonArrayBuilder groups = Json.createArrayBuilder();
SortCriteria sortCriteria = new SortCriteria(sortColumn, asc);
GroupDao groupDao = new GroupDao();
List<GroupDto> groupDtoList = groupDao.findByCriteria(new GroupCriteria(), sortCriteria);
for (GroupDto groupDto : groupDtoList) {
groups.add(Json.createObjectBuilder()
.add("name", groupDto.getName())
.add("parent", JsonUtil.nullable(groupDto.getParentName())));
}
JsonObjectBuilder response = Json.createObjectBuilder()
.add("groups", groups);
return Response.ok().entity(response.build()).build();
}
}

View File

@ -532,8 +532,6 @@ public class UserResource extends BaseResource {
/**
* Returns all active users.
*
* @param limit Page limit
* @param offset Page offset
* @param sortColumn Sort index
* @param asc If true, ascending sorting, else descending
* @return Response

View File

@ -108,7 +108,8 @@ public class TestCommentResource extends BaseJerseyTest {
.put(Entity.form(new Form()
.param("source", document1Id)
.param("perm", "READ")
.param("username", "comment2")), JsonObject.class);
.param("target", "comment2")
.param("type", "USER")), JsonObject.class);
// Create a comment with comment2
json = target().path("/comment").request()

View File

@ -41,11 +41,24 @@ public class TestGroupResource extends BaseJerseyTest {
clientUtil.createUser("group1", "g112", "g12");
String group1Token = clientUtil.login("group1");
// Check admin groups (all computed groups)
JsonObject json = target().path("/user").request()
// Get all groups
JsonObject json = target().path("/group")
.queryParam("sort_column", "1")
.queryParam("asc", "true")
.request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.get(JsonObject.class);
JsonArray groups = json.getJsonArray("groups");
Assert.assertEquals(6, groups.size());
JsonObject groupG11 = groups.getJsonObject(2);
Assert.assertEquals("g11", groupG11.getString("name"));
Assert.assertEquals("g1", groupG11.getString("parent"));
// Check admin groups (all computed groups)
json = target().path("/user").request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.get(JsonObject.class);
groups = json.getJsonArray("groups");
Assert.assertEquals(1, groups.size());
Assert.assertEquals("administrators", groups.getString(0));