-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from antkorwin/feature/expected-data-set
Add ExpectedDataSet annotation
- Loading branch information
Showing
36 changed files
with
1,945 additions
and
89 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/com/antkorwin/springtestmongo/annotation/ExpectedMongoDataSet.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,27 @@ | ||
package com.antkorwin.springtestmongo.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* You can use this annotation in tests | ||
* to check a state of the mongodb after the test execution. | ||
* | ||
* After test execution, all document collections will check | ||
* to match to expected data set in the selected file. | ||
* | ||
* @author Korovin Anatoliy | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface ExpectedMongoDataSet { | ||
|
||
/** | ||
* Path to the file with an expected data set (after test execution) | ||
* | ||
* @return path to file with an expected data set | ||
*/ | ||
String value(); | ||
} |
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
81 changes: 81 additions & 0 deletions
81
src/main/java/com/antkorwin/springtestmongo/internal/expect/AssertGraph.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,81 @@ | ||
package com.antkorwin.springtestmongo.internal.expect; | ||
|
||
import com.antkorwin.commonutils.exceptions.InternalException; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
/** | ||
* Evaluate the IndexedGraph and | ||
* assert that all patterns applied to any data record. | ||
*/ | ||
public class AssertGraph { | ||
|
||
private final IndexedGraph indexGraph; | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
private boolean failed = false; | ||
private List<String> errors = new ArrayList<>(); | ||
|
||
|
||
public AssertGraph(Graph graph) { | ||
this.indexGraph = new IndexedGraph(graph); | ||
} | ||
|
||
public void doAssert() { | ||
validateDataRecords(indexGraph.evaluateDataIndexes()); | ||
validatePatterns(indexGraph.evaluatePatternIndexes()); | ||
if (failed) { | ||
throw new Error("\nExpectedDataSet of " + indexGraph.getDocumentName() + " \n\n" + | ||
errors.stream().collect(Collectors.joining("\n")) + "\n"); | ||
} | ||
} | ||
|
||
private void validateDataRecords(Set<Integer> indexes) { | ||
if (indexes.size() != indexGraph.dataCount()) { | ||
|
||
String notFoundDataRecords = IntStream.range(0, indexGraph.dataCount()) | ||
.boxed() | ||
.filter(i -> !indexes.contains(i)) | ||
.map(indexGraph::getDataRecord) | ||
.map(this::mapToString) | ||
.collect(Collectors.joining("\n")); | ||
|
||
error("Not expected: \n" + notFoundDataRecords + "\n"); | ||
} | ||
} | ||
|
||
|
||
private void validatePatterns(Set<Integer> indexes) { | ||
if (indexes.size() != indexGraph.patternCount()) { | ||
|
||
String notFoundPatterns = IntStream.range(0, indexGraph.patternCount()) | ||
.boxed() | ||
.filter(i -> !indexes.contains(i)) | ||
.map(indexGraph::getPattern) | ||
.map(this::mapToString) | ||
.collect(Collectors.joining("\n")); | ||
|
||
error("Expected but not found: \n" + notFoundPatterns + "\n"); | ||
} | ||
} | ||
|
||
private String mapToString(Map<String, Object> stringObjectMap) { | ||
try { | ||
return objectMapper.writeValueAsString(stringObjectMap); | ||
} catch (JsonProcessingException e) { | ||
e.printStackTrace(); | ||
throw new InternalException(e); | ||
} | ||
} | ||
|
||
private void error(String message) { | ||
failed = true; | ||
errors.add(message); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/antkorwin/springtestmongo/internal/expect/Graph.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,53 @@ | ||
package com.antkorwin.springtestmongo.internal.expect; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Created on 09.12.2018. | ||
* | ||
* Graph is a model to calculate the matching of data in | ||
* mongodb and patterns in provided data sets, for one | ||
* type of document collection. | ||
* | ||
* @author Korovin Anatoliy | ||
*/ | ||
public interface Graph { | ||
|
||
/** | ||
* convert graph to matrix | ||
* | ||
* @return matrix view of graph | ||
*/ | ||
boolean[][] calculate(); | ||
|
||
/** | ||
* @return count of data records from mongodb | ||
*/ | ||
int dataCount(); | ||
|
||
/** | ||
* @return count of patterns from data set file | ||
*/ | ||
int patternCount(); | ||
|
||
/** | ||
* retrieve a data record by the index in graph | ||
* | ||
* @param index position of needed data record | ||
* @return data record from mongodb by the index in graph | ||
*/ | ||
Map<String, Object> getDataRecord(int index); | ||
|
||
/** | ||
* retrieve the pattern from data set file | ||
* | ||
* @param index position of this pattern in the graph | ||
* @return pattern object from a data set file by the index from the graph | ||
*/ | ||
Map<String, Object> getPattern(int index); | ||
|
||
/** | ||
* @return name of the mongodb document associated to this graph | ||
*/ | ||
String getDocumentName(); | ||
} |
92 changes: 92 additions & 0 deletions
92
src/main/java/com/antkorwin/springtestmongo/internal/expect/IndexedGraph.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 @@ | ||
package com.antkorwin.springtestmongo.internal.expect; | ||
|
||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* Add in graph indexes of successful matched patterns | ||
* and data records. | ||
*/ | ||
public class IndexedGraph implements Graph { | ||
|
||
private final Graph graph; | ||
private boolean indexReady; | ||
private Set<Integer> patternIndexes; | ||
private Set<Integer> dataIndexes; | ||
|
||
public IndexedGraph(Graph graph) { | ||
this.graph = graph; | ||
this.dataIndexes = new HashSet<>(); | ||
this.patternIndexes = new HashSet<>(); | ||
} | ||
|
||
@Override | ||
public boolean[][] calculate() { | ||
return graph.calculate(); | ||
} | ||
|
||
@Override | ||
public int dataCount() { | ||
return graph.dataCount(); | ||
} | ||
|
||
@Override | ||
public int patternCount() { | ||
return graph.patternCount(); | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getDataRecord(int index) { | ||
return graph.getDataRecord(index); | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getPattern(int index) { | ||
return graph.getPattern(index); | ||
} | ||
|
||
@Override | ||
public String getDocumentName() { | ||
return graph.getDocumentName(); | ||
} | ||
|
||
/** | ||
* @return set with indexes of patterns used in this graph | ||
*/ | ||
public Set<Integer> evaluatePatternIndexes() { | ||
if (!indexReady) { | ||
evaluateIndexes(); | ||
} | ||
return patternIndexes; | ||
} | ||
|
||
/** | ||
* @return set with indexes of data records used in this graph | ||
*/ | ||
public Set<Integer> evaluateDataIndexes() { | ||
if (!indexReady) { | ||
evaluateIndexes(); | ||
} | ||
return dataIndexes; | ||
} | ||
|
||
/** | ||
* calculate all used indexes of patterns and | ||
* data records matched for this patterns | ||
*/ | ||
private void evaluateIndexes() { | ||
|
||
boolean[][] matrix = graph.calculate(); | ||
|
||
for (int i = 0; i < dataCount(); i++) { | ||
for (int j = 0; j < patternCount(); j++) { | ||
if (matrix[i][j]) { | ||
dataIndexes.add(i); | ||
patternIndexes.add(j); | ||
} | ||
} | ||
} | ||
indexReady = true; | ||
} | ||
} |
Oops, something went wrong.