Search in sources :

Example 1 with HashedMap

use of org.apache.commons.collections4.map.HashedMap in project metron by apache.

the class StringFunctionsTest method testLeftRightFills.

@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);
}
Also used : HashMap(java.util.HashMap) ParseException(org.apache.metron.stellar.dsl.ParseException) HashedMap(org.apache.commons.collections4.map.HashedMap) Test(org.junit.Test)

Example 2 with HashedMap

use of org.apache.commons.collections4.map.HashedMap in project metron by apache.

the class StringFunctionsTest method testChomp.

/**
 * 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(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;
    // Variable missing
    try {
        run("CHOMP(msg)", new HashedMap());
    } catch (ParseException pe) {
        thrown = true;
    }
    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);
}
Also used : ParseException(org.apache.metron.stellar.dsl.ParseException) HashedMap(org.apache.commons.collections4.map.HashedMap) Test(org.junit.Test)

Example 3 with HashedMap

use of org.apache.commons.collections4.map.HashedMap in project metron by apache.

the class StringFunctionsTest method testCountMatches.

/**
 * 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);
}
Also used : ParseException(org.apache.metron.stellar.dsl.ParseException) HashedMap(org.apache.commons.collections4.map.HashedMap) Test(org.junit.Test)

Example 4 with HashedMap

use of org.apache.commons.collections4.map.HashedMap in project metron by apache.

the class StringFunctionsTest method testChop.

/**
 * 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("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;
    // Variable missing
    try {
        run("CHOMP(msg)", new HashedMap());
    } catch (ParseException pe) {
        thrown = true;
    }
    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);
}
Also used : ParseException(org.apache.metron.stellar.dsl.ParseException) HashedMap(org.apache.commons.collections4.map.HashedMap) Test(org.junit.Test)

Aggregations

HashedMap (org.apache.commons.collections4.map.HashedMap)4 ParseException (org.apache.metron.stellar.dsl.ParseException)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)1