Skip to content
Snippets Groups Projects
Commit 72276dca authored by srosse's avatar srosse
Browse files

CL-155: add a method to create groups with the REST API

parent 26ae51e4
No related branches found
No related tags found
No related merge requests found
...@@ -179,6 +179,49 @@ public class LearningGroupWebService { ...@@ -179,6 +179,49 @@ public class LearningGroupWebService {
return response.build(); return response.build();
} }
/**
* Create a group.
* @response.representation.qname {http://www.example.com}groupVO
* @response.representation.mediaType application/xml, application/json
* @response.representation.doc A business group in the OLAT system
* @response.representation.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_GROUPVO}
* @response.representation.200.qname {http://www.example.com}groupVO
* @response.representation.200.mediaType application/xml, application/json
* @response.representation.200.doc The saved business group
* @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_GROUPVO}
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.404.doc The business group cannot be found
* @param groupKey The key of the group
* @param group The group
* @param request The HTTP request
* @return
*/
@PUT
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response createGroup(final GroupVO group, @Context HttpServletRequest request) {
Identity identity = RestSecurityHelper.getIdentity(request);
if(identity == null || !isGroupManager(request)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
final BusinessGroupService bgs = CoreSpringFactory.getImpl(BusinessGroupService.class);
if(group.getKey() != null && group.getKey().longValue() > 0) {
return postGroup(group.getKey(), group, request);
}
if(!StringHelper.containsNonWhitespace(group.getName())) {
return Response.serverError().status(Status.NOT_ACCEPTABLE).build();
}
Integer minPart = normalize(group.getMinParticipants());
Integer maxPart = normalize(group.getMaxParticipants());
BusinessGroup newBG = bgs.createBusinessGroup(identity, group.getName(), group.getDescription(), minPart, maxPart, false, false, null);
GroupVO savedVO = ObjectFactory.get(newBG);
return Response.ok(savedVO).build();
}
/** /**
* Updates a group. * Updates a group.
* @response.representation.qname {http://www.example.com}groupVO * @response.representation.qname {http://www.example.com}groupVO
......
...@@ -415,6 +415,33 @@ public class GroupMgmtTest extends OlatJerseyTestCase { ...@@ -415,6 +415,33 @@ public class GroupMgmtTest extends OlatJerseyTestCase {
assertEquals(bg.getDescription(), "rest-g1 description"); assertEquals(bg.getDescription(), "rest-g1 description");
} }
@Test
public void testCreateCourseGroup() throws IOException, URISyntaxException {
assertTrue(conn.login("administrator", "openolat"));
GroupVO vo = new GroupVO();
vo.setName("rest-g5-new");
vo.setDescription("rest-g5 description");
vo.setType("BuddyGroup");
URI request = UriBuilder.fromUri(getContextURI()).path("groups").build();
HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true);
conn.addJsonEntity(method, vo);
HttpResponse response = conn.execute(method);
assertTrue(response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 201);
GroupVO newGroupVo = conn.parse(response, GroupVO.class);
assertNotNull(newGroupVo);
BusinessGroup bg = businessGroupService.loadBusinessGroup(newGroupVo.getKey());
assertNotNull(bg);
assertEquals(bg.getKey(), newGroupVo.getKey());
assertEquals(bg.getName(), "rest-g5-new");
assertEquals(bg.getDescription(), "rest-g5 description");
}
@Test @Test
public void testDeleteCourseGroup() throws IOException, URISyntaxException { public void testDeleteCourseGroup() throws IOException, URISyntaxException {
assertTrue(conn.login("administrator", "openolat")); assertTrue(conn.login("administrator", "openolat"));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment