### Question:
CountExpression extends AbstractAggregationFunction { @Override public ExpressionResult apply(ExpressionContext context, ExpressionResult input) { ExpressionContext scoped = context.scoped(this); AtomicLong counter = scoped.computeIfAbsent(alias, AtomicLong::new); ExpressionResult expressionResult = expression == null ? input : expression.apply(context, input); if( expressionResult == null) { return new NumericExpressionResult(counter.get()); } return new NumericExpressionResult(counter.addAndGet(expressionResult.count())); } CountExpression(String alias, Expression expression); @Override ExpressionResult apply(ExpressionContext context, ExpressionResult input);  }### Answer:
@Test public void testCountValues() { ExpressionResult actual1 = testSubject.apply(expressionContext, mapValue("value", stringValue("string"))); ExpressionResult actual2 = testSubject.apply(expressionContext, mapValue("value", numericValue(1L))); ExpressionResult actual3 = testSubject.apply(expressionContext, mapValue("value", stringValue("string"))); assertEquals(1, actual1.getNumericValue().longValue()); assertEquals(2, actual2.getNumericValue().longValue()); assertEquals(3, actual3.getNumericValue().longValue()); }
@Test public void testNullValuesNotCounted() { ExpressionResult actual1 = testSubject.apply(expressionContext, mapValue("value", stringValue("string"))); ExpressionResult actual2 = testSubject.apply(expressionContext, mapValue("value", nullValue())); ExpressionResult actual3 = testSubject.apply(expressionContext, mapValue("value", numericValue(1L))); assertEquals(1, actual1.getNumericValue().longValue()); assertEquals(1, actual2.getNumericValue().longValue()); assertEquals(2, actual3.getNumericValue().longValue()); }
@Test public void testCountIncreasedByCollectionSize() { ExpressionResult actual1 = testSubject.apply(expressionContext, mapValue("value", stringValue("string"))); ExpressionResult actual2 = testSubject.apply(expressionContext, mapValue("value", listValue(listValue(), nullValue(), stringValue("string")))); ExpressionResult actual3 = testSubject.apply(expressionContext, mapValue("value", numericValue(1L))); assertEquals(1, actual1.getNumericValue().longValue()); assertEquals(3, actual2.getNumericValue().longValue()); assertEquals(4, actual3.getNumericValue().longValue()); }