Adds test for HgConfigInstallationsResource, its DTO and mappers.

This commit is contained in:
Johannes Schnatterer
2018-08-06 17:00:50 +02:00
parent 7d59975c80
commit 45e48e1834
6 changed files with 192 additions and 16 deletions

View File

@@ -16,6 +16,8 @@ import javax.ws.rs.Produces;
public class HgConfigInstallationsResource {
public static final String PATH_HG = "hg";
public static final String PATH_PYTHON = "python";
private final HgConfigInstallationsToDtoMapper hgConfigInstallationsToDtoMapper;
@Inject
@@ -27,7 +29,7 @@ public class HgConfigInstallationsResource {
* Returns the hg installations.
*/
@GET
@Path("hg")
@Path(PATH_HG)
@Produces(HgVndMediaType.INSTALLATIONS)
@TypeHint(HalRepresentation.class)
@StatusCodes({
@@ -40,14 +42,15 @@ public class HgConfigInstallationsResource {
ConfigurationPermissions.read(HgConfig.PERMISSION).check();
return hgConfigInstallationsToDtoMapper.map(HgInstallerFactory.createInstaller().getHgInstallations());
return hgConfigInstallationsToDtoMapper.map(
HgInstallerFactory.createInstaller().getHgInstallations(), PATH_HG);
}
/**
* Returns the python installations.
*/
@GET
@Path("python")
@Path(PATH_PYTHON)
@Produces(HgVndMediaType.INSTALLATIONS)
@TypeHint(HalRepresentation.class)
@StatusCodes({
@@ -60,6 +63,7 @@ public class HgConfigInstallationsResource {
ConfigurationPermissions.read(HgConfig.PERMISSION).check();
return hgConfigInstallationsToDtoMapper.map(HgInstallerFactory.createInstaller().getPythonInstallations());
return hgConfigInstallationsToDtoMapper.map(
HgInstallerFactory.createInstaller().getPythonInstallations(), PATH_PYTHON);
}
}

View File

@@ -1,21 +1,26 @@
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 static de.otto.edison.hal.Links.linkingTo;
public class HgConfigInstallationsToDtoMapper {
@Inject private UriInfoStore uriInfoStore;
public HalRepresentation map(List<String> installations) {
return new HgConfigInstallationsDto(linkingTo().self(createSelfLink()).build(), installations);
private UriInfoStore uriInfoStore;
@Inject
public HgConfigInstallationsToDtoMapper(UriInfoStore uriInfoStore, String path) {
this.uriInfoStore = uriInfoStore;
}
private String createSelfLink() {
LinkBuilder linkBuilder = new LinkBuilder(uriInfoStore.get(), HgConfigInstallationsResource.class);
return linkBuilder.method("get").parameters().href();
public HgConfigInstallationsDto map(List<String> installations, String path) {
return new HgConfigInstallationsDto(linkingTo().self(createSelfLink(path)).build(), installations);
}
private String createSelfLink(String path) {
LinkBuilder linkBuilder = new LinkBuilder(uriInfoStore.get(), HgConfigResource.class);
return linkBuilder.method("getInstallationsResource").parameters().href() + '/' + path;
}
}

View File

@@ -4,6 +4,8 @@ import sonia.scm.installer.HgPackage;
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> {
static final String COLLECTION_NAME = "packages";