Move info initialization to filter

This commit is contained in:
René Pfeuffer
2018-09-12 09:23:04 +02:00
parent af55bf6117
commit b809abaa45
4 changed files with 14 additions and 16 deletions

View File

@@ -1,7 +1,5 @@
package sonia.scm.api.v2.resources;
import javax.ws.rs.core.UriInfo;
public class ScmPathInfoStore {
private ScmPathInfo pathInfo;
@@ -10,11 +8,11 @@ public class ScmPathInfoStore {
return pathInfo;
}
public void setFromRestRequest(UriInfo uriInfo) {
public void set(ScmPathInfo info) {
if (this.pathInfo != null) {
throw new IllegalStateException("UriInfo already set");
}
this.pathInfo = uriInfo::getBaseUri;
this.pathInfo = info;
}
}

View File

@@ -5,6 +5,7 @@ import sonia.scm.api.v2.resources.ScmPathInfoStore;
import javax.inject.Inject;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.Provider;
@Provider
@@ -19,6 +20,7 @@ public class UriInfoFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) {
storeProvider.get().setFromRestRequest(requestContext.getUriInfo());
UriInfo uriInfo = requestContext.getUriInfo();
storeProvider.get().set(uriInfo::getBaseUri);
}
}

View File

@@ -11,7 +11,6 @@ import org.junit.Test;
import sonia.scm.PageResult;
import sonia.scm.group.Group;
import javax.ws.rs.core.UriInfo;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
@@ -28,7 +27,7 @@ import static sonia.scm.PageResult.createPage;
public class GroupCollectionToDtoMapperTest {
private final UriInfo uriInfo = mock(UriInfo.class);
private final ScmPathInfo uriInfo = mock(ScmPathInfo.class);
private final ScmPathInfoStore scmPathInfoStore = new ScmPathInfoStore();
private final ResourceLinks resourceLinks = new ResourceLinks(scmPathInfoStore);
private final GroupToGroupDtoMapper groupToDtoMapper = mock(GroupToGroupDtoMapper.class);
@@ -41,10 +40,10 @@ public class GroupCollectionToDtoMapperTest {
@Before
public void init() throws URISyntaxException {
scmPathInfoStore.setFromRestRequest(uriInfo);
scmPathInfoStore.set(uriInfo);
URI baseUri = new URI("http://example.com/base/");
expectedBaseUri = baseUri.resolve(GroupRootResource.GROUPS_PATH_V2 + "/");
when(uriInfo.getBaseUri()).thenReturn(baseUri);
when(uriInfo.getApiRestUri()).thenReturn(baseUri);
subjectThreadState.bind();
ThreadContext.bind(subject);
}

View File

@@ -2,7 +2,6 @@ package sonia.scm.api.v2.resources;
import org.junit.Test;
import javax.ws.rs.core.UriInfo;
import java.net.URI;
import static org.junit.Assert.assertSame;
@@ -15,22 +14,22 @@ public class ScmPathInfoStoreTest {
public void shouldReturnSetInfo() {
URI someUri = URI.create("/anything");
UriInfo uriInfo = mock(UriInfo.class);
ScmPathInfo uriInfo = mock(ScmPathInfo.class);
ScmPathInfoStore scmPathInfoStore = new ScmPathInfoStore();
when(uriInfo.getBaseUri()).thenReturn(someUri);
when(uriInfo.getApiRestUri()).thenReturn(someUri);
scmPathInfoStore.setFromRestRequest(uriInfo);
scmPathInfoStore.set(uriInfo);
assertSame(someUri, scmPathInfoStore.get().getApiRestUri());
}
@Test(expected = IllegalStateException.class)
public void shouldFailIfSetTwice() {
UriInfo uriInfo = mock(UriInfo.class);
ScmPathInfo uriInfo = mock(ScmPathInfo.class);
ScmPathInfoStore scmPathInfoStore = new ScmPathInfoStore();
scmPathInfoStore.setFromRestRequest(uriInfo);
scmPathInfoStore.setFromRestRequest(uriInfo);
scmPathInfoStore.set(uriInfo);
scmPathInfoStore.set(uriInfo);
}
}