-
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.
Model tagging check assertion inversion; Add model title uniqueness test
- Loading branch information
1 parent
02d3cf2
commit e1fd41f
Showing
6 changed files
with
145 additions
and
20 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
46 changes: 46 additions & 0 deletions
46
...on-core/src/main/java/com/mastercard/test/flow/validation/check/ModelUniquenessCheck.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,46 @@ | ||
/* | ||
* Copyright (c) 2024 Mastercard. All rights reserved. | ||
*/ | ||
|
||
package com.mastercard.test.flow.validation.check; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
import com.mastercard.test.flow.Model; | ||
import com.mastercard.test.flow.validation.Check; | ||
import com.mastercard.test.flow.validation.Validation; | ||
import com.mastercard.test.flow.validation.Violation; | ||
|
||
/** | ||
* Checks that all {@link Model} have a unique title | ||
*/ | ||
public final class ModelUniquenessCheck implements Validation { | ||
@Override | ||
public String name() { | ||
return "Model uniqueness"; | ||
} | ||
|
||
@Override | ||
public String explanation() { | ||
return "Models should be uniquely identified by its title"; | ||
} | ||
|
||
@Override | ||
public Stream<Check> checks( Model model ) { | ||
Set<String> modelTitles = new HashSet<>(); | ||
return checkModelTitles( model, modelTitles ).stream(); | ||
} | ||
|
||
private Set<Check> checkModelTitles( Model model, Set<String> modelTitles ) { | ||
Set<Check> checks = new HashSet<>(); | ||
if( !modelTitles.add( model.title() ) ) { | ||
checks.add( new Check( this, model.title(), | ||
() -> new Violation( this, "Duplicate model title found: " + model.title() ) ) ); | ||
} | ||
model.subModels() | ||
.forEach( subModel -> checks.addAll( checkModelTitles( subModel, modelTitles ) ) ); | ||
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
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
75 changes: 75 additions & 0 deletions
75
...ore/src/test/java/com/mastercard/test/flow/validation/check/ModelUniquenessCheckTest.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,75 @@ | ||
/* | ||
* Copyright (c) 2024 Mastercard. All rights reserved. | ||
*/ | ||
|
||
package com.mastercard.test.flow.validation.check; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.mastercard.test.flow.Model; | ||
|
||
/** | ||
* Exercises {@link ModelUniquenessCheck} | ||
*/ | ||
class ModelUniquenessCheckTest extends AbstractValidationTest { | ||
|
||
ModelUniquenessCheckTest() { | ||
super( new ModelUniquenessCheck(), "Model uniqueness", | ||
"Models should be uniquely identified by its title" ); | ||
} | ||
|
||
private List<Model> createMockModels( List<String> titles ) { | ||
return titles.stream().map( title -> { | ||
Model model = mock( Model.class ); | ||
when( model.title() ).thenReturn( title ); | ||
when( model.subModels() ).thenReturn( Stream.empty() ); | ||
return model; | ||
} ).collect( Collectors.toList() ); | ||
} | ||
|
||
@Test | ||
void testModelUniquenessFailureAssertion() { | ||
// Create a list of model titles | ||
List<String> titles = Arrays.asList( "Model1", "Model2", "Model1" ); | ||
|
||
// Create mock models | ||
List<Model> models = createMockModels( titles ); | ||
|
||
// Create a parent model and set its subModels | ||
Model parentModel = mock( Model.class ); | ||
when( parentModel.title() ).thenReturn( "ParentModel" ); | ||
when( parentModel.subModels() ).thenReturn( models.stream() ); | ||
|
||
test( parentModel, " details: Duplicate model title found: Model1\n" | ||
+ " expected: null\n" | ||
+ " actual: null\n" | ||
+ "offenders: " ); | ||
|
||
} | ||
|
||
@Test | ||
void testModelUniquenessSuccessAssertion() { | ||
// Create a list of model titles | ||
List<String> titles = Arrays.asList( "Model1", "Model2", "Model3" ); | ||
|
||
// Create mock models | ||
List<Model> models = createMockModels( titles ); | ||
|
||
// Create a parent model and set its subModels | ||
Model parentModel = mock( Model.class ); | ||
when( parentModel.title() ).thenReturn( "ParentModel" ); | ||
when( parentModel.subModels() ).thenReturn( models.stream() ); | ||
|
||
test( parentModel ); | ||
|
||
} | ||
|
||
} |