-
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.
#13 Scenario generation was re-structured to be more modular
- Loading branch information
Showing
9 changed files
with
362 additions
and
152 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/main/java/com/github/mkolisnyk/aerial/datagenerators/CaseScenarioGenerator.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,63 @@ | ||
package com.github.mkolisnyk.aerial.datagenerators; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import com.github.mkolisnyk.aerial.document.CaseSection; | ||
import com.github.mkolisnyk.aerial.document.DocumentSection; | ||
import com.github.mkolisnyk.aerial.document.InputRecord; | ||
|
||
public abstract class CaseScenarioGenerator { | ||
private String ls = System.lineSeparator(); | ||
|
||
private CaseSection section; | ||
private List<InputRecord> records; | ||
private Map<String, List<String>> testData; | ||
|
||
public CaseScenarioGenerator( | ||
CaseSection sectionData, | ||
List<InputRecord> recordsList, | ||
Map<String, List<String>> testDataMap) { | ||
this.section = sectionData; | ||
this.records = recordsList; | ||
this.testData = testDataMap; | ||
} | ||
|
||
/** | ||
* @return the records | ||
*/ | ||
public final List<InputRecord> getRecords() { | ||
return records; | ||
} | ||
|
||
/** | ||
* @return the testData | ||
*/ | ||
public final Map<String, List<String>> getTestData() { | ||
return testData; | ||
} | ||
|
||
/** | ||
* @return the section | ||
*/ | ||
public final CaseSection getSection() { | ||
return section; | ||
} | ||
|
||
public String generatePreRequisites(ArrayList<DocumentSection<?>> preRequisites) throws Exception { | ||
String result = ""; | ||
if (preRequisites != null) { | ||
for (DocumentSection<?> item : preRequisites) { | ||
result = result.concat(item.generate() + ls); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public abstract String generate() throws Exception; | ||
|
||
public abstract boolean isApplicable(); | ||
|
||
public abstract String getScenarioName(); | ||
} |
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
29 changes: 29 additions & 0 deletions
29
.../java/com/github/mkolisnyk/aerial/datagenerators/cases/NegativeCaseScenarioGenerator.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,29 @@ | ||
package com.github.mkolisnyk.aerial.datagenerators.cases; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import com.github.mkolisnyk.aerial.document.CaseSection; | ||
import com.github.mkolisnyk.aerial.document.InputRecord; | ||
|
||
public class NegativeCaseScenarioGenerator extends PositiveCaseScenarioGenerator { | ||
|
||
public NegativeCaseScenarioGenerator(CaseSection sectionData, | ||
List<InputRecord> recordsList, | ||
Map<String, List<String>> testDataMap) { | ||
super(sectionData, recordsList, testDataMap); | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see com.github.mkolisnyk.aerial.datagenerators.cases.PositiveCaseScenarioGenerator#isPositive() | ||
*/ | ||
@Override | ||
public boolean isPositive() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String getScenarioName() { | ||
return " negative test"; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
.../java/com/github/mkolisnyk/aerial/datagenerators/cases/PositiveCaseScenarioGenerator.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,74 @@ | ||
package com.github.mkolisnyk.aerial.datagenerators.cases; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
|
||
import com.github.mkolisnyk.aerial.datagenerators.CaseScenarioGenerator; | ||
import com.github.mkolisnyk.aerial.document.CaseSection; | ||
import com.github.mkolisnyk.aerial.document.InputRecord; | ||
import com.github.mkolisnyk.aerial.document.Tokens; | ||
|
||
public class PositiveCaseScenarioGenerator extends | ||
CaseScenarioGenerator { | ||
private int offset = 1; | ||
private String ls = System.lineSeparator(); | ||
|
||
public PositiveCaseScenarioGenerator(CaseSection sectionData, | ||
List<InputRecord> recordsList, | ||
Map<String, List<String>> testDataMap) { | ||
super(sectionData, recordsList, testDataMap); | ||
// TODO Auto-generated constructor stub | ||
} | ||
|
||
private String generateTestData(Map<String, List<String>> testData, boolean positive) { | ||
String content = StringUtils.repeat(" ", offset + 1) + "| " | ||
+ StringUtils.join(testData.keySet().iterator(), " | ") + " |" + ls; | ||
int count = testData.get(testData.keySet().iterator().next()).size(); | ||
for (int i = 0; i < count; i++) { | ||
if (testData.get("ValidInput").get(i).trim().equals("false") == positive) { | ||
continue; | ||
} | ||
content = content.concat(StringUtils.repeat(" ", offset + 1)); | ||
for (Entry<String, List<String>> entry : testData.entrySet()) { | ||
content = content.concat("| " + entry.getValue().get(i) + " "); | ||
} | ||
content = content.concat("|" + ls); | ||
} | ||
return content; | ||
} | ||
|
||
public boolean isPositive() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public String generate() throws Exception { | ||
String content = StringUtils.repeat(" ", offset) + "Scenario Outline: " | ||
+ this.getSection().getName() + this.getScenarioName() + ls; | ||
|
||
content += this.generatePreRequisites(this.getSection().getSections().get(Tokens.PREREQUISITES_TOKEN)); | ||
content += this.getSection().getSections().get(Tokens.ACTION_TOKEN).get(0).generate() + ls; | ||
if (isPositive()) { | ||
content += this.getSection().getSections().get(Tokens.VALID_OUTPUT_TOKEN).get(0).generate() + ls; | ||
} else { | ||
content += this.getSection().getSections().get(Tokens.ERROR_OUTPUT_TOKEN).get(0).generate() + ls; | ||
} | ||
content += StringUtils.repeat(" ", offset) | ||
+ "Examples:" + ls + this.generateTestData(this.getTestData(), isPositive()) + ls; | ||
return content; | ||
} | ||
|
||
@Override | ||
public boolean isApplicable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public String getScenarioName() { | ||
return " positive test"; | ||
} | ||
|
||
} |
155 changes: 155 additions & 0 deletions
155
...va/com/github/mkolisnyk/aerial/datagenerators/cases/UniqueValueCaseScenarioGenerator.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,155 @@ | ||
package com.github.mkolisnyk.aerial.datagenerators.cases; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.Map.Entry; | ||
|
||
import org.apache.commons.lang.ArrayUtils; | ||
import org.apache.commons.lang.StringUtils; | ||
|
||
import com.github.mkolisnyk.aerial.datagenerators.CaseScenarioGenerator; | ||
import com.github.mkolisnyk.aerial.document.CaseSection; | ||
import com.github.mkolisnyk.aerial.document.InputRecord; | ||
import com.github.mkolisnyk.aerial.document.Tokens; | ||
|
||
public class UniqueValueCaseScenarioGenerator extends | ||
CaseScenarioGenerator { | ||
private int offset = 1; | ||
private String ls = System.lineSeparator(); | ||
|
||
public UniqueValueCaseScenarioGenerator(CaseSection sectionData, | ||
List<InputRecord> recordsList, | ||
Map<String, List<String>> testDataMap) { | ||
super(sectionData, recordsList, testDataMap); | ||
} | ||
|
||
private String[] getFieldsWithUniqueAttributes( | ||
List<InputRecord> input) { | ||
Set<String> fields = new HashSet<String>(); | ||
String[] result; | ||
for (InputRecord record : input) { | ||
if (record.isUnique()) { | ||
fields.add(record.getName()); | ||
} | ||
} | ||
result = new String[fields.size()]; | ||
result = fields.toArray(result); | ||
return result; | ||
} | ||
|
||
Map<String, List<String>> filterBy( | ||
Map<String, List<String>> testData, String field, | ||
String value) { | ||
Map<String, List<String>> result = new HashMap<String, List<String>>(); | ||
for (String key : testData.keySet()) { | ||
result.put(key, new ArrayList<String>()); | ||
} | ||
int count = testData.get(testData.keySet().iterator().next()) | ||
.size(); | ||
for (int i = 0; i < count; i++) { | ||
if (testData.get(field).get(i).trim().equals(value)) { | ||
for (String key : testData.keySet()) { | ||
result.get(key).add(testData.get(key).get(i)); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private String getValueDifferentFrom(List<String> input, | ||
String value) { | ||
for (String item : input) { | ||
if (!item.equals(value)) { | ||
return item; | ||
} | ||
} | ||
return value; | ||
} | ||
|
||
private String generateUniqueScenarioData( | ||
Map<String, List<String>> testData, String[] uniqueFields) { | ||
Map<String, List<String>> filteredData = filterBy(testData, | ||
"ValidInput", "true"); | ||
Map<String, String> uniqueRow = new LinkedHashMap<String, String>(); | ||
for (Entry<String, List<String>> entry : filteredData | ||
.entrySet()) { | ||
String value = entry.getValue().get(0); | ||
uniqueRow.put(entry.getKey(), value); | ||
if (ArrayUtils.contains(uniqueFields, entry.getKey())) { | ||
uniqueRow.put("Modified " + entry.getKey(), value); | ||
} | ||
} | ||
String content = StringUtils.repeat(" ", offset + 1) | ||
+ "| " | ||
+ StringUtils.join(uniqueRow.keySet().iterator(), | ||
" | ") + " |" + ls; | ||
|
||
for (String field : uniqueFields) { | ||
content = content.concat(StringUtils.repeat(" ", | ||
offset + 1)); | ||
for (Entry<String, String> entry : uniqueRow.entrySet()) { | ||
String key = entry.getKey(); | ||
String value = entry.getValue(); | ||
if (key.startsWith("Modified ")) { | ||
String modField = key.replaceFirst("Modified ", | ||
""); | ||
if (!modField.equals(field)) { | ||
value = getValueDifferentFrom( | ||
filteredData.get(modField), value); | ||
} | ||
} else { | ||
if (!key.equalsIgnoreCase(field) | ||
&& !ArrayUtils | ||
.contains(uniqueFields, key)) { | ||
value = getValueDifferentFrom( | ||
filteredData.get(key), value); | ||
} | ||
} | ||
content = content.concat("| " + value.trim() + " "); | ||
} | ||
content = content.concat("|" + ls); | ||
} | ||
return content; | ||
} | ||
|
||
@Override | ||
public String generate() throws Exception { | ||
String[] uniqueRecords = getFieldsWithUniqueAttributes(this.getRecords()); | ||
String content = StringUtils.repeat(" ", offset) | ||
+ "Scenario Outline: " + this.getSection().getName() | ||
+ this.getScenarioName() + ls; | ||
content += this.generatePreRequisites(this.getSection().getSections().get(Tokens.PREREQUISITES_TOKEN)); | ||
content += this.getSection().getSections().get(Tokens.ACTION_TOKEN).get(0).generate() + ls; | ||
content += this.getSection().getSections().get(Tokens.VALID_OUTPUT_TOKEN).get(0).generate() + ls; | ||
String secondPass = this.getSection().getSections().get(Tokens.ACTION_TOKEN).get(0).generate() + ls | ||
+ this.getSection().getSections().get(Tokens.ERROR_OUTPUT_TOKEN).get(0).generate() + ls; | ||
for (String field : uniqueRecords) { | ||
secondPass = secondPass.replaceAll("<" + field + ">", | ||
"<Modified " + field + ">"); | ||
} | ||
content += secondPass; | ||
content += StringUtils.repeat(" ", offset) | ||
+ "Examples:" | ||
+ ls | ||
+ this.generateUniqueScenarioData(this.getTestData(), | ||
uniqueRecords) + ls; | ||
return content; | ||
} | ||
|
||
@Override | ||
public boolean isApplicable() { | ||
String[] uniqueRecords = getFieldsWithUniqueAttributes(this | ||
.getRecords()); | ||
return uniqueRecords.length > 0; | ||
} | ||
|
||
@Override | ||
public String getScenarioName() { | ||
return " unique values test"; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/github/mkolisnyk/aerial/datagenerators/cases/package-info.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,8 @@ | ||
/** | ||
* . | ||
*/ | ||
/** | ||
* @author Myk Kolisnyk | ||
* | ||
*/ | ||
package com.github.mkolisnyk.aerial.datagenerators.cases; |
Oops, something went wrong.