mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
Create endpoint for groups
This commit is contained in:
@@ -15,6 +15,7 @@ public class VndMediaType {
|
||||
public static final String USER = PREFIX + "user" + SUFFIX;
|
||||
public static final String GROUP = PREFIX + "group" + SUFFIX;
|
||||
public static final String USER_COLLECTION = PREFIX + "userCollection" + SUFFIX;
|
||||
public static final String GROUP_COLLECTION = PREFIX + "groupCollection" + SUFFIX;
|
||||
|
||||
private VndMediaType() {
|
||||
}
|
||||
|
||||
@@ -1,4 +1,78 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
public class GroupCollectionResource {
|
||||
import com.google.inject.Inject;
|
||||
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
|
||||
import com.webcohesion.enunciate.metadata.rs.ResponseHeader;
|
||||
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
|
||||
import com.webcohesion.enunciate.metadata.rs.TypeHint;
|
||||
import sonia.scm.api.rest.resources.AbstractManagerResource;
|
||||
import sonia.scm.group.Group;
|
||||
import sonia.scm.group.GroupException;
|
||||
import sonia.scm.group.GroupManager;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.GenericEntity;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
@Produces(VndMediaType.GROUP_COLLECTION)
|
||||
public class GroupCollectionResource extends AbstractManagerResource<Group, GroupException> {
|
||||
public static final int DEFAULT_PAGE_SIZE = 10;
|
||||
private final GroupDto2GroupMapper dtoToGroupMapper;
|
||||
private final Group2GroupDtoMapper groupToDtoMapper;
|
||||
|
||||
@Inject
|
||||
public GroupCollectionResource(GroupManager manager, GroupDto2GroupMapper dtoToGroupMapper, Group2GroupDtoMapper groupToDtoMapper) {
|
||||
super(manager);
|
||||
this.dtoToGroupMapper = dtoToGroupMapper;
|
||||
this.groupToDtoMapper = groupToDtoMapper;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new group.
|
||||
* @param groupDto The group to be created.
|
||||
* @return A response with the link to the new group (if created successfully).
|
||||
*/
|
||||
@POST
|
||||
@Path("")
|
||||
@StatusCodes({
|
||||
@ResponseCode(code = 201, condition = "create success", additionalHeaders = {
|
||||
@ResponseHeader(name = "Location", description = "uri to the created group")
|
||||
}),
|
||||
@ResponseCode(code = 403, condition = "forbidden, the current user has no admin privileges"),
|
||||
@ResponseCode(code = 500, condition = "internal server error")
|
||||
})
|
||||
@TypeHint(TypeHint.NO_CONTENT.class)
|
||||
@Consumes(VndMediaType.GROUP)
|
||||
public Response create(@Context UriInfo uriInfo, GroupDto groupDto) throws IOException, GroupException {
|
||||
Group group = dtoToGroupMapper.groupDtoToGroup(groupDto);
|
||||
System.out.println(group);
|
||||
manager.create(group);
|
||||
|
||||
LinkBuilder builder = new LinkBuilder(uriInfo, GroupV2Resource.class, GroupSubResource.class);
|
||||
return Response.created(builder.method("getGroupSubResource").parameters(group.getName()).method("get").parameters().create()).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GenericEntity<Collection<Group>> createGenericEntity(Collection<Group> items) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getId(Group item) {
|
||||
return item.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getPathPart() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.github.sdorra.shiro.ShiroRule;
|
||||
import com.github.sdorra.shiro.SubjectAware;
|
||||
import com.google.common.io.Resources;
|
||||
import org.jboss.resteasy.core.Dispatcher;
|
||||
import org.jboss.resteasy.mock.MockDispatcherFactory;
|
||||
import org.jboss.resteasy.mock.MockHttpRequest;
|
||||
import org.jboss.resteasy.mock.MockHttpResponse;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import sonia.scm.group.Group;
|
||||
import sonia.scm.group.GroupException;
|
||||
import sonia.scm.group.GroupManager;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
|
||||
@SubjectAware(
|
||||
username = "trillian",
|
||||
password = "secret",
|
||||
configuration = "classpath:sonia/scm/repository/shiro.ini"
|
||||
)
|
||||
public class GroupV2ResourceTest {
|
||||
|
||||
@Rule
|
||||
public ShiroRule shiro = new ShiroRule();
|
||||
|
||||
private Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
||||
|
||||
@Mock
|
||||
private GroupManager groupManager;
|
||||
@InjectMocks
|
||||
GroupDto2GroupMapperImpl dtoToGroupMapper;
|
||||
@InjectMocks
|
||||
Group2GroupDtoMapperImpl groupToDtoMapper;
|
||||
|
||||
ArgumentCaptor<Group> groupCaptor = ArgumentCaptor.forClass(Group.class);
|
||||
|
||||
@Before
|
||||
public void prepareEnvironment() throws IOException, GroupException {
|
||||
initMocks(this);
|
||||
doNothing().when(groupManager).create(groupCaptor.capture());
|
||||
|
||||
GroupCollectionResource groupCollectionResource = new GroupCollectionResource(groupManager, dtoToGroupMapper, groupToDtoMapper);
|
||||
GroupSubResource groupSubResource = new GroupSubResource(groupToDtoMapper);
|
||||
GroupV2Resource groupV2Resource = new GroupV2Resource(groupCollectionResource, groupSubResource);
|
||||
|
||||
dispatcher.getRegistry().addSingletonResource(groupV2Resource);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateNewGroupWithMembers() throws URISyntaxException, IOException {
|
||||
URL url = Resources.getResource("sonia/scm/api/v2/group-test-create.json");
|
||||
byte[] groupJson = Resources.toByteArray(url);
|
||||
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.post("/" + GroupV2Resource.GROUPS_PATH_V2)
|
||||
.contentType(VndMediaType.GROUP)
|
||||
.content(groupJson);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(201, response.getStatus());
|
||||
Group createdGroup = groupCaptor.getValue();
|
||||
assertNotNull(createdGroup);
|
||||
assertEquals(2, createdGroup.getMembers().size());
|
||||
assertEquals("user1", createdGroup.getMembers().get(0));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"description": "Tolle Gruppe",
|
||||
"name": "dev",
|
||||
"type": "developers",
|
||||
"_embedded": {
|
||||
"members": [
|
||||
{
|
||||
"name": "user1"
|
||||
},
|
||||
{
|
||||
"name": "user2"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user