Small serach fields refactoring (add lombok & style) (#1823)

Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2024-08-23 12:24:29 +03:00
committed by GitHub
parent ac34b952d9
commit 55cc600114
23 changed files with 238 additions and 693 deletions

View File

@@ -12,19 +12,14 @@ package org.eclipse.hawkbit.repository.exception;
import org.eclipse.hawkbit.exception.AbstractServerRtException;
import org.eclipse.hawkbit.exception.SpServerError;
import java.io.Serial;
/**
* Exception used by the REST API in case of invalid field name in the rsql
* search parameter.
*
*
*
*
* Exception used by the REST API in case of invalid field name in the rsql search parameter.
*/
public class RSQLParameterUnsupportedFieldException extends AbstractServerRtException {
/**
*
*/
@Serial
private static final long serialVersionUID = 1L;
/**
@@ -35,12 +30,21 @@ public class RSQLParameterUnsupportedFieldException extends AbstractServerRtExce
super(SpServerError.SP_REST_RSQL_PARAM_INVALID_FIELD);
}
/**
* Creates a new RSQLParameterUnsupportedFieldException with
* {@link SpServerError#SP_REST_RSQL_PARAM_INVALID_FIELD} error.
*
* @param message the message of the exception
*/
public RSQLParameterUnsupportedFieldException(final String message) {
super(message, SpServerError.SP_REST_RSQL_PARAM_INVALID_FIELD);
}
/**
* Creates a new RSQLParameterUnsupportedFieldException with
* {@link SpServerError#SP_REST_RSQL_PARAM_INVALID_FIELD} error.
*
* @param cause
* the cause (which is saved for later retrieval by the
* @param cause the cause (which is saved for later retrieval by the
* getCause() method). (A null value is permitted, and indicates
* that the cause is nonexistent or unknown.)
*/
@@ -52,10 +56,8 @@ public class RSQLParameterUnsupportedFieldException extends AbstractServerRtExce
* Creates a new RSQLParameterUnsupportedFieldException with
* {@link SpServerError#SP_REST_RSQL_PARAM_INVALID_FIELD} error.
*
* @param message
* the message of the exception
* @param cause
* the cause (which is saved for later retrieval by the
* @param message the message of the exception
* @param cause the cause (which is saved for later retrieval by the
* getCause() method). (A null value is permitted, and indicates
* that the cause is nonexistent or unknown.)
*/

View File

@@ -31,11 +31,8 @@ public abstract class AbstractFieldNameRSQLVisitor<A extends Enum<A> & FieldName
}
protected A getFieldEnumByName(final ComparisonNode node) {
String enumName = node.getSelector();
final String[] graph = enumName.split("\\" + FieldNameProvider.SUB_ATTRIBUTE_SEPARATOR);
if (graph.length != 0) {
enumName = graph[0];
}
final String[] graph = node.getSelector().split(FieldNameProvider.SUB_ATTRIBUTE_SPLIT_REGEX);
final String enumName = graph.length == 0 ? node.getSelector() : graph[0];
log.debug("get field identifier by name {} of enum type {}", enumName, fieldNameProvider);
try {
return Enum.valueOf(fieldNameProvider, enumName.toUpperCase());
@@ -45,25 +42,21 @@ public abstract class AbstractFieldNameRSQLVisitor<A extends Enum<A> & FieldName
}
protected String getAndValidatePropertyFieldName(final A propertyEnum, final ComparisonNode node) {
final String[] graph = propertyEnum.getSubAttributes(node.getSelector());
validateMapParameter(propertyEnum, node, graph);
final String[] subAttributes = propertyEnum.getSubAttributes(node.getSelector());
validateMapParameter(propertyEnum, node, subAttributes);
// sub entity need minimum 1 dot
if (!propertyEnum.getSubEntityAttributes().isEmpty() && graph.length < 2) {
if (!propertyEnum.getSubEntityAttributes().isEmpty() && subAttributes.length < 2) {
throw createRSQLParameterUnsupportedException(node, null);
}
final StringBuilder fieldNameBuilder = new StringBuilder(propertyEnum.getFieldName());
for (int i = 1; i < graph.length; i++) {
final String propertyField = getFormattedSubEntityAttribute(propertyEnum ,graph[i]);
for (int i = 1; i < subAttributes.length; i++) {
final String propertyField = getFormattedSubEntityAttribute(propertyEnum ,subAttributes[i]);
fieldNameBuilder.append(FieldNameProvider.SUB_ATTRIBUTE_SEPARATOR).append(propertyField);
// the key of map is not in the graph
if (propertyEnum.isMap() && graph.length == (i + 1)) {
if (propertyEnum.isMap() && subAttributes.length == (i + 1)) {
continue;
}
@@ -75,30 +68,26 @@ public abstract class AbstractFieldNameRSQLVisitor<A extends Enum<A> & FieldName
return fieldNameBuilder.toString();
}
protected void validateMapParameter(final A propertyEnum, final ComparisonNode node, final String[] graph) {
private void validateMapParameter(final A propertyEnum, final ComparisonNode node, final String[] subAttributes) {
if (!propertyEnum.isMap()) {
return;
}
if (!propertyEnum.getSubEntityAttributes().isEmpty()) {
throw new UnsupportedOperationException(
"Currently subentity attributes for maps are not supported, alternatively you could use the key/value tuple, defined by SimpleImmutableEntry class");
"Currently sub-entity attributes for maps are not supported, alternatively you could use the key/value tuple, defined by SimpleImmutableEntry class");
}
// enum.key
final int minAttributeForMap = 2;
if (graph.length != minAttributeForMap) {
throw new RSQLParameterUnsupportedFieldException("The syntax of the given map search parameter field {"
+ node.getSelector() + "} is wrong. Syntax is: fieldname.keyname", new Exception());
if (subAttributes.length != 2) {
throw new RSQLParameterUnsupportedFieldException("The syntax of the given map search parameter field {" +
node.getSelector() + "} is wrong. Syntax is: <enum name>.<key name>");
}
}
/**
* @param node
* current processing node
* @param rootException
* in case there is a cause otherwise {@code null}
* @param node current processing node
* @param rootException in case there is a cause otherwise {@code null}
* @return Exception with prepared message extracted from the comparison node.
*/
protected RSQLParameterUnsupportedFieldException createRSQLParameterUnsupportedException(
@@ -110,20 +99,20 @@ public abstract class AbstractFieldNameRSQLVisitor<A extends Enum<A> & FieldName
}
private String getFormattedSubEntityAttribute(final A propertyEnum, final String propertyField) {
return propertyEnum.getSubEntityAttributes().stream().filter(attr -> attr.equalsIgnoreCase(propertyField))
.findFirst().orElse(propertyField);
return propertyEnum.getSubEntityAttributes().stream()
.filter(attr -> attr.equalsIgnoreCase(propertyField))
.findFirst().orElse(propertyField);
}
private List<String> getExpectedFieldList() {
final List<String> expectedFieldList = Arrays.stream(fieldNameProvider.getEnumConstants())
.filter(enumField -> enumField.getSubEntityAttributes().isEmpty()).map(enumField -> {
final String enumFieldName = enumField.name().toLowerCase();
if (enumField.isMap()) {
return enumFieldName + FieldNameProvider.SUB_ATTRIBUTE_SEPARATOR + "keyName";
} else {
return enumFieldName;
}
return enumFieldName;
}).collect(Collectors.toList());
final List<String> expectedSubFieldList = Arrays.stream(fieldNameProvider.getEnumConstants())
@@ -131,11 +120,11 @@ public abstract class AbstractFieldNameRSQLVisitor<A extends Enum<A> & FieldName
final List<String> subEntity = enumField
.getSubEntityAttributes().stream().map(fieldName -> enumField.name().toLowerCase()
+ FieldNameProvider.SUB_ATTRIBUTE_SEPARATOR + fieldName)
.collect(Collectors.toList());
.toList();
return subEntity.stream();
}).collect(Collectors.toList());
}).toList();
expectedFieldList.addAll(expectedSubFieldList);
return expectedFieldList;
}
}
}

View File

@@ -56,7 +56,6 @@ public class RsqlParserValidationOracle implements RsqlValidationOracle {
@Override
public ValidationOracleContext suggest(final String rsqlQuery, final int cursorPosition) {
final List<SuggestToken> expectedTokens = new ArrayList<>();
final ValidationOracleContext context = new ValidationOracleContext();
context.setSyntaxError(true);
@@ -92,8 +91,8 @@ public class RsqlParserValidationOracle implements RsqlValidationOracle {
final Collection<String> tokenImages = TokenDescription.getTokenImage(TokenDescription.LOGICAL_OP);
final List<SuggestToken> logicalOps = new ArrayList<>(tokenImages.size());
for (final String tokenImage : tokenImages) {
logicalOps.add(new SuggestToken(currentQueryLength, currentQueryLength + tokenImage.length(), null,
tokenImage));
logicalOps.add(
new SuggestToken(currentQueryLength, currentQueryLength + tokenImage.length(), null, tokenImage));
}
return logicalOps;
}

View File

@@ -18,6 +18,7 @@ import java.util.stream.Collectors;
import org.eclipse.hawkbit.repository.TargetFields;
import org.eclipse.hawkbit.repository.jpa.AbstractJpaIntegrationTest;
import org.eclipse.hawkbit.repository.rsql.RsqlValidationOracle;
import org.eclipse.hawkbit.repository.rsql.SuggestToken;
import org.eclipse.hawkbit.repository.rsql.ValidationOracleContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -36,7 +37,7 @@ public class RSQLParserValidationOracleTest extends AbstractJpaIntegrationTest {
private static final String[] OP_SUGGESTIONS = new String[] { "==", "!=", "=ge=", "=le=", "=gt=", "=lt=", "=in=",
"=out=" };
private static final String[] FIELD_SUGGESTIONS = Arrays.stream(TargetFields.values())
.map(field -> field.name().toLowerCase()).toArray(size -> new String[size]);
.map(field -> field.name().toLowerCase()).toArray(String[]::new);
private static final String[] AND_OR_SUGGESTIONS = new String[] { "and", "or" };
private static final String[] NAME_VERSION_SUGGESTIONS = new String[] { "name", "version" };
@@ -73,10 +74,9 @@ public class RSQLParserValidationOracleTest extends AbstractJpaIntegrationTest {
}
private List<String> getSuggestions(final String rsqlQuery) {
final ValidationOracleContext suggest = rsqlValidationOracle.suggest(rsqlQuery, -1);
final List<String> currentSuggestions = suggest.getSuggestionContext().getSuggestions().stream()
.map(suggestion -> suggestion.getSuggestion()).collect(Collectors.toList());
return currentSuggestions;
return rsqlValidationOracle
.suggest(rsqlQuery, -1).getSuggestionContext().getSuggestions().stream()
.map(SuggestToken::getSuggestion)
.collect(Collectors.toList());
}
}
}