Github user nickwallen commented on a diff in the pull request:
https://github.com/apache/incubator-metron/pull/207#discussion_r74420961
--- Diff:
metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/StellarTest.java
---
@@ -0,0 +1,422 @@
+/**
+ * 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.metron.common.stellar;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import org.apache.metron.common.dsl.MapVariableResolver;
+import org.apache.metron.common.dsl.ParseException;
+import org.apache.metron.common.dsl.VariableResolver;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class StellarTest {
+ @Test
+ public void testIfThenElse() {
+ {
+ String query = "if 1 < 2 then 'one' else 'two'";
+ Assert.assertEquals("one", run(query, ImmutableMap.of("foo", "casey
")));
+ }
+ {
+ String query = "if 1 + 1 < 2 then 'one' else 'two'";
+ Assert.assertEquals("two", run(query, ImmutableMap.of("foo", "casey
")));
+ }
+ {
+ String query = "1 < 2 ? 'one' : 'two'";
+ Assert.assertEquals("one", run(query, ImmutableMap.of("foo", "casey
")));
+ }
+ {
+ String query = "if not(1 < 2) then 'one' else 'two'";
+ Assert.assertEquals("two", run(query, ImmutableMap.of("foo", "casey
")));
+ }
+ {
+ String query = "if 1 == 1.000001 then 'one' else 'two'";
+ Assert.assertEquals("one", run(query, ImmutableMap.of("foo", "casey
")));
+ }
+ {
+ String query = "if one < two then 'one' else 'two'";
+ Assert.assertEquals("one", run(query, ImmutableMap.of("one", 1,
"two", 2)));
+ }
+ {
+ String query = "if one == very_nearly_one then 'one' else 'two'";
+ Assert.assertEquals("one", run(query, ImmutableMap.of("one", 1,
"very_nearly_one", 1.000001)));
+ }
+ }
+
+ @Test
+ public void testNumericOperations() {
+ {
+ String query = "TO_INTEGER(1 + 2*2 + 3 - 4 - 0.5)";
+ Assert.assertEquals(3, (Integer)run(query, ImmutableMap.of("one", 1,
"very_nearly_one", 1.000001)), 1e-6);
+ }
+ {
+ String query = "1 + 2*2 + 3 - 4 - 0.5";
+ Assert.assertEquals(3.5, (Double)run(query, ImmutableMap.of("one",
1, "very_nearly_one", 1.000001)), 1e-6);
+ }
+ {
+ String query = "2*one*(1 + 2*2 + 3 - 4)";
+ Assert.assertEquals(8, (Double)run(query, ImmutableMap.of("one", 1,
"very_nearly_one", 1.000001)), 1e-6);
+ }
+ {
+ String query = "2*(1 + 2 + 3 - 4)";
+ Assert.assertEquals(4, (Double)run(query, ImmutableMap.of("one", 1,
"very_nearly_one", 1.000001)), 1e-6);
+ }
+ {
+ String query = "1 + 2 + 3 - 4 - 2";
+ Assert.assertEquals(0, (Double)run(query, ImmutableMap.of("one", 1,
"very_nearly_one", 1.000001)), 1e-6);
+ }
+ {
+ String query = "1 + 2 + 3 + 4";
+ Assert.assertEquals(10, (Double)run(query, ImmutableMap.of("one", 1,
"very_nearly_one", 1.000001)), 1e-6);
+ }
+ {
+ String query = "(one + 2)*3";
+ Assert.assertEquals(9, (Double)run(query, ImmutableMap.of("one", 1,
"very_nearly_one", 1.000001)), 1e-6);
+ }
+ {
+ String query = "TO_INTEGER((one + 2)*3.5)";
+ Assert.assertEquals(10, (Integer)run(query, ImmutableMap.of("one",
1, "very_nearly_one", 1.000001)), 1e-6);
+ }
+ {
+ String query = "1 + 2*3";
+ Assert.assertEquals(7, (Double)run(query, ImmutableMap.of("one", 1,
"very_nearly_one", 1.000001)), 1e-6);
+ }
+
+ }
+
+
+ @Test
+ public void testHappyPath() {
+ String query = "TO_UPPER(TRIM(foo))";
+ Assert.assertEquals("CASEY", run(query, ImmutableMap.of("foo", "casey
")));
+ }
+
+ @Test
+ public void testJoin() {
+ String query = "JOIN( [ TO_UPPER(TRIM(foo)), 'bar' ], ',')";
+ Assert.assertEquals("CASEY,bar", run(query, ImmutableMap.of("foo",
"casey ")));
+ }
+
+ @Test
+ public void testSplit() {
+ String query = "JOIN( SPLIT(foo, ':'), ',')";
+ Assert.assertEquals("casey,bar", run(query, ImmutableMap.of("foo",
"casey:bar")));
+ }
+
+ @Test
+ public void testMapGet() {
+ String query = "MAP_GET(dc, dc2tz, 'UTC')";
+ Assert.assertEquals("UTC"
+ , run(query, ImmutableMap.of("dc", "nyc"
+ ,"dc2tz",
ImmutableMap.of("la", "PST")
+ )
+ )
+ );
+ Assert.assertEquals("EST"
+ , run(query, ImmutableMap.of("dc", "nyc"
+ ,"dc2tz",
ImmutableMap.of("nyc", "EST")
+ )
+ )
+ );
+ }
+ @Test
+ public void testTLDExtraction() {
+ String query = "DOMAIN_TO_TLD(foo)";
+ Assert.assertEquals("co.uk", run(query, ImmutableMap.of("foo",
"www.google.co.uk")));
+ }
+
+ @Test
+ public void testTLDRemoval() {
+ String query = "DOMAIN_REMOVE_TLD(foo)";
+ Assert.assertEquals("www.google", run(query, ImmutableMap.of("foo",
"www.google.co.uk")));
+ }
+
+ @Test
+ public void testSubdomainRemoval() {
+ String query = "DOMAIN_REMOVE_SUBDOMAINS(foo)";
+ Assert.assertEquals("google.co.uk", run(query, ImmutableMap.of("foo",
"www.google.co.uk")));
+ Assert.assertEquals("google.com", run(query, ImmutableMap.of("foo",
"www.google.com")));
+ }
+ @Test
+ public void testURLToHost() {
+ String query = "URL_TO_HOST(foo)";
+ Assert.assertEquals("www.google.co.uk", run(query,
ImmutableMap.of("foo", "http://www.google.co.uk/my/path")));
+ }
+
+ @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 testURLToProtocol() {
+ String query = "URL_TO_PROTOCOL(foo)";
+ Assert.assertEquals("http", run(query, ImmutableMap.of("foo",
"http://www.google.co.uk/my/path")));
+ }
+
+ @Test
+ public void testURLToPath() {
+ String query = "URL_TO_PATH(foo)";
+ Assert.assertEquals("/my/path", run(query, ImmutableMap.of("foo",
"http://www.google.co.uk/my/path")));
+ }
+
+ @Test
+ public void testProtocolToName() {
+ String query = "PROTOCOL_TO_NAME(protocol)";
+ Assert.assertEquals("TCP", run(query, ImmutableMap.of("protocol",
"6")));
+ Assert.assertEquals("TCP", run(query, ImmutableMap.of("protocol", 6)));
+ Assert.assertEquals(null, run(query, ImmutableMap.of("foo", 6)));
+ Assert.assertEquals("chicken", run(query, ImmutableMap.of("protocol",
"chicken")));
+ }
+ @Test
+ public void testDateConversion() {
+ long expected =1452013350000L;
+ {
+ String query = "TO_EPOCH_TIMESTAMP(foo, 'yyyy-MM-dd HH:mm:ss',
'UTC')";
+ Assert.assertEquals(expected, run(query, ImmutableMap.of("foo",
"2016-01-05 17:02:30")));
+ }
+ {
+ String query = "TO_EPOCH_TIMESTAMP(foo, 'yyyy-MM-dd HH:mm:ss')";
+ Long ts = (Long) run(query, ImmutableMap.of("foo", "2016-01-05
17:02:30"));
+ //is it within 24 hours of the UTC?
+ Assert.assertTrue(Math.abs(ts - expected) < 8.64e+7);
+ }
+ }
+
+ @Test
+ public void testToString() {
+ Assert.assertEquals("5", run("TO_STRING(foo)", ImmutableMap.of("foo",
5)));
+ }
+
+ @Test
+ public void testToInteger() {
+ Assert.assertEquals(5, run("TO_INTEGER(foo)", ImmutableMap.of("foo",
"5")));
+ Assert.assertEquals(5, run("TO_INTEGER(foo)", ImmutableMap.of("foo",
5)));
+ }
+
+ @Test
+ public void testToDouble() {
+ Assert.assertEquals(new Double(5.1), run("TO_DOUBLE(foo)",
ImmutableMap.of("foo", 5.1d)));
+ Assert.assertEquals(new Double(5.1), run("TO_DOUBLE(foo)",
ImmutableMap.of("foo", "5.1")));
+ }
+ @Test
+ public void testGet() {
+ Map<String, Object> variables = ImmutableMap.of("foo",
"www.google.co.uk");
+ Assert.assertEquals("www",
run("GET_FIRST(SPLIT(DOMAIN_REMOVE_TLD(foo), '.'))", variables));
+ Assert.assertEquals("www", run("GET(SPLIT(DOMAIN_REMOVE_TLD(foo),
'.'), 0)", variables));
+ Assert.assertEquals("google",
run("GET_LAST(SPLIT(DOMAIN_REMOVE_TLD(foo), '.'))", variables));
+ Assert.assertEquals("google", run("GET(SPLIT(DOMAIN_REMOVE_TLD(foo),
'.'), 1)", variables));
+ }
+ private static Object run(String rule, Map<String, Object> variables) {
+ StellarProcessor processor = new StellarProcessor();
+ Assert.assertTrue(rule + " not valid.", processor.validate(rule));
+ return processor.parse(rule, x -> variables.get(x));
+ }@Test
--- End diff --
Easy enough to have Intellij fix the spacing? Sometimes we have spaces
between methods, sometimes not, here there is no line break at all. All
simple, but add up to making it more difficult to read than it might need to be.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---