@Test public void testDate() { final Calendar now = Calendar.getInstance(); final int year = now.get(Calendar.YEAR); final Map<String, String> attributes = new HashMap<>(); attributes.put("entryDate", String.valueOf(now.getTimeInMillis())); verifyEquals( "${entryDate:toNumber():toDate():format('yyyy')}", attributes, String.valueOf(year)); attributes.clear(); attributes.put("month", "3"); attributes.put("day", "4"); attributes.put("year", "2013"); assertEquals( "63", Query.evaluateExpressions( "${year:append('/'):append(${month}):append('/'):append(${day}):toDate('yyyy/MM/dd'):format('D')}", attributes, null)); assertEquals( "63", Query.evaluateExpressions( "${year:append('/'):append('${month}'):append('/'):append('${day}'):toDate('yyyy/MM/dd'):format('D')}", attributes, null)); verifyEquals( "${year:append('/'):append(${month}):append('/'):append(${day}):toDate('yyyy/MM/dd'):format('D')}", attributes, "63"); }
@Test public void testAnyDelineatedValue() { final Map<String, String> attributes = new HashMap<>(); attributes.put("abc", "a,b,c"); attributes.put("xyz", "abc"); final String query = "${anyDelineatedValue('${abc}', ','):equals('b')}"; assertEquals(ResultType.BOOLEAN, Query.getResultType(query)); assertEquals("true", Query.evaluateExpressions(query, attributes, null)); assertEquals( "true", Query.evaluateExpressions( "${anyDelineatedValue('${abc}', ','):equals('a')}", attributes, null)); assertEquals( "true", Query.evaluateExpressions( "${anyDelineatedValue('${abc}', ','):equals('c')}", attributes, null)); assertEquals( "false", Query.evaluateExpressions( "${anyDelineatedValue('${abc}', ','):equals('d')}", attributes, null)); verifyEquals("${anyDelineatedValue(${abc}, ','):equals('b')}", attributes, true); verifyEquals("${anyDelineatedValue(${abc}, ','):equals('a')}", attributes, true); verifyEquals("${anyDelineatedValue(${abc}, ','):equals('c')}", attributes, true); verifyEquals("${anyDelineatedValue(${abc}, ','):equals('d')}", attributes, false); }
@Test public void testWithTicksOutside() { final Map<String, String> attributes = new HashMap<>(); attributes.put("attr", "My Value"); assertEquals(1, Query.extractExpressionRanges("\"${attr}").size()); assertEquals(1, Query.extractExpressionRanges("'${attr}").size()); assertEquals(1, Query.extractExpressionRanges("'${attr}'").size()); assertEquals(1, Query.extractExpressionRanges("${attr}").size()); assertEquals("'My Value'", Query.evaluateExpressions("'${attr}'", attributes, null)); assertEquals("'My Value", Query.evaluateExpressions("'${attr}", attributes, null)); }
@Test public void testEmbeddedExpressionsAndQuotes() { final Map<String, String> attributes = new HashMap<>(); attributes.put("x", "abc"); attributes.put("a", "abc"); verifyEquals("${x:equals(${a})}", attributes, true); Query.validateExpression("${x:equals('${a}')}", false); assertEquals("true", Query.evaluateExpressions("${x:equals('${a}')}", attributes, null)); Query.validateExpression("${x:equals(\"${a}\")}", false); assertEquals("true", Query.evaluateExpressions("${x:equals(\"${a}\")}", attributes, null)); }
@Test public void testNewLinesAndTabsInQuery() { final String query = "${ abc:equals('abc'):or( \n\t${xx:isNull()}\n) }"; assertEquals(ResultType.BOOLEAN, Query.getResultType(query)); Query.validateExpression(query, false); assertEquals("true", Query.evaluateExpressions(query)); }
@Test public void testEqualsEmbedded() { final Map<String, String> attributes = new HashMap<>(); attributes.put("x", "hello"); attributes.put("y", "good-bye"); verifyEquals("${x:equals( ${y} )}", attributes, false); attributes.put("y", "hello"); verifyEquals("${x:equals( ${y} )}", attributes, true); attributes.put("x", "4"); attributes.put("y", "3"); attributes.put("z", "1"); attributes.put("h", "100"); verifyEquals("${x:toNumber():lt( ${y:toNumber():plus( ${h:toNumber()} )} )}", attributes, true); verifyEquals("${h:toNumber():ge( ${y:toNumber():plus( ${z:toNumber()} )} )}", attributes, true); verifyEquals( "${x:toNumber():equals( ${y:toNumber():plus( ${z:toNumber()} )} )}", attributes, true); attributes.put("x", "88"); verifyEquals("${x:toNumber():gt( ${y:toNumber():plus( ${z:toNumber()} )} )}", attributes, true); attributes.put("y", "88"); assertEquals("true", Query.evaluateExpressions("${x:equals( '${y}' )}", attributes, null)); }
@Test public void testSubjectAsEmbeddedExpressionWithSurroundChars() { final Map<String, String> attributes = new HashMap<>(); attributes.put("b", "x"); attributes.put("abcxcba", "hello"); final String evaluated = Query.evaluateExpressions("${ 'abc${b}cba':substring(0, 1) }", attributes, null); assertEquals("h", evaluated); }
@Test public void testFind() { final Map<String, String> attributes = new HashMap<>(); attributes.put("abc", "1234xyz4321"); attributes.put("end", "xyz"); attributes.put("xyz", "4132"); attributes.put("hello", "world!"); attributes.put("dotted", "abc.xyz"); final String evaluated = Query.evaluateExpressions("${abc:find('1234${end}4321')}", attributes, null); assertEquals("true", evaluated); attributes.put("end", "888"); final String secondEvaluation = Query.evaluateExpressions("${abc:find('${end}4321')}", attributes, null); assertEquals("false", secondEvaluation); verifyEquals("${dotted:find('\\.')}", attributes, true); }
private String evaluateQueryForEscape( final String queryString, final Map<String, String> attributes) { FlowFile mockFlowFile = Mockito.mock(FlowFile.class); Mockito.when(mockFlowFile.getAttributes()).thenReturn(attributes); Mockito.when(mockFlowFile.getId()).thenReturn(1L); Mockito.when(mockFlowFile.getEntryDate()).thenReturn(System.currentTimeMillis()); Mockito.when(mockFlowFile.getSize()).thenReturn(1L); Mockito.when(mockFlowFile.getLineageIdentifiers()).thenReturn(new HashSet<String>()); Mockito.when(mockFlowFile.getLineageStartDate()).thenReturn(System.currentTimeMillis()); return Query.evaluateExpressions(queryString, mockFlowFile); }
@Test public void testCompileEmbedded() { final String expression = "${x:equals( ${y} )}"; final Query query = Query.compile(expression); final Tree tree = query.getTree(); System.out.println(printTree(tree)); final Map<String, String> attributes = new HashMap<>(); attributes.put("x", "x"); attributes.put("y", "x"); final String result = Query.evaluateExpressions(expression, attributes, null); assertEquals("true", result); Query.validateExpression(expression, false); }
@Test public void testEscapeQuotes() { final long timestamp = 1403620278642L; final Map<String, String> attributes = new HashMap<>(); attributes.put("date", String.valueOf(timestamp)); final String format = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; final String query = "startDateTime=\"${date:toNumber():toDate():format(\"" + format + "\")}\""; final String result = Query.evaluateExpressions(query, attributes, null); final String expectedTime = new SimpleDateFormat(format, Locale.US).format(timestamp); assertEquals("startDateTime=\"" + expectedTime + "\"", result); final List<Range> ranges = Query.extractExpressionRanges(query); assertEquals(1, ranges.size()); }
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)); Query query = Query.compile(expression); 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()); }
@Test public void testSystemProperty() { System.setProperty("hello", "good-bye"); assertEquals("good-bye", Query.evaluateExpressions("${hello}")); assertEquals("good-bye", Query.compile("${hello}").evaluate().getValue()); }