mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-17 18:51:10 +01:00
Implemented handling of concurrent modification of objects
based on lastModified date
This commit is contained in:
@@ -4,6 +4,7 @@ import org.jboss.resteasy.core.Dispatcher;
|
||||
import org.jboss.resteasy.mock.MockDispatcherFactory;
|
||||
import sonia.scm.api.rest.AlreadyExistsExceptionMapper;
|
||||
import sonia.scm.api.rest.AuthorizationExceptionMapper;
|
||||
import sonia.scm.api.rest.ConcurrentModificationExceptionMapper;
|
||||
|
||||
public class DispatcherMock {
|
||||
public static Dispatcher createDispatcher(Object resource) {
|
||||
@@ -12,6 +13,7 @@ public class DispatcherMock {
|
||||
dispatcher.getProviderFactory().registerProvider(NotFoundExceptionMapper.class);
|
||||
dispatcher.getProviderFactory().registerProvider(AlreadyExistsExceptionMapper.class);
|
||||
dispatcher.getProviderFactory().registerProvider(AuthorizationExceptionMapper.class);
|
||||
dispatcher.getProviderFactory().registerProvider(ConcurrentModificationExceptionMapper.class);
|
||||
return dispatcher;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import com.fasterxml.jackson.databind.node.TextNode;
|
||||
import org.junit.Test;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
import sonia.scm.group.Group;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class GroupDtoToGroupMapperTest {
|
||||
@@ -16,8 +16,10 @@ public class GroupDtoToGroupMapperTest {
|
||||
public void shouldMapAttributes() {
|
||||
GroupDto dto = new GroupDto();
|
||||
dto.setName("group");
|
||||
dto.setLastModified(Instant.ofEpochMilli(1234));
|
||||
Group group = Mappers.getMapper(GroupDtoToGroupMapper.class).map(dto);
|
||||
assertEquals("group", group.getName());
|
||||
assertThat(group.getLastModified()).isEqualTo(dto.getLastModified().toEpochMilli());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -13,6 +13,8 @@ import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import sonia.scm.PageResult;
|
||||
import sonia.scm.api.rest.JSONContextResolver;
|
||||
import sonia.scm.api.rest.ObjectMapperProvider;
|
||||
import sonia.scm.group.Group;
|
||||
import sonia.scm.group.GroupManager;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
@@ -28,8 +30,8 @@ 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.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.MockitoAnnotations.initMocks;
|
||||
@@ -74,6 +76,7 @@ public class GroupRootResourceTest {
|
||||
GroupRootResource groupRootResource = new GroupRootResource(MockProvider.of(groupCollectionResource), MockProvider.of(groupResource));
|
||||
|
||||
dispatcher = createDispatcher(groupRootResource);
|
||||
dispatcher.getProviderFactory().registerProviderInstance(new JSONContextResolver(new ObjectMapperProvider().get()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -143,6 +146,23 @@ public class GroupRootResourceTest {
|
||||
assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateShouldFailOnConcurrentModification() throws URISyntaxException, IOException {
|
||||
URL url = Resources.getResource("sonia/scm/api/v2/group-test-update-concurrent-modification.json");
|
||||
byte[] groupJson = Resources.toByteArray(url);
|
||||
|
||||
MockHttpRequest request = MockHttpRequest
|
||||
.put("/" + GroupRootResource.GROUPS_PATH_V2 + "admin")
|
||||
.contentType(VndMediaType.GROUP)
|
||||
.content(groupJson);
|
||||
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(HttpServletResponse.SC_CONFLICT, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDeleteGroup() throws URISyntaxException {
|
||||
Group group = createDummyGroup();
|
||||
@@ -216,6 +236,7 @@ public class GroupRootResourceTest {
|
||||
group.setName("admin");
|
||||
group.setCreationDate(0L);
|
||||
group.setMembers(Collections.singletonList("user"));
|
||||
group.setLastModified(1234L);
|
||||
return group;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user