Skip to content

Commit

Permalink
increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
abuttaro committed Feb 9, 2018
1 parent 727c7f1 commit d3e06b0
Show file tree
Hide file tree
Showing 11 changed files with 393 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.github.restup.errors.Errors;
import com.github.restup.path.ResourcePath;
import com.github.restup.path.ResourcePath.Builder.Mode;
import com.github.restup.query.criteria.OrCriteria;
import com.github.restup.query.criteria.ResourcePathFilter;
import com.github.restup.query.criteria.ResourcePathFilter.Operator;
import com.github.restup.query.criteria.ResourceQueryCriteria;
Expand Down Expand Up @@ -353,7 +352,7 @@ public Builder addCriteria(List<ResourcePath> paths, Operator operator, Object v
for (ResourcePath path : paths) {
criteria.add(new ResourcePathFilter<Object>(path, operator, value));
}
return addCriteria(new OrCriteria(criteria));
return addCriteria(ResourceQueryCriteria.or(criteria));
}
return me();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.github.restup.query.criteria;

import java.util.List;
import com.google.common.collect.ImmutableList;

/**
* Tag interface for query criteria
* Defines query criteria
*/
public interface ResourceQueryCriteria {

Expand All @@ -10,4 +13,20 @@ public interface ResourceQueryCriteria {
*/
boolean filter(Object t);

static ResourceQueryCriteria and(ResourceQueryCriteria... criteria) {
return and(ImmutableList.copyOf(criteria));
}

static ResourceQueryCriteria and(List<ResourceQueryCriteria> criteria) {
return new AndCriteria(criteria);
}

static ResourceQueryCriteria or(ResourceQueryCriteria... criteria) {
return or(ImmutableList.copyOf(criteria));
}

static ResourceQueryCriteria or(List<ResourceQueryCriteria> criteria) {
return new OrCriteria(criteria);
}

}
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
package com.github.restup.service.filters;

import com.github.restup.annotations.filter.PreCreateFilter;
import com.github.restup.annotations.filter.PreUpdateFilter;
import com.github.restup.errors.ErrorBuilder;
import com.github.restup.errors.Errors;
import com.github.restup.mapping.MappedClass;
import com.github.restup.mapping.fields.IterableField;
import com.github.restup.mapping.fields.MappedField;
import com.github.restup.mapping.fields.ReadableField;
import com.github.restup.path.IndexPathValue;
import com.github.restup.path.MappedFieldPathValue;
import com.github.restup.path.ResourcePath;
import com.github.restup.registry.Resource;
import com.github.restup.registry.ResourceRegistry;
import com.github.restup.service.ServiceFilter;
import com.github.restup.service.model.request.CreateRequest;
import com.github.restup.service.model.request.UpdateRequest;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
Expand All @@ -33,6 +16,22 @@
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.restup.annotations.filter.PreCreateFilter;
import com.github.restup.annotations.filter.PreUpdateFilter;
import com.github.restup.errors.ErrorBuilder;
import com.github.restup.errors.Errors;
import com.github.restup.mapping.MappedClass;
import com.github.restup.mapping.fields.IterableField;
import com.github.restup.mapping.fields.MappedField;
import com.github.restup.mapping.fields.ReadableField;
import com.github.restup.path.IndexPathValue;
import com.github.restup.path.MappedFieldPathValue;
import com.github.restup.path.ResourcePath;
import com.github.restup.registry.Resource;
import com.github.restup.registry.ResourceRegistry;
import com.github.restup.service.ServiceFilter;
import com.github.restup.service.model.request.CreateRequest;
import com.github.restup.service.model.request.UpdateRequest;

public class JavaxValidationFilter implements ServiceFilter {

Expand Down Expand Up @@ -159,17 +158,7 @@ protected <T, ID extends Serializable> void addViolationErrors(Errors errors, Se
if (constraint != null) {
Annotation ann = constraint.getAnnotation();
if (ann != null) {
if (ann instanceof Max) {
err.meta("actualLength", length(value));
err.meta("max", constraint.getAttributes().get("value"));
} else if (ann instanceof Min) {
err.meta("actualLength", length(value));
err.meta("min", value);
} else if (ann instanceof Size) {
err.meta("actualLength", length(value));
err.meta("min", constraint.getAttributes().get("min"));
err.meta("max", constraint.getAttributes().get("max"));
}
addMeta(err, ann, constraint, value);
}
}

Expand All @@ -178,7 +167,21 @@ protected <T, ID extends Serializable> void addViolationErrors(Errors errors, Se
}
}

@Override
void addMeta(ErrorBuilder err, Annotation ann, ConstraintDescriptor<?> constraint, Object value) {
if (ann instanceof Max) {
err.meta("actualLength", length(value));
err.meta("max", constraint.getAttributes().get("value"));
} else if (ann instanceof Min) {
err.meta("actualLength", length(value));
err.meta("min", constraint.getAttributes().get("value"));
} else if (ann instanceof Size) {
err.meta("actualLength", length(value));
err.meta("min", constraint.getAttributes().get("min"));
err.meta("max", constraint.getAttributes().get("max"));
}
}

@Override
public <T, ID extends Serializable> boolean accepts(Resource<T, ID> resource) {
return resource.getType() instanceof Class;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class NotFoundFilter {
*/
@PostReadFilter
public <T, ID extends Serializable> void assertResourceNotFound(Resource<T, ID> resource, ReadRequest<T, ID> request, ReadResult<T> result) {
if (result != null && result.getData() == null) {
if (result == null || result.getData() == null) {
ErrorBuilder.builder().resource(resource)
.status(ErrorBuilder.ErrorCodeStatus.NOT_FOUND)
.code("RESOURCE_NOT_FOUND")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.github.restup.assertions;

import java.util.Map;
import org.assertj.core.api.AbstractThrowableAssert;
import org.assertj.core.api.Assertions;
import com.github.restup.errors.ErrorObjectException;
import com.github.restup.errors.RequestError;

public class ErrorObjectExceptionAssertions<ASSERT extends AbstractThrowableAssert<ASSERT, ErrorObjectException>> extends DelegatingAbstractThrowableAssertions<ErrorObjectExceptionAssertions<ASSERT>,ASSERT,ErrorObjectException> {

Expand All @@ -23,5 +25,35 @@ public ErrorObjectExceptionAssertions<ASSERT> code(String code) {
assertThat(exception.getCode()).isEqualTo(code);
return me();
}

public ErrorObjectExceptionAssertions<ASSERT> detail(String detail) {
return detail(0, detail);
}

public ErrorObjectExceptionAssertions<ASSERT> detail(int i, String detail) {
assertThat(getError(i).getDetail()).isEqualTo(detail);
return me();
}

public ErrorObjectExceptionAssertions<ASSERT> meta(String key, Object value) {
return meta(0, key, value);
}

public ErrorObjectExceptionAssertions<ASSERT> meta(int i, String key, Object value) {
assertThat(getMeta(i).get(key)).isEqualTo(value);
return me();
}

private RequestError getError(int i) {
return exception.getErrors().get(i);
}

@SuppressWarnings("unchecked")
private Map<String,Object> getMeta(int i) {
Object meta = getError(i).getMeta();
if ( meta instanceof Map ) {
return (Map<String,Object>) meta;
}
throw new IllegalStateException("Meta is not a map");
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.github.restup.query.criteria;

import org.junit.Assert;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import com.github.restup.query.criteria.ResourcePathFilter.Operator;

public class OperatorTest {

@Test
public void testOf() {
Assert.assertEquals(ResourcePathFilter.Operator.eq, ResourcePathFilter.Operator.of(null));
assertEquals(ResourcePathFilter.Operator.eq, ResourcePathFilter.Operator.of(null));
for (ResourcePathFilter.Operator op : ResourcePathFilter.Operator.values()) {
Assert.assertEquals(op, ResourcePathFilter.Operator.of(op.name()));
assertEquals(op, ResourcePathFilter.Operator.of(op.name()));
for (String s : op.getOperators()) {
Assert.assertEquals(op, ResourcePathFilter.Operator.of(s));
assertEquals(op, ResourcePathFilter.Operator.of(s));
}
}
assertNull(Operator.of("bad"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.github.restup.query.criteria;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import org.junit.Test;

public class ResourceQueryCriteriaTest {

private ResourceQueryCriteria criteria(Object target, boolean result) {
ResourceQueryCriteria criteria = mock(ResourceQueryCriteria.class);
when(criteria.filter(target)).thenReturn(result);
return criteria;
}

@Test
public void testAndFalse() {
Object foo = "";
ResourceQueryCriteria positive = criteria(foo, true);
ResourceQueryCriteria negative = criteria(foo, false);

assertFalse(ResourceQueryCriteria.and(positive, negative).filter(foo));

// negative filter applied positive will not be
ResourceQueryCriteria negative2 = criteria(foo, false);
assertFalse(ResourceQueryCriteria.and(negative2, positive).filter(foo));

verifyFilter(foo, positive, negative, negative2);
verifyNoMoreInteractions(positive, negative, negative2);
}

@Test
public void testAndTrue() {
Object foo = "";
ResourceQueryCriteria a = criteria(foo, true);
ResourceQueryCriteria b = criteria(foo, true);

assertTrue(ResourceQueryCriteria.and(a, b).filter(foo));

verifyFilter(foo, a, b);
verifyNoMoreInteractions(a, b);
}

@Test
public void testOrFalse() {
Object foo = "";
ResourceQueryCriteria a = criteria(foo, false);
ResourceQueryCriteria b = criteria(foo, false);

assertFalse(ResourceQueryCriteria.or(a, b).filter(foo));

verifyFilter(foo, a, b);
verifyNoMoreInteractions(a, b);
}

@Test
public void testOrTrue() {
Object foo = "";
ResourceQueryCriteria a = criteria(foo, true);
ResourceQueryCriteria b = criteria(foo, true);

// b verify not executed
assertTrue(ResourceQueryCriteria.or(a, b).filter(foo));

ResourceQueryCriteria c = criteria(foo, false);
assertTrue(ResourceQueryCriteria.or(c, b).filter(foo));

verifyFilter(foo, a, b, c);
verifyNoMoreInteractions(a, b);
}

private void verifyFilter(Object target, ResourceQueryCriteria... array) {
for ( ResourceQueryCriteria criteria : array) {
verify(criteria).filter(target);
}
}

}
Loading

0 comments on commit d3e06b0

Please sign in to comment.