Skip to content

Commit

Permalink
improve OrderSearch request builder mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude committed Apr 10, 2024
1 parent e52b264 commit 29dd4b3
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,122 +4,122 @@
import java.util.function.Function;

public interface OrderSearchQueryBuilderMixin {
public default OrderSearchQuery and(final OrderSearchQuery... expression) {
public default OrderSearchAndExpression and(final OrderSearchQuery... expression) {
return OrderSearchAndExpression.builder().and(expression).build();
}

public default OrderSearchQuery or(final OrderSearchQuery... expression) {
public default OrderSearchOrExpression or(final OrderSearchQuery... expression) {
return OrderSearchOrExpression.builder().or(expression).build();
}

public default OrderSearchQuery not(final OrderSearchQuery... expression) {
public default OrderSearchNotExpression not(final OrderSearchQuery... expression) {
return OrderSearchNotExpression.builder().not(expression).build();
}

public default OrderSearchQuery filter(final OrderSearchQueryExpression... expression) {
public default OrderSearchFilterExpression filter(final OrderSearchQueryExpression... expression) {
return OrderSearchFilterExpression.builder().filter(expression).build();
}

public default OrderSearchQuery exact(final OrderSearchAnyValue value) {
public default OrderSearchExactExpression exact(final OrderSearchAnyValue value) {
return OrderSearchExactExpression.builder().exact(value).build();
}

public default OrderSearchQuery fullText(final OrderSearchFullTextValue value) {
public default OrderSearchFullTextExpression fullText(final OrderSearchFullTextValue value) {
return OrderSearchFullTextExpression.builder().fullText(value).build();
}

public default OrderSearchQuery prefix(final OrderSearchStringValue value) {
public default OrderSearchPrefixExpression prefix(final OrderSearchStringValue value) {
return OrderSearchPrefixExpression.builder().prefix(value).build();
}

public default OrderSearchQuery dateRange(final OrderSearchDateRangeValue value) {
public default OrderSearchDateRangeExpression dateRange(final OrderSearchDateRangeValue value) {
return OrderSearchDateRangeExpression.builder().range(value).build();
}

public default OrderSearchQuery numberRange(final OrderSearchNumberRangeValue value) {
public default OrderSearchNumberRangeExpression numberRange(final OrderSearchNumberRangeValue value) {
return OrderSearchNumberRangeExpression.builder().range(value).build();
}

public default OrderSearchQuery longRange(final OrderSearchLongRangeValue value) {
public default OrderSearchLongRangeExpression longRange(final OrderSearchLongRangeValue value) {
return OrderSearchLongRangeExpression.builder().range(value).build();
}

public default OrderSearchQuery wildcard(final OrderSearchStringValue value) {
public default OrderSearchWildCardExpression wildcard(final OrderSearchStringValue value) {
return OrderSearchWildCardExpression.builder().wildcard(value).build();
}

public default OrderSearchQuery exists(final OrderSearchQueryExpressionValue value) {
public default OrderSearchExistsExpression exists(final OrderSearchQueryExpressionValue value) {
return OrderSearchExistsExpression.builder().exists(value).build();
}

public default OrderSearchQuery and(
public default OrderSearchAndExpression and(
final Function<OrderSearchAndExpressionBuilder, OrderSearchAndExpressionBuilder> query) {
return query.apply(OrderSearchAndExpressionBuilder.of()).build();
return query.apply(OrderSearchAndExpression.builder()).build();
}

public default OrderSearchQuery or(
public default OrderSearchOrExpression or(
final Function<OrderSearchOrExpressionBuilder, OrderSearchOrExpressionBuilder> query) {
return query.apply(OrderSearchOrExpressionBuilder.of()).build();
return query.apply(OrderSearchOrExpression.builder()).build();
}

public default OrderSearchQuery not(
public default OrderSearchNotExpression not(
final Function<OrderSearchNotExpressionBuilder, OrderSearchNotExpressionBuilder> query) {
return query.apply(OrderSearchNotExpressionBuilder.of()).build();
return query.apply(OrderSearchNotExpression.builder()).build();
}

public default OrderSearchQuery filter(
public default OrderSearchFilterExpression filter(
final Function<OrderSearchFilterExpressionBuilder, OrderSearchFilterExpressionBuilder> filter) {
return filter.apply(OrderSearchFilterExpressionBuilder.of()).build();
}

public default OrderSearchQuery exact(
public default OrderSearchExactExpression exact(
final Function<OrderSearchAnyValueBuilder, OrderSearchAnyValueBuilder> value) {
return OrderSearchExactExpression.builder().exact(value.apply(OrderSearchAnyValue.builder()).build()).build();
}

public default OrderSearchQuery fullText(
public default OrderSearchFullTextExpression fullText(
final Function<OrderSearchFullTextValueBuilder, OrderSearchFullTextValueBuilder> value) {
return OrderSearchFullTextExpression.builder()
.fullText(value.apply(OrderSearchFullTextValue.builder()).build())
.build();
}

public default OrderSearchQuery prefix(
public default OrderSearchPrefixExpression prefix(
final Function<OrderSearchStringValueBuilder, OrderSearchStringValueBuilder> value) {
return OrderSearchPrefixExpression.builder()
.prefix(value.apply(OrderSearchStringValue.builder()).build())
.build();
}

public default OrderSearchQuery dateRange(
public default OrderSearchDateRangeExpression dateRange(
final Function<OrderSearchDateRangeValueBuilder, OrderSearchDateRangeValueBuilder> value) {
return OrderSearchDateRangeExpression.builder()
.range(value.apply(OrderSearchDateRangeValue.builder()).build())
.build();
}

public default OrderSearchQuery numberRange(
public default OrderSearchNumberRangeExpression numberRange(
final Function<OrderSearchNumberRangeValueBuilder, OrderSearchNumberRangeValueBuilder> value) {
return OrderSearchNumberRangeExpression.builder()
.range(value.apply(OrderSearchNumberRangeValue.builder()).build())
.build();
}

public default OrderSearchQuery longRange(
public default OrderSearchLongRangeExpression longRange(
final Function<OrderSearchLongRangeValueBuilder, OrderSearchLongRangeValueBuilder> value) {
return OrderSearchLongRangeExpression.builder()
.range(value.apply(OrderSearchLongRangeValue.builder()).build())
.build();
}

public default OrderSearchQuery wildcard(
public default OrderSearchWildCardExpression wildcard(
final Function<OrderSearchStringValueBuilder, OrderSearchStringValueBuilder> value) {
return OrderSearchWildCardExpression.builder()
.wildcard(value.apply(OrderSearchStringValue.builder()).build())
.build();
}

public default OrderSearchQuery exists(
public default OrderSearchExistsExpression exists(
final Function<OrderSearchQueryExpressionValueBuilder, OrderSearchQueryExpressionValueBuilder> value) {
return OrderSearchExistsExpression.builder()
.exists(value.apply(OrderSearchQueryExpressionValue.builder()).build())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@

package com.commercetools;

import com.commercetools.api.models.order.OrderSearchRequest;
import com.commercetools.api.models.order.OrderSearchRequestBuilder;
import com.commercetools.api.models.order.OrderSearchSortOrder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.tngtech.junit.dataprovider.DataProvider;
import com.tngtech.junit.dataprovider.DataProviderExtension;
import com.tngtech.junit.dataprovider.UseDataProvider;
import com.tngtech.junit.dataprovider.UseDataProviderExtension;

import io.vrap.rmf.base.client.utils.json.JsonUtils;

import org.assertj.core.api.Assertions;
import org.json.JSONException;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.skyscreamer.jsonassert.JSONAssert;

@ExtendWith(UseDataProviderExtension.class)
@ExtendWith(DataProviderExtension.class)
public class OrderQueryBuilderTest {
@TestTemplate
@UseDataProvider("searchBuilder")
public void buildUnchecked(OrderSearchRequestBuilder builder, String expectedQuery)
throws JSONException, JsonProcessingException {
OrderSearchRequest request = builder.buildUnchecked();
Assertions.assertThat(request).isInstanceOf(OrderSearchRequest.class);
JSONAssert.assertEquals(expectedQuery, JsonUtils.toJsonString(request), true);
}

@DataProvider
public static Object[][] searchBuilder() {
return new Object[][] { new Object[] { OrderSearchRequest.builder()
.withQuery(q -> q.and(b -> b
.addAnd(a -> a.fullText(t -> t.field("customLineItems.name").language("en").value("banana")))
.addAnd(a -> a.filter(f -> f
.plusFilter(a.exact(e -> e.field("store.name").language("en").value("fruit_store")))))))
.plusSort(s -> s.field("customLineItems.name").language("en").order(OrderSearchSortOrder.DESC))
.limit(50)
.offset(0), """
{
"query": {
"and": [
{
"fullText": {
"field": "customLineItems.name",
"language": "en",
"value": "banana"
}
},
{
"filter": [
{
"exact": {
"field": "store.name",
"language": "en",
"value": "fruit_store"
}
}
]
}
]
},
"sort": [
{
"field": "customLineItems.name",
"language": "en",
"order": "desc"
}
],
"limit": 50,
"offset": 0
}""" },
new Object[] {
OrderSearchRequest.builder()
.withQuery(q -> q.and(
b -> b.addAnd(a -> a.exact(t -> t.field("totalPrice.currencyCode").value("EUR")))
.addAnd(a -> a.exact(t -> t.field("totalPrice.centAmount").value(2222))))),
"""
{
"query": {
"and": [
{
"exact": {
"field": "totalPrice.currencyCode",
"value": "EUR"
}
},
{
"exact": {
"field": "totalPrice.centAmount",
"value": 2222
}
}
]
}
}""" },
new Object[] { OrderSearchRequest.builder()
.withQuery(q -> q.exact(
t -> t.field("custom.myOrderField").value("special order").customType("StringType"))),
"""
{
"query": {
"exact": {
"field": "custom.myOrderField",
"value": "special order",
"customType": "StringType"
}
}
}""" }, };
}
}
Loading

0 comments on commit 29dd4b3

Please sign in to comment.