mirror of https://github.com/apache/nifi.git
NIFI-7109 Unit tests should be able to determine if item validator was called
- Create a mock Validator to track count of calls to validate(). We cannot use Mockito for this, because it can't mock all the StandardValidators refactor based on review comments fix naming in comments moved to main based on review Signed-off-by: Matthew Burgess <mattyb149@apache.org> This closes #4043
This commit is contained in:
parent
62606ff89a
commit
6e8f10c4f6
|
@ -0,0 +1,92 @@
|
|||
/*
|
||||
* 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.util.validator;
|
||||
|
||||
import org.apache.nifi.components.ValidationContext;
|
||||
import org.apache.nifi.components.ValidationResult;
|
||||
import org.apache.nifi.components.Validator;
|
||||
|
||||
/**:
|
||||
* InstrumentedStandarValidator wraps a {@class Validator} and provides statistics on it's interactions.
|
||||
* Because many of the {@class Validator} instances returned from {@class StandardValidator }
|
||||
* are not mockable with with mockito, this is required to know, when running a test, if a
|
||||
* {@class Validator} was in fact called, for example.
|
||||
*/
|
||||
public class InstrumentedStandardValidator implements Validator {
|
||||
|
||||
/**
|
||||
* Flag to reset a count after retrieving it.
|
||||
* Thus not having to explicitly call reset() for simple cases.
|
||||
*/
|
||||
boolean doReset = false;
|
||||
|
||||
/**
|
||||
* Count the number of calls to validate()
|
||||
*/
|
||||
private int validateCallCount;
|
||||
private Validator mockedValidator;
|
||||
|
||||
/**
|
||||
* Constructs a new {@class InstrumentedStandarValidator}.
|
||||
*
|
||||
* @param mockedValidator the {@class Validator} to wrap.
|
||||
*/
|
||||
public InstrumentedStandardValidator(Validator mockedValidator) {
|
||||
this(mockedValidator,false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new {@class InstrumentedStandarValidator}.
|
||||
*
|
||||
* @param mockedValidator the {@class Validator} to wrap.
|
||||
*/
|
||||
public InstrumentedStandardValidator(Validator mockedValidator, boolean resetOnGet) {
|
||||
this.mockedValidator = mockedValidator;
|
||||
this.doReset = resetOnGet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default constructor without wrapping not supported.
|
||||
*
|
||||
*/
|
||||
private InstrumentedStandardValidator(){}
|
||||
|
||||
@Override
|
||||
public ValidationResult validate(String subject, String input, ValidationContext context) {
|
||||
validateCallCount++;
|
||||
return mockedValidator.validate(subject, input, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the count of times validate was called
|
||||
* @return count of validate() calls
|
||||
*/
|
||||
public int getValidateCallCount() {
|
||||
int count = validateCallCount;
|
||||
if (doReset) {
|
||||
validateCallCount = 0;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the count of all calls to 0.
|
||||
*/
|
||||
public void resetAll() {
|
||||
validateCallCount = 0;
|
||||
}
|
||||
}
|
|
@ -16,6 +16,13 @@
|
|||
*/
|
||||
package org.apache.nifi.util.validator;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.nifi.components.ValidationContext;
|
||||
import org.apache.nifi.components.ValidationResult;
|
||||
import org.apache.nifi.components.Validator;
|
||||
|
@ -23,12 +30,6 @@ import org.apache.nifi.processor.util.StandardValidators;
|
|||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
public class TestStandardValidators {
|
||||
|
||||
@Test
|
||||
|
@ -102,7 +103,8 @@ public class TestStandardValidators {
|
|||
|
||||
@Test
|
||||
public void testTimePeriodValidator() {
|
||||
Validator val = StandardValidators.createTimePeriodValidator(1L, TimeUnit.SECONDS, Long.MAX_VALUE, TimeUnit.NANOSECONDS);
|
||||
Validator val = StandardValidators
|
||||
.createTimePeriodValidator(1L, TimeUnit.SECONDS, Long.MAX_VALUE, TimeUnit.NANOSECONDS);
|
||||
ValidationResult vr;
|
||||
|
||||
final ValidationContext validationContext = Mockito.mock(ValidationContext.class);
|
||||
|
@ -159,90 +161,133 @@ public class TestStandardValidators {
|
|||
|
||||
@Test
|
||||
public void testListValidator() {
|
||||
Validator val = StandardValidators.createListValidator(true, false, StandardValidators.NON_EMPTY_VALIDATOR);
|
||||
// use the TestMockValidator to be sure the item validator get's called when we think it should
|
||||
// note that it will reset the count after every get call.
|
||||
// note that we fail fast, so if there are 3 items, and the second fails validation, the call
|
||||
// count will be 2
|
||||
InstrumentedStandardValidator mockValidator = new InstrumentedStandardValidator(StandardValidators.NON_EMPTY_VALIDATOR, true);
|
||||
Validator val = StandardValidators.createListValidator(true, false, mockValidator);
|
||||
ValidationResult vr;
|
||||
|
||||
final ValidationContext validationContext = Mockito.mock(ValidationContext.class);
|
||||
|
||||
vr = val.validate("List", null, validationContext);
|
||||
assertFalse(vr.isValid());
|
||||
assertEquals(0, mockValidator.getValidateCallCount());
|
||||
|
||||
vr = val.validate("List", "", validationContext);
|
||||
assertFalse(vr.isValid());
|
||||
assertEquals(1, mockValidator.getValidateCallCount());
|
||||
|
||||
// Whitespace will be trimmed
|
||||
vr = val.validate("List", " ", validationContext);
|
||||
assertFalse(vr.isValid());
|
||||
assertEquals(1, mockValidator.getValidateCallCount());
|
||||
|
||||
vr = val.validate("List", "1", validationContext);
|
||||
assertTrue(vr.isValid());
|
||||
assertEquals(1, mockValidator.getValidateCallCount());
|
||||
|
||||
vr = val.validate("List", "1,2,3", validationContext);
|
||||
assertTrue(vr.isValid());
|
||||
assertEquals(3, mockValidator.getValidateCallCount());
|
||||
|
||||
// The parser will not bother with whitespace after the last comma
|
||||
vr = val.validate("List", "a,", validationContext);
|
||||
assertTrue(vr.isValid());
|
||||
assertEquals(1, mockValidator.getValidateCallCount());
|
||||
|
||||
// However it will bother if there is an empty element in the list (two commas in a row, e.g.)
|
||||
vr = val.validate("List", "a,,c", validationContext);
|
||||
assertFalse(vr.isValid());
|
||||
assertEquals(2, mockValidator.getValidateCallCount());
|
||||
|
||||
vr = val.validate("List", "a, ,c, ", validationContext);
|
||||
assertFalse(vr.isValid());
|
||||
assertEquals(2, mockValidator.getValidateCallCount());
|
||||
|
||||
// Try without trim and use a non-blank validator instead of a non-empty one
|
||||
val = StandardValidators.createListValidator(false, true, StandardValidators.NON_BLANK_VALIDATOR);
|
||||
//
|
||||
// use the TestMockValidator to be sure the item validator get's called when we think it should
|
||||
// note that it will reset the count after every get call.
|
||||
// note that we fail fast, so if there are 3 items, and the second fails validation, the call
|
||||
// count will be 2
|
||||
mockValidator = new InstrumentedStandardValidator(StandardValidators.NON_BLANK_VALIDATOR, true);
|
||||
val = StandardValidators.createListValidator(false, true, mockValidator);
|
||||
|
||||
vr = val.validate("List", null, validationContext);
|
||||
assertFalse(vr.isValid());
|
||||
assertEquals(0, mockValidator.getValidateCallCount());
|
||||
|
||||
// Validator will ignore empty entries
|
||||
// List Validator will ignore empty entries
|
||||
vr = val.validate("List", "", validationContext);
|
||||
assertTrue(vr.isValid());
|
||||
assertEquals(0, mockValidator.getValidateCallCount());
|
||||
|
||||
// Whitespace will not be trimmed, but it is still invalid because a non-blank validator is used
|
||||
vr = val.validate("List", " ", validationContext);
|
||||
assertFalse(vr.isValid());
|
||||
assertEquals(1, mockValidator.getValidateCallCount());
|
||||
|
||||
vr = val.validate("List", "a,,c", validationContext);
|
||||
assertTrue(vr.isValid());
|
||||
assertEquals(2, mockValidator.getValidateCallCount());
|
||||
|
||||
vr = val.validate("List", "a, ,c, ", validationContext);
|
||||
assertFalse(vr.isValid());
|
||||
assertEquals(2, mockValidator.getValidateCallCount());
|
||||
|
||||
// Try without trim and use a non-empty validator
|
||||
val = StandardValidators.createListValidator(false, false, StandardValidators.NON_EMPTY_VALIDATOR);
|
||||
// use the TestMockValidator to be sure the item validator get's called when we think it should
|
||||
// note that it will reset the count after every get call.
|
||||
// note that we fail fast, so if there are 3 items, and the second fails validation, the call
|
||||
// count will be 2
|
||||
mockValidator = new InstrumentedStandardValidator(StandardValidators.NON_EMPTY_VALIDATOR, true);
|
||||
val = StandardValidators.createListValidator(false, false, mockValidator);
|
||||
|
||||
vr = val.validate("List", null, validationContext);
|
||||
assertFalse(vr.isValid());
|
||||
assertEquals(0, mockValidator.getValidateCallCount());
|
||||
|
||||
vr = val.validate("List", "", validationContext);
|
||||
assertFalse(vr.isValid());
|
||||
assertEquals(1, mockValidator.getValidateCallCount());
|
||||
|
||||
// Whitespace will not be trimmed
|
||||
vr = val.validate("List", " ", validationContext);
|
||||
assertTrue(vr.isValid());
|
||||
assertEquals(1, mockValidator.getValidateCallCount());
|
||||
|
||||
vr = val.validate("List", "a, ,c, ", validationContext);
|
||||
assertTrue(vr.isValid());
|
||||
assertEquals(4, mockValidator.getValidateCallCount());
|
||||
|
||||
// Try with trim and use a boolean validator
|
||||
val = StandardValidators.createListValidator(true, true, StandardValidators.BOOLEAN_VALIDATOR);
|
||||
// Try with trim and use a boolean validator // Try without trim and use a non-empty validator
|
||||
// use the TestMockValidator to be sure the item validator get's called when we think it should
|
||||
// note that it will reset the count after every get call.
|
||||
// note that we fail fast, so if there are 3 items, and the second fails validation, the call
|
||||
// count will be 2
|
||||
mockValidator = new InstrumentedStandardValidator(StandardValidators.BOOLEAN_VALIDATOR, true);
|
||||
val = StandardValidators.createListValidator(true, true, mockValidator);
|
||||
vr = val.validate("List", "notbool", validationContext);
|
||||
assertFalse(vr.isValid());
|
||||
assertEquals(1, mockValidator.getValidateCallCount());
|
||||
|
||||
vr = val.validate("List", " notbool \n ", validationContext);
|
||||
assertFalse(vr.isValid());
|
||||
assertEquals(1, mockValidator.getValidateCallCount());
|
||||
|
||||
vr = val.validate("List", "true", validationContext);
|
||||
assertTrue(vr.isValid());
|
||||
assertEquals(1, mockValidator.getValidateCallCount());
|
||||
|
||||
vr = val.validate("List", " true \n ", validationContext);
|
||||
assertTrue(vr.isValid());
|
||||
assertEquals(1, mockValidator.getValidateCallCount());
|
||||
|
||||
vr = val.validate("List", " , false, true,\n", validationContext);
|
||||
assertTrue(vr.isValid());
|
||||
assertEquals(2, mockValidator.getValidateCallCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -0,0 +1,92 @@
|
|||
/*
|
||||
* 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.util.validator;
|
||||
|
||||
import org.apache.nifi.components.ValidationContext;
|
||||
import org.apache.nifi.components.ValidationResult;
|
||||
import org.apache.nifi.components.Validator;
|
||||
|
||||
/**
|
||||
* InstrumentedValidator wraps a {@class Validator} and provides statistics on it's interactions.
|
||||
* Because many of the {@class Validator} instances returned from {@class StandardValidator }
|
||||
* are not mockable with with mockito, this is required to know, when running a test, if a
|
||||
* {@class Validator} was in fact called, for example.
|
||||
*/
|
||||
public class InstrumentedValidator implements Validator {
|
||||
|
||||
/**
|
||||
* Flag to reset a count after retrieving it.
|
||||
* Thus not having to explicitly call reset() for simple cases.
|
||||
*/
|
||||
boolean doReset = false;
|
||||
|
||||
/**
|
||||
* Count the number of calls to validate()
|
||||
*/
|
||||
private int validateCallCount;
|
||||
private Validator mockedValidator;
|
||||
|
||||
/**
|
||||
* Constructs a new {@class InstrumentedValidator}.
|
||||
*
|
||||
* @param mockedValidator the {@class Validator} to wrap.
|
||||
*/
|
||||
public InstrumentedValidator(Validator mockedValidator) {
|
||||
this(mockedValidator,false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new {@class InstrumentedValidator}.
|
||||
*
|
||||
* @param mockedValidator the {@class Validator} to wrap.
|
||||
*/
|
||||
public InstrumentedValidator(Validator mockedValidator, boolean resetOnGet) {
|
||||
this.mockedValidator = mockedValidator;
|
||||
this.doReset = resetOnGet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default constructor without wrapping not supported.
|
||||
*
|
||||
*/
|
||||
private InstrumentedValidator(){}
|
||||
|
||||
@Override
|
||||
public ValidationResult validate(String subject, String input, ValidationContext context) {
|
||||
validateCallCount++;
|
||||
return mockedValidator.validate(subject, input, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the count of times validate was called
|
||||
* @return count of validate() calls
|
||||
*/
|
||||
public int getValidateCallCount() {
|
||||
int count = validateCallCount;
|
||||
if (doReset) {
|
||||
validateCallCount = 0;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the count of all calls to 0.
|
||||
*/
|
||||
public void resetAll() {
|
||||
validateCallCount = 0;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue