Fix test to determine number of schema in DB prior to checking pagination

This commit is contained in:
Tim Donohue
2021-03-31 12:18:56 -05:00
parent 34c1e82275
commit b85b15f876

View File

@@ -29,6 +29,7 @@ import org.dspace.app.rest.projection.Projection;
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
import org.dspace.builder.MetadataSchemaBuilder;
import org.dspace.content.MetadataSchema;
import org.dspace.content.factory.ContentServiceFactory;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -271,74 +272,85 @@ public class MetadataSchemaRestRepositoryIT extends AbstractControllerIntegratio
@Test
public void findAllPaginationTest() throws Exception {
// Determine number of schemas from database
int numberOfSchema = ContentServiceFactory.getInstance()
.getMetadataSchemaService().findAll(context).size();
// If we return 6 schema per page, determine number of pages we expect
int pageSize = 6;
int numberOfPages = (int) Math.ceil((double) numberOfSchema / pageSize);
// In these tests we just validate the first 3 pages, as we currently have at least that many schema
getClient().perform(get("/api/core/metadataschemas")
.param("size", "6")
.param("size", String.valueOf(pageSize))
.param("page", "0"))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._embedded.metadataschemas", Matchers.hasItem(matchEntry())))
.andExpect(jsonPath("$._links.first.href", Matchers.allOf(
Matchers.containsString("/api/core/metadataschemas?"),
Matchers.containsString("page=0"), Matchers.containsString("size=6"))))
Matchers.containsString("page=0"), Matchers.containsString("size=" + pageSize))))
.andExpect(jsonPath("$._links.self.href", Matchers.allOf(
Matchers.containsString("/api/core/metadataschemas?"),
Matchers.containsString("page=0"), Matchers.containsString("size=6"))))
Matchers.containsString("page=0"), Matchers.containsString("size=" + pageSize))))
.andExpect(jsonPath("$._links.next.href", Matchers.allOf(
Matchers.containsString("/api/core/metadataschemas?"),
Matchers.containsString("page=1"), Matchers.containsString("size=6"))))
Matchers.containsString("page=1"), Matchers.containsString("size=" + pageSize))))
.andExpect(jsonPath("$._links.last.href", Matchers.allOf(
Matchers.containsString("/api/core/metadataschemas?"),
Matchers.containsString("page=2"), Matchers.containsString("size=6"))))
.andExpect(jsonPath("$.page.totalElements", is(16)))
.andExpect(jsonPath("$.page.totalPages", is(3)))
.andExpect(jsonPath("$.page.size", is(6)));
Matchers.containsString("page=" + (numberOfPages - 1)),
Matchers.containsString("size=" + pageSize))))
.andExpect(jsonPath("$.page.totalElements", is(numberOfSchema)))
.andExpect(jsonPath("$.page.totalPages", is(numberOfPages)))
.andExpect(jsonPath("$.page.size", is(pageSize)));
getClient().perform(get("/api/core/metadataschemas")
.param("size", "6")
.param("size", String.valueOf(pageSize))
.param("page", "1"))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._embedded.metadataschemas", Matchers.hasItem(matchEntry())))
.andExpect(jsonPath("$._links.first.href", Matchers.allOf(
Matchers.containsString("/api/core/metadataschemas?"),
Matchers.containsString("page=0"), Matchers.containsString("size=6"))))
Matchers.containsString("page=0"), Matchers.containsString("size=" + pageSize))))
.andExpect(jsonPath("$._links.prev.href", Matchers.allOf(
Matchers.containsString("/api/core/metadataschemas?"),
Matchers.containsString("page=0"), Matchers.containsString("size=6"))))
Matchers.containsString("page=0"), Matchers.containsString("size=" + pageSize))))
.andExpect(jsonPath("$._links.self.href", Matchers.allOf(
Matchers.containsString("/api/core/metadataschemas?"),
Matchers.containsString("page=1"), Matchers.containsString("size=6"))))
Matchers.containsString("page=1"), Matchers.containsString("size=" + pageSize))))
.andExpect(jsonPath("$._links.next.href", Matchers.allOf(
Matchers.containsString("/api/core/metadataschemas?"),
Matchers.containsString("page=2"), Matchers.containsString("size=6"))))
Matchers.containsString("page=2"), Matchers.containsString("size=" + pageSize))))
.andExpect(jsonPath("$._links.last.href", Matchers.allOf(
Matchers.containsString("/api/core/metadataschemas?"),
Matchers.containsString("page=2"), Matchers.containsString("size=6"))))
.andExpect(jsonPath("$.page.totalElements", is(16)))
.andExpect(jsonPath("$.page.totalPages", is(3)))
.andExpect(jsonPath("$.page.size", is(6)));
Matchers.containsString("page=" + (numberOfPages - 1)),
Matchers.containsString("size=" + pageSize))))
.andExpect(jsonPath("$.page.totalElements", is(numberOfSchema)))
.andExpect(jsonPath("$.page.totalPages", is(numberOfPages)))
.andExpect(jsonPath("$.page.size", is(pageSize)));
getClient().perform(get("/api/core/metadataschemas")
.param("size", "6")
.param("size", String.valueOf(pageSize))
.param("page", "2"))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$._embedded.metadataschemas", Matchers.hasItem(matchEntry())))
.andExpect(jsonPath("$._links.first.href", Matchers.allOf(
Matchers.containsString("/api/core/metadataschemas?"),
Matchers.containsString("page=0"), Matchers.containsString("size=6"))))
Matchers.containsString("page=0"), Matchers.containsString("size=" + pageSize))))
.andExpect(jsonPath("$._links.prev.href", Matchers.allOf(
Matchers.containsString("/api/core/metadataschemas?"),
Matchers.containsString("page=1"), Matchers.containsString("size=6"))))
Matchers.containsString("page=1"), Matchers.containsString("size=" + pageSize))))
.andExpect(jsonPath("$._links.self.href", Matchers.allOf(
Matchers.containsString("/api/core/metadataschemas?"),
Matchers.containsString("page=2"), Matchers.containsString("size=6"))))
Matchers.containsString("page=2"), Matchers.containsString("size=" + pageSize))))
.andExpect(jsonPath("$._links.last.href", Matchers.allOf(
Matchers.containsString("/api/core/metadataschemas?"),
Matchers.containsString("page=2"), Matchers.containsString("size=6"))))
.andExpect(jsonPath("$.page.totalElements", is(16)))
.andExpect(jsonPath("$.page.totalPages", is(3)))
.andExpect(jsonPath("$.page.size", is(6)));
Matchers.containsString("page=" + (numberOfPages - 1)),
Matchers.containsString("size=" + pageSize))))
.andExpect(jsonPath("$.page.totalElements", is(numberOfSchema)))
.andExpect(jsonPath("$.page.totalPages", is(numberOfPages)))
.andExpect(jsonPath("$.page.size", is(pageSize)));
}