Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LPS-172979 | master #2558

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public Predicate visitUnaryExpressionOperation(
defaultPredicate.getRightExpression());
}

return Predicate.not(defaultPredicate);
return Predicate.notOf(defaultPredicate);
}

private PredicateExpressionVisitorImpl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,23 @@
import com.liferay.object.rest.internal.resource.v1_0.test.util.ObjectRelationshipTestUtil;
import com.liferay.object.service.ObjectDefinitionLocalService;
import com.liferay.object.service.ObjectEntryLocalService;
import com.liferay.object.service.ObjectEntryLocalServiceUtil;
import com.liferay.object.service.ObjectRelationshipLocalService;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.json.JSONUtil;
import com.liferay.portal.kernel.servlet.HttpHeaders;
import com.liferay.portal.kernel.test.rule.AggregateTestRule;
import com.liferay.portal.kernel.test.util.RandomTestUtil;
import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
import com.liferay.portal.kernel.test.util.TestPropsValues;
import com.liferay.portal.kernel.util.Base64;
import com.liferay.portal.kernel.util.ContentTypes;
import com.liferay.portal.kernel.util.HashMapBuilder;
import com.liferay.portal.kernel.util.Http;
import com.liferay.portal.kernel.util.HttpUtil;
import com.liferay.portal.kernel.util.MapUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringUtil;
Expand All @@ -49,6 +56,9 @@
import com.liferay.portal.test.rule.PermissionCheckerMethodTestRule;
import com.liferay.portal.util.PropsUtil;

import java.io.Serializable;

import java.util.Arrays;
import java.util.Collections;

import javax.ws.rs.NotSupportedException;
Expand All @@ -65,6 +75,8 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.http.HttpStatus;

/**
* @author Luis Miguel Barcos
*/
Expand Down Expand Up @@ -178,6 +190,80 @@ public void testFilterObjectEntriesByRelatedObjectEntries()
).build());
}

@Test
public void testFilterObjectEntriesWithPrecisionDecimalField()
throws Exception {

PropsUtil.addProperties(
UnicodePropertiesBuilder.setProperty(
"feature.flag.LPS-154672", "true"
).build());

ObjectDefinition objectDefinition1 =
ObjectDefinitionTestUtil.publishObjectDefinition(
Arrays.asList(
ObjectFieldUtil.createObjectField(
"Text", "String", true, true, null, "TestString",
"xtestname", false),
ObjectFieldUtil.createObjectField(
"PrecisionDecimal", "BigDecimal", true, true, null,
"TestPrecisionDecimal", "xtestnumber", false)));

ObjectEntryLocalServiceUtil.addObjectEntry(
TestPropsValues.getUserId(), 0,
objectDefinition1.getObjectDefinitionId(),
HashMapBuilder.<String, Serializable>put(
"xtestname", RandomTestUtil.randomString()
).put(
"xtestnumber", 123.45
).build(),
ServiceContextTestUtil.getServiceContext());

String testName2 = RandomTestUtil.randomString();

ObjectEntryLocalServiceUtil.addObjectEntry(
TestPropsValues.getUserId(), 0,
objectDefinition1.getObjectDefinitionId(),
HashMapBuilder.<String, Serializable>put(
"xtestname", testName2
).put(
"xtestnumber", 234.56
).build(),
ServiceContextTestUtil.getServiceContext());

Http.Options options = _createOptions(
StringBundler.concat(
objectDefinition1.getRESTContextPath(), "?filter=",
URLCodec.encodeURL(
"(not (xtestnumber lt 200.5)) or (xtestnumber lt 0)",
true)),
Http.Method.GET);

String responseString = HttpUtil.URLtoString(options);

Http.Response response = options.getResponse();

Assert.assertTrue(
"Unexpected result: " + responseString,
response.getResponseCode() == HttpStatus.OK.value());

JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
responseString);

JSONArray itemsJSONArray = jsonObject.getJSONArray("items");

Assert.assertEquals(1, itemsJSONArray.length());

JSONObject itemJSONObject = itemsJSONArray.getJSONObject(0);

Assert.assertEquals(itemJSONObject.getString("xtestname"), testName2);

PropsUtil.addProperties(
UnicodePropertiesBuilder.setProperty(
"feature.flag.LPS-154672", "false"
).build());
}

@Test
public void testGetNestedFieldDetailsInOneToManyRelationships()
throws Exception {
Expand Down Expand Up @@ -930,6 +1016,22 @@ private JSONArray _createObjectEntriesJSONArray(
return jsonArray;
}

private Http.Options _createOptions(
String endpoint, Http.Method httpMethod) {

Http.Options options = new Http.Options();

options.addHeader(
HttpHeaders.CONTENT_TYPE, ContentTypes.APPLICATION_JSON);
options.addHeader(
"Authorization",
"Basic " + Base64.encode("[email protected]:test".getBytes()));
options.setLocation("http://localhost:8080/o/" + endpoint);
options.setMethod(httpMethod);

return options;
}

private void _postObjectEntryWithKeywords(String... keywords)
throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public static Predicate not(Predicate predicate) {
return predicate.not((Expression<Boolean>)withParentheses(predicate));
}

public static Predicate notOf(Predicate predicate) {
return predicate.notOf((Expression<Boolean>)withParentheses(predicate));
}

public static Predicate or(
Predicate leftPredicate, Predicate rightPredicate) {

Expand Down Expand Up @@ -71,6 +75,8 @@ public default <T extends Throwable> Predicate not(
return not(unsafeSupplier.get());
}

public Predicate notOf(Expression<Boolean> expression);

public Predicate or(Expression<Boolean> expression);

public default <T extends Throwable> Predicate or(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ public Predicate not(Expression<Boolean> expression) {
return new DefaultPredicate(this, Operand.NOT, expression);
}

@Override
public Predicate notOf(Expression<Boolean> expression) {
if (expression == null) {
return this;
}

return new DefaultPredicate(
new DoNothingExpression<>(), Operand.NOT, expression);
}

@Override
public Predicate or(Expression<Boolean> expression) {
if (expression == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/

package com.liferay.petra.sql.dsl.spi.expression;

import com.liferay.petra.sql.dsl.ast.ASTNodeListener;
import com.liferay.petra.sql.dsl.expression.Alias;
import com.liferay.petra.sql.dsl.expression.Expression;
import com.liferay.petra.sql.dsl.expression.Predicate;
import com.liferay.petra.sql.dsl.query.DSLQuery;
import com.liferay.petra.sql.dsl.query.sort.OrderByExpression;

import java.util.function.Consumer;

/**
* @author Tai Pham
*/
public class DoNothingExpression<T> implements Expression<T> {

@Override
public Alias<T> as(String name) {
throw new UnsupportedOperationException();
}

@Override
public OrderByExpression ascending() {
throw new UnsupportedOperationException();
}

@Override
public OrderByExpression descending() {
throw new UnsupportedOperationException();
}

@Override
public Predicate eq(Expression<T> expression) {
throw new UnsupportedOperationException();
}

@Override
public Predicate eq(T value) {
throw new UnsupportedOperationException();
}

@Override
public Predicate gt(Expression<T> expression) {
throw new UnsupportedOperationException();
}

@Override
public Predicate gt(Object value) {
throw new UnsupportedOperationException();
}

@Override
public Predicate gte(Expression<T> expression) {
throw new UnsupportedOperationException();
}

@Override
public Predicate gte(Object value) {
throw new UnsupportedOperationException();
}

@Override
public Predicate in(DSLQuery dslQuery) {
throw new UnsupportedOperationException();
}

@Override
public Predicate in(Object[] values) {
throw new UnsupportedOperationException();
}

@Override
public Predicate isNotNull() {
throw new UnsupportedOperationException();
}

@Override
public Predicate isNull() {
throw new UnsupportedOperationException();
}

@Override
public Predicate like(Expression<String> expression) {
throw new UnsupportedOperationException();
}

@Override
public Predicate like(String value) {
throw new UnsupportedOperationException();
}

@Override
public Predicate lt(Expression<T> expression) {
throw new UnsupportedOperationException();
}

@Override
public Predicate lt(Object value) {
throw new UnsupportedOperationException();
}

@Override
public Predicate lte(Expression<T> expression) {
throw new UnsupportedOperationException();
}

@Override
public Predicate lte(Object value) {
throw new UnsupportedOperationException();
}

@Override
public Predicate neq(Expression<T> expression) {
throw new UnsupportedOperationException();
}

@Override
public Predicate neq(Object value) {
throw new UnsupportedOperationException();
}

@Override
public Predicate notIn(DSLQuery dslQuery) {
throw new UnsupportedOperationException();
}

@Override
public Predicate notIn(Object[] values) {
throw new UnsupportedOperationException();
}

@Override
public Predicate notLike(Expression<String> expression) {
throw new UnsupportedOperationException();
}

@Override
public Predicate notLike(String value) {
throw new UnsupportedOperationException();
}

@Override
public void toSQL(
Consumer<String> consumer, ASTNodeListener astNodeListener) {
}

}