blob: f7ee2050c9d6b22df933256532adb0ab16ae4e40 [file] [log] [blame]
package org.eclipse.sensinact.gateway.nthbnd.test.jsonpath.internal.function;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPathException;
import org.eclipse.sensinact.gateway.nthbnd.test.jsonpath.BaseTestConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
/**
* Defines functional tests around executing:
* <p>
* - sum
* - avg
* - stddev
* <p>
* for each of the above, executes the test and verifies that the results are as expected based on a static input
* and static output.
* <p>
* Created by mattg on 6/26/15.
*/
@RunWith(Parameterized.class)
public class NumericPathFunctionTest extends BaseFunctionTest {
@Parameterized.Parameters
public static Iterable<Configuration> configurations() {
return BaseTestConfiguration.configurations();
}
private static final Logger logger = LoggerFactory.getLogger(NumericPathFunctionTest.class);
private Configuration conf = BaseTestConfiguration.JSON_ORG_CONFIGURATION;
public NumericPathFunctionTest(Configuration conf) {
logger.debug("Testing with configuration {}", conf.getClass().getName());
this.conf = conf;
}
@Test
public void testAverageOfDoubles() {
verifyMathFunction(conf, "$.numbers.avg()", 5.5);
}
@Test
public void testAverageOfEmptyListNegative() {
try {
verifyMathFunction(conf, "$.empty.avg()", null);
} catch (JsonPathException e) {
assertEquals(e.getMessage(), "Aggregation function attempted to calculate value using empty array");
}
}
@Test
public void testSumOfDouble() {
verifyMathFunction(conf, "$.numbers.sum()", (10d * (10d + 1d)) / 2d);
}
@Test
public void testSumOfEmptyListNegative() {
try {
verifyMathFunction(conf, "$.empty.sum()", null);
} catch (JsonPathException e) {
assertEquals(e.getMessage(), "Aggregation function attempted to calculate value using empty array");
}
}
@Test
public void testMaxOfDouble() {
verifyMathFunction(conf, "$.numbers.max()", 10d);
}
@Test
public void testMaxOfEmptyListNegative() {
try {
verifyMathFunction(conf, "$.empty.max()", null);
} catch (JsonPathException e) {
assertEquals(e.getMessage(), "Aggregation function attempted to calculate value using empty array");
}
}
@Test
public void testMinOfDouble() {
verifyMathFunction(conf, "$.numbers.min()", 1d);
}
@Test
public void testMinOfEmptyListNegative() {
try {
verifyMathFunction(conf, "$.empty.min()", null);
} catch (JsonPathException e) {
assertEquals(e.getMessage(), "Aggregation function attempted to calculate value using empty array");
}
}
@Test
public void testStdDevOfDouble() {
verifyMathFunction(conf, "$.numbers.stddev()", 2.8722813232690143d);
}
@Test
public void testStddevOfEmptyListNegative() {
try {
verifyMathFunction(conf, "$.empty.stddev()", null);
} catch (JsonPathException e) {
assertEquals(e.getMessage(), "Aggregation function attempted to calculate value using empty array");
}
}
/**
* Expect that for an invalid function name we'll get back the original input to the function
*/
// @Test
// @Ignore
// public void testInvalidFunctionNameNegative() {
// JSONArray numberSeries = new JSONArray();
// numberSeries.addAll(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
// assertThat(using(conf).parse(NUMBER_SERIES).read("$.numbers.foo()")).isEqualTo(numberSeries);
// }
}