forked from apache/nifi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NIFI-7109 Unit tests should be able to determine if item validator wa…
…s 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 <[email protected]> This closes apache#4043
- Loading branch information
1 parent
62606ff
commit 6e8f10c
Showing
3 changed files
with
243 additions
and
14 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
...ifi-utils/src/test/java/org/apache/nifi/util/validator/InstrumentedStandardValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
nifi-mock/src/main/java/org/apache/nifi/util/validator/InstrumentedValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |