Skip to content

Commit

Permalink
crnk-project#767 used client's object mapper to serialize filters
Browse files Browse the repository at this point in the history
  • Loading branch information
JochenReinhardt committed Jul 24, 2020
1 parent 2087f0a commit 11b05fc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public ClientStubBase(CrnkClient client, UrlBuilder urlBuilder, Class<?> resourc
this.filterCriteriaInRequestBody = filterCriteriaInRequestBody;
if (filterCriteriaInRequestBody) {
filterSpecMapper = ((DefaultQuerySpecUrlMapper) client.getUrlMapper()).getJsonParser();
compactMapper = new ObjectMapper();
compactMapper = client.getObjectMapper();
} else {
filterSpecMapper = null;
compactMapper = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.crnk.client;

import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.crnk.client.http.apache.HttpClientAdapter;
import io.crnk.core.boot.CrnkProperties;
import io.crnk.core.exception.InternalServerErrorException;
Expand All @@ -10,10 +12,15 @@
import io.crnk.core.queryspec.mapper.DefaultQuerySpecUrlMapper;
import io.crnk.core.repository.ResourceRepository;
import io.crnk.core.resource.list.ResourceList;
import io.crnk.test.mock.models.PrimitiveAttributeResource;
import io.crnk.test.mock.models.Project;
import org.apache.commons.lang3.time.DateUtils;
import org.junit.Before;
import org.junit.Test;

import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -24,16 +31,19 @@ public class FilterCriteriaInBodyClientTest extends AbstractClientTest {
private static final int PROJECT_COUNT = 10;

private ResourceRepository<Project, Long> projectRepo;
private ResourceRepository<PrimitiveAttributeResource, Object> primitiveRepo;

@Before
public void setup() {
super.setup();
projectRepo = client.getRepositoryForType(Project.class);
createProjects();
primitiveRepo = client.getRepositoryForType(PrimitiveAttributeResource.class);
}

private void createProjects() {
for (long i = 0; i < PROJECT_COUNT; i++) {
OffsetDateTime dueBase = OffsetDateTime.now();
for (int i = 0; i < PROJECT_COUNT; i++) {
Project project = new Project();
project.setName("Project " + (char) ('A' + i));
if (i == 5) {
Expand All @@ -58,19 +68,19 @@ protected void setupClient(CrnkClient client) {
client.setHttpAdapter(new HttpClientAdapter());
client.setFilterCriteriaInRequestBody(true);
((DefaultQuerySpecUrlMapper) client.getUrlMapper()).setAllowUnknownAttributes(true);
client.getObjectMapper().disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
client.getObjectMapper().registerModule(new JavaTimeModule());
}

@Test
public void testWithoutFilter() {
ResourceRepository<Project, Long> projectRepo = client.getRepositoryForType(Project.class);
ResourceList<Project> projects = projectRepo.findAll(new QuerySpec(Project.class));
assertNotNull(projects);
assertEquals(PROJECT_COUNT, projects.size());
}

@Test
public void testWithSingleFilter() {
ResourceRepository<Project, Long> projectRepo = client.getRepositoryForType(Project.class);
QuerySpec querySpec = new QuerySpec(Project.class);
querySpec.addFilter(PathSpec.of("id").filter(FilterOperator.LT, 5));
ResourceList<Project> projects = projectRepo.findAll(querySpec);
Expand All @@ -80,7 +90,6 @@ public void testWithSingleFilter() {

@Test
public void testWithCombinedFilter() {
ResourceRepository<Project, Long> projectRepo = client.getRepositoryForType(Project.class);
QuerySpec querySpec = new QuerySpec(Project.class);
querySpec.addFilter(FilterSpec.or(
PathSpec.of("id").filter(FilterOperator.LT, 5),
Expand All @@ -98,4 +107,37 @@ public void testUnknownFilterAttributeIsPassedToServer() {
querySpec.addFilter(PathSpec.of(unknownPath).filter(FilterOperator.EQ, 1));
projectRepo.findAll(querySpec);
}

@Test
public void filterByDateWithPlainDate() {
Date now = new Date();
createResource(now);

QuerySpec querySpec = new QuerySpec(PrimitiveAttributeResource.class);
Date max = DateUtils.addDays(now, 1);
querySpec.addFilter(PathSpec.of("dateValue").filter(FilterOperator.LT, max));
ResourceList<PrimitiveAttributeResource> resources = primitiveRepo.findAll(querySpec);
assertNotNull(resources);
assertEquals(1, resources.size());
}

private void createResource(Date date) {
PrimitiveAttributeResource resource = new PrimitiveAttributeResource();
resource.setId(1L);
resource.setDateValue(date);
primitiveRepo.create(resource);
}

@Test
public void filterByDateWithLocalDate() {
Date now = new Date();
createResource(now);

QuerySpec querySpec = new QuerySpec(PrimitiveAttributeResource.class);
LocalDate max = LocalDate.now().plusDays(-1);
querySpec.addFilter(PathSpec.of("dateValue").filter(FilterOperator.GT, max));
ResourceList<PrimitiveAttributeResource> resources = primitiveRepo.findAll(querySpec);
assertNotNull(resources);
assertEquals(1, resources.size());
}
}

0 comments on commit 11b05fc

Please sign in to comment.