All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.bogdanlivadariu.reporting.cucumber.helpers.Helpers Maven / Gradle / Ivy

There is a newer version: 2.2.1
Show newest version
package com.github.bogdanlivadariu.reporting.cucumber.helpers;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.UUID;

import org.joda.time.Period;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;

import com.github.bogdanlivadariu.reporting.cucumber.json.models.Row;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Options;

public class Helpers {
    private Handlebars handlebar;

    public Helpers(Handlebars handlebar) {
        this.handlebar = handlebar;
    }

    public Handlebars registerHelpers() {
        handlebar.registerHelper("date", dateHelper());
        handlebar.registerHelper("embedding", embeddingHelper());
        handlebar.registerHelper("result-color", resultColorHelper());
        handlebar.registerHelper("resolve-title", resolveTitleHelper());
        handlebar.registerHelper("is-collapsed", isCollapsedHelper());
        handlebar.registerHelper("now", nowHelper());
        handlebar.registerHelper("do_table", doTableHelper());
        return handlebar;
    }

    private Helper dateHelper() {
        return new Helper() {
            public CharSequence apply(Long arg0, Options arg1) throws IOException {
                PeriodFormatter formatter = new PeriodFormatterBuilder()
                    .appendDays()
                    .appendSuffix(" d : ")
                    .appendHours()
                    .appendSuffix(" h : ")
                    .appendMinutes()
                    .appendSuffix(" m : ")
                    .appendSeconds()
                    .appendSuffix(" s : ")
                    .appendMillis()
                    .appendSuffix(" ms")
                    .toFormatter();
                return formatter.print(new Period((arg0 * 1) / 1000000));
            }
        };
    }

    private Helper embeddingHelper() {
        return new Helper() {
            @Override
            public CharSequence apply(String arg0, Options arg1) throws IOException {
                String toReturn;
                String id = UUID.randomUUID().toString();
                int index = arg1.param(1);
                if (arg1.param(0).toString().contains("image")) {
                    toReturn =
                        "";
                    toReturn +=
                        "
" + " " + "
"; } else { toReturn = "
" + arg0 + "
"; } return toReturn; } }; } private Helper resultColorHelper() { return new Helper() { @Override public CharSequence apply(String arg0, Options arg1) throws IOException { return checkState( arg0.toLowerCase(), Constants.INFO, Constants.SUCCESS, Constants.DANGER, Constants.WARNING); } }; } private Helper resolveTitleHelper() { return new Helper() { @Override public CharSequence apply(String arg0, Options arg1) throws IOException { return checkState( arg0.toLowerCase(), Constants.THIS_STEP_HAS_BEEN_SKIPPED, Constants.THIS_STEP_HAS_PASSED, Constants.THIS_STEP_HAS_FAILED, Constants.THIS_STEP_HAS_NOT_BEEN_DEFINED); } }; } private Helper isCollapsedHelper() { return new Helper() { @Override public CharSequence apply(String arg0, Options arg1) throws IOException { return checkState( arg0.toLowerCase(), Constants.COLLAPSE_IN, Constants.COLLAPSE, Constants.COLLAPSE_IN, Constants.COLLAPSE_IN); } }; } private Helper nowHelper() { return new Helper() { @Override public CharSequence apply(Object context, Options options) throws IOException { Calendar cal = Calendar.getInstance(); Date date = cal.getTime(); String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").format(date); TimeZone tz = cal.getTimeZone(); return now + " " + tz.getID(); } }; } private Helper> doTableHelper() { return new Helper>() { @Override public CharSequence apply(List rows, Options arg1) throws IOException { String tableContent = ""; int indexRow = 0; for (Row row : rows) { indexRow++; if (indexRow == 1) { tableContent += ""; } else if (indexRow == 2) { tableContent += ""; } else { tableContent += ""; } for (String cell : row.getCells()) { if (indexRow == 1) { tableContent += ""; } else { tableContent += ""; } } if (indexRow == 1) { tableContent += ""; } else { tableContent += ""; } } tableContent += "
" + cell + "" + cell + "
"; return tableContent; } }; } private CharSequence checkState(String arg0, String retValue1, String retValue2, String retValue3, String retValue4) { switch (arg0.toLowerCase()) { case Constants.SKIPPED: return retValue1; case Constants.PASSED: return retValue2; case Constants.FAILED: return retValue3; case Constants.UNDEFINED: return retValue4; default: } return Constants.UNDEFINED; } }