Skip to content

Instantly share code, notes, and snippets.

@alopresto
Created March 23, 2016 05:17
Show Gist options
  • Save alopresto/faff5051f3c7b7102990 to your computer and use it in GitHub Desktop.
Save alopresto/faff5051f3c7b7102990 to your computer and use it in GitHub Desktop.
A Java JUnit test demonstrating an expression language query which will return true if and only if all attributes enumerated are not empty (i.e. not null, zero-length, or all whitespace characters).
/*
* 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.nifi.attribute.expression.language;
import org.antlr.runtime.tree.Tree;
import org.apache.nifi.attribute.expression.language.Query.Range;
import org.apache.nifi.attribute.expression.language.evaluation.QueryResult;
import org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException;
import org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException;
import org.apache.nifi.expression.AttributeExpression.ResultType;
import org.apache.nifi.flowfile.FlowFile;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class TestQuery {
@Test
public void testShouldEvaluateEmptyAttributes() {
/* This test came from a mailing list comment requesting notEmpty()
*
* This expression should return true IFF all attributes are:
* - not null
* - not empty (zero length String)
* - not consisting entirely of whitespace characters
* */
// Arrange
// This map of attributes has all "empty" elements
final Map<String, String> emptyAttributes = new HashMap<>();
emptyAttributes.put("attr1", "");
emptyAttributes.put("attr2", null);
emptyAttributes.put("attr3", " ");
// This map has some populated and some "empty" elements
final Map<String, String> semiPopulatedAttributes = new HashMap<>();
semiPopulatedAttributes.put("attr1", "test");
semiPopulatedAttributes.put("attr2", "");
semiPopulatedAttributes.put("attr3", " ");
// This map has every element populated
final Map<String, String> populatedAttributes = new HashMap<>();
populatedAttributes.put("attr1", "test");
populatedAttributes.put("attr2", "for");
populatedAttributes.put("attr3", "Paul");
// Act
String anyEmptyQuery = "${anyAttribute(\"attr1\", \"attr2\", \"attr3\"):isEmpty()}";
String noneEmptyQuery = "${allAttributes(\"attr1\", \"attr2\", \"attr3\"):isEmpty():not()}";
// Assert
verifyEquals(anyEmptyQuery, emptyAttributes, true);
verifyEquals(noneEmptyQuery, emptyAttributes, false);
verifyEquals(anyEmptyQuery, semiPopulatedAttributes, true);
verifyEquals(noneEmptyQuery, semiPopulatedAttributes, false);
verifyEquals(anyEmptyQuery, populatedAttributes, false);
verifyEquals(noneEmptyQuery, populatedAttributes, true);
}
private void verifyEquals(final String expression, final Map<String, String> attributes, final Object expectedResult) {
Query.validateExpression(expression, false);
assertEquals(String.valueOf(expectedResult), Query.evaluateExpressions(expression, attributes, null));
final Query query = Query.compile(expression);
final QueryResult<?> result = query.evaluate(attributes);
if (expectedResult instanceof Number) {
assertEquals(ResultType.NUMBER, result.getResultType());
} else if (expectedResult instanceof Boolean) {
assertEquals(ResultType.BOOLEAN, result.getResultType());
} else {
assertEquals(ResultType.STRING, result.getResultType());
}
assertEquals(expectedResult, result.getValue());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment