Fix the wildcard search and add additional junit on software module (#1327)

* fix the wildcard search and add additional junit on software module
* remove unwanted line of code
* Altered the test class to check the jenkins build
* trying other umlout characters as well
* fixed the jenkins issue and reverted all jenkins related changes
* Fixed PR comments
* Fixed PR comments
* made the variable final
* removed the unwanted uppercase conversion

Signed-off-by: Shruthi Manavalli Ramanna <shruthimanavalli.ramanna@bosch-si.com>
This commit is contained in:
Shruthi Manavalli Ramanna
2023-03-08 15:22:51 +01:00
committed by GitHub
parent bc70d51c94
commit 92256ed787
2 changed files with 51 additions and 28 deletions

View File

@@ -21,7 +21,6 @@ import java.util.Set;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression; import javax.persistence.criteria.Expression;
@@ -35,6 +34,13 @@ import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import javax.persistence.criteria.Subquery; import javax.persistence.criteria.Subquery;
import com.google.common.collect.Lists;
import cz.jirutka.rsql.parser.ast.AndNode;
import cz.jirutka.rsql.parser.ast.ComparisonNode;
import cz.jirutka.rsql.parser.ast.LogicalNode;
import cz.jirutka.rsql.parser.ast.Node;
import cz.jirutka.rsql.parser.ast.OrNode;
import cz.jirutka.rsql.parser.ast.RSQLVisitor;
import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.math.NumberUtils;
import org.eclipse.hawkbit.repository.FieldNameProvider; import org.eclipse.hawkbit.repository.FieldNameProvider;
import org.eclipse.hawkbit.repository.FieldValueConverter; import org.eclipse.hawkbit.repository.FieldValueConverter;
@@ -49,15 +55,6 @@ import org.springframework.orm.jpa.vendor.Database;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import com.google.common.collect.Lists;
import cz.jirutka.rsql.parser.ast.AndNode;
import cz.jirutka.rsql.parser.ast.ComparisonNode;
import cz.jirutka.rsql.parser.ast.LogicalNode;
import cz.jirutka.rsql.parser.ast.Node;
import cz.jirutka.rsql.parser.ast.OrNode;
import cz.jirutka.rsql.parser.ast.RSQLVisitor;
/** /**
* An implementation of the {@link RSQLVisitor} to visit the parsed tokens and * An implementation of the {@link RSQLVisitor} to visit the parsed tokens and
* build JPA where clauses. * build JPA where clauses.
@@ -76,6 +73,7 @@ public class JpaQueryRsqlVisitor<A extends Enum<A> & FieldNameProvider, T> exten
public static final Character LIKE_WILDCARD = '*'; public static final Character LIKE_WILDCARD = '*';
private static final char ESCAPE_CHAR = '\\'; private static final char ESCAPE_CHAR = '\\';
private static final List<String> NO_JOINS_OPERATOR = Lists.newArrayList("!=", "=out="); private static final List<String> NO_JOINS_OPERATOR = Lists.newArrayList("!=", "=out=");
private static final String ESCAPE_CHAR_WITH_ASTERISK = ESCAPE_CHAR +"*";
private final Map<Integer, Set<Join<Object, Object>>> joinsInLevel = new HashMap<>(3); private final Map<Integer, Set<Join<Object, Object>>> joinsInLevel = new HashMap<>(3);
@@ -571,8 +569,18 @@ public class JpaQueryRsqlVisitor<A extends Enum<A> & FieldNameProvider, T> exten
} else { } else {
escaped = transformedValue.replace("%", ESCAPE_CHAR + "%").replace("_", ESCAPE_CHAR + "_"); escaped = transformedValue.replace("%", ESCAPE_CHAR + "%").replace("_", ESCAPE_CHAR + "_");
} }
return replaceIfRequired(escaped);
}
return escaped.replace(LIKE_WILDCARD, '%').toUpperCase(); private String replaceIfRequired(final String escapedValue) {
final String finalizedValue;
if (escapedValue.contains(ESCAPE_CHAR_WITH_ASTERISK)) {
finalizedValue = escapedValue.replace(ESCAPE_CHAR_WITH_ASTERISK, "$").replace(LIKE_WILDCARD, '%')
.replace("$", ESCAPE_CHAR_WITH_ASTERISK);
} else {
finalizedValue = escapedValue.replace(LIKE_WILDCARD, '%');
}
return finalizedValue.toUpperCase();
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@@ -39,7 +39,10 @@ public class RSQLSoftwareModuleFieldTest extends AbstractJpaIntegrationTest {
softwareModuleManagement.create(entityFactory.softwareModule().create().type(runtimeType).name("oracle-jre") softwareModuleManagement.create(entityFactory.softwareModule().create().type(runtimeType).name("oracle-jre")
.version("1.7.2").description("aa")); .version("1.7.2").description("aa"));
softwareModuleManagement.create( softwareModuleManagement.create(
entityFactory.softwareModule().create().type(osType).name("poky").version("3.0.2").description("aa")); entityFactory.softwareModule().create().type(osType).name("poki").version("3.0.2").description("aa"));
softwareModuleManagement
.create(entityFactory.softwareModule().create().type(osType).name("*§$%&/&%ÄÜ*Ö@").version("1.0.0")
.description("wildcard testing"));
softwareModuleManagement softwareModuleManagement
.create(entityFactory.softwareModule().create().type(osType).name("noDesc").version("noDesc")); .create(entityFactory.softwareModule().create().type(osType).name("noDesc").version("noDesc"));
@@ -59,62 +62,74 @@ public class RSQLSoftwareModuleFieldTest extends AbstractJpaIntegrationTest {
@Description("Test filter software module by id") @Description("Test filter software module by id")
public void testFilterByParameterId() { public void testFilterByParameterId() {
assertRSQLQuery(SoftwareModuleFields.ID.name() + "==" + ah.getId(), 1); assertRSQLQuery(SoftwareModuleFields.ID.name() + "==" + ah.getId(), 1);
assertRSQLQuery(SoftwareModuleFields.ID.name() + "!=" + ah.getId(), 4); assertRSQLQuery(SoftwareModuleFields.ID.name() + "!=" + ah.getId(), 5);
assertRSQLQuery(SoftwareModuleFields.ID.name() + "==" + -1, 0); assertRSQLQuery(SoftwareModuleFields.ID.name() + "==" + -1, 0);
assertRSQLQuery(SoftwareModuleFields.ID.name() + "!=" + -1, 5); assertRSQLQuery(SoftwareModuleFields.ID.name() + "!=" + -1, 6);
// Not supported for numbers // Not supported for numbers
if (Database.POSTGRESQL.equals(getDatabase())) { if (Database.POSTGRESQL.equals(getDatabase())) {
return; return;
} }
assertRSQLQuery(SoftwareModuleFields.ID.name() + "==*", 5); assertRSQLQuery(SoftwareModuleFields.ID.name() + "==*", 6);
assertRSQLQuery(SoftwareModuleFields.ID.name() + "==noexist*", 0); assertRSQLQuery(SoftwareModuleFields.ID.name() + "==noexist*", 0);
assertRSQLQuery(SoftwareModuleFields.ID.name() + "=in=(" + ah.getId() + ",1000000)", 1); assertRSQLQuery(SoftwareModuleFields.ID.name() + "=in=(" + ah.getId() + ",1000000)", 1);
assertRSQLQuery(SoftwareModuleFields.ID.name() + "=out=(" + ah.getId() + ",1000000)", 4); assertRSQLQuery(SoftwareModuleFields.ID.name() + "=out=(" + ah.getId() + ",1000000)", 5);
} }
@Test @Test
@Description("Test filter software module by name") @Description("Test filter software module by name")
public void testFilterByParameterName() { public void testFilterByParameterName() {
assertRSQLQuery(SoftwareModuleFields.NAME.name() + "==agent-hub", 1); assertRSQLQuery(SoftwareModuleFields.NAME.name() + "==agent-hub", 1);
assertRSQLQuery(SoftwareModuleFields.NAME.name() + "!=agent-hub", 4); assertRSQLQuery(SoftwareModuleFields.NAME.name() + "!=agent-hub", 5);
assertRSQLQuery(SoftwareModuleFields.NAME.name() + "==agent-hub*", 2); assertRSQLQuery(SoftwareModuleFields.NAME.name() + "==agent-hub*", 2);
assertRSQLQuery(SoftwareModuleFields.NAME.name() + "!=agent-hub*", 3); assertRSQLQuery(SoftwareModuleFields.NAME.name() + "!=agent-hub*", 4);
assertRSQLQuery(SoftwareModuleFields.NAME.name() + "==noExist*", 0); assertRSQLQuery(SoftwareModuleFields.NAME.name() + "==noExist*", 0);
assertRSQLQuery(SoftwareModuleFields.NAME.name() + "=in=(agent-hub,notexist)", 1); assertRSQLQuery(SoftwareModuleFields.NAME.name() + "=in=(agent-hub,notexist)", 1);
assertRSQLQuery(SoftwareModuleFields.NAME.name() + "=out=(agent-hub,notexist)", 4); assertRSQLQuery(SoftwareModuleFields.NAME.name() + "=out=(agent-hub,notexist)", 5);
//wildcard entries
assertRSQLQuery(SoftwareModuleFields.NAME.name() + "==*$*", 1);
assertRSQLQuery(SoftwareModuleFields.NAME.name() + "==*§*", 1);
assertRSQLQuery(SoftwareModuleFields.NAME.name() + "==*Ö*", 1);
assertRSQLQuery(SoftwareModuleFields.NAME.name() + "==*Ä*", 1);
assertRSQLQuery(SoftwareModuleFields.NAME.name() + "==*Ü*", 1);
assertRSQLQuery(SoftwareModuleFields.NAME.name() + "==*@*", 1);
assertRSQLQuery(SoftwareModuleFields.NAME.name() + "==*/*", 1);
assertRSQLQuery(SoftwareModuleFields.NAME.name() + "==*&*", 1);
assertRSQLQuery(SoftwareModuleFields.NAME.name() + "==***", 6);
assertRSQLQuery(SoftwareModuleFields.NAME.name() + "==*\\**", 1);
} }
@Test @Test
@Description("Test filter software module by description") @Description("Test filter software module by description")
public void testFilterByParameterDescription() { public void testFilterByParameterDescription() {
assertRSQLQuery(SoftwareModuleFields.DESCRIPTION.name() + "==''", 1); assertRSQLQuery(SoftwareModuleFields.DESCRIPTION.name() + "==''", 1);
assertRSQLQuery(SoftwareModuleFields.DESCRIPTION.name() + "!=''", 4); assertRSQLQuery(SoftwareModuleFields.DESCRIPTION.name() + "!=''", 5);
assertRSQLQuery(SoftwareModuleFields.DESCRIPTION.name() + "==agent-hub", 1); assertRSQLQuery(SoftwareModuleFields.DESCRIPTION.name() + "==agent-hub", 1);
assertRSQLQuery(SoftwareModuleFields.DESCRIPTION.name() + "!=agent-hub", 4); assertRSQLQuery(SoftwareModuleFields.DESCRIPTION.name() + "!=agent-hub", 5);
assertRSQLQuery(SoftwareModuleFields.DESCRIPTION.name() + "==noExist*", 0); assertRSQLQuery(SoftwareModuleFields.DESCRIPTION.name() + "==noExist*", 0);
assertRSQLQuery(SoftwareModuleFields.DESCRIPTION.name() + "=in=(agent-hub,notexist)", 1); assertRSQLQuery(SoftwareModuleFields.DESCRIPTION.name() + "=in=(agent-hub,notexist)", 1);
assertRSQLQuery(SoftwareModuleFields.DESCRIPTION.name() + "=out=(agent-hub,notexist)", 4); assertRSQLQuery(SoftwareModuleFields.DESCRIPTION.name() + "=out=(agent-hub,notexist)", 5);
} }
@Test @Test
@Description("Test filter software module by version") @Description("Test filter software module by version")
public void testFilterByParameterVersion() { public void testFilterByParameterVersion() {
assertRSQLQuery(SoftwareModuleFields.VERSION.name() + "==1.0.1", 2); assertRSQLQuery(SoftwareModuleFields.VERSION.name() + "==1.0.1", 2);
assertRSQLQuery(SoftwareModuleFields.VERSION.name() + "!=v1.0", 5); assertRSQLQuery(SoftwareModuleFields.VERSION.name() + "!=v1.0", 6);
assertRSQLQuery(SoftwareModuleFields.VERSION.name() + "=in=(1.0.1,1.0.2)", 2); assertRSQLQuery(SoftwareModuleFields.VERSION.name() + "=in=(1.0.1,1.0.2)", 2);
assertRSQLQuery(SoftwareModuleFields.VERSION.name() + "=out=(1.0.1)", 3); assertRSQLQuery(SoftwareModuleFields.VERSION.name() + "=out=(1.0.1)", 4);
} }
@Test @Test
@Description("Test filter software module by type key") @Description("Test filter software module by type key")
public void testFilterByType() { public void testFilterByType() {
assertRSQLQuery(SoftwareModuleFields.TYPE.name() + "==" + TestdataFactory.SM_TYPE_APP, 2); assertRSQLQuery(SoftwareModuleFields.TYPE.name() + "==" + TestdataFactory.SM_TYPE_APP, 2);
assertRSQLQuery(SoftwareModuleFields.TYPE.name() + "!=" + TestdataFactory.SM_TYPE_APP, 3); assertRSQLQuery(SoftwareModuleFields.TYPE.name() + "!=" + TestdataFactory.SM_TYPE_APP, 4);
assertRSQLQuery(SoftwareModuleFields.TYPE.name() + "==noExist*", 0); assertRSQLQuery(SoftwareModuleFields.TYPE.name() + "==noExist*", 0);
assertRSQLQuery(SoftwareModuleFields.TYPE.name() + "=in=(" + TestdataFactory.SM_TYPE_APP + ")", 2); assertRSQLQuery(SoftwareModuleFields.TYPE.name() + "=in=(" + TestdataFactory.SM_TYPE_APP + ")", 2);
assertRSQLQuery(SoftwareModuleFields.TYPE.name() + "=out=(" + TestdataFactory.SM_TYPE_APP + ")", 3); assertRSQLQuery(SoftwareModuleFields.TYPE.name() + "=out=(" + TestdataFactory.SM_TYPE_APP + ")", 4);
} }
@Test @Test
@@ -131,10 +146,10 @@ public class RSQLSoftwareModuleFieldTest extends AbstractJpaIntegrationTest {
} }
private void assertRSQLQuery(final String rsqlParam, final long excpectedEntity) { private void assertRSQLQuery(final String rsqlParam, final long expectedEntity) {
final Page<SoftwareModule> find = softwareModuleManagement.findByRsql(PageRequest.of(0, 100), rsqlParam); final Page<SoftwareModule> find = softwareModuleManagement.findByRsql(PageRequest.of(0, 100), rsqlParam);
final long countAll = find.getTotalElements(); final long countAll = find.getTotalElements();
assertThat(find).isNotNull(); assertThat(find).isNotNull();
assertThat(countAll).isEqualTo(excpectedEntity); assertThat(countAll).isEqualTo(expectedEntity);
} }
} }