Skip to content

Commit

Permalink
Render JS using the velocity engine
Browse files Browse the repository at this point in the history
It is preferable to render js through the template engine over rendering it at in java. This ensures that any transformations happen as late as possible
  • Loading branch information
mpkorstanje committed Jan 4, 2017
1 parent a88da85 commit 3ecb043
Show file tree
Hide file tree
Showing 16 changed files with 124 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import net.masterthought.cucumber.Configuration;
import net.masterthought.cucumber.ReportResult;
import net.masterthought.cucumber.json.support.Status;
Expand Down Expand Up @@ -38,14 +36,14 @@ public void prepareReport() {
context.put("chart_data", generateTagValues(filterExcludedTags(tags)));
}

static String generateTagLabels(List<TagObject> tagsObjectList) {
static String[] generateTagLabels(List<TagObject> tagsObjectList) {
int tagCount = tagsObjectList.size();
String[] tagNames = new String[tagCount];

for (int i = 0; i < tagCount; i++) {
tagNames[i] = StringUtils.wrap(tagsObjectList.get(i).getName(), "\"");
tagNames[i] = tagsObjectList.get(i).getName();
}
return "[" + StringUtils.join(tagNames, ",") + "]";
return tagNames;
}

private List<TagObject> filterExcludedTags(List<TagObject> tagsObjectList) {
Expand All @@ -68,7 +66,7 @@ private boolean shouldIncludeTag(String tagName) {
return true;
}

static List<String> generateTagValues(List<TagObject> tagsObjectList) {
static String[][] generateTagValues(List<TagObject> tagsObjectList) {
int tagsCount = tagsObjectList.size();
String[][] values = new String[Status.values().length][tagsCount];
for (int i = 0; i < tagsCount; i++) {
Expand All @@ -81,11 +79,6 @@ static List<String> generateTagValues(List<TagObject> tagsObjectList) {
values[4][i] = Util.formatAsDecimal(tagObject.getUndefinedSteps(), allSteps);
}

List<String> statuses = new ArrayList<>();
for (int i = 0; i < Status.values().length; i++) {
statuses.add("[" + StringUtils.join(values[i], ", ") + "]");
}

return statuses;
return values;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.masterthought.cucumber.generators;

import org.apache.commons.lang3.StringUtils;

import net.masterthought.cucumber.Configuration;
import net.masterthought.cucumber.ReportResult;
import net.masterthought.cucumber.Trends;
Expand All @@ -27,37 +25,20 @@ public String getWebPage() {

@Override
public void prepareReport() {
context.put("buildNumbers", toJavaScriptArray(trends.getBuildNumbers()));

context.put("failedFeatures", toJavaScriptArray(trends.getFailedFeatures()));
context.put("totalFeatures", toJavaScriptArray(trends.getTotalFeatures()));
context.put("failedScenarios", toJavaScriptArray(trends.getFailedScenarios()));
context.put("totalScenarios", toJavaScriptArray(trends.getTotalScenarios()));
context.put("buildNumbers", trends.getBuildNumbers());

context.put("passedSteps", toJavaScriptArray(trends.getPassedSteps()));
context.put("failedSteps", toJavaScriptArray(trends.getFailedSteps()));
context.put("skippedSteps", toJavaScriptArray(trends.getSkippedSteps()));
context.put("pendingSteps", toJavaScriptArray(trends.getPendingSteps()));
context.put("undefinedSteps", toJavaScriptArray(trends.getUndefinedSteps()));

context.put("durations", toJavaScriptArray(trends.getDurations()));
}
context.put("failedFeatures", trends.getFailedFeatures());
context.put("totalFeatures", trends.getTotalFeatures());
context.put("failedScenarios", trends.getFailedScenarios());
context.put("totalScenarios", trends.getTotalScenarios());

private static String toJavaScriptArray(String[] array) {
int itemCount = array.length;
String[] names = new String[itemCount];
context.put("passedSteps", trends.getPassedSteps());
context.put("failedSteps", trends.getFailedSteps());
context.put("skippedSteps", trends.getSkippedSteps());
context.put("pendingSteps", trends.getPendingSteps());
context.put("undefinedSteps", trends.getUndefinedSteps());

for (int i = 0; i < itemCount; i++) {
names[i] = StringUtils.wrap(array[i], "\"");
}
return "[" + StringUtils.join(names, ",") + "]";
context.put("durations", trends.getDurations());
}

private static String toJavaScriptArray(int[] array) {
return "[" + StringUtils.join(array, ',') + "]";
}

private static String toJavaScriptArray(long[] array) {
return "[" + StringUtils.join(array, ',') + "]";
}
}
6 changes: 3 additions & 3 deletions src/main/resources/templates/generators/overviewFeatures.vm
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#includeTitle("Features Overview")

<script type="text/javascript">
#parse("/templates/js/steps-chart.js")
#parse("/templates/js/scenarios-chart.js")
#parse("/templates/js/features-chart.js")
#parse("/templates/js/steps-chart.js.vm")
#parse("/templates/js/scenarios-chart.js.vm")
#parse("/templates/js/features-chart.js.vm")
</script>
</head>

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/generators/overviewTags.vm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#includeTitle("Tags Overview")

<script type="text/javascript">
#parse("/templates/js/tags-chart.js")
#parse("/templates/js/tags-chart.js.vm")
</script>
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/generators/overviewTrends.vm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#includeTitle("Trends Overview")

<script type="text/javascript">
#parse("/templates/js/trends-chart.js")
#parse("/templates/js/trends-chart.js.vm")
</script>
</head>
<body>
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/templates/headers.vm
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

#include("/templates/head.vm")

#parse("/templates/macros/array.js.vm")

#parse("/templates/macros/page/buildinfo.vm")
#parse("/templates/macros/page/classifications.vm")
#parse("/templates/macros/page/lead.vm")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
$(document).ready(function() {

var chartData = {
labels: $chart_categories,
labels: #stringArray($chart_categories),
datasets: [
{
label: "Passed",
backgroundColor: "#92DD96",
data: $chart_data.get(0)
data: #numberArray($chart_data[0])
},
{
label: "Failed",
backgroundColor: "#F2928C",
data: $chart_data.get(1)
data: #numberArray($chart_data[1])
},
{
label: "Skipped",
backgroundColor: "#8AF",
data: $chart_data.get(2)
data: #numberArray($chart_data[2])
},
{
label: "Pending",
backgroundColor: "#F5F28F",
data: $chart_data.get(3)
data: #numberArray($chart_data[3])
},
{
label: "Undefined",
backgroundColor: "#F5B975",
data: $chart_data.get(4)
data: #numberArray($chart_data[4])
}
]
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
$(document).ready(function() {

var buildNumbers = $buildNumbers;
var buildNumbers = #stringArray($buildNumbers);

var failedFeatures = $failedFeatures;
var totalFeatures = $totalFeatures;
var failedFeatures = #numberArray($failedFeatures);
var totalFeatures = #numberArray($totalFeatures);

var failedScenarios = $failedScenarios;
var totalScenarios = $totalScenarios;
var failedScenarios = #numberArray($failedScenarios);
var totalScenarios = #numberArray($totalScenarios);

var passedSteps = $passedSteps;
var failedSteps = $failedSteps;
var skippedSteps = $skippedSteps;
var pendingSteps = $pendingSteps;
var undefinedSteps = $undefinedSteps;
var passedSteps = #numberArray($passedSteps);
var failedSteps = #numberArray($failedSteps);
var skippedSteps = #numberArray($skippedSteps);
var pendingSteps = #numberArray($pendingSteps);
var undefinedSteps = #numberArray($undefinedSteps);

var durations = $durations;
var durations = #numberArray($durations);



Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/templates/macros/array.js.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#macro(stringArray $array) [#foreach($element in $array) "$element", #end] #end
#macro(numberArray $array) [#foreach($element in $array) $element, #end] #end
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ public void prepareReport_setTagsToExcludeFromChart_ReturnsFilteredTags() {

// then
VelocityContext context = page.context;
assertThat(context.get("chart_categories")).isEqualTo("[\"@fast\"]");
assertThat(context.get("chart_data")).isEqualTo(asList(
"[100.00]",
"[0.00]",
"[0.00]",
"[0.00]",
"[0.00]"));
assertThat(context.get("chart_categories")).isEqualTo(new String[]{"@fast"});
assertThat(context.get("chart_data")).isEqualTo(new String[][]{
{"100.00"},
{"0.00"},
{"0.00"},
{"0.00"},
{"0.00"}
});
}

@Test
Expand All @@ -82,10 +83,10 @@ public void generateTagLabels_ReturnsTags() {
List<TagObject> allTags = this.tags;

// when
String labels = TagsOverviewPage.generateTagLabels(allTags);
String[] labels = TagsOverviewPage.generateTagLabels(allTags);

// then
assertThat(labels).isEqualTo("[\"@checkout\",\"@fast\",\"@featureTag\"]");
assertThat(labels).isEqualTo(new String[]{"@checkout","@fast","@featureTag"});
}

@Test
Expand All @@ -95,14 +96,15 @@ public void generateTagValues_ReturnsTagValues() {
List<TagObject> allTags = this.tags;

// when
List<String> labels = TagsOverviewPage.generateTagValues(allTags);
String[][] labels = TagsOverviewPage.generateTagValues(allTags);

// then
assertThat(labels).containsExactly(
"[62.50, 100.00, 100.00]",
"[6.25, 0.00, 0.00]",
"[12.50, 0.00, 0.00]",
"[6.25, 0.00, 0.00]",
"[12.50, 0.00, 0.00]");
assertThat(labels).isEqualTo(new String[][]{
{"62.50", "100.00", "100.00"},
{"6.25", "0.00", "0.00"},
{"12.50", "0.00", "0.00"},
{"6.25", "0.00", "0.00"},
{"12.50", "0.00", "0.00"}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,61 +66,19 @@ public void prepareReport_AddsCustomProperties() {
VelocityContext context = page.context;
assertThat(context.getKeys()).hasSize(18);

assertThat(context.get("buildNumbers")).isEqualTo("[\"01_first\",\"other build\",\"05last\"]");
assertThat(context.get("failedFeatures")).isEqualTo("[1,2,5]");
assertThat(context.get("totalFeatures")).isEqualTo("[10,20,30]");
assertThat(context.get("buildNumbers")).isEqualTo(new String[]{"01_first","other build","05last"});
assertThat(context.get("failedFeatures")).isEqualTo(new int[]{1,2,5});
assertThat(context.get("totalFeatures")).isEqualTo(new int[]{10,20,30});

assertThat(context.get("failedScenarios")).isEqualTo("[10,20,20]");
assertThat(context.get("totalScenarios")).isEqualTo("[10,2,5]");
assertThat(context.get("failedScenarios")).isEqualTo(new int[]{10,20,20});
assertThat(context.get("totalScenarios")).isEqualTo(new int[]{10,2,5});

assertThat(context.get("passedSteps")).isEqualTo("[1,3,5]");
assertThat(context.get("failedSteps")).isEqualTo("[10,30,50]");
assertThat(context.get("skippedSteps")).isEqualTo("[100,300,500]");
assertThat(context.get("pendingSteps")).isEqualTo("[1000,3000,5000]");
assertThat(context.get("undefinedSteps")).isEqualTo("[10000,30000,50000]");
assertThat(context.get("passedSteps")).isEqualTo(new int[]{1,3,5});
assertThat(context.get("failedSteps")).isEqualTo(new int[]{10,30,50});
assertThat(context.get("skippedSteps")).isEqualTo(new int[]{100,300,500});
assertThat(context.get("pendingSteps")).isEqualTo(new int[]{1000,3000,5000});
assertThat(context.get("undefinedSteps")).isEqualTo(new int[]{10000,30000,50000});

assertThat(context.get("durations")).isEqualTo("[3206126182398,3206126182399,3206126182310]");
}

@Test
public void toJavaScriptArray_ReturnsStringArraysAsString() {

// given
final Object toConvert = new String[]{"1", "2", "5"};

// when
Class<?>[] types = {String[].class};
String converted = Deencapsulation.invoke(TrendsOverviewPage.class, "toJavaScriptArray", types, toConvert);

// then
assertThat(converted).isEqualTo("[\"1\",\"2\",\"5\"]");
}

@Test
public void toJavaScriptArray_ReturnsIntArraysAsString() {

// given
final Object toConvert = new int[]{10, 20, 50};

// when
Class<?>[] types = {int[].class};
String converted = Deencapsulation.invoke(TrendsOverviewPage.class, "toJavaScriptArray", types, toConvert);

// then
assertThat(converted).isEqualTo("[10,20,50]");
}

@Test
public void toJavaScriptArray_ReturnsLongArraysAsString() {

// given
final Object toConvert = new long[]{10, 20, 50};

// when
Class<?>[] types = {long[].class};
String converted = Deencapsulation.invoke(TrendsOverviewPage.class, "toJavaScriptArray", types, toConvert);

// then
assertThat(converted).isEqualTo("[10,20,50]");
assertThat(context.get("durations")).isEqualTo(new long[]{3206126182398L, 3206126182399L, 3206126182310L});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ public void generatePage_generatesCharts() {
assertThat(document.byId("charts", WebAssertion.class)).isNotNull();
}

@Test
public void generatePage_insertsChartData() {

// given
setUpWithJson(SAMPLE_JSON);
page = new TagsOverviewPage(reportResult, configuration);

// when
page.generatePage();

// then
DocumentAssertion document = documentFrom(page.getWebPage());

// check that data used by the charts is correctly inserted into the script section

assertThat(document.html()).contains("labels: [ \"@checkout\", \"@fast\", \"@featureTag\", ]");
assertThat(document.html()).contains("data: [ 62.50, 100.00, 100.00, ]");
assertThat(document.html()).contains("data: [ 6.25, 0.00, 0.00, ]");
assertThat(document.html()).contains("data: [ 12.50, 0.00, 0.00, ]");
assertThat(document.html()).contains("data: [ 6.25, 0.00, 0.00, ]");
assertThat(document.html()).contains("data: [ 12.50, 0.00, 0.00, ]");

}

@Test
public void generatePage_generatesStatsTableHeader() {

Expand Down
Loading

0 comments on commit 3ecb043

Please sign in to comment.