-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validation check for lazy model tagging (#546)
* Validation check for lazy model tagging * test coverage fix * name clarity, sonar fixen
- Loading branch information
1 parent
009f0bc
commit e5cbf1a
Showing
11 changed files
with
434 additions
and
11 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
73 changes: 73 additions & 0 deletions
73
.../src/main/java/com/mastercard/test/flow/validation/check/ReflectiveModelTaggingCheck.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,73 @@ | ||
package com.mastercard.test.flow.validation.check; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
import com.mastercard.test.flow.Model; | ||
import com.mastercard.test.flow.util.TaggedGroup; | ||
import com.mastercard.test.flow.validation.Check; | ||
import com.mastercard.test.flow.validation.Validation; | ||
import com.mastercard.test.flow.validation.Violation; | ||
|
||
/** | ||
* Checks that model implementations that offer reflective access to model | ||
* tagging information do so in an accurate manner | ||
*/ | ||
public class ReflectiveModelTaggingCheck implements Validation { | ||
|
||
/** | ||
* The name of the <code>public static final TaggedGroup</code> field that | ||
* lazily-constructed model types are assumed to have | ||
*/ | ||
public static final String MODEL_TAGS_FIELD_NAME = "MODEL_TAGS"; | ||
|
||
@Override | ||
public String name() { | ||
return "Reflective model tagging"; | ||
} | ||
|
||
@Override | ||
public String explanation() { | ||
return "Models that offer reflective tagging information do so accurately"; | ||
} | ||
|
||
@Override | ||
public Stream<Check> checks( Model model ) { | ||
return buildChecks( model, new ArrayList<>() ).stream(); | ||
} | ||
|
||
private List<Check> buildChecks( Model model, List<Check> checks ) { | ||
Optional<Field> mf = Stream.of( model.getClass().getDeclaredFields() ) | ||
.filter( f -> Modifier.isPublic( f.getModifiers() ) ) | ||
.filter( f -> Modifier.isStatic( f.getModifiers() ) ) | ||
.filter( f -> Modifier.isFinal( f.getModifiers() ) ) | ||
.filter( f -> TaggedGroup.class.isAssignableFrom( f.getType() ) ) | ||
.filter( f -> MODEL_TAGS_FIELD_NAME.equals( f.getName() ) ) | ||
.findFirst(); | ||
|
||
if( mf.isPresent() ) { | ||
try { | ||
TaggedGroup reflective = (TaggedGroup) mf.get().get( null ); | ||
TaggedGroup method = model.tags(); | ||
|
||
checks.add( new Check( this, model.title(), () -> reflective != method | ||
? new Violation( this, String.format( | ||
"%s.tags() should just return %s", | ||
model.getClass().getName(), MODEL_TAGS_FIELD_NAME ) ) | ||
: null ) ); | ||
} | ||
catch( Exception e ) { | ||
throw new IllegalStateException( "Failed to access tags for " + model.getClass(), e ); | ||
} | ||
} | ||
|
||
model.subModels().forEach( child -> buildChecks( child, checks ) ); | ||
|
||
return checks; | ||
} | ||
|
||
} |
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
Oops, something went wrong.