Skip to content

Commit

Permalink
Fixes and updates for March 2024 Part 4 (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethonyango authored May 28, 2024
1 parent 08d48f8 commit e54eb9e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/falu/common/BasicListOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class BasicListOptions {
/**
* The order to use for sorting the objects returned.
*/
private String sorting;
private String[] sorting;

/**
* The maximum number of objects to return.
Expand All @@ -36,7 +36,7 @@ public void populate(QueryValues values) {
if (values == null) return;

values
.add("sort", new String[] {sorting})
.add("sort", sorting)
.add("count", count)
.add("created", new QueryValues().fromRange(created))
.add("updated", new QueryValues().fromRange(updated));
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/falu/common/QueryValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ public <T> QueryValues fromRange(RangeFilteringOptions<T> options) {
add("lte", options.lessThanOrEqualTo);
add("gt", options.greaterThan);
add("gte", options.greaterThanOrEqualTo);

return this;
}
return this;
return null;
}

/***/
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/io/falu/common/RangeFilteringOptions.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package io.falu.common;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.Nullable;

/**
* Standard options for range filtering
*/
@Getter
@Setter
@Builder
public class RangeFilteringOptions<T> {
public T lessThan;

Expand Down
34 changes: 33 additions & 1 deletion src/test/java/io/falu/QueryValueTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.falu.common.BasicListOptions;
import io.falu.common.QueryValues;
import io.falu.common.RangeFilteringOptions;
import io.falu.models.payments.PaymentsListOptions;
import okhttp3.HttpUrl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -10,6 +12,8 @@

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -45,7 +49,7 @@ public void test_QueryIsGenerated() {
@Test
public void test_BasicListOptionsWorks() {
BasicListOptions opt = BasicListOptions.builder()
.sorting("descending")
.sorting(new String[]{"descending"})
.count(12)
.created(null)
.updated(null)
Expand Down Expand Up @@ -80,6 +84,34 @@ public void test_MultipleQueryValuesWorks() {
Assertions.assertEquals("https://example.com/events?count=100&sort=desc&type=transfer.created&type=transfer.failed&type=transfer.succeeded", url);
}

@Test
public void test_PopulatingPaymentListOptions() {
LocalDate startDate = LocalDate.parse("2018-05-05");
LocalDate endDate = LocalDate.parse("2018-05-07");

RangeFilteringOptions<Date> createdRangeFilter = RangeFilteringOptions.<Date>builder()
.greaterThan(Date.from(startDate.atStartOfDay().toInstant(ZoneOffset.MIN)))
.lessThan(Date.from(endDate.atStartOfDay().toInstant(ZoneOffset.MIN)))
.build();

PaymentsListOptions options = PaymentsListOptions.builder()
.created(createdRangeFilter)
.build();

HttpUrl.Builder builder = new HttpUrl.Builder();
builder.scheme("https");
builder.host("example.com");
builder.addPathSegments("payments");

QueryValues values = new QueryValues();
options.populate(values);
values.getQueryParameters(builder);

HttpUrl url = builder.build();
String expectation = "https://example.com/payments?created.gt=2018-05-05T18:00:00Z&created.lt=2018-05-07T18:00:00Z";
Assertions.assertEquals(expectation, url.url().toString());
}

private Date toDate(String dateToConsider) {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ssss");
try {
Expand Down

0 comments on commit e54eb9e

Please sign in to comment.