Merged changes

This commit is contained in:
Philipp Czora
2018-06-25 14:30:07 +02:00
4 changed files with 81 additions and 10 deletions

View File

@@ -13,6 +13,7 @@ import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import sonia.scm.PageResult;
import sonia.scm.group.Group;
import sonia.scm.group.GroupException;
import sonia.scm.group.GroupManager;
@@ -26,9 +27,12 @@ import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collections;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
@@ -56,8 +60,6 @@ public class GroupRootResourceTest {
private GroupDtoToGroupMapperImpl dtoToGroupMapper;
@InjectMocks
private GroupToGroupDtoMapperImpl groupToDtoMapper;
@InjectMocks
private GroupCollectionToDtoMapper groupCollectionToDtoMapper;
private ArgumentCaptor<Group> groupCaptor = ArgumentCaptor.forClass(Group.class);
@@ -67,8 +69,10 @@ public class GroupRootResourceTest {
doNothing().when(groupManager).create(groupCaptor.capture());
Group group = createDummyGroup();
when(groupManager.getPage(any(), eq(0), eq(10))).thenReturn(new PageResult<>(singletonList(group), 1));
when(groupManager.get("admin")).thenReturn(group);
GroupCollectionToDtoMapper groupCollectionToDtoMapper = new GroupCollectionToDtoMapper(groupToDtoMapper, uriInfoStore);
GroupCollectionResource groupCollectionResource = new GroupCollectionResource(groupManager, dtoToGroupMapper, groupToDtoMapper, groupCollectionToDtoMapper);
GroupResource groupResource = new GroupResource(groupManager, groupToDtoMapper);
GroupRootResource groupRootResource = new GroupRootResource(MockProvider.of(groupCollectionResource), MockProvider.of(groupResource));
@@ -126,6 +130,31 @@ public class GroupRootResourceTest {
assertEquals("user1", createdGroup.getMembers().get(0));
}
@Test
public void shouldFailForMissingContent() throws URISyntaxException {
MockHttpRequest request = MockHttpRequest
.post("/" + GroupRootResource.GROUPS_PATH_V2)
.contentType(VndMediaType.GROUP)
.content(new byte[] {});
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
assertEquals(400, response.getStatus());
}
@Test
public void shouldGetAll() throws URISyntaxException {
MockHttpRequest request = MockHttpRequest.get("/" + GroupRootResource.GROUPS_PATH_V2);
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertTrue(response.getContentAsString().contains("\"name\":\"admin\""));
assertTrue(response.getContentAsString().contains("\"self\":{\"href\":\"/v2/groups/admin\"}"));
}
private Group createDummyGroup() {
Group group = new Group();
group.setName("admin");

View File

@@ -30,11 +30,11 @@ import java.net.URL;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
@@ -73,6 +73,8 @@ public class UserRootResourceTest {
when(userManager.getPage(any(), eq(0), eq(10))).thenReturn(new PageResult<>(singletonList(dummyUser), 1));
when(userManager.get("Neo")).thenReturn(dummyUser);
doNothing().when(userManager).create(userCaptor.capture());
doNothing().when(userManager).modify(userCaptor.capture());
doNothing().when(userManager).delete(userCaptor.capture());
UserCollectionToDtoMapper userCollectionToDtoMapper = new UserCollectionToDtoMapper(userToDtoMapper, uriInfoStore);
UserCollectionResource userCollectionResource = new UserCollectionResource(userManager, dtoToUserMapper, userToDtoMapper,
@@ -115,7 +117,7 @@ public class UserRootResourceTest {
}
@Test
public void shouldCreateNewUserWithEncryptedPassword() throws URISyntaxException, IOException {
public void shouldCreateNewUserWithEncryptedPassword() throws URISyntaxException, IOException, UserException {
URL url = Resources.getResource("sonia/scm/api/v2/user-test-create.json");
byte[] userJson = Resources.toByteArray(url);
@@ -128,12 +130,32 @@ public class UserRootResourceTest {
dispatcher.invoke(request, response);
assertEquals(201, response.getStatus());
assertEquals(HttpServletResponse.SC_CREATED, response.getStatus());
verify(userManager).create(any(User.class));
User createdUser = userCaptor.getValue();
assertNotNull(createdUser);
assertEquals("encrypted123", createdUser.getPassword());
}
@Test
public void shouldUpdateChangedUserWithEncryptedPassword() throws URISyntaxException, IOException, UserException {
URL url = Resources.getResource("sonia/scm/api/v2/user-test-update.json");
byte[] userJson = Resources.toByteArray(url);
MockHttpRequest request = MockHttpRequest
.put("/" + UserRootResource.USERS_PATH_V2 + "Neo")
.contentType(VndMediaType.USER)
.content(userJson);
MockHttpResponse response = new MockHttpResponse();
when(passwordService.encryptPassword("pwd123")).thenReturn("encrypted123");
dispatcher.invoke(request, response);
assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus());
verify(userManager).modify(any(User.class));
User updatedUser = userCaptor.getValue();
assertEquals("encrypted123", updatedUser.getPassword());
}
@Test
public void shouldFailForMissingContent() throws URISyntaxException {
MockHttpRequest request = MockHttpRequest
@@ -145,7 +167,7 @@ public class UserRootResourceTest {
dispatcher.invoke(request, response);
assertEquals(400, response.getStatus());
assertEquals(HttpServletResponse.SC_BAD_REQUEST, response.getStatus());
}
@Test
@@ -158,6 +180,17 @@ public class UserRootResourceTest {
assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus());
}
@Test
public void shouldDeleteUser() throws URISyntaxException, IOException, UserException {
MockHttpRequest request = MockHttpRequest.delete("/" + UserRootResource.USERS_PATH_V2 + "Neo");
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
verify(userManager).delete(any(User.class));
assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus());
}
private User createDummyUser() {
User user = new User();
user.setName("Neo");

View File

@@ -1,9 +1,9 @@
{
"active": true,
"admin": false,
"displayName": "rpf",
"mail": "x@abcde.cd",
"name": "rpf",
"displayName": "someone",
"mail": "x@example.com",
"name": "someone",
"password": "pwd123",
"type": "xml"
}

View File

@@ -0,0 +1,9 @@
{
"active": true,
"admin": false,
"displayName": "Neo",
"mail": "neo@example.com",
"name": "Neo",
"password": "pwd123",
"type": "xml"
}