You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@metron.apache.org by ni...@apache.org on 2016/09/12 15:46:43 UTC
incubator-metron git commit: METRON-399 Stellar Date Functions Should
Default to Current Time (nickwallen) closes apache/incubator-metron#237
Repository: incubator-metron
Updated Branches:
refs/heads/master 3dfd7be6f -> 4212927b0
METRON-399 Stellar Date Functions Should Default to Current Time (nickwallen) closes apache/incubator-metron#237
Project: http://git-wip-us.apache.org/repos/asf/incubator-metron/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-metron/commit/4212927b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-metron/tree/4212927b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-metron/diff/4212927b
Branch: refs/heads/master
Commit: 4212927b0c6a2f86e51b0289e68fd2ed2438c636
Parents: 3dfd7be
Author: nickwallen <ni...@nickallen.org>
Authored: Mon Sep 12 11:45:48 2016 -0400
Committer: Nick Allen <ni...@nickallen.org>
Committed: Mon Sep 12 11:45:48 2016 -0400
----------------------------------------------------------------------
.../common/dsl/functions/DateFunctions.java | 65 +++++++---
.../common/stellar/DateFunctionsTest.java | 128 +++++++++++++++++++
2 files changed, 172 insertions(+), 21 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-metron/blob/4212927b/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/DateFunctions.java
----------------------------------------------------------------------
diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/DateFunctions.java b/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/DateFunctions.java
index 709a523..e28afd3 100644
--- a/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/DateFunctions.java
+++ b/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/DateFunctions.java
@@ -144,9 +144,32 @@ public class DateFunctions {
}
/**
+ * Gets the value from a list of arguments.
+ *
+ * If the argument at the specified position does not exist, a default value will be returned.
+ * If the argument at the specified position exists, but cannot be coerced to the right type, null is returned.
+ * Otherwise, the argument value is returned.
+ *
+ * @param args A list of arguments.
+ * @param position The position of the argument to get.
+ * @param clazz The type of class expected.
+ * @param defaultValue The default value.
+ * @param <T> The expected type of the argument.
+ */
+ private static <T> T getOrDefault(List<Object> args, int position, Class<T> clazz, T defaultValue) {
+ T result = defaultValue;
+ if(args.size() > position) {
+ result = ConversionUtils.convert(args.get(position), clazz);
+ }
+ return result;
+ }
+
+ /**
* Stellar Function: DAY_OF_WEEK
*
* The numbered day within the week. The first day of the week, Sunday, has a value of 1.
+ *
+ * If no argument is supplied, returns the current day of week.
*/
@Stellar( name="DAY_OF_WEEK"
, description="The numbered day within the week. The first day of the week, Sunday, has a value of 1."
@@ -157,10 +180,10 @@ public class DateFunctions {
@Override
public Object apply(List<Object> args) {
- // expect epoch milliseconds
- Long epochMillis = ConversionUtils.convert(args.get(0), Long.class);
+ // expects epoch millis, otherwise defaults to current time
+ Long epochMillis = getOrDefault(args, 0, Long.class, System.currentTimeMillis());
if(epochMillis == null) {
- return null;
+ return null; // invalid argument
}
// create a calendar
@@ -185,10 +208,10 @@ public class DateFunctions {
@Override
public Object apply(List<Object> args) {
- // expect epoch milliseconds
- Long epochMillis = ConversionUtils.convert(args.get(0), Long.class);
+ // expects epoch millis, otherwise defaults to current time
+ Long epochMillis = getOrDefault(args, 0, Long.class, System.currentTimeMillis());
if(epochMillis == null) {
- return null;
+ return null; // invalid argument
}
// create a calendar
@@ -213,10 +236,10 @@ public class DateFunctions {
@Override
public Object apply(List<Object> args) {
- // expect epoch milliseconds
- Long epochMillis = ConversionUtils.convert(args.get(0), Long.class);
+ // expects epoch millis, otherwise defaults to current time
+ Long epochMillis = getOrDefault(args, 0, Long.class, System.currentTimeMillis());
if(epochMillis == null) {
- return null;
+ return null; // invalid argument
}
// create a calendar
@@ -241,10 +264,10 @@ public class DateFunctions {
@Override
public Object apply(List<Object> args) {
- // expect epoch milliseconds
- Long epochMillis = ConversionUtils.convert(args.get(0), Long.class);
+ // expects epoch millis, otherwise defaults to current time
+ Long epochMillis = getOrDefault(args, 0, Long.class, System.currentTimeMillis());
if(epochMillis == null) {
- return null;
+ return null; // invalid argument
}
// create a calendar
@@ -269,10 +292,10 @@ public class DateFunctions {
@Override
public Object apply(List<Object> args) {
- // expect epoch milliseconds
- Long epochMillis = ConversionUtils.convert(args.get(0), Long.class);
+ // expects epoch millis, otherwise defaults to current time
+ Long epochMillis = getOrDefault(args, 0, Long.class, System.currentTimeMillis());
if(epochMillis == null) {
- return null;
+ return null; // invalid argument
}
// create a calendar
@@ -298,10 +321,10 @@ public class DateFunctions {
@Override
public Object apply(List<Object> args) {
- // expect epoch milliseconds
- Long epochMillis = ConversionUtils.convert(args.get(0), Long.class);
+ // expects epoch millis, otherwise defaults to current time
+ Long epochMillis = getOrDefault(args, 0, Long.class, System.currentTimeMillis());
if(epochMillis == null) {
- return null;
+ return null; // invalid argument
}
// create a calendar
@@ -327,10 +350,10 @@ public class DateFunctions {
@Override
public Object apply(List<Object> args) {
- // expect epoch milliseconds
- Long epochMillis = ConversionUtils.convert(args.get(0), Long.class);
+ // expects epoch millis, otherwise defaults to current time
+ Long epochMillis = getOrDefault(args, 0, Long.class, System.currentTimeMillis());
if(epochMillis == null) {
- return null;
+ return null; // invalid argument
}
// create a calendar
http://git-wip-us.apache.org/repos/asf/incubator-metron/blob/4212927b/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
index caa690d..49a982b 100644
--- 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
@@ -39,6 +39,7 @@ import static java.lang.String.format;
public class DateFunctionsTest {
private Map<String, Object> variables = new HashMap<>();
+ private Calendar calendar;
/**
* Runs a Stellar expression.
@@ -58,6 +59,7 @@ public class DateFunctionsTest {
@Before
public void setup() {
variables.put("epoch", AUG2016);
+ calendar = Calendar.getInstance();
}
@Test
@@ -66,39 +68,165 @@ public class DateFunctionsTest {
assertEquals(Calendar.THURSDAY, result);
}
+ /**
+ * If no argument, then return the current day of week.
+ */
+ @Test
+ public void testDayOfWeekNow() {
+ Object result = run(format("DAY_OF_WEEK()", AUG2016));
+ 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(format("DAY_OF_WEEK(nada)", AUG2016));
+ assertEquals(null, result);
+ }
+
@Test
public void testWeekOfMonth() {
Object result = run(format("WEEK_OF_MONTH(epoch)", AUG2016));
assertEquals(4, result);
}
+ /**
+ * If no argument, then return the current week of month.
+ */
+ @Test
+ public void testWeekOfMonthNow() {
+ Object result = run(format("WEEK_OF_MONTH()", AUG2016));
+ 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(format("WEEK_OF_MONTH(nada)", AUG2016));
+ assertEquals(null, result);
+ }
+
@Test
public void testMonth() {
Object result = run(format("MONTH(epoch)", AUG2016));
assertEquals(Calendar.AUGUST, result);
}
+ /**
+ * If no argument, then return the current month.
+ */
+ @Test
+ public void testMonthNow() {
+ Object result = run(format("MONTH()", AUG2016));
+ assertEquals(calendar.get(Calendar.MONTH), result);
+ }
+
+ /**
+ * If refer to variable that does not exist, expect null returned.
+ */
+ @Test
+ public void testMonthNull() {
+ Object result = run(format("MONTH(nada)", AUG2016));
+ assertEquals(null, result);
+ }
+
@Test
public void testYear() {
Object result = run(format("YEAR(epoch)", AUG2016));
assertEquals(2016, result);
}
+ /**
+ * If no argument, then return the current year.
+ */
+ @Test
+ public void testYearNow() {
+ Object result = run(format("YEAR()", AUG2016));
+ assertEquals(calendar.get(Calendar.YEAR), result);
+ }
+
+ /**
+ * If refer to variable that does not exist, expect null returned.
+ */
+ @Test
+ public void testYearNull() {
+ Object result = run(format("YEAR(nada)", AUG2016));
+ assertEquals(null, result);
+ }
+
@Test
public void testDayOfMonth() {
Object result = run(format("DAY_OF_MONTH(epoch)", AUG2016));
assertEquals(25, result);
}
+ /**
+ * If no argument, then return the current day of month.
+ */
+ @Test
+ public void testDayOfMonthNow() {
+ Object result = run(format("DAY_OF_MONTH()", AUG2016));
+ 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(format("DAY_OF_MONTH(nada)", AUG2016));
+ assertEquals(null, result);
+ }
+
@Test
public void testWeekOfYear() {
Object result = run(format("WEEK_OF_YEAR(epoch)", AUG2016));
assertEquals(35, result);
}
+ /**
+ * If no argument, then return the current week of year.
+ */
+ @Test
+ public void testWeekOfYearNow() {
+ Object result = run(format("WEEK_OF_YEAR()", AUG2016));
+ 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(format("WEEK_OF_YEAR(nada)", AUG2016));
+ assertEquals(null, result);
+ }
+
@Test
public void testDayOfYear() {
Object result = run(format("DAY_OF_YEAR(epoch)", AUG2016));
assertEquals(238, result);
}
+
+ /**
+ * If no argument, then return the current day of year.
+ */
+ @Test
+ public void testDayOfYearNow() {
+ Object result = run(format("DAY_OF_YEAR()", AUG2016));
+ 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(format("DAY_OF_YEAR(nada)", AUG2016));
+ assertEquals(null, result);
+ }
}