Skip to content

Commit

Permalink
RANGER-3888: fixed unit tests for earlier commit in RANGER-3888 - #2
Browse files Browse the repository at this point in the history
(cherry picked from commit b6e4324)
  • Loading branch information
mneethiraj committed Oct 28, 2022
1 parent b5dd2e2 commit ed040f5
Show file tree
Hide file tree
Showing 3 changed files with 256 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public class RangerValidityScheduleValidator {
private static final ThreadLocal<DateFormat> DATE_FORMATTER = new ThreadLocal<DateFormat>() {
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat(RangerValiditySchedule.VALIDITY_SCHEDULE_DATE_STRING_SPECIFICATION);
SimpleDateFormat sd = new SimpleDateFormat(RangerValiditySchedule.VALIDITY_SCHEDULE_DATE_STRING_SPECIFICATION);
sd.setLenient(false);
return sd;
}
};

Expand Down Expand Up @@ -183,7 +185,8 @@ private boolean validateValidityInterval(RangerValidityRecurrence recurrence, Li

if (validityInterval.getDays() < 0
|| (validityInterval.getHours() < 0 || validityInterval.getHours() > 23)
|| (validityInterval.getMinutes() < 0 || validityInterval.getMinutes() > 59)) {
|| (validityInterval.getMinutes() < 0 || validityInterval.getMinutes() > 59)
|| (validityInterval.getDays() == 0 && validityInterval.getHours() == 0 && validityInterval.getMinutes() == 0 )) {
validationFailures.add(new ValidationFailureDetails(0, "interval", "", false, true, false, "invalid interval"));
ret = false;
}
Expand Down Expand Up @@ -226,6 +229,26 @@ private boolean validateFieldSpec(RangerValidityRecurrence recurrence, RangerVal
int maximum = field == RangerValidityRecurrence.RecurrenceSchedule.ScheduleFieldSpec.month ? field.maximum + 1 : field.maximum;
ret = validateRanges(recurrence, field, minimum, maximum, validationFailures);
}

if(ret) {
final int minimum;
final int maximum;

if (field == RecurrenceSchedule.ScheduleFieldSpec.year) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy");

minimum = Integer.valueOf(formatter.format(startTime));
maximum = Integer.valueOf(formatter.format(endTime));
} else if (field == RecurrenceSchedule.ScheduleFieldSpec.month) {
minimum = field.minimum + 1;
maximum = field.maximum + 1;
} else {
minimum = field.minimum;
maximum = field.maximum;
}

ret = validateRanges(recurrence, field, minimum, maximum, validationFailures);
}
return ret;
}

Expand Down Expand Up @@ -340,36 +363,47 @@ public int compare(Range me, Range other) {
if (StringUtils.isNotEmpty(spec)) {
// Range
if (spec.startsWith("-") || spec.endsWith("-")) {
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect range spec"));
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect range spec: " + spec));
ret = false;
} else {
String[] ranges = StringUtils.split(spec, "-");
if (ranges.length > 2) {
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect range spec"));
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect range spec: " + spec));
ret = false;
} else if (ranges.length == 2) {
int val1 = minValidValue, val2 = maxValidValue;
if (!StringUtils.equals(ranges[0], RangerValidityRecurrence.RecurrenceSchedule.WILDCARD)) {
val1 = Integer.valueOf(ranges[0]);
if (val1 < minValidValue || val1 > maxValidValue) {
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect lower range value"));
ret = false;
}
} else {
value = RangerValidityRecurrence.RecurrenceSchedule.WILDCARD;
}

if (!StringUtils.equals(ranges[1], RangerValidityRecurrence.RecurrenceSchedule.WILDCARD)) {
val2 = Integer.valueOf(ranges[1]);
if (val1 < minValidValue || val2 > maxValidValue) {
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect upper range value"));
ret = false;
}
} else {
value = RangerValidityRecurrence.RecurrenceSchedule.WILDCARD;
}

if (field == RecurrenceSchedule.ScheduleFieldSpec.year) { // for year, one bound (lower or upper) can be outside the range
if (val1 < minValidValue && val2 > maxValidValue) {
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect range: (" + val1 + ", " + val2 + "). valid range: (" + minValidValue + ", " + maxValidValue + ")"));
ret = false;
}
} else { // for month/dayOfMonth/dayOfWeek/hour/minute both bounds (lower and upper) must be within range
if (val1 < minValidValue || val1 > maxValidValue) {
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect lower range: " + val1 + ". valid range: (" + minValidValue + ", " + maxValidValue + ")"));
ret = false;
}

if (val2 < minValidValue || val2 > maxValidValue) {
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect upper range: " + val2 + ". valid range: (" + minValidValue + ", " + maxValidValue + ")"));
ret = false;
}
}

if (ret) {
if (val1 >= val2) {
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect range"));
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect range: min=" + val1 + ", max=" + val2));
ret = false;
} else {
value = RangerValidityRecurrence.RecurrenceSchedule.WILDCARD;
Expand All @@ -389,7 +423,7 @@ public int compare(Range me, Range other) {
if (!StringUtils.equals(ranges[0], RangerValidityRecurrence.RecurrenceSchedule.WILDCARD)) {
int val = Integer.valueOf(ranges[0]);
if (val < minValidValue || val > maxValidValue) {
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect value"));
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "incorrect value: " + val + ". Valid range: (" + minValidValue + "-" + maxValidValue + ")"));
ret = false;
} else {
if (!StringUtils.equals(value, RangerValidityRecurrence.RecurrenceSchedule.WILDCARD)) {
Expand All @@ -414,7 +448,7 @@ public int compare(Range me, Range other) {
int upper = range.upper;
for (int j = i+1; j < rangeOfValues.size(); j++) {
Range r = rangeOfValues.get(j);
if (upper < r.upper) {
if (upper > r.lower) {
validationFailures.add(new ValidationFailureDetails(0, fieldName, "", false, true, false, "overlapping range value"));
ret = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -905,9 +905,9 @@ private void runValiditySchedulerTests(String resourceName) {
}
}

assertTrue(testCase.name, isValid == testCase.result.isValid);
assertTrue(testCase.name, isApplicable == testCase.result.isApplicable);
assertTrue(testCase.name + ", [" + validationFailures +"]", validationFailures.size() == testCase.result.validationFailureCount);
assertEquals(testCase.name + " - isValid (validationFailures: " + validationFailures + ")", testCase.result.isValid, isValid);
assertEquals(testCase.name + " - isApplicable (validationFailures: " + validationFailures + ")", testCase.result.isApplicable, isApplicable);
assertEquals(testCase.name + " - validationFailureCount (validationFailures: " + validationFailures +")", testCase.result.validationFailureCount, validationFailures.size());
}
}
TimeZone.setDefault(defaultTZ);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,209 @@
"isApplicable": false,
"validationFailureCount": 2
}
},
{
"name": "invalid lower bound - minute",
"validitySchedules": [
{
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
"recurrences": [
{
"schedule": { "minute": "-1", "hour": "0", "dayOfMonth": "1", "dayOfWeek": "1", "month": "1", "year": "2018" },
"interval": { "minutes": 10 }
}
]
}
],
"accessTime": "20180112-18:32:27.000-0800",
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
}
},
{
"name": "invalid upper bound - minute",
"validitySchedules": [
{
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
"recurrences": [
{
"schedule": { "minute": "60", "hour": "0", "dayOfMonth": "1", "dayOfWeek": "1", "month": "1", "year": "2018" },
"interval": { "minutes": 10 }
}
]
}
],
"accessTime": "20180112-18:32:27.000-0800",
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
}
},
{
"name": "invalid lower bound - hour",
"validitySchedules": [
{
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
"recurrences": [
{
"schedule": { "minute": "0", "hour": "-1", "dayOfMonth": "1", "dayOfWeek": "1", "month": "1", "year": "2018" },
"interval": { "minutes": 10 }
}
]
}
],
"accessTime": "20180112-18:32:27.000-0800",
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
}
},
{
"name": "invalid upper bound - hour",
"validitySchedules": [
{
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
"recurrences": [
{
"schedule": { "minute": "0", "hour": "24", "dayOfMonth": "1", "dayOfWeek": "1", "month": "1", "year": "2018" },
"interval": { "minutes": 10 }
}
]
}
],
"accessTime": "20180112-18:32:27.000-0800",
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
}
},
{
"name": "invalid lower bound - dayOfMonth",
"validitySchedules": [
{
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
"recurrences": [
{
"schedule": { "minute": "0", "hour": "0", "dayOfMonth": "0", "dayOfWeek": "1", "month": "1", "year": "2018" },
"interval": { "minutes": 10 }
}
]
}
],
"accessTime": "20180112-18:32:27.000-0800",
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
}
},
{
"name": "invalid upper bound - dayOfMonth",
"validitySchedules": [
{
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
"recurrences": [
{
"schedule": { "minute": "0", "hour": "0", "dayOfMonth": "32", "dayOfWeek": "1", "month": "1", "year": "2018" },
"interval": { "minutes": 10 }
}
]
}
],
"accessTime": "20180112-18:32:27.000-0800",
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
}
},
{
"name": "invalid lower bound - dayOfWeek",
"validitySchedules": [
{
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
"recurrences": [
{
"schedule": { "minute": "0", "hour": "0", "dayOfMonth": "1", "dayOfWeek": "0", "month": "1", "year": "2018" },
"interval": { "minutes": 10 }
}
]
}
],
"accessTime": "20180112-18:32:27.000-0800",
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
}
},
{
"name": "invalid upper bound - dayOfWeek",
"validitySchedules": [
{
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
"recurrences": [
{
"schedule": { "minute": "0", "hour": "0", "dayOfMonth": "1", "dayOfWeek": "8", "month": "1", "year": "2018" },
"interval": { "minutes": 10 }
}
]
}
],
"accessTime": "20180112-18:32:27.000-0800",
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
}
},
{
"name": "invalid lower bound - month",
"validitySchedules": [
{
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
"recurrences": [
{
"schedule": { "minute": "0", "hour": "0", "dayOfMonth": "1", "dayOfWeek": "1", "month": "0", "year": "2018" },
"interval": { "minutes": 10 }
}
]
}
],
"accessTime": "20180112-18:32:27.000-0800",
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
}
},
{
"name": "invalid upper bound - month",
"validitySchedules": [
{
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
"recurrences": [
{
"schedule": { "minute": "0", "hour": "0", "dayOfMonth": "1", "dayOfWeek": "1", "month": "13", "year": "2018" },
"interval": { "minutes": 10 }
}
]
}
],
"accessTime": "20180112-18:32:27.000-0800",
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
}
},
{
"name": "invalid lower bound - year",
"validitySchedules": [
{
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
"recurrences": [
{
"schedule": { "minute": "0", "hour": "0", "dayOfMonth": "1", "dayOfWeek": "1", "month": 1, "year": "2016" },
"interval": { "minutes": 10 }
}
]
}
],
"accessTime": "20180112-18:32:27.000-0800",
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
}
},
{
"name": "invalid upper bound - year",
"validitySchedules": [
{
"startTime": "2018/01/12 14:32:00", "endTime": "2050/01/23 06:30:00",
"recurrences": [
{
"schedule": { "minute": "0", "hour": "0", "dayOfMonth": "1", "dayOfWeek": "1", "month": "1", "year": "2101" },
"interval": { "minutes": 10 }
}
]
}
],
"accessTime": "20180112-18:32:27.000-0800",
"result": { "isValid": false, "isApplicable": false, "validationFailureCount": 1
}
}
]

0 comments on commit ed040f5

Please sign in to comment.