mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 07:55:47 +01:00
Adds test for HgConfigInstallationsResource, its DTO and mappers.
This commit is contained in:
@@ -16,6 +16,8 @@ import javax.ws.rs.Produces;
|
|||||||
|
|
||||||
public class HgConfigInstallationsResource {
|
public class HgConfigInstallationsResource {
|
||||||
|
|
||||||
|
public static final String PATH_HG = "hg";
|
||||||
|
public static final String PATH_PYTHON = "python";
|
||||||
private final HgConfigInstallationsToDtoMapper hgConfigInstallationsToDtoMapper;
|
private final HgConfigInstallationsToDtoMapper hgConfigInstallationsToDtoMapper;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@@ -27,7 +29,7 @@ public class HgConfigInstallationsResource {
|
|||||||
* Returns the hg installations.
|
* Returns the hg installations.
|
||||||
*/
|
*/
|
||||||
@GET
|
@GET
|
||||||
@Path("hg")
|
@Path(PATH_HG)
|
||||||
@Produces(HgVndMediaType.INSTALLATIONS)
|
@Produces(HgVndMediaType.INSTALLATIONS)
|
||||||
@TypeHint(HalRepresentation.class)
|
@TypeHint(HalRepresentation.class)
|
||||||
@StatusCodes({
|
@StatusCodes({
|
||||||
@@ -40,14 +42,15 @@ public class HgConfigInstallationsResource {
|
|||||||
|
|
||||||
ConfigurationPermissions.read(HgConfig.PERMISSION).check();
|
ConfigurationPermissions.read(HgConfig.PERMISSION).check();
|
||||||
|
|
||||||
return hgConfigInstallationsToDtoMapper.map(HgInstallerFactory.createInstaller().getHgInstallations());
|
return hgConfigInstallationsToDtoMapper.map(
|
||||||
|
HgInstallerFactory.createInstaller().getHgInstallations(), PATH_HG);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the python installations.
|
* Returns the python installations.
|
||||||
*/
|
*/
|
||||||
@GET
|
@GET
|
||||||
@Path("python")
|
@Path(PATH_PYTHON)
|
||||||
@Produces(HgVndMediaType.INSTALLATIONS)
|
@Produces(HgVndMediaType.INSTALLATIONS)
|
||||||
@TypeHint(HalRepresentation.class)
|
@TypeHint(HalRepresentation.class)
|
||||||
@StatusCodes({
|
@StatusCodes({
|
||||||
@@ -60,6 +63,7 @@ public class HgConfigInstallationsResource {
|
|||||||
|
|
||||||
ConfigurationPermissions.read(HgConfig.PERMISSION).check();
|
ConfigurationPermissions.read(HgConfig.PERMISSION).check();
|
||||||
|
|
||||||
return hgConfigInstallationsToDtoMapper.map(HgInstallerFactory.createInstaller().getPythonInstallations());
|
return hgConfigInstallationsToDtoMapper.map(
|
||||||
|
HgInstallerFactory.createInstaller().getPythonInstallations(), PATH_PYTHON);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,26 @@
|
|||||||
package sonia.scm.api.v2.resources;
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
import de.otto.edison.hal.HalRepresentation;
|
import com.google.inject.Inject;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static de.otto.edison.hal.Links.linkingTo;
|
import static de.otto.edison.hal.Links.linkingTo;
|
||||||
|
|
||||||
public class HgConfigInstallationsToDtoMapper {
|
public class HgConfigInstallationsToDtoMapper {
|
||||||
@Inject private UriInfoStore uriInfoStore;
|
|
||||||
|
|
||||||
public HalRepresentation map(List<String> installations) {
|
private UriInfoStore uriInfoStore;
|
||||||
return new HgConfigInstallationsDto(linkingTo().self(createSelfLink()).build(), installations);
|
|
||||||
|
@Inject
|
||||||
|
public HgConfigInstallationsToDtoMapper(UriInfoStore uriInfoStore, String path) {
|
||||||
|
this.uriInfoStore = uriInfoStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String createSelfLink() {
|
public HgConfigInstallationsDto map(List<String> installations, String path) {
|
||||||
LinkBuilder linkBuilder = new LinkBuilder(uriInfoStore.get(), HgConfigInstallationsResource.class);
|
return new HgConfigInstallationsDto(linkingTo().self(createSelfLink(path)).build(), installations);
|
||||||
return linkBuilder.method("get").parameters().href();
|
}
|
||||||
|
|
||||||
|
private String createSelfLink(String path) {
|
||||||
|
LinkBuilder linkBuilder = new LinkBuilder(uriInfoStore.get(), HgConfigResource.class);
|
||||||
|
return linkBuilder.method("getInstallationsResource").parameters().href() + '/' + path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import sonia.scm.installer.HgPackage;
|
|||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
// TODO could this be simplified similar to HgConfigInstallationsToDtoMapper?
|
||||||
|
// That is, do we really need the packages as _embedded list?
|
||||||
public class HgConfigPackageCollectionToDtoMapper extends CollectionToDtoMapper<HgPackage, HgConfigPackageDto> {
|
public class HgConfigPackageCollectionToDtoMapper extends CollectionToDtoMapper<HgPackage, HgConfigPackageDto> {
|
||||||
|
|
||||||
static final String COLLECTION_NAME = "packages";
|
static final String COLLECTION_NAME = "packages";
|
||||||
|
|||||||
@@ -0,0 +1,118 @@
|
|||||||
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
|
import com.github.sdorra.shiro.ShiroRule;
|
||||||
|
import com.github.sdorra.shiro.SubjectAware;
|
||||||
|
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.junit.rules.ExpectedException;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Answers;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.runners.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
import javax.inject.Provider;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@SubjectAware(
|
||||||
|
configuration = "classpath:sonia/scm/configuration/shiro.ini",
|
||||||
|
password = "secret"
|
||||||
|
)
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class HgConfigInstallationsResourceTest {
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public ShiroRule shiro = new ShiroRule();
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public ExpectedException thrown = ExpectedException.none();
|
||||||
|
|
||||||
|
private Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
||||||
|
|
||||||
|
private final URI baseUri = URI.create("/");
|
||||||
|
|
||||||
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
|
private UriInfoStore uriInfoStore;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private HgConfigInstallationsToDtoMapper mapper;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Provider<HgConfigInstallationsResource> resourceProvider;
|
||||||
|
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void prepareEnvironment() {
|
||||||
|
HgConfigInstallationsResource resource = new HgConfigInstallationsResource(mapper);
|
||||||
|
|
||||||
|
when(resourceProvider.get()).thenReturn(resource);
|
||||||
|
dispatcher.getRegistry().addSingletonResource(
|
||||||
|
new HgConfigResource(null, null, null, null,
|
||||||
|
null, resourceProvider));
|
||||||
|
|
||||||
|
when(uriInfoStore.get().getBaseUri()).thenReturn(baseUri);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SubjectAware(username = "readOnly")
|
||||||
|
public void shouldGetHgInstallations() throws Exception {
|
||||||
|
MockHttpResponse response = get("hg");
|
||||||
|
|
||||||
|
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
||||||
|
|
||||||
|
String contentAsString = response.getContentAsString();
|
||||||
|
assertThat(contentAsString).contains("{\"paths\":[");
|
||||||
|
assertThat(contentAsString).contains("hg");
|
||||||
|
assertThat(contentAsString).doesNotContain("python");
|
||||||
|
|
||||||
|
assertThat(contentAsString).contains("\"self\":{\"href\":\"/v2/config/hg/installations/hg");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SubjectAware(username = "writeOnly")
|
||||||
|
public void shouldGetHgInstallationsOnlyWhenAuthorized() throws Exception {
|
||||||
|
thrown.expectMessage("Subject does not have permission [configuration:read:hg]");
|
||||||
|
|
||||||
|
get("hg");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SubjectAware(username = "readOnly")
|
||||||
|
public void shouldGetPythonInstallations() throws Exception {
|
||||||
|
MockHttpResponse response = get("python");
|
||||||
|
|
||||||
|
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
|
||||||
|
|
||||||
|
String contentAsString = response.getContentAsString();
|
||||||
|
assertThat(contentAsString).contains("{\"paths\":[");
|
||||||
|
assertThat(contentAsString).contains("python");
|
||||||
|
|
||||||
|
assertThat(contentAsString).contains("\"self\":{\"href\":\"/v2/config/hg/installations/python");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SubjectAware(username = "writeOnly")
|
||||||
|
public void shouldGetPythonInstallationsOnlyWhenAuthorized() throws Exception {
|
||||||
|
thrown.expectMessage("Subject does not have permission [configuration:read:hg]");
|
||||||
|
|
||||||
|
get("python");
|
||||||
|
}
|
||||||
|
|
||||||
|
private MockHttpResponse get(String path) throws URISyntaxException {
|
||||||
|
MockHttpRequest request = MockHttpRequest.get("/" + HgConfigResource.HG_CONFIG_PATH_V2 + "/installations/" + path);
|
||||||
|
MockHttpResponse response = new MockHttpResponse();
|
||||||
|
dispatcher.invoke(request, response);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package sonia.scm.api.v2.resources;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Answers;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.runners.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class HgConfigInstallationsToDtoMapperTest {
|
||||||
|
|
||||||
|
|
||||||
|
private URI baseUri = URI.create("http://example.com/base/");
|
||||||
|
|
||||||
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
|
private UriInfoStore uriInfoStore;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private HgConfigInstallationsToDtoMapper mapper;
|
||||||
|
|
||||||
|
private URI expectedBaseUri;
|
||||||
|
|
||||||
|
private String expectedPath = "path";
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
when(uriInfoStore.get().getBaseUri()).thenReturn(baseUri);
|
||||||
|
expectedBaseUri = baseUri.resolve(HgConfigResource.HG_CONFIG_PATH_V2 + "/installations/" + expectedPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldMapFields() {
|
||||||
|
List<String> installations = Arrays.asList("/hg", "/bin/hg");
|
||||||
|
|
||||||
|
HgConfigInstallationsDto dto = mapper.map(installations, expectedPath);
|
||||||
|
|
||||||
|
assertThat(dto.getPaths()).isEqualTo(installations);
|
||||||
|
|
||||||
|
assertEquals(expectedBaseUri.toString(), dto.getLinks().getLinkBy("self").get().getHref());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,7 +13,6 @@ import org.junit.Before;
|
|||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.rules.ExpectedException;
|
import org.junit.rules.ExpectedException;
|
||||||
import org.junit.rules.TemporaryFolder;
|
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Answers;
|
import org.mockito.Answers;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
@@ -53,9 +52,6 @@ public class HgConfigPackageResourceTest {
|
|||||||
@Rule
|
@Rule
|
||||||
public ExpectedException thrown = ExpectedException.none();
|
public ExpectedException thrown = ExpectedException.none();
|
||||||
|
|
||||||
@Rule
|
|
||||||
public TemporaryFolder folder= new TemporaryFolder();
|
|
||||||
|
|
||||||
private Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
private Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
|
||||||
|
|
||||||
private final URI baseUri = java.net.URI.create("/");
|
private final URI baseUri = java.net.URI.create("/");
|
||||||
|
|||||||
Reference in New Issue
Block a user