@Test(expected=ParseException.class) @SuppressWarnings("unchecked") public void multisetInitTest_wrongType() throws Exception { Map<Object, Integer> s = (Map<Object, Integer>) StellarProcessorUtils.run("MULTISET_INIT({ 'foo' : 'bar'})", new HashMap<>()); }
/** * FORMAT - Not passing a format string will throw an exception */ @Test(expected = ParseException.class) public void testFormatWithNoArguments() throws Exception { run("FORMAT()", Collections.emptyMap()); }
@Test public void ableToDivideByZero() throws Exception { assertEquals(0F/0F, run("0F/0F", ImmutableMap.of())); assertEquals(0D/0D, run("0D/0D", ImmutableMap.of())); assertEquals(0D/0F, run("0D/0F", ImmutableMap.of())); assertEquals(0F/0D, run("0F/0D", ImmutableMap.of())); assertEquals(0F/0, run("0F/0", ImmutableMap.of())); assertEquals(0D/0, run("0D/0", ImmutableMap.of())); assertEquals(0/0D, run("0/0D", ImmutableMap.of())); assertEquals(0/0F, run("0/0F", ImmutableMap.of())); } }
@SuppressWarnings("ConstantConditions") @Test public void checkComparisonOperationsWithFunctions() throws Exception { assertEquals(1f >= 2, run("TO_FLOAT(1) >= 2", ImmutableMap.of())); assertEquals(1f <= 2, run("TO_FLOAT(1) <= TO_FLOAT(2)", ImmutableMap.of())); assertEquals(1f == 2, run("TO_FLOAT(1) == TO_LONG(2)", ImmutableMap.of())); assertEquals(12.31f == 10.2f, run("TO_FLOAT(12.31) < 10.2f", ImmutableMap.of())); }
@SuppressWarnings("PointlessArithmeticExpression") @Test public void happyPathLongArithmetic() throws Exception { assertEquals(0L * 1L, run("0L * 1L", ImmutableMap.of())); assertEquals(0l / 1L, run("0l / 1L", ImmutableMap.of())); assertEquals(1L - 1l, run("1L - 1l", ImmutableMap.of())); assertEquals(2147483648L + 1L, run("2147483648L + 1L", ImmutableMap.of())); }
@Test public void tlsh_insufficientComplexity() throws Exception { final Map<String, Object> variables = new HashMap<>(); String data = "Metron is the best"; variables.put("toHash", data); assertNull(run("HASH(toHash, 'tlsh')", variables)); }
@Test public void testLengthCollection(){ String query = "LENGTH(foo)"; Collection c = Arrays.asList(1,2,3,4,5); Assert.assertEquals(5, run(query,ImmutableMap.of("foo",c))); }
@Test public void testURLToPort() { String query = "URL_TO_PORT(foo)"; Assert.assertEquals(80, run(query, ImmutableMap.of("foo", "http://www.google.co.uk/my/path"))); }
@Test public void testGetEndpointWithWrongVersion() throws Exception { String stellar = "MAAS_GET_ENDPOINT('dga', '2.0')"; Object result = run(stellar, new HashMap<>(), context); Assert.assertNull(result); }
@Test public void parseExceptionMultipleLeadingZerosOnInteger() throws Exception { exception.expect(ParseException.class); run("000000", ImmutableMap.of()); }
@Test public void parseExceptionMultipleLeadingZerosOnLong() throws Exception { exception.expect(ParseException.class); run("000000l", ImmutableMap.of()); }
@Test public void parseExceptionMultipleLeadingNegativeSignsFloat() throws Exception { exception.expect(ParseException.class); run("--000000f", ImmutableMap.of()); }
@Test public void nonStringValueThatIsSerializableHashesSuccessfully() throws Exception { final String algorithm = "'md5'"; final String valueToHash = "'My value to hash'"; final Serializable input = (Serializable) Collections.singletonList(valueToHash); final MessageDigest expected = MessageDigest.getInstance(algorithm.replace("'", "")); expected.update(SerializationUtils.serialize(input)); final Map<String, Object> variables = new HashMap<>(); variables.put("toHash", input); assertEquals(expectedHexString(expected), run("HASH(toHash, " + algorithm + ")", variables)); }
@Test public void callingHashFunctionsWithVariablesAsInputHashesSuccessfully() throws Exception { final String algorithm = "md5"; final String valueToHash = "'My value to hash'"; final Serializable input = (Serializable) Collections.singletonList(valueToHash); final MessageDigest expected = MessageDigest.getInstance(algorithm); expected.update(SerializationUtils.serialize(input)); final Map<String, Object> variables = new HashMap<>(); variables.put("toHash", input); variables.put("hashType", algorithm); assertEquals(expectedHexString(expected), run("HASH(toHash, hashType)", variables)); }
@Test public void testGetEndpointWithoutVersion() throws Exception { String stellar = "MAAS_GET_ENDPOINT('dga')"; Object result = run(stellar, new HashMap<>(), context); Assert.assertTrue(result instanceof Map); Map<String, String> resMap = (Map<String, String>)result; Assert.assertEquals(resMap.get("url"), "http://localhost:8282"); Assert.assertEquals(resMap.get("name"), "dga"); Assert.assertEquals(resMap.get("version"), "1.0"); Assert.assertEquals(resMap.get("endpoint:apply"), "apply"); }
@Test public void testGetEndpointWithVersion() throws Exception { String stellar = "MAAS_GET_ENDPOINT('dga', '1.0')"; Object result = run(stellar, new HashMap<>(), context); Assert.assertTrue(result instanceof Map); Map<String, String> resMap = (Map<String, String>)result; Assert.assertEquals(resMap.get("url"), "http://localhost:8282"); Assert.assertEquals(resMap.get("name"), "dga"); Assert.assertEquals(resMap.get("version"), "1.0"); Assert.assertEquals(resMap.get("endpoint:apply"), "apply"); }
@Test @SuppressWarnings("unchecked") public void testShortCircuitWithThrows() { Assert.assertEquals("ok", run("match{ foo > 100 => THROW('oops'), foo > 200 => THROW('oh no'), default => 'ok' }", new HashMap() {{ put("foo", 50); }})); }
@Test @SuppressWarnings("unchecked") public void testTernaryFuncWithoutIfCheck() { Assert.assertEquals("a", run("match{ foo == 5 ? true : false => 'a', default => 'ok' }", new HashMap() {{ put("foo", 5); }})); }
@Test @SuppressWarnings("unchecked") public void testVariableIFCheck() { Assert.assertEquals("a", run("match{ IF foo == 5 THEN true ELSE false => 'a', default => 'ok' }", new HashMap() {{ put("foo", 5); }})); }
@Test @SuppressWarnings("unchecked") public void testVariableOnly() { Assert.assertEquals("a", run("match{ foo => 'a', default => null}", new HashMap() {{ put("foo", true); }})); }