ARTEMIS-5075 support NOT_EQUALS op to filter some mngmnt results

This commit adds support for a NOT_EQUALS operator to the management
operations which already support filtering.

It also adds a handful of tests for the predicate since there didn't
seem to be any such tests.
This commit is contained in:
Justin Bertram 2024-09-27 15:08:38 -05:00 committed by Timothy Bish
parent ca617d79bd
commit 821005ab6e
9 changed files with 141 additions and 12 deletions

View File

@ -110,6 +110,7 @@ var Artemis;
],
operationOptions: [
{id: 'EQUALS', name: 'Equals'},
{id: 'NOT_EQUALS', name: 'Not Equals'},
{id: 'CONTAINS', name: 'Contains'},
{id: 'NOT_CONTAINS', name: 'Does Not Contain'},
{id: 'GREATER_THAN', name: 'Greater Than'},

View File

@ -132,6 +132,7 @@ var Artemis;
],
operationOptions: [
{id: 'EQUALS', name: 'Equals'},
{id: 'NOT_EQUALS', name: 'Not Equals'},
{id: 'CONTAINS', name: 'Contains'},
{id: 'NOT_CONTAINS', name: 'Does Not Contain'},
{id: 'GREATER_THAN', name: 'Greater Than'},

View File

@ -150,6 +150,7 @@ var Artemis;
],
operationOptions: [
{id: 'EQUALS', name: 'Equals'},
{id: 'NOT_EQUALS', name: 'Not Equals'},
{id: 'CONTAINS', name: 'Contains'},
{id: 'NOT_CONTAINS', name: 'Does Not Contain'},
{id: 'GREATER_THAN', name: 'Greater Than'},

View File

@ -119,6 +119,7 @@ var Artemis;
],
operationOptions: [
{id: 'EQUALS', name: 'Equals'},
{id: 'NOT_EQUALS', name: 'Not Equals'},
{id: 'CONTAINS', name: 'Contains'}
],
sortOptions: [

View File

@ -148,6 +148,7 @@ var Artemis;
],
operationOptions: [
{id: 'EQUALS', name: 'Equals'},
{id: 'NOT_EQUALS', name: 'Not Equals'},
{id: 'CONTAINS', name: 'Contains'},
{id: 'NOT_CONTAINS', name: 'Does Not Contain'},
{id: 'GREATER_THAN', name: 'Greater Than'},

View File

@ -129,6 +129,7 @@ var Artemis;
],
operationOptions: [
{id: 'EQUALS', name: 'Equals'},
{id: 'NOT_EQUALS', name: 'Not Equals'},
{id: 'CONTAINS', name: 'Contains'},
{id: 'NOT_CONTAINS', name: 'Does Not Contain'},
{id: 'GREATER_THAN', name: 'Greater Than'},

View File

@ -23,7 +23,7 @@ import java.util.function.Predicate;
public class ActiveMQFilterPredicate<T> implements Predicate<T> {
public enum Operation {
CONTAINS, NOT_CONTAINS, EQUALS, GREATER_THAN, LESS_THAN;
CONTAINS, NOT_CONTAINS, EQUALS, NOT_EQUALS, GREATER_THAN, LESS_THAN;
}
protected String field;
@ -32,10 +32,6 @@ public class ActiveMQFilterPredicate<T> implements Predicate<T> {
protected Operation operation;
public static boolean contains(String field, String value) {
return field.contains(value);
}
public ActiveMQFilterPredicate() {
}
@ -65,7 +61,7 @@ public class ActiveMQFilterPredicate<T> implements Predicate<T> {
}
public void setOperation(String operation) {
if (operation != null && !operation.equals("")) {
if (operation != null && !operation.isBlank()) {
this.operation = Operation.valueOf(operation);
}
}
@ -75,6 +71,8 @@ public class ActiveMQFilterPredicate<T> implements Predicate<T> {
switch (operation) {
case EQUALS:
return equals(field, value);
case NOT_EQUALS:
return !equals(field, value);
case CONTAINS:
return contains(field, value);
case NOT_CONTAINS:
@ -104,16 +102,22 @@ public class ActiveMQFilterPredicate<T> implements Predicate<T> {
longValue = Long.parseLong(value);
} catch (NumberFormatException ex) {
//cannot compare
return false;
if (operation == Operation.NOT_EQUALS || operation == Operation.NOT_CONTAINS) {
return true;
} else {
return false;
}
}
switch (operation) {
case EQUALS:
return field == longValue;
case NOT_EQUALS:
return field != longValue;
case CONTAINS:
return false;
case NOT_CONTAINS:
return false;
return true;
case LESS_THAN:
return field < longValue;
case GREATER_THAN:
@ -131,16 +135,22 @@ public class ActiveMQFilterPredicate<T> implements Predicate<T> {
intValue = Integer.parseInt(value);
} catch (NumberFormatException ex) {
//cannot compare
return false;
if (operation == Operation.NOT_EQUALS || operation == Operation.NOT_CONTAINS) {
return true;
} else {
return false;
}
}
switch (operation) {
case EQUALS:
return field == intValue;
case NOT_EQUALS:
return field != intValue;
case CONTAINS:
return false;
case NOT_CONTAINS:
return false;
return true;
case LESS_THAN:
return field < intValue;
case GREATER_THAN:
@ -158,16 +168,22 @@ public class ActiveMQFilterPredicate<T> implements Predicate<T> {
floatValue = Float.parseFloat(value);
} catch (NumberFormatException ex) {
//cannot compare
return false;
if (operation == Operation.NOT_EQUALS || operation == Operation.NOT_CONTAINS) {
return true;
} else {
return false;
}
}
switch (operation) {
case EQUALS:
return field == floatValue;
case NOT_EQUALS:
return field != floatValue;
case CONTAINS:
return false;
case NOT_CONTAINS:
return false;
return true;
case LESS_THAN:
return field < floatValue;
case GREATER_THAN:

View File

@ -0,0 +1,106 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.artemis.core.management.impl.view.predicate;
import org.apache.activemq.artemis.utils.RandomUtil;
import org.junit.jupiter.api.Test;
import static org.apache.activemq.artemis.core.management.impl.view.predicate.ActiveMQFilterPredicate.Operation.CONTAINS;
import static org.apache.activemq.artemis.core.management.impl.view.predicate.ActiveMQFilterPredicate.Operation.EQUALS;
import static org.apache.activemq.artemis.core.management.impl.view.predicate.ActiveMQFilterPredicate.Operation.GREATER_THAN;
import static org.apache.activemq.artemis.core.management.impl.view.predicate.ActiveMQFilterPredicate.Operation.LESS_THAN;
import static org.apache.activemq.artemis.core.management.impl.view.predicate.ActiveMQFilterPredicate.Operation.NOT_CONTAINS;
import static org.apache.activemq.artemis.core.management.impl.view.predicate.ActiveMQFilterPredicate.Operation.NOT_EQUALS;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class PredicateTest {
@Test
public void testBasePredicateEquals() {
String string = RandomUtil.randomString();
ActiveMQFilterPredicate<String> predicate = new ActiveMQFilterPredicate<>();
predicate.setOperation(EQUALS.name());
predicate.setValue(string);
assertTrue(predicate.matches(string));
assertFalse(predicate.matches(RandomUtil.randomString()));
assertFalse(predicate.matches(0L));
assertFalse(predicate.matches(0f));
assertFalse(predicate.matches(0));
}
@Test
public void testBasePredicateNotEquals() {
String string = RandomUtil.randomString();
ActiveMQFilterPredicate<String> predicate = new ActiveMQFilterPredicate<>();
predicate.setOperation(NOT_EQUALS.name());
predicate.setValue(string);
assertFalse(predicate.matches(string));
assertTrue(predicate.matches(RandomUtil.randomString()));
assertTrue(predicate.matches(0L));
assertTrue(predicate.matches(0f));
assertTrue(predicate.matches(0));
}
@Test
public void testBasePredicateContains() {
ActiveMQFilterPredicate<String> predicate = new ActiveMQFilterPredicate<>();
predicate.setOperation(CONTAINS.name());
predicate.setValue("12");
assertTrue(predicate.matches("0123"));
assertFalse(predicate.matches("43"));
assertFalse(predicate.matches(0L));
assertFalse(predicate.matches(0f));
assertFalse(predicate.matches(0));
}
@Test
public void testBasePredicateNotContains() {
ActiveMQFilterPredicate<String> predicate = new ActiveMQFilterPredicate<>();
predicate.setOperation(NOT_CONTAINS.name());
predicate.setValue("12");
assertFalse(predicate.matches("0123"));
assertTrue(predicate.matches("42"));
assertTrue(predicate.matches(0L));
assertTrue(predicate.matches(0f));
assertTrue(predicate.matches(0));
}
@Test
public void testBasePredicateLessThan() {
ActiveMQFilterPredicate<Integer> predicate = new ActiveMQFilterPredicate<>();
predicate.setOperation(LESS_THAN.name());
predicate.setValue("12");
assertFalse(predicate.matches("foo"));
assertFalse(predicate.matches(42));
assertTrue(predicate.matches(0L));
assertTrue(predicate.matches(0f));
assertTrue(predicate.matches(0));
}
@Test
public void testBasePredicateGreaterThan() {
ActiveMQFilterPredicate<Integer> predicate = new ActiveMQFilterPredicate<>();
predicate.setOperation(GREATER_THAN.name());
predicate.setValue("12");
assertFalse(predicate.matches("foo"));
assertTrue(predicate.matches(42));
assertFalse(predicate.matches(0L));
assertFalse(predicate.matches(0f));
assertFalse(predicate.matches(0));
}
}

View File

@ -684,6 +684,7 @@ A handful of management operations support a special JSON syntax to filter resul
** `CONTAINS`
** `NOT_CONTAINS`
** `EQUALS`
** `NOT_EQUALS`
** `GREATER_THAN`
** `LESS_THAN`
* `value`