Skip to content

Commit

Permalink
increase coverage, more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
abuttaro committed Feb 7, 2018
1 parent 7a86251 commit 2125784
Show file tree
Hide file tree
Showing 22 changed files with 512 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private Builder<T> addSort(String rawParameterName, String rawParameterValue, Re
if (!validatePath(rawParameterName, rawParameterValue, path, resource, field)) {
return me();
}
return addSort(new ResourceSort(path, asc));
return addSort(ResourceSort.of(path, asc));
}

public void addFilter(List<ResourcePath> paths, Collection<Object> joinIds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"title": "Invalid Path",
"detail": "The specified path is not valid.",
"source": {
"pointer": "/data/foo"
"pointer": "/data/bar"
},
"meta": {
"resource": "university"
Expand All @@ -19,7 +19,7 @@
"title": "Invalid Path",
"detail": "The specified path is not valid.",
"source": {
"pointer": "/data/bar"
"pointer": "/data/foo"
},
"meta": {
"resource": "university"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"errors": [
{
"id": "${json-unit.regex}[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
"code": "WRAP_FIELDS_WITH_ATTRIBUTES",
"title": "field must be wrapped",
"detail": "The field is valid, but it must be wrapped in attributes",
"code": "TYPE_REQUIRED",
"title": "type is required",
"detail": "Missing type at specified path",
"source": {
"pointer": "/data/name"
"pointer": "/data"
},
"meta": {
"resource": "course"
Expand All @@ -15,11 +15,11 @@
},
{
"id": "${json-unit.regex}[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
"code": "TYPE_REQUIRED",
"title": "type is required",
"detail": "Missing type at specified path",
"code": "WRAP_FIELDS_WITH_ATTRIBUTES",
"title": "field must be wrapped",
"detail": "The field is valid, but it must be wrapped in attributes",
"source": {
"pointer": "/data"
"pointer": "/data/name"
},
"meta": {
"resource": "course"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"errors": [
{
"errors": [{
"id": "${json-unit.regex}[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
"code": "WRAP_FIELDS_WITH_ATTRIBUTES",
"title": "field must be wrapped",
"detail": "The field is valid, but it must be wrapped in attributes",
"code": "ID_REQUIRED",
"title": "id is required",
"detail": "Missing identifier at specified path",
"source": {
"pointer": "/data/name"
"pointer": "/data"
},
"meta": {
"resource": "course"
Expand All @@ -15,9 +14,9 @@
},
{
"id": "${json-unit.regex}[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
"code": "ID_REQUIRED",
"title": "id is required",
"detail": "Missing identifier at specified path",
"code": "TYPE_REQUIRED",
"title": "type is required",
"detail": "Missing type at specified path",
"source": {
"pointer": "/data"
},
Expand All @@ -28,11 +27,11 @@
},
{
"id": "${json-unit.regex}[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
"code": "TYPE_REQUIRED",
"title": "type is required",
"detail": "Missing type at specified path",
"code": "WRAP_FIELDS_WITH_ATTRIBUTES",
"title": "field must be wrapped",
"detail": "The field is valid, but it must be wrapped in attributes",
"source": {
"pointer": "/data"
"pointer": "/data/name"
},
"meta": {
"resource": "course"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ private String getDefaultCode() {
return errorCode.name();
}

if (status != null) {
return status.name();
}

List<String> parts = new ArrayList<String>();
if (codePrefix != null) {
for (String prefix : codePrefix) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;

/**
* An exception containing {@link RequestError}s
Expand Down Expand Up @@ -87,17 +88,16 @@ public RequestError getPrimaryError() {
private final class RequestErrorComparator implements Comparator<RequestError> {

public int compare(RequestError a, RequestError b) {
if (a.getStatus() != b.getStatus()) {
return a.getHttpStatus() > b.getHttpStatus() ? -1 : 1;
int result = StringUtils.compare(a.getStatus(), b.getStatus());
if ( result == 0) {
String sourceA = getSource(a.getSource());
String sourceB = getSource(b.getSource());
result = StringUtils.compare(sourceA, sourceB);
} else {
// want descending status
result *= -1;
}
String sourceA = getSource(a.getSource());
String sourceB = getSource(b.getSource());
if (sourceA == null) {
return sourceB == null ? 0 : 1;
} else if (sourceB == null) {
return -1;
}
return sourceA.compareTo(sourceB);
return result;
}

private String getSource(ErrorSource err) {
Expand Down
57 changes: 57 additions & 0 deletions up-core/src/main/java/com/github/restup/query/BasicPagination.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.github.restup.query;

/**
* Represents Pagination for default settings and requests
*
* @author abuttaro
*/
class BasicPagination implements Pagination {

// pagination
private final Integer limit;
private final Integer offset;
private final boolean pagingDisabled;
private final boolean withTotalsDisabled;

/**
* Paging enabled with provided limit, offset, totalsEnabled
* @param limit
* @param offset
* @param withTotalsDisabled
*/
BasicPagination(Integer limit, Integer offset, boolean withTotalsDisabled) {
super();
this.limit = limit;
this.offset = offset;
this.withTotalsDisabled = withTotalsDisabled;
this.pagingDisabled = false;
}

/**
* Paging & totals disabled & null limit & offset
*/
BasicPagination() {
super();
this.limit = null;
this.offset = null;
this.pagingDisabled = true;
this.withTotalsDisabled = true;
}

public Integer getLimit() {
return limit;
}

public Integer getOffset() {
return offset;
}

public boolean isPagingDisabled() {
return pagingDisabled;
}

public boolean isWithTotalsDisabled() {
return withTotalsDisabled;
}

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

import java.util.Objects;
import com.github.restup.path.ResourcePath;
import com.github.restup.registry.Resource;
import com.github.restup.util.Assert;

/**
* Specifies sort behavior for a resource query
*/
class BasicResourceSort implements ResourceSort {

private final ResourcePath path;
private final Boolean ascending;

BasicResourceSort(ResourcePath path, Boolean ascending) {
super();
Assert.notNull(path, "path cannot be null");
this.path = path;
this.ascending = ascending;
}

BasicResourceSort(ResourcePath path) {
this(path, true);
}

BasicResourceSort(Resource<?, ?> resource, String beanPath) {
this(ResourcePath.path(resource, beanPath));
}

/**
* @return true if ascending is true or null, false otherwise
*/
public boolean isAscending() {
return !Objects.equals(Boolean.FALSE, ascending);
}

/**
* true if ascending, false if descending, null if unspecified
*/
public Boolean getAscending() {
return ascending;
}

/**
* @return path to be sorted
*/
public ResourcePath getPath() {
return path;
}

}
72 changes: 34 additions & 38 deletions up-core/src/main/java/com/github/restup/query/Pagination.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,43 @@
*
* @author abuttaro
*/
public class Pagination {
public interface Pagination {

// pagination
private final Integer limit;
private final Integer offset;
private final boolean pagingDisabled;
private final boolean withTotalsDisabled;
Integer getLimit();

public Pagination(Integer limit, Integer offset, boolean pagingDisabled, boolean withTotalsDisabled) {
super();
this.limit = limit;
this.offset = offset;
this.pagingDisabled = pagingDisabled;
this.withTotalsDisabled = withTotalsDisabled;
Integer getOffset();

boolean isPagingDisabled();

default boolean isPagingEnabled() {
return !isPagingDisabled();
}

public static int getStart(Pagination pagination) {
default boolean isWithTotalsEnabled() {
return !isWithTotalsDisabled();
}

boolean isWithTotalsDisabled();

/**
* Pagination with specified limit and offset and paging and totals enabled
* @param limit
* @param offset
* @return
*/
static Pagination of(Integer limit, Integer offset) {
return of(limit, offset, false);
}

static Pagination of(Integer limit, Integer offset, boolean withTotalsDisabled) {
return new BasicPagination(limit, offset, withTotalsDisabled);
}

static Pagination disabled() {
return new BasicPagination();
}

static int getStart(Pagination pagination) {
Integer offset = pagination.getOffset();
if (offset == null) {
offset = 0;
Expand All @@ -35,34 +55,10 @@ public static int getStart(Pagination pagination) {
return offset * pageSize;
}

public static boolean isPagedListRequired(Pagination pagination, Long totalCount) {
static boolean isPagedListRequired(Pagination pagination, Long totalCount) {
return (pagination.isWithTotalsDisabled() || totalCount > 0)
&& pagination.getLimit() != null
&& pagination.getLimit() > 0;
}

public Integer getLimit() {
return limit;
}

public Integer getOffset() {
return offset;
}

public boolean isPagingDisabled() {
return pagingDisabled;
}

public boolean isPagingEnabled() {
return !isPagingDisabled();
}

public boolean isWithTotalsEnabled() {
return !isWithTotalsDisabled();
}

public boolean isWithTotalsDisabled() {
return withTotalsDisabled;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public List<ResourceSort> getSort() {
public void setSort(String... beanPaths) {
List<ResourceSort> sort = new ArrayList<ResourceSort>();
for (String beanPath : beanPaths) {
sort.add(new ResourceSort(resource, beanPath));
sort.add(ResourceSort.of(resource, beanPath));
}
setSort(sort);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,10 @@ public ResourceQueryStatement build() {
if (pageLimit != null || pageOffset != null || withTotalsDisabled != null || pagingDisabled != null) {
Integer limit = pageLimit != null ? pageLimit : pagination.getLimit();
Integer offset = pageOffset != null ? pageOffset : pagination.getOffset();
Boolean disablePaging = pagingDisabled != null ? pagingDisabled : pagination.isPagingDisabled();
Boolean disableTotals = withTotalsDisabled != null ? withTotalsDisabled : pagination.isWithTotalsDisabled();
boolean disablePaging = pagingDisabled != null ? pagingDisabled : pagination.isPagingDisabled();
boolean disableTotals = withTotalsDisabled != null ? withTotalsDisabled : pagination.isWithTotalsDisabled();

pagination = new Pagination(limit, offset, disablePaging, disableTotals);
pagination = disablePaging ? Pagination.disabled() : Pagination.of(limit, offset, disableTotals);
}
Type t = type;
if (t == null) {
Expand Down
Loading

0 comments on commit 2125784

Please sign in to comment.