Skip to content

Commit

Permalink
#6 Single unique value scenario was implemented and tested
Browse files Browse the repository at this point in the history
  • Loading branch information
mkolisnyk committed Jan 13, 2015
1 parent 9c189f4 commit 9fcd2dd
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
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.Map.Entry;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.junit.Assert;

Expand Down Expand Up @@ -75,14 +77,59 @@ private String generateTestData(Map<String, List<String>> testData, boolean posi
return content;
}

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,
List<InputRecord> input,
String[] uniqueFields) {
System.out.println(testData.size());
System.out.println(input.size());
System.out.println(uniqueFields.length);
return "";
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 value = entry.getValue();
if (!entry.getKey().equalsIgnoreCase("Modified " + field) &&
!entry.getKey().equalsIgnoreCase(field)) {
value = getValueDifferentFrom(filteredData.get(entry.getKey()), value);
}
content = content.concat("| " + value.trim() + " ");
}
content = content.concat("|" + ls);
}
return content;
}

private String[] getFieldsWithUniqueAttributes(List<InputRecord> input) {
Expand Down Expand Up @@ -137,10 +184,14 @@ public String generate() throws Exception {

content += this.getSections().get(Tokens.ACTION_TOKEN).get(0).generate() + ls;
content += this.getSections().get(Tokens.VALID_OUTPUT_TOKEN).get(0).generate() + ls;
content += this.getSections().get(Tokens.ACTION_TOKEN).get(0).generate() + ls;
content += this.getSections().get(Tokens.ERROR_OUTPUT_TOKEN).get(0).generate() + ls;
String secondPass = this.getSections().get(Tokens.ACTION_TOKEN).get(0).generate() + ls
+ this.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(testData, input.getInputs(), uniqueRecords) + ls;
+ ls + this.generateUniqueScenarioData(testData, uniqueRecords) + ls;
}
return content;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,36 @@ public void testGenerateShouldReturnValidFormattedText() throws Exception {
String actual = section.generate();
Assert.assertEquals(expected, actual);
}

@Test
public void testGenerateWithUniqueFieldsShouldReturnUniqueValueScenario() throws Exception {
String shortOffset = StringUtils.repeat(" ", 1);
String midOffset = StringUtils.repeat(" ", 2);
String sampleUniqueCaseText = sampleCaseDescription + ls
+ "Action:" + ls
+ "I test unique <Unique> value with <Non-Unique> value" + ls
+ "Input:" + ls
+ "| Name | Type | Value | Unique |" + ls
+ "| Unique | int | [0;100) | true |" + ls
+ "| Non-Unique | Date | [01-01-2000;02-10-2010], Format: dd-MM-yyyy | false |" + ls
+ "On Success:" + ls
+ sampleCaseValidOutput + ls
+ "On Failure:" + ls
+ sampleCaseErrorOutput + ls
+ "Pre-requisites:" + ls
+ samplePrerequisites + ls;
String expected = ls
+ midOffset + "Given These are our pre-requisites" + ls
+ midOffset + "When I test unique <Unique> value with <Non-Unique> value" + ls
+ midOffset + "Then This is what we see on success" + ls
+ midOffset + "When I test unique <Modified Unique> value with <Non-Unique> value" + ls
+ midOffset + "Then This is what we see on error" + ls
+ shortOffset + "Examples:" + ls
+ midOffset + "| Unique | Modified Unique | ValidInput | Non-Unique |" + ls
+ midOffset + "| 50 | 50 | true | 18-05-2005 |" + ls
+ ls;
section.parse(sampleUniqueCaseText);
String actual = section.generate();
Assert.assertEquals(expected, actual.split("Scenario Outline: Sample Name unique values test")[1]);
}
}

0 comments on commit 9fcd2dd

Please sign in to comment.