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 <alopresto@apache.org>
This commit is contained in:
Will Song 2016-06-17 11:32:39 -07:00 committed by Andy LoPresto
parent 679ad93f57
commit 9546bef86e
No known key found for this signature in database
GPG Key ID: 3C6EF65B2F7DEF69
3 changed files with 139 additions and 1 deletions

View File

@ -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);

View File

@ -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 }
}
}