http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/StringFunctionsTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/StringFunctionsTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/StringFunctionsTest.java deleted file mode 100644 index 8899535..0000000 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/StringFunctionsTest.java +++ /dev/null @@ -1,431 +0,0 @@ -/** - * 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.dsl.functions; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import org.apache.commons.collections.map.HashedMap; -import org.apache.commons.collections.map.SingletonMap; -import org.apache.metron.common.dsl.ParseException; -import org.junit.Assert; -import org.junit.Test; - -import java.util.Calendar; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import static org.apache.metron.common.utils.StellarProcessorUtils.run; -import static org.apache.metron.common.utils.StellarProcessorUtils.runPredicate; - -public class StringFunctionsTest { - - @Test - public void testStringFunctions() throws Exception { - final Map<String, String> variableMap = new HashMap<String, String>() {{ - put("foo", "casey"); - put("ip", "192.168.0.1"); - put("empty", ""); - put("spaced", "metron is great"); - }}; - Assert.assertTrue(runPredicate("true and TO_UPPER(foo) == 'CASEY'", v -> variableMap.get(v))); - Assert.assertTrue(runPredicate("foo in [ TO_LOWER('CASEY'), 'david' ]", v -> variableMap.get(v))); - Assert.assertTrue(runPredicate("TO_UPPER(foo) in [ TO_UPPER('casey'), 'david' ] and IN_SUBNET(ip, '192.168.0.0/24')", v -> variableMap.get(v))); - Assert.assertFalse(runPredicate("TO_LOWER(foo) in [ TO_UPPER('casey'), 'david' ]", v -> variableMap.get(v))); - } - - @Test - public void testStringFunctions_advanced() throws Exception { - final Map<String, Object> variableMap = new HashMap<String, Object>() {{ - put("foo", "casey"); - put("bar", "bar.casey.grok"); - put("ip", "192.168.0.1"); - put("empty", ""); - put("spaced", "metron is great"); - put("myList", ImmutableList.of("casey", "apple", "orange")); - }}; - Assert.assertTrue(runPredicate("foo in SPLIT(bar, '.')", v -> variableMap.get(v))); - Assert.assertFalse(runPredicate("foo in SPLIT(ip, '.')", v -> variableMap.get(v))); - Assert.assertTrue(runPredicate("foo in myList", v -> variableMap.get(v))); - Assert.assertFalse(runPredicate("foo not in myList", v -> variableMap.get(v))); - } - - @Test - public void testLeftRightFills() throws Exception { - final Map<String, Object> variableMap = new HashMap<String, Object>() {{ - put("foo", null); - put("bar", null); - put("notInt", "oh my"); - }}; - - //LEFT - Object left = run("FILL_LEFT('123','X', 10)", new HashedMap()); - Assert.assertNotNull(left); - Assert.assertEquals(10, ((String) left).length()); - Assert.assertEquals("XXXXXXX123", (String) left); - - //RIGHT - Object right = run("FILL_RIGHT('123','X', 10)", new HashedMap()); - Assert.assertNotNull(right); - Assert.assertEquals(10, ((String) right).length()); - Assert.assertEquals("123XXXXXXX", (String) right); - - //INPUT ALREADY LENGTH - Object same = run("FILL_RIGHT('123','X', 3)", new HashedMap()); - Assert.assertEquals(3, ((String) same).length()); - Assert.assertEquals("123", (String) same); - - //INPUT BIGGER THAN LENGTH - Object tooBig = run("FILL_RIGHT('1234567890','X', 3)", new HashedMap()); - Assert.assertEquals(10, ((String) tooBig).length()); - Assert.assertEquals("1234567890", (String) tooBig); - - //NULL VARIABLES - boolean thrown = false; - try { - run("FILL_RIGHT('123',foo,bar)", variableMap); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("are both required")); - } - Assert.assertTrue(thrown); - thrown = false; - - // NULL LENGTH - try { - run("FILL_RIGHT('123','X',bar)", variableMap); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("are both required")); - } - Assert.assertTrue(thrown); - thrown = false; - - // NULL FILL - try { - run("FILL_RIGHT('123',foo, 7)", variableMap); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("are both required")); - } - Assert.assertTrue(thrown); - thrown = false; - - // NON INTEGER LENGTH - try { - run("FILL_RIGHT('123','X', 'z' )", new HashedMap()); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("not a valid Integer")); - } - Assert.assertTrue(thrown); - thrown = false; - - // EMPTY STRING PAD - try { - Object returnValue = run("FILL_RIGHT('123','', 10 )", new HashedMap()); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("cannot be an empty")); - } - Assert.assertTrue(thrown); - thrown = false; - - //MISSING LENGTH PARAMETER - try { - run("FILL_RIGHT('123',foo)", variableMap); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("expects three")); - } - Assert.assertTrue(thrown); - } - - @Test - public void shannonEntropyTest() throws Exception { - //test empty string - Assert.assertEquals(0.0, (Double) run("STRING_ENTROPY('')", new HashMap<>()), 0.0); - Assert.assertEquals(0.0, (Double) run("STRING_ENTROPY(foo)", ImmutableMap.of("foo", "")), 0.0); - - /* - Now consider the string aaaaaaaaaabbbbbccccc or 10 a's followed by 5 b's and 5 c's. - The probabilities of each character is as follows: - p(a) = 1/2 - p(b) = 1/4 - p(c) = 1/4 - so the shannon entropy should be - -p(a)*log_2(p(a)) - p(b)*log_2(p(b)) - p(c)*log_2(p(c)) = - -0.5*-1 - 0.25*-2 - 0.25*-2 = 1.5 - */ - Assert.assertEquals(1.5, (Double) run("STRING_ENTROPY(foo)", ImmutableMap.of("foo", "aaaaaaaaaabbbbbccccc")), 0.0); - } - - @Test - public void testFormat() throws Exception { - - Map<String, Object> vars = ImmutableMap.of( - "cal", new Calendar.Builder().setDate(2017, 02, 02).build(), - "x", 234, - "y", 3); - - Assert.assertEquals("no args", run("FORMAT('no args')", vars)); - Assert.assertEquals("234.0", run("FORMAT('%.1f', TO_DOUBLE(234))", vars)); - Assert.assertEquals("000234", run("FORMAT('%06d', 234)", vars)); - Assert.assertEquals("03 2,2017", run("FORMAT('%1$tm %1$te,%1$tY', cal)", vars)); - Assert.assertEquals("234 > 3", run("FORMAT('%d > %d', x, y)", vars)); - Assert.assertEquals("missing: null", run("FORMAT('missing: %d', missing)", vars)); - } - - /** - * FORMAT - Not passing a format string will throw an exception - */ - @Test(expected = ParseException.class) - public void testFormatWithNoArguments() throws Exception { - run("FORMAT()", Collections.emptyMap()); - } - - /** - * FORMAT - Forgetting to pass an argument required by the format string will throw an exception. - */ - @Test(expected = ParseException.class) - public void testFormatWithMissingArguments() throws Exception { - run("FORMAT('missing arg: %d')", Collections.emptyMap()); - } - - - /** - * CHOMP StringFunction - * - * @throws Exception - */ - @Test - public void testChomp() throws Exception { - Assert.assertEquals("abc", run("CHOMP('abc')", new HashedMap())); - Assert.assertEquals("abc", run("CHOMP(msg)", ImmutableMap.of("msg", "abc\r\n"))); - Assert.assertEquals("", run("CHOMP(msg)", ImmutableMap.of("msg", "\n"))); - Assert.assertEquals("", run("CHOMP('')", new HashedMap())); - Assert.assertEquals(null, run("CHOMP(msg)", new HashedMap())); - Assert.assertEquals(null, run("CHOMP(null)", new HashedMap())); - - // No input - boolean thrown = false; - try { - run("CHOMP()", Collections.emptyMap()); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("missing argument")); - } - Assert.assertTrue(thrown); - thrown = false; - - // Integer input - try { - run("CHOMP(123)", Collections.emptyMap()); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("cannot be cast")); - } - Assert.assertTrue(thrown); - - } - - /** - * CHOP StringFunction - * - * @throws Exception - */ - @Test - public void testChop() throws Exception { - Assert.assertEquals("ab", run("CHOP('abc')", new HashedMap())); - Assert.assertEquals(null, run("CHOP(null)", new HashedMap())); - Assert.assertEquals(null, run("CHOP(msg)", new HashedMap())); - Assert.assertEquals("abc", run("CHOP(msg)", ImmutableMap.of("msg", "abc\r\n"))); - Assert.assertEquals("", run("CHOP(msg)", ImmutableMap.of("msg", ""))); - Assert.assertEquals("", run("CHOP(msg)", ImmutableMap.of("msg", "\n"))); - Assert.assertEquals("", run("CHOP('')", new HashedMap())); - - // No input - boolean thrown = false; - try { - run("CHOP()", Collections.emptyMap()); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("missing argument")); - } - Assert.assertTrue(thrown); - thrown = false; - - // Integer input - try { - run("CHOP(123)", Collections.emptyMap()); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("cannot be cast")); - } - Assert.assertTrue(thrown); - - } - - /** - * PREPEND_IF_MISSING StringFunction - */ - @Test - public void testPrependIfMissing() throws Exception { - Assert.assertEquals("xyzabc", run("PREPEND_IF_MISSING('abc', 'xyz')", new HashedMap())); - Assert.assertEquals("xyzXYZabc", run("PREPEND_IF_MISSING('XYZabc', 'xyz', 'mno')", new HashedMap())); - Assert.assertEquals("mnoXYZabc", run("PREPEND_IF_MISSING('mnoXYZabc', 'xyz', 'mno')", new HashedMap())); - Assert.assertEquals(null, run("PREPEND_IF_MISSING(null, null, null)", new HashedMap())); - Assert.assertEquals("xyz", run("PREPEND_IF_MISSING('', 'xyz', null)", new HashedMap())); - - // No input - boolean thrown = false; - try { - run("PREPEND_IF_MISSING()", Collections.emptyMap()); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("incorrect arguments")); - } - Assert.assertTrue(thrown); - thrown = false; - - // Incorrect number of arguments - 1 - try { - run("PREPEND_IF_MISSING('abc')", Collections.emptyMap()); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("incorrect arguments")); - } - Assert.assertTrue(thrown); - thrown = false; - - // Incorrect number of arguments - 2 - try { - run("PREPEND_IF_MISSING('abc', 'def', 'ghi', 'jkl')", Collections.emptyMap()); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("incorrect arguments")); - } - Assert.assertTrue(thrown); - thrown = false; - - // Integer input - try { - run("PREPEND_IF_MISSING(123, 'abc')", Collections.emptyMap()); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("cannot be cast")); - } - Assert.assertTrue(thrown); - - } - - /** - * APPEND_IF_MISSING StringFunction - */ - @Test - public void testAppendIfMissing() throws Exception { - Assert.assertEquals("apachemetron", run("APPEND_IF_MISSING('apache', 'metron')", new HashedMap())); - Assert.assertEquals("abcXYZxyz", run("APPEND_IF_MISSING('abcXYZ', 'xyz', 'mno')", new HashedMap())); - Assert.assertEquals(null, run("APPEND_IF_MISSING(null, null, null)", new HashedMap())); - Assert.assertEquals("xyz", run("APPEND_IF_MISSING('', 'xyz', null)", new HashedMap())); - - // No input - boolean thrown = false; - try { - run("APPEND_IF_MISSING()", Collections.emptyMap()); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("incorrect arguments")); - } - Assert.assertTrue(thrown); - thrown = false; - - // Incorrect number of arguments - 1 - try { - run("APPEND_IF_MISSING('abc')", Collections.emptyMap()); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("incorrect arguments")); - } - Assert.assertTrue(thrown); - thrown = false; - - // Incorrect number of arguments - 2 - try { - run("APPEND_IF_MISSING('abc', 'def', 'ghi', 'jkl')", Collections.emptyMap()); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("incorrect arguments")); - } - Assert.assertTrue(thrown); - thrown = false; - - // Integer input - try { - run("APPEND_IF_MISSING(123, 'abc')", Collections.emptyMap()); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("cannot be cast")); - } - Assert.assertTrue(thrown); - - } - - /** - * COUNT_MATCHES StringFunction - */ - @Test - public void testCountMatches() throws Exception { - Assert.assertEquals(0, (int) run("COUNT_MATCHES(null, '*')", new HashedMap())); - Assert.assertEquals(2, (int) run("COUNT_MATCHES('apachemetron', 'e')", new HashedMap())); - Assert.assertEquals(2, (int) run("COUNT_MATCHES('anand', 'an')", new HashedMap())); - Assert.assertEquals(0, (int) run("COUNT_MATCHES('abcd', null)", new HashedMap())); - - // No input - boolean thrown = false; - try { - run("COUNT_MATCHES()", Collections.emptyMap()); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("incorrect arguments")); - } - Assert.assertTrue(thrown); - thrown = false; - - // Incorrect number of arguments - 1 - try { - run("COUNT_MATCHES('abc')", Collections.emptyMap()); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("incorrect arguments")); - } - Assert.assertTrue(thrown); - thrown = false; - - // Integer input - try { - run("COUNT_MATCHES(123, 456)", Collections.emptyMap()); - } catch (ParseException pe) { - thrown = true; - Assert.assertTrue(pe.getMessage().contains("cannot be cast")); - } - Assert.assertTrue(thrown); - - } -}
http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/SystemFunctionsTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/SystemFunctionsTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/SystemFunctionsTest.java deleted file mode 100644 index 67ec325..0000000 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/SystemFunctionsTest.java +++ /dev/null @@ -1,78 +0,0 @@ -/** - * 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.dsl.functions; - -import com.google.common.collect.ImmutableList; -import org.apache.metron.common.system.Environment; -import org.junit.Test; - -import java.util.ArrayList; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class SystemFunctionsTest { - - @Test - public void smoke_test_non_mocked_env() { - SystemFunctions.EnvGet envGet = new SystemFunctions.EnvGet(); - String envVal = (String) envGet.apply(ImmutableList.of("ENV_GET_VAR")); - assertThat("Value should not exist", envVal, equalTo(null)); - } - - @Test - public void env_get_returns_value() { - Environment env = mock(Environment.class); - when(env.get("ENV_GET_VAR")).thenReturn("ENV_GET_VALUE"); - SystemFunctions.EnvGet envGet = new SystemFunctions.EnvGet(env); - String envVal = (String) envGet.apply(ImmutableList.of("ENV_GET_VAR")); - assertThat("Value should match", envVal, equalTo("ENV_GET_VALUE")); - } - - @Test - public void env_get_returns_null_if_key_is_not_string() { - SystemFunctions.EnvGet envGet = new SystemFunctions.EnvGet(); - String envVal = (String) envGet.apply(ImmutableList.of(new ArrayList())); - assertThat("Value should be null", envVal, equalTo(null)); - } - - @Test - public void property_get_returns_value() { - System.getProperties().put("ENV_GET_VAR", "ENV_GET_VALUE"); - SystemFunctions.PropertyGet propertyGet = new SystemFunctions.PropertyGet(); - String propertyVal = (String) propertyGet.apply(ImmutableList.of("ENV_GET_VAR")); - assertThat("Value should match", propertyVal, equalTo("ENV_GET_VALUE")); - } - - @Test - public void property_get_nonexistent_returns_null() { - SystemFunctions.PropertyGet propertyGet = new SystemFunctions.PropertyGet(); - String propertyVal = (String) propertyGet.apply(ImmutableList.of("PROPERTY_MISSING")); - assertThat("Value should not exist", propertyVal, equalTo(null)); - } - - @Test - public void property_get_returns_null_if_key_is_not_string() { - SystemFunctions.PropertyGet propertyGet = new SystemFunctions.PropertyGet(); - String propertyVal = (String) propertyGet.apply(ImmutableList.of(new ArrayList())); - assertThat("Value should be null", propertyVal, equalTo(null)); - } - -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/resolver/ClasspathFunctionResolverIntegrationTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/resolver/ClasspathFunctionResolverIntegrationTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/resolver/ClasspathFunctionResolverIntegrationTest.java deleted file mode 100644 index ac9ea7e..0000000 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/resolver/ClasspathFunctionResolverIntegrationTest.java +++ /dev/null @@ -1,87 +0,0 @@ -/** - * 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.dsl.functions.resolver; - -import com.google.common.collect.Lists; -import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.fs.FileStatus; -import org.apache.hadoop.fs.FileSystem; -import org.apache.hadoop.fs.Path; -import org.apache.hadoop.hdfs.MiniDFSCluster; -import org.apache.hadoop.yarn.conf.YarnConfiguration; -import org.apache.metron.common.dsl.Context; -import org.apache.metron.common.dsl.functions.resolver.ClasspathFunctionResolver; -import org.apache.metron.integration.components.MRComponent; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.io.File; -import java.io.IOException; -import java.util.HashSet; -import java.util.Properties; - -import static org.apache.metron.common.dsl.functions.resolver.ClasspathFunctionResolver.Config.STELLAR_VFS_PATHS; - -public class ClasspathFunctionResolverIntegrationTest { - static MRComponent component; - static Configuration configuration; - @BeforeClass - public static void setup() { - component = new MRComponent().withBasePath("target"); - component.start(); - configuration = component.getConfiguration(); - - try { - FileSystem fs = FileSystem.newInstance(configuration); - fs.mkdirs(new Path("/classpath-resources")); - fs.copyFromLocalFile(new Path("src/test/classpath-resources/custom-1.0-SNAPSHOT.jar"), new Path("/classpath-resources")); - } catch (IOException e) { - throw new RuntimeException("Unable to start cluster", e); - } - } - - @AfterClass - public static void teardown() { - component.stop(); - } - - - public static ClasspathFunctionResolver create(Properties config) { - ClasspathFunctionResolver resolver = new ClasspathFunctionResolver(); - - Context context = new Context.Builder() - .with(Context.Capabilities.STELLAR_CONFIG, () -> config) - .build(); - resolver.initialize(context); - - return resolver; - } - - @Test - public void test() throws Exception { - Properties config = new Properties(); - config.put(STELLAR_VFS_PATHS.param(), configuration.get("fs.defaultFS") + "/classpath-resources/.*.jar"); - ClasspathFunctionResolver resolver = create(config); - HashSet<String> functions = new HashSet<>(Lists.newArrayList(resolver.getFunctions())); - Assert.assertTrue(functions.contains("NOW")); - } - -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/resolver/ClasspathFunctionResolverTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/resolver/ClasspathFunctionResolverTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/resolver/ClasspathFunctionResolverTest.java deleted file mode 100644 index d460edf..0000000 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/resolver/ClasspathFunctionResolverTest.java +++ /dev/null @@ -1,135 +0,0 @@ -/** - * 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.dsl.functions.resolver; - -import com.google.common.collect.Lists; -import org.apache.commons.vfs2.*; -import org.apache.metron.common.dsl.Context; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -import java.io.File; -import java.util.*; - -import static org.apache.metron.common.dsl.functions.resolver.ClasspathFunctionResolver.Config.STELLAR_SEARCH_EXCLUDES_KEY; -import static org.apache.metron.common.dsl.functions.resolver.ClasspathFunctionResolver.Config.STELLAR_SEARCH_INCLUDES_KEY; -import static org.apache.metron.common.dsl.functions.resolver.ClasspathFunctionResolver.Config.STELLAR_VFS_PATHS; - -public class ClasspathFunctionResolverTest { - - private static List<String> expectedFunctions; - - @BeforeClass - public static void setup() { - - // search the entire classpath for functions - provides a baseline to test against - Properties config = new Properties(); - // use a permissive regex that should not filter anything - ClasspathFunctionResolver resolver = create(config); - - expectedFunctions = Lists.newArrayList(resolver.getFunctions()); - } - - /** - * Create a function resolver to test. - * @param config The configuration for Stellar. - */ - public static ClasspathFunctionResolver create(Properties config) { - ClasspathFunctionResolver resolver = new ClasspathFunctionResolver(); - - Context context = new Context.Builder() - .with(Context.Capabilities.STELLAR_CONFIG, () -> config) - .build(); - resolver.initialize(context); - - return resolver; - } - - @Test - public void testInclude() { - - // setup - include all `org.apache.metron.*` functions - Properties config = new Properties(); - config.put(STELLAR_SEARCH_INCLUDES_KEY.param(), "org.apache.metron.*"); - - // execute - ClasspathFunctionResolver resolver = create(config); - List<String> actual = Lists.newArrayList(resolver.getFunctions()); - - // validate - should have found all of the functions - Assert.assertEquals(expectedFunctions, actual); - } - - @Test - public void testWithMultipleIncludes() { - - // setup - include all of the common and management functions, which is most of them - Properties config = new Properties(); - config.put(STELLAR_SEARCH_INCLUDES_KEY.param(), "org.apache.metron.common.*, org.apache.metron.management.*"); - - // execute - ClasspathFunctionResolver resolver = create(config); - List<String> actual = Lists.newArrayList(resolver.getFunctions()); - - // validate - should have found all of the functions - Assert.assertTrue(actual.size() > 0); - Assert.assertTrue(actual.size() <= expectedFunctions.size()); - } - - @Test - public void testExclude() { - - // setup - exclude all `org.apache.metron.*` functions - Properties config = new Properties(); - config.put(STELLAR_SEARCH_EXCLUDES_KEY.param(), "org.apache.metron.*"); - - // use a permissive regex that should not filter anything - ClasspathFunctionResolver resolver = create(config); - List<String> actual = Lists.newArrayList(resolver.getFunctions()); - - // both should have resolved the same functions - Assert.assertEquals(0, actual.size()); - } - - @Test - public void testExternalLocal() throws FileSystemException, ClassNotFoundException { - File jar = new File("src/test/classpath-resources"); - Assert.assertTrue(jar.exists()); - Properties config = new Properties(); - config.put(STELLAR_VFS_PATHS.param(), jar.toURI() + "/.*.jar"); - - ClasspathFunctionResolver resolver = create(config); - HashSet<String> functions = new HashSet<>(Lists.newArrayList(resolver.getFunctions())); - Assert.assertTrue(functions.contains("NOW")); - } - - - @Test - public void testExternalHDFS() throws FileSystemException, ClassNotFoundException { - File jar = new File("src/test/classpath-resources"); - Assert.assertTrue(jar.exists()); - Properties config = new Properties(); - config.put(STELLAR_VFS_PATHS.param(), jar.toURI() + "/.*.jar"); - - ClasspathFunctionResolver resolver = create(config); - HashSet<String> functions = new HashSet<>(Lists.newArrayList(resolver.getFunctions())); - Assert.assertTrue(functions.contains("NOW")); - } -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/resolver/SimpleFunctionResolverTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/resolver/SimpleFunctionResolverTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/resolver/SimpleFunctionResolverTest.java deleted file mode 100644 index 5d846cf..0000000 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/resolver/SimpleFunctionResolverTest.java +++ /dev/null @@ -1,122 +0,0 @@ -/** - * 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.dsl.functions.resolver; - -import com.google.common.collect.Lists; -import org.apache.metron.common.dsl.BaseStellarFunction; -import org.apache.metron.common.dsl.Context; -import org.apache.metron.common.dsl.ParseException; -import org.apache.metron.common.dsl.Stellar; -import org.apache.metron.common.dsl.StellarFunction; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import java.util.List; - -/** - * Tests the SimpleFunctionResolver class. - */ -public class SimpleFunctionResolverTest { - - private SimpleFunctionResolver resolver; - - @Before - public void setup() { - resolver = new SimpleFunctionResolver(); - } - - @Test - public void testFunctionResolution() { - resolver.withClass(IAmAFunction.class); - List<String> functions = Lists.newArrayList(resolver.getFunctions()); - Assert.assertEquals(1, functions.size()); - Assert.assertTrue(functions.contains("namespace_function")); - } - - /** - * The function resolver should be able to instantiate an instance of the function's implementation. - */ - @Test - public void testApply() { - resolver.withClass(IAmAFunction.class); - final String functionName = "namespace_function"; - StellarFunction fn = resolver.apply(functionName); - Assert.assertTrue(fn instanceof IAmAFunction); - } - - /** - * All Stellar functions must be annotated. - */ - @Test - public void testFunctionResolutionWithMissingAnnotation() { - resolver.withClass(MissingAnnotation.class); - List<String> functions = Lists.newArrayList(resolver.getFunctions()); - Assert.assertEquals(0, functions.size()); - } - - /** - * If the resolver comes across the same function definition twice, nothing bad should happen. - */ - @Test - public void testIgnoreDuplicates() { - resolver.withClass(IAmAFunction.class); - resolver.withClass(IAmAFunction.class); - List<String> functions = Lists.newArrayList(resolver.getFunctions()); - Assert.assertEquals(1, functions.size()); - } - - /** - * I am the real deal. I am a Stellar function. - */ - @Stellar(namespace="namespace", name="function", description="description", returns="returns", params={"param1"}) - private static class IAmAFunction extends BaseStellarFunction { - - public IAmAFunction() { - } - - @Override - public Object apply(List<Object> args) { - return null; - } - } - - /** - * This is not a Stellar function. It implements StellarFunction, but is not annotated. - */ - private static class MissingAnnotation implements StellarFunction { - - public MissingAnnotation() { - } - - @Override - public Object apply(List<Object> args, Context context) throws ParseException { - return null; - } - - @Override - public void initialize(Context context) { - } - - @Override - public boolean isInitialized() { - return false; - } - } -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/transformation/FieldTransformationTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/transformation/FieldTransformationTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/transformation/FieldTransformationTest.java index b4df514..57de964 100644 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/transformation/FieldTransformationTest.java +++ b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/transformation/FieldTransformationTest.java @@ -26,7 +26,7 @@ import org.adrianwalker.multilinestring.Multiline; import org.apache.hadoop.hbase.util.Bytes; import org.apache.metron.common.configuration.FieldTransformer; import org.apache.metron.common.configuration.SensorParserConfig; -import org.apache.metron.common.dsl.Context; +import org.apache.metron.stellar.dsl.Context; import org.json.simple.JSONObject; import org.junit.Assert; import org.junit.Test; http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/transformation/RemoveTransformationTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/transformation/RemoveTransformationTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/transformation/RemoveTransformationTest.java index fcd006d..72f7480 100644 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/transformation/RemoveTransformationTest.java +++ b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/transformation/RemoveTransformationTest.java @@ -23,7 +23,7 @@ import org.adrianwalker.multilinestring.Multiline; import org.apache.hadoop.hbase.util.Bytes; import org.apache.metron.common.configuration.FieldTransformer; import org.apache.metron.common.configuration.SensorParserConfig; -import org.apache.metron.common.dsl.Context; +import org.apache.metron.stellar.dsl.Context; import org.json.simple.JSONObject; import org.junit.Assert; import org.junit.Test; http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/transformation/StellarTransformationTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/transformation/StellarTransformationTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/transformation/StellarTransformationTest.java index ac9b74c..01008ca 100644 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/transformation/StellarTransformationTest.java +++ b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/transformation/StellarTransformationTest.java @@ -23,7 +23,7 @@ import org.adrianwalker.multilinestring.Multiline; import org.apache.hadoop.hbase.util.Bytes; import org.apache.metron.common.configuration.FieldTransformer; import org.apache.metron.common.configuration.SensorParserConfig; -import org.apache.metron.common.dsl.Context; +import org.apache.metron.stellar.dsl.Context; import org.json.simple.JSONObject; import org.junit.Assert; import org.junit.Test; http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/BaseValidationTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/BaseValidationTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/BaseValidationTest.java index 22cb47a..cb96d12 100644 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/BaseValidationTest.java +++ b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/BaseValidationTest.java @@ -21,7 +21,7 @@ package org.apache.metron.common.field.validation; import org.apache.hadoop.hbase.util.Bytes; import org.apache.metron.common.configuration.Configurations; import org.apache.metron.common.configuration.FieldValidator; -import org.apache.metron.common.dsl.Context; +import org.apache.metron.stellar.dsl.Context; import org.json.simple.JSONObject; import java.io.IOException; http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/DomainValidationTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/DomainValidationTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/DomainValidationTest.java index 779c6fc..ee57efa 100644 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/DomainValidationTest.java +++ b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/DomainValidationTest.java @@ -26,7 +26,7 @@ import org.junit.Test; import java.io.IOException; -import static org.apache.metron.common.utils.StellarProcessorUtils.runPredicate; +import static org.apache.metron.stellar.common.utils.StellarProcessorUtils.runPredicate; public class DomainValidationTest extends BaseValidationTest{ /** http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/EmailValidationTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/EmailValidationTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/EmailValidationTest.java index ed8896e..e45c467 100644 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/EmailValidationTest.java +++ b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/EmailValidationTest.java @@ -26,7 +26,7 @@ import org.junit.Test; import java.io.IOException; -import static org.apache.metron.common.utils.StellarProcessorUtils.runPredicate; +import static org.apache.metron.stellar.common.utils.StellarProcessorUtils.runPredicate; public class EmailValidationTest extends BaseValidationTest { /** http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/IPValidationTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/IPValidationTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/IPValidationTest.java index 5c7483f..de2a1c4 100644 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/IPValidationTest.java +++ b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/IPValidationTest.java @@ -26,7 +26,7 @@ import org.junit.Test; import java.io.IOException; -import static org.apache.metron.common.utils.StellarProcessorUtils.runPredicate; +import static org.apache.metron.stellar.common.utils.StellarProcessorUtils.runPredicate; public class IPValidationTest extends BaseValidationTest { /** http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/URLValidationTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/URLValidationTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/URLValidationTest.java index 59e9f47..1099df7 100644 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/URLValidationTest.java +++ b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/network/URLValidationTest.java @@ -26,7 +26,7 @@ import org.junit.Test; import java.io.IOException; -import static org.apache.metron.common.utils.StellarProcessorUtils.runPredicate; +import static org.apache.metron.stellar.common.utils.StellarProcessorUtils.runPredicate; public class URLValidationTest extends BaseValidationTest { /** http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/primitive/DateValidationTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/primitive/DateValidationTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/primitive/DateValidationTest.java index 3aaf899..c39ea7b 100644 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/primitive/DateValidationTest.java +++ b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/primitive/DateValidationTest.java @@ -26,7 +26,7 @@ import org.junit.Test; import java.io.IOException; -import static org.apache.metron.common.utils.StellarProcessorUtils.runPredicate; +import static org.apache.metron.stellar.common.utils.StellarProcessorUtils.runPredicate; public class DateValidationTest extends BaseValidationTest{ /** http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/primitive/IntegerValidationTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/primitive/IntegerValidationTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/primitive/IntegerValidationTest.java index 887918d..bfd309d 100644 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/primitive/IntegerValidationTest.java +++ b/metron-platform/metron-common/src/test/java/org/apache/metron/common/field/validation/primitive/IntegerValidationTest.java @@ -26,7 +26,7 @@ import org.junit.Test; import java.io.IOException; -import static org.apache.metron.common.utils.StellarProcessorUtils.runPredicate; +import static org.apache.metron.stellar.common.utils.StellarProcessorUtils.runPredicate; public class IntegerValidationTest extends BaseValidationTest{ /** http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/BaseStellarProcessorTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/BaseStellarProcessorTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/BaseStellarProcessorTest.java deleted file mode 100644 index 00fd3f3..0000000 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/BaseStellarProcessorTest.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * 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 org.apache.metron.common.dsl.Context; -import org.apache.metron.common.dsl.ParseException; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import static org.junit.Assert.*; - -@SuppressWarnings("ALL") -public class BaseStellarProcessorTest { - @Rule - public final ExpectedException exception = ExpectedException.none(); - - BaseStellarProcessor<Object> processor; - - @Before - public void setUp() throws Exception { - processor = new BaseStellarProcessor<>(Object.class); - } - - @Test - public void makeSureBasicValidationIsWorking() throws Exception { - { - assertTrue(processor.validate("")); - assertTrue(processor.validate(null)); - assertTrue(processor.validate("'ah'")); - assertTrue(processor.validate("true")); - assertTrue(processor.validate("1")); - assertTrue(processor.validate("1L")); - assertTrue(processor.validate("1F")); - assertTrue(processor.validate("1 < 2 // always true")); - assertTrue(processor.validate("1 < foo")); - assertTrue(processor.validate("(1 < foo)")); - assertTrue(processor.validate("foo < bar")); - assertTrue(processor.validate("foo < TO_FLOAT(bar)", false, Context.EMPTY_CONTEXT())); - assertTrue(processor.validate("if true then b else c")); - assertTrue(processor.validate("IF false THEN b ELSE c")); - } - - { - assertFalse(processor.validate("'", false, Context.EMPTY_CONTEXT())); - assertFalse(processor.validate("(", false, Context.EMPTY_CONTEXT())); - assertFalse(processor.validate("()", false, Context.EMPTY_CONTEXT())); - assertFalse(processor.validate("'foo", false, Context.EMPTY_CONTEXT())); - assertFalse(processor.validate("foo << foo", false, Context.EMPTY_CONTEXT())); - assertFalse(processor.validate("1-1", false, Context.EMPTY_CONTEXT())); - assertFalse(processor.validate("1 -1", false, Context.EMPTY_CONTEXT())); - assertFalse(processor.validate("If a then b else c", false, Context.EMPTY_CONTEXT())); - } - } - - @Test - public void validateShouldProperlyThrowExceptionOnInvalidStellarExpression() throws Exception { - exception.expect(ParseException.class); - exception.expectMessage("Unable to parse ': "); - - processor.validate("'", true, Context.EMPTY_CONTEXT()); - } - - @Test - public void validateShouldProperlyThrowExceptionByDefaultOnInvalidStellarExpression() throws Exception { - exception.expect(ParseException.class); - exception.expectMessage("Unable to parse ': "); - - processor.validate("'", Context.EMPTY_CONTEXT()); - } - - @Test - public void validateShouldProperlyThrowExceptionByDefaultOnInvalidStellarExpression2() throws Exception { - exception.expect(ParseException.class); - exception.expectMessage("Unable to parse ': "); - - processor.validate("'"); - } - - @Test - public void validateMethodShouldFailOnUnknownFunctions() throws Exception { - exception.expect(ParseException.class); - exception.expectMessage(" Unable to resolve function named 'UNKNOWN_FUNCTION'."); - - assertTrue(processor.validate("1 < UNKNOWN_FUNCTION(3)", Context.EMPTY_CONTEXT())); - } - - @Test - public void validateMethodShouldNotFailOnUnknownvariables() throws Exception { - assertTrue(processor.validate("unknown_variable\n\n")); - assertTrue(processor.validate("unknown_variable > 2", Context.EMPTY_CONTEXT())); - } - - @Test - public void makeSureBasicLexerErrorsAreCaughtDuringValidation() throws Exception { - assertFalse(processor.validate("true â ", false, Context.EMPTY_CONTEXT())); - assertFalse(processor.validate("¢ (1 + 2)", false, Context.EMPTY_CONTEXT())); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/BloomFilterTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/BloomFilterTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/BloomFilterTest.java deleted file mode 100644 index df283c9..0000000 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/BloomFilterTest.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * - * 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.ImmutableMap; -import org.apache.metron.common.utils.BloomFilter; -import org.junit.Assert; -import org.junit.Test; - -import java.util.HashMap; -import java.util.Map; - -import static org.apache.metron.common.utils.StellarProcessorUtils.run; - -public class BloomFilterTest { - private Map<String, Object> variables = new HashMap<String, Object>() {{ - put("string", "casey"); - put("double", 1.0); - put("integer", 1); - put("map", ImmutableMap.of("key1", "value1", "key2", "value2")); - }}; - - @Test - public void testMerge() { - - BloomFilter bloomString = (BloomFilter)run("BLOOM_ADD(BLOOM_INIT(), string)", variables); - BloomFilter bloomDouble = (BloomFilter)run("BLOOM_ADD(BLOOM_INIT(), double)", variables); - BloomFilter bloomInteger= (BloomFilter)run("BLOOM_ADD(BLOOM_INIT(), integer)", variables); - BloomFilter bloomMap= (BloomFilter)run("BLOOM_ADD(BLOOM_INIT(), map)", variables); - BloomFilter merged = (BloomFilter)run("BLOOM_MERGE([stringFilter, doubleFilter, integerFilter, mapFilter])" - , ImmutableMap.of("stringFilter", bloomString - ,"doubleFilter", bloomDouble - ,"integerFilter", bloomInteger - ,"mapFilter", bloomMap - ) - ); - Assert.assertNotNull(merged); - for(Object val : variables.values()) { - Assert.assertTrue(merged.mightContain(val)); - } - } - - @Test - public void testAdd() { - BloomFilter result = (BloomFilter)run("BLOOM_ADD(BLOOM_INIT(), string, double, integer, map)", variables); - for(Object val : variables.values()) { - Assert.assertTrue(result.mightContain(val)); - } - Assert.assertTrue(result.mightContain(ImmutableMap.of("key1", "value1", "key2", "value2"))); - } - - @Test - public void testExists() { - { - Boolean result = (Boolean) run("BLOOM_EXISTS(BLOOM_ADD(BLOOM_INIT(), string, double, integer, map), 'casey')", variables); - Assert.assertTrue(result); - } - { - Boolean result = (Boolean) run("BLOOM_EXISTS(BLOOM_ADD(BLOOM_INIT(), string, double, integer, map), double)", variables); - Assert.assertTrue(result); - } - { - Boolean result = (Boolean) run("BLOOM_EXISTS(BLOOM_ADD(BLOOM_INIT(), string, double, integer, map), integer)", variables); - Assert.assertTrue(result); - } - { - Boolean result = (Boolean) run("BLOOM_EXISTS(BLOOM_ADD(BLOOM_INIT(), string, double, integer, map), map)", variables); - Assert.assertTrue(result); - } - { - Boolean result = (Boolean) run("BLOOM_EXISTS(BLOOM_ADD(BLOOM_INIT(), string, double, integer, map), 'samantha')", variables); - Assert.assertFalse(result); - } - { - Boolean result = (Boolean) run("BLOOM_EXISTS(BLOOM_ADD(BLOOM_INIT(), string, double, integer, map), sam)", variables); - Assert.assertFalse(result); - } - } -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/DateFunctionsTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/DateFunctionsTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/DateFunctionsTest.java deleted file mode 100644 index 997da12..0000000 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/DateFunctionsTest.java +++ /dev/null @@ -1,231 +0,0 @@ -/* - * - * 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 org.apache.metron.common.dsl.Context; -import org.apache.metron.common.dsl.StellarFunctions; -import org.junit.Before; -import org.junit.Test; - -import java.util.Calendar; -import java.util.HashMap; -import java.util.Map; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** - * Tests the DateFunctions class. - */ -public class DateFunctionsTest { - - private Map<String, Object> variables = new HashMap<>(); - private Calendar calendar; - - /** - * Runs a Stellar expression. - * @param expr The expression to run. - */ - private Object run(String expr) { - StellarProcessor processor = new StellarProcessor(); - assertTrue(processor.validate(expr)); - return processor.parse(expr, x -> variables.get(x), StellarFunctions.FUNCTION_RESOLVER(), Context.EMPTY_CONTEXT()); - } - - /** - * Thu Aug 25 2016 09:27:10 EST - */ - private long AUG2016 = 1472131630748L; - - @Before - public void setup() { - variables.put("epoch", AUG2016); - calendar = Calendar.getInstance(); - } - - @Test - public void testDayOfWeek() { - Object result = run("DAY_OF_WEEK(epoch)"); - assertEquals(Calendar.THURSDAY, result); - } - - /** - * If no argument, then return the current day of week. - */ - @Test - public void testDayOfWeekNow() { - Object result = run("DAY_OF_WEEK()"); - assertEquals(calendar.get(Calendar.DAY_OF_WEEK), result); - } - - /** - * If refer to variable that does not exist, expect null returned. - */ - @Test - public void testDayOfWeekNull() { - Object result = run("DAY_OF_WEEK(nada)"); - assertEquals(null, result); - } - - @Test - public void testWeekOfMonth() { - Object result = run("WEEK_OF_MONTH(epoch)"); - assertEquals(4, result); - } - - /** - * If no argument, then return the current week of month. - */ - @Test - public void testWeekOfMonthNow() { - Object result = run("WEEK_OF_MONTH()"); - assertEquals(calendar.get(Calendar.WEEK_OF_MONTH), result); - } - - /** - * If refer to variable that does not exist, expect null returned. - */ - @Test - public void testWeekOfMonthNull() { - Object result = run("WEEK_OF_MONTH(nada)"); - assertEquals(null, result); - } - - @Test - public void testMonth() { - Object result = run("MONTH(epoch)"); - assertEquals(Calendar.AUGUST, result); - } - - /** - * If no argument, then return the current month. - */ - @Test - public void testMonthNow() { - Object result = run("MONTH()"); - assertEquals(calendar.get(Calendar.MONTH), result); - } - - /** - * If refer to variable that does not exist, expect null returned. - */ - @Test - public void testMonthNull() { - Object result = run("MONTH(nada)"); - assertEquals(null, result); - } - - @Test - public void testYear() { - Object result = run("YEAR(epoch)"); - assertEquals(2016, result); - } - - /** - * If no argument, then return the current year. - */ - @Test - public void testYearNow() { - Object result = run("YEAR()"); - assertEquals(calendar.get(Calendar.YEAR), result); - } - - /** - * If refer to variable that does not exist, expect null returned. - */ - @Test - public void testYearNull() { - Object result = run("YEAR(nada)"); - assertEquals(null, result); - } - - @Test - public void testDayOfMonth() { - Object result = run("DAY_OF_MONTH(epoch)"); - assertEquals(25, result); - } - - /** - * If no argument, then return the current day of month. - */ - @Test - public void testDayOfMonthNow() { - Object result = run("DAY_OF_MONTH()"); - assertEquals(calendar.get(Calendar.DAY_OF_MONTH), result); - } - - /** - * If refer to variable that does not exist, expect null returned. - */ - @Test - public void testDayOfMonthNull() { - Object result = run("DAY_OF_MONTH(nada)"); - assertEquals(null, result); - } - - @Test - public void testWeekOfYear() { - Object result = run("WEEK_OF_YEAR(epoch)"); - assertEquals(35, result); - } - - /** - * If no argument, then return the current week of year. - */ - @Test - public void testWeekOfYearNow() { - Object result = run("WEEK_OF_YEAR()"); - assertEquals(calendar.get(Calendar.WEEK_OF_YEAR), result); - } - - /** - * If refer to variable that does not exist, expect null returned. - */ - @Test - public void testWeekOfYearNull() { - Object result = run("WEEK_OF_YEAR(nada)"); - assertEquals(null, result); - } - - @Test - public void testDayOfYear() { - Object result = run("DAY_OF_YEAR(epoch)"); - assertEquals(238, result); - } - - /** - * If no argument, then return the current day of year. - */ - @Test - public void testDayOfYearNow() { - Object result = run("DAY_OF_YEAR()"); - assertEquals(calendar.get(Calendar.DAY_OF_YEAR), result); - } - - /** - * If refer to variable that does not exist, expect null returned. - */ - @Test - public void testDayOfYearNull() { - Object result = run("DAY_OF_YEAR(nada)"); - assertEquals(null, result); - } -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/StellarArithmeticTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/StellarArithmeticTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/StellarArithmeticTest.java deleted file mode 100644 index e33a22b..0000000 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/StellarArithmeticTest.java +++ /dev/null @@ -1,332 +0,0 @@ -/* - * 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.ImmutableMap; -import org.apache.commons.lang3.tuple.Pair; -import org.apache.metron.common.dsl.ParseException; -import org.apache.metron.common.dsl.Token; -import org.junit.Assert; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import java.util.HashMap; -import java.util.Map; - -import static org.apache.metron.common.utils.StellarProcessorUtils.run; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -@SuppressWarnings("unchecked") -public class StellarArithmeticTest { - @Rule - public final ExpectedException exception = ExpectedException.none(); - - @Test - public void addingLongsShouldYieldLong() throws Exception { - final long timestamp = 1452013350000L; - String query = "TO_EPOCH_TIMESTAMP('2016-01-05 17:02:30', 'yyyy-MM-dd HH:mm:ss', 'UTC') + 2"; - assertEquals(timestamp + 2, run(query, new HashMap<>())); - } - - @Test - public void addingIntegersShouldYieldAnInteger() throws Exception { - String query = "1 + 2"; - assertEquals(3, run(query, new HashMap<>())); - } - - @Test - public void addingDoublesShouldYieldADouble() throws Exception { - String query = "1.0 + 2.0"; - assertEquals(3.0, run(query, new HashMap<>())); - } - - @Test - public void addingDoubleAndIntegerWhereSubjectIsDoubleShouldYieldADouble() throws Exception { - String query = "2.1 + 1"; - assertEquals(3.1, run(query, new HashMap<>())); - } - - @Test - public void addingDoubleAndIntegerWhereSubjectIsIntegerShouldYieldADouble() throws Exception { - String query = "1 + 2.1"; - assertEquals(3.1, run(query, new HashMap<>())); - } - - @Test - public void testArithmetic() { - assertEquals(3, run("1 + 2", new HashMap<>())); - assertEquals(3.2, run("1.2 + 2", new HashMap<>())); - assertEquals(1.2e-3 + 2, run("1.2e-3 + 2", new HashMap<>())); - assertEquals(1.2f + 3.7, run("1.2f + 3.7", new HashMap<>())); - assertEquals(12L * (1.2f + 7), run("12L*(1.2f + 7)", new HashMap<>())); - assertEquals(12.2f * (1.2f + 7L), run("TO_FLOAT(12.2) * (1.2f + 7L)", new HashMap<>())); - } - - @Test - public void testNumericOperations() { - { - String query = "TO_INTEGER(1 + 2*2 + 3 - 4 - 0.5)"; - assertEquals(3, (Integer) run(query, new HashMap<>()), 1e-6); - } - { - String query = "1 + 2*2 + 3 - 4 - 0.5"; - assertEquals(3.5, (Double) run(query, new HashMap<>()), 1e-6); - } - { - String query = "2*one*(1 + 2*2 + 3 - 4)"; - assertEquals(8, run(query, ImmutableMap.of("one", 1))); - } - { - String query = "2*(1 + 2 + 3 - 4)"; - assertEquals(4, (Integer) run(query, ImmutableMap.of("one", 1, "very_nearly_one", 1.000001)), 1e-6); - } - { - String query = "1 + 2 + 3 - 4 - 2"; - assertEquals(0, (Integer) run(query, ImmutableMap.of("one", 1, "very_nearly_one", 1.000001)), 1e-6); - } - { - String query = "1 + 2 + 3 + 4"; - assertEquals(10, (Integer) run(query, ImmutableMap.of("one", 1, "very_nearly_one", 1.000001)), 1e-6); - } - { - String query = "(one + 2)*3"; - assertEquals(9, (Integer) run(query, ImmutableMap.of("one", 1, "very_nearly_one", 1.000001)), 1e-6); - } - { - String query = "TO_INTEGER((one + 2)*3.5)"; - assertEquals(10, (Integer) run(query, ImmutableMap.of("one", 1, "very_nearly_one", 1.000001)), 1e-6); - } - { - String query = "1 + 2*3"; - assertEquals(7, (Integer) run(query, ImmutableMap.of("one", 1, "very_nearly_one", 1.000001)), 1e-6); - } - { - String query = "TO_LONG(foo)"; - Assert.assertNull(run(query, ImmutableMap.of("foo", "not a number"))); - } - { - String query = "TO_LONG(foo)"; - assertEquals(232321L, run(query, ImmutableMap.of("foo", "00232321"))); - } - { - String query = "TO_LONG(foo)"; - assertEquals(Long.MAX_VALUE, run(query, ImmutableMap.of("foo", Long.toString(Long.MAX_VALUE)))); - } - } - - @Test - public void verifyExpectedReturnTypes() throws Exception { - Token<Integer> integer = mock(Token.class); - when(integer.getValue()).thenReturn(1); - - Token<Long> lng = mock(Token.class); - when(lng.getValue()).thenReturn(1L); - - Token<Double> dbl = mock(Token.class); - when(dbl.getValue()).thenReturn(1.0D); - - Token<Float> flt = mock(Token.class); - when(flt.getValue()).thenReturn(1.0F); - - Map<Pair<String, String>, Class<? extends Number>> expectedReturnTypeMappings = - new HashMap<Pair<String, String>, Class<? extends Number>>() {{ - put(Pair.of("TO_FLOAT(3.0)", "TO_LONG(1)"), Float.class); - put(Pair.of("TO_FLOAT(3)", "3.0"), Double.class); - put(Pair.of("TO_FLOAT(3)", "TO_FLOAT(3)"), Float.class); - put(Pair.of("TO_FLOAT(3)", "3"), Float.class); - - put(Pair.of("TO_LONG(1)", "TO_LONG(1)"), Long.class); - put(Pair.of("TO_LONG(1)", "3.0"), Double.class); - put(Pair.of("TO_LONG(1)", "TO_FLOAT(3)"), Float.class); - put(Pair.of("TO_LONG(1)", "3"), Long.class); - - put(Pair.of("3.0", "TO_LONG(1)"), Double.class); - put(Pair.of("3.0", "3.0"), Double.class); - put(Pair.of("3.0", "TO_FLOAT(3)"), Double.class); - put(Pair.of("3.0", "3"), Double.class); - - put(Pair.of("3", "TO_LONG(1)"), Long.class); - put(Pair.of("3", "3.0"), Double.class); - put(Pair.of("3", "TO_FLOAT(3)"), Float.class); - put(Pair.of("3", "3"), Integer.class); - }}; - - expectedReturnTypeMappings.forEach((pair, expectedClass) -> { - assertTrue(run(pair.getLeft() + " * " + pair.getRight(), ImmutableMap.of()).getClass() == expectedClass); - assertTrue(run(pair.getLeft() + " + " + pair.getRight(), ImmutableMap.of()).getClass() == expectedClass); - assertTrue(run(pair.getLeft() + " - " + pair.getRight(), ImmutableMap.of()).getClass() == expectedClass); - assertTrue(run(pair.getLeft() + " / " + pair.getRight(), ImmutableMap.of()).getClass() == expectedClass); - }); - } - - @Test - public void happyPathFloatArithmetic() throws Exception { - Object run = run(".0f * 1", ImmutableMap.of()); - assertEquals(.0f * 1, run); - assertEquals(Float.class, run.getClass()); - - Object run1 = run("0.f / 1F", ImmutableMap.of()); - assertEquals(0.f / 1F, run1); - assertEquals(Float.class, run1.getClass()); - - Object run2 = run(".0F + 1.0f", ImmutableMap.of()); - assertEquals(.0F + 1.0f, run2); - assertEquals(Float.class, run2.getClass()); - - Object run3 = run("0.0f - 0.1f", ImmutableMap.of()); - assertEquals(0.0f - 0.1f, run3); - assertEquals(Float.class, run2.getClass()); - } - - @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())); - } - - @SuppressWarnings("NumericOverflow") - @Test - public void checkInterestingCases() throws Exception { - assertEquals((((((1L) + .5d)))) * 6.f, run("(((((1L) + .5d)))) * 6.f", ImmutableMap.of())); - assertEquals((((((1L) + .5d)))) * 6.f / 0.f, run("(((((1L) + .5d)))) * 6.f / 0.f", ImmutableMap.of())); - assertEquals(Double.class, run("(((((1L) + .5d)))) * 6.f / 0.f", ImmutableMap.of()).getClass()); - } - - @Test - public void makeSureStellarProperlyEvaluatesLiteralsToExpectedTypes() throws Exception { - { - assertEquals(Float.class, run("6.f", ImmutableMap.of()).getClass()); - assertEquals(Float.class, run(".0f", ImmutableMap.of()).getClass()); - assertEquals(Float.class, run("6.0F", ImmutableMap.of()).getClass()); - assertEquals(Float.class, run("6f", ImmutableMap.of()).getClass()); - assertEquals(Float.class, run("6e-6f", ImmutableMap.of()).getClass()); - assertEquals(Float.class, run("6e+6f", ImmutableMap.of()).getClass()); - assertEquals(Float.class, run("6e6f", ImmutableMap.of()).getClass()); - assertEquals(Float.class, run("TO_FLOAT(1231)", ImmutableMap.of()).getClass()); - assertEquals(Float.class, run("TO_FLOAT(12.31)", ImmutableMap.of()).getClass()); - assertEquals(Float.class, run("TO_FLOAT(12.31f)", ImmutableMap.of()).getClass()); - assertEquals(Float.class, run("TO_FLOAT(12L)", ImmutableMap.of()).getClass()); - } - { - assertEquals(Double.class, run("6.d", ImmutableMap.of()).getClass()); - assertEquals(Double.class, run("6.D", ImmutableMap.of()).getClass()); - assertEquals(Double.class, run("6.0d", ImmutableMap.of()).getClass()); - assertEquals(Double.class, run("6D", ImmutableMap.of()).getClass()); - assertEquals(Double.class, run("6e5D", ImmutableMap.of()).getClass()); - assertEquals(Double.class, run("6e-5D", ImmutableMap.of()).getClass()); - assertEquals(Double.class, run("6e+5D", ImmutableMap.of()).getClass()); - assertEquals(Double.class, run("TO_DOUBLE(1231)", ImmutableMap.of()).getClass()); - assertEquals(Double.class, run("TO_DOUBLE(12.31)", ImmutableMap.of()).getClass()); - assertEquals(Double.class, run("TO_DOUBLE(12.31f)", ImmutableMap.of()).getClass()); - assertEquals(Double.class, run("TO_DOUBLE(12L)", ImmutableMap.of()).getClass()); - } - { - assertEquals(Integer.class, run("6", ImmutableMap.of()).getClass()); - assertEquals(Integer.class, run("60000000", ImmutableMap.of()).getClass()); - assertEquals(Integer.class, run("-0", ImmutableMap.of()).getClass()); - assertEquals(Integer.class, run("-60000000", ImmutableMap.of()).getClass()); - assertEquals(Integer.class, run("TO_INTEGER(1231)", ImmutableMap.of()).getClass()); - assertEquals(Integer.class, run("TO_INTEGER(12.31)", ImmutableMap.of()).getClass()); - assertEquals(Integer.class, run("TO_INTEGER(12.31f)", ImmutableMap.of()).getClass()); - assertEquals(Integer.class, run("TO_INTEGER(12L)", ImmutableMap.of()).getClass()); - } - { - assertEquals(Long.class, run("12345678910l", ImmutableMap.of()).getClass()); - assertEquals(Long.class, run("0l", ImmutableMap.of()).getClass()); - assertEquals(Long.class, run("-0l", ImmutableMap.of()).getClass()); - assertEquals(Long.class, run("-60000000L", ImmutableMap.of()).getClass()); - assertEquals(Long.class, run("-60000000L", ImmutableMap.of()).getClass()); - assertEquals(Long.class, run("TO_LONG(1231)", ImmutableMap.of()).getClass()); - assertEquals(Long.class, run("TO_LONG(12.31)", ImmutableMap.of()).getClass()); - assertEquals(Long.class, run("TO_LONG(12.31f)", ImmutableMap.of()).getClass()); - assertEquals(Long.class, run("TO_LONG(12L)", ImmutableMap.of()).getClass()); - } - } - - @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 parseExceptionMultipleLeadingZerosOnDouble() throws Exception { - exception.expect(ParseException.class); - run("000000d", ImmutableMap.of()); - } - - @Test - public void parseExceptionMultipleLeadingZerosOnFloat() throws Exception { - exception.expect(ParseException.class); - run("000000f", ImmutableMap.of()); - } - - @Test - public void parseExceptionMultipleLeadingNegativeSignsFloat() throws Exception { - exception.expect(ParseException.class); - run("--000000f", ImmutableMap.of()); - } - - @Test - public void parseExceptionMultipleLeadingNegativeSignsDouble() throws Exception { - exception.expect(ParseException.class); - run("--000000D", ImmutableMap.of()); - } - - @Test - public void parseExceptionMultipleLeadingNegativeSignsLong() throws Exception { - exception.expect(ParseException.class); - run("--000000L", ImmutableMap.of()); - } - - @Test(expected = ParseException.class) - public void unableToDivideByZeroWithIntegers() throws Exception { - run("0/0", ImmutableMap.of()); - } - - @Test(expected = ParseException.class) - public void unableToDivideByZeroWithLongs() throws Exception { - run("0L/0L", ImmutableMap.of()); - } - - @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())); - } -} http://git-wip-us.apache.org/repos/asf/metron/blob/a5b13777/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/StellarAssignmentTest.java ---------------------------------------------------------------------- diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/StellarAssignmentTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/StellarAssignmentTest.java deleted file mode 100644 index c4a6060..0000000 --- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/stellar/StellarAssignmentTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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 org.junit.Assert; -import org.junit.Test; - -public class StellarAssignmentTest { - - @Test - public void testAssignment() { - for(String statement : ImmutableList.of( "foo := bar + grok" - , "foo := bar + grok" - , "foo := bar + grok " - ) - ) - { - StellarAssignment assignment = StellarAssignment.from(statement); - Assert.assertEquals("foo", assignment.getKey()); - Assert.assertEquals("foo", assignment.getVariable()); - Assert.assertEquals("bar + grok", assignment.getStatement()); - Assert.assertEquals("bar + grok", assignment.getValue()); - } - } - - @Test - public void testNonAssignment() { - for(String statement : ImmutableList.of( "bar + grok" - , " bar + grok" - , "bar + grok " - ) - ) - { - StellarAssignment assignment = StellarAssignment.from(statement); - Assert.assertNull( assignment.getKey()); - Assert.assertNull( assignment.getVariable()); - Assert.assertEquals("bar + grok", assignment.getStatement()); - Assert.assertEquals("bar + grok", assignment.getValue()); - } - } - - @Test(expected=UnsupportedOperationException.class) - public void testImmutability() { - StellarAssignment assignment = StellarAssignment.from("foo := bar"); - assignment.setValue("myval"); - } -}
