From 72276dcaf2f110fe8565ab982d76b900add813c0 Mon Sep 17 00:00:00 2001 From: srosse <none@none> Date: Wed, 15 Aug 2012 15:39:15 +0200 Subject: [PATCH] CL-155: add a method to create groups with the REST API --- .../group/LearningGroupWebService.java | 43 +++++++++++++++++++ .../java/org/olat/restapi/GroupMgmtTest.java | 27 ++++++++++++ 2 files changed, 70 insertions(+) diff --git a/src/main/java/org/olat/restapi/group/LearningGroupWebService.java b/src/main/java/org/olat/restapi/group/LearningGroupWebService.java index 7f340f72957..23c07a370a9 100644 --- a/src/main/java/org/olat/restapi/group/LearningGroupWebService.java +++ b/src/main/java/org/olat/restapi/group/LearningGroupWebService.java @@ -179,6 +179,49 @@ public class LearningGroupWebService { 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. * @response.representation.qname {http://www.example.com}groupVO diff --git a/src/test/java/org/olat/restapi/GroupMgmtTest.java b/src/test/java/org/olat/restapi/GroupMgmtTest.java index 70329e783d2..1566774b5f5 100644 --- a/src/test/java/org/olat/restapi/GroupMgmtTest.java +++ b/src/test/java/org/olat/restapi/GroupMgmtTest.java @@ -415,6 +415,33 @@ public class GroupMgmtTest extends OlatJerseyTestCase { 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 public void testDeleteCourseGroup() throws IOException, URISyntaxException { assertTrue(conn.login("administrator", "openolat")); -- GitLab