Create endpoint for groups

This commit is contained in:
René Pfeuffer
2018-06-13 12:45:32 +02:00
parent 6dfd0353ce
commit 8bfe6bacb0
4 changed files with 173 additions and 1 deletions

View File

@@ -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));
}
}