Repository: nifi Updated Branches: refs/heads/master 679ad93f5 -> 9546bef86
NIFI-1958 Added "wks" as an acceptable parameter and added unit test (+6 squashed commits) Squashed commits: [16dd4ba] NIFI-1958 fixed logic on incoming time units. Removed feature tests to convert to weeks because it will not be implemented. [1b22e58] NIFI-1958 added logic to getTimeDuration to handle weeks as a string value to parse but did not add TimeUnit yet. 3/5 feature test pass. [7136544] NIFI-1958 Moved tests to correct module. [7d95653] NIFI-1958 Added feature tests for negative values. [ffc3941] NIFI-1958 Added second feature test. [7d16bbe] NIFI-1958 Added new feature test and regression test for week conversion. This closes #544. Signed-off-by: Andy LoPresto <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/nifi/repo Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/9546bef8 Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/9546bef8 Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/9546bef8 Branch: refs/heads/master Commit: 9546bef86e8b9eaf293943bfbdaeaefedb0b8fc1 Parents: 679ad93 Author: Will Song <[email protected]> Authored: Fri Jun 17 11:32:39 2016 -0700 Committer: Andy LoPresto <[email protected]> Committed: Fri Jun 17 17:24:19 2016 -0700 ---------------------------------------------------------------------- .../apache/nifi/processor/TestFormatUtils.java | 40 ------ .../java/org/apache/nifi/util/FormatUtils.java | 10 +- .../nifi/processor/TestFormatUtilsGroovy.groovy | 130 +++++++++++++++++++ .../apache/nifi/processor/TestFormatUtils.java | 40 ++++++ 4 files changed, 179 insertions(+), 41 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nifi/blob/9546bef8/nifi-commons/nifi-processor-utilities/src/test/java/org/apache/nifi/processor/TestFormatUtils.java ---------------------------------------------------------------------- diff --git a/nifi-commons/nifi-processor-utilities/src/test/java/org/apache/nifi/processor/TestFormatUtils.java b/nifi-commons/nifi-processor-utilities/src/test/java/org/apache/nifi/processor/TestFormatUtils.java deleted file mode 100644 index 359def2..0000000 --- a/nifi-commons/nifi-processor-utilities/src/test/java/org/apache/nifi/processor/TestFormatUtils.java +++ /dev/null @@ -1,40 +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.nifi.processor; - -import static org.junit.Assert.assertEquals; - -import java.util.concurrent.TimeUnit; - -import org.apache.nifi.util.FormatUtils; - -import org.junit.Test; - -public class TestFormatUtils { - - @Test - public void testParse() { - assertEquals(3, FormatUtils.getTimeDuration("3000 ms", TimeUnit.SECONDS)); - assertEquals(3000, FormatUtils.getTimeDuration("3000 s", TimeUnit.SECONDS)); - assertEquals(0, FormatUtils.getTimeDuration("999 millis", TimeUnit.SECONDS)); - assertEquals(4L * 24L * 60L * 60L * 1000000000L, FormatUtils.getTimeDuration("4 days", TimeUnit.NANOSECONDS)); - assertEquals(24, FormatUtils.getTimeDuration("1 DAY", TimeUnit.HOURS)); - assertEquals(60, FormatUtils.getTimeDuration("1 hr", TimeUnit.MINUTES)); - assertEquals(60, FormatUtils.getTimeDuration("1 Hrs", TimeUnit.MINUTES)); - } - -} http://git-wip-us.apache.org/repos/asf/nifi/blob/9546bef8/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/FormatUtils.java ---------------------------------------------------------------------- diff --git a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/FormatUtils.java b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/FormatUtils.java index 03afec0..a30c14e 100644 --- a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/FormatUtils.java +++ b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/FormatUtils.java @@ -41,8 +41,9 @@ public class FormatUtils { private static final String MINS = join(UNION, "m", "min", "mins", "minute", "minutes"); private static final String HOURS = join(UNION, "h", "hr", "hrs", "hour", "hours"); private static final String DAYS = join(UNION, "d", "day", "days"); + private static final String WEEKS = join(UNION, "w", "wk", "wks", "week", "weeks"); - private static final String VALID_TIME_UNITS = join(UNION, NANOS, MILLIS, SECS, MINS, HOURS, DAYS); + private static final String VALID_TIME_UNITS = join(UNION, NANOS, MILLIS, SECS, MINS, HOURS, DAYS, WEEKS); public static final String TIME_DURATION_REGEX = "(\\d+)\\s*(" + VALID_TIME_UNITS + ")"; public static final Pattern TIME_DURATION_PATTERN = Pattern.compile(TIME_DURATION_REGEX); @@ -176,6 +177,13 @@ public class FormatUtils { case "days": specifiedTimeUnit = TimeUnit.DAYS; break; + case "w": + case "wk": + case "wks": + case "week": + case "weeks": + final long durationVal = Long.parseLong(duration); + return desiredUnit.convert(durationVal, TimeUnit.DAYS)*7; } final long durationVal = Long.parseLong(duration); http://git-wip-us.apache.org/repos/asf/nifi/blob/9546bef8/nifi-commons/nifi-utils/src/test/groovy/org/apache/nifi/processor/TestFormatUtilsGroovy.groovy ---------------------------------------------------------------------- diff --git a/nifi-commons/nifi-utils/src/test/groovy/org/apache/nifi/processor/TestFormatUtilsGroovy.groovy b/nifi-commons/nifi-utils/src/test/groovy/org/apache/nifi/processor/TestFormatUtilsGroovy.groovy new file mode 100644 index 0000000..f3e4f46 --- /dev/null +++ b/nifi-commons/nifi-utils/src/test/groovy/org/apache/nifi/processor/TestFormatUtilsGroovy.groovy @@ -0,0 +1,130 @@ +/* + * 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.nifi.processor + +import org.apache.nifi.util.FormatUtils +import org.junit.After +import org.junit.Before +import org.junit.BeforeClass +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.slf4j.Logger +import org.slf4j.LoggerFactory + +import java.util.concurrent.TimeUnit + +@RunWith(JUnit4.class) +class TestFormatUtilsGroovy extends GroovyTestCase { + private static final Logger logger = LoggerFactory.getLogger(TestFormatUtilsGroovy.class) + + @BeforeClass + public static void setUpOnce() throws Exception { + logger.metaClass.methodMissing = { String name, args -> + logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}") + } + } + + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + + /** + * New feature test + */ + @Test + void testShouldConvertWeeks() { + // Arrange + final List WEEKS = ["1 week", "1 wk", "1 w", "1 wks", "1 weeks"] + final long EXPECTED_DAYS = 7L + + // Act + List days = WEEKS.collect { String week -> + FormatUtils.getTimeDuration(week, TimeUnit.DAYS) + } + logger.converted(days) + + // Assert + assert days.every { it == EXPECTED_DAYS } + } + + + + @Test + void testShouldHandleNegativeWeeks() { + // Arrange + final List WEEKS = ["-1 week", "-1 wk", "-1 w", "-1 weeks", "- 1 week"] + + // Act + List msgs = WEEKS.collect { String week -> + shouldFail(IllegalArgumentException) { + FormatUtils.getTimeDuration(week, TimeUnit.DAYS) + } + } + + // Assert + assert msgs.every { it =~ /Value '.*' is not a valid Time Duration/ } + } + + + + /** + * Regression test + */ + @Test + void testShouldHandleInvalidAbbreviations() { + // Arrange + final List WEEKS = ["1 work", "1 wek", "1 k"] + + // Act + List msgs = WEEKS.collect { String week -> + shouldFail(IllegalArgumentException) { + FormatUtils.getTimeDuration(week, TimeUnit.DAYS) + } + } + + // Assert + assert msgs.every { it =~ /Value '.*' is not a valid Time Duration/ } + + } + + + /** + * New feature test + */ + @Test + void testShouldHandleNoSpaceInInput() { + // Arrange + final List WEEKS = ["1week", "1wk", "1w", "1wks", "1weeks"] + final long EXPECTED_DAYS = 7L + + // Act + List days = WEEKS.collect { String week -> + FormatUtils.getTimeDuration(week, TimeUnit.DAYS) + } + logger.converted(days) + + // Assert + assert days.every { it == EXPECTED_DAYS } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/nifi/blob/9546bef8/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/processor/TestFormatUtils.java ---------------------------------------------------------------------- diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/processor/TestFormatUtils.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/processor/TestFormatUtils.java new file mode 100644 index 0000000..359def2 --- /dev/null +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/processor/TestFormatUtils.java @@ -0,0 +1,40 @@ +/* + * 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.nifi.processor; + +import static org.junit.Assert.assertEquals; + +import java.util.concurrent.TimeUnit; + +import org.apache.nifi.util.FormatUtils; + +import org.junit.Test; + +public class TestFormatUtils { + + @Test + public void testParse() { + assertEquals(3, FormatUtils.getTimeDuration("3000 ms", TimeUnit.SECONDS)); + assertEquals(3000, FormatUtils.getTimeDuration("3000 s", TimeUnit.SECONDS)); + assertEquals(0, FormatUtils.getTimeDuration("999 millis", TimeUnit.SECONDS)); + assertEquals(4L * 24L * 60L * 60L * 1000000000L, FormatUtils.getTimeDuration("4 days", TimeUnit.NANOSECONDS)); + assertEquals(24, FormatUtils.getTimeDuration("1 DAY", TimeUnit.HOURS)); + assertEquals(60, FormatUtils.getTimeDuration("1 hr", TimeUnit.MINUTES)); + assertEquals(60, FormatUtils.getTimeDuration("1 Hrs", TimeUnit.MINUTES)); + } + +}
