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";

View File

@@ -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;
}
}

View File

@@ -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());
}
}

View File

@@ -13,7 +13,6 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.InjectMocks;
@@ -53,9 +52,6 @@ public class HgConfigPackageResourceTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Rule
public TemporaryFolder folder= new TemporaryFolder();
private Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
private final URI baseUri = java.net.URI.create("/");