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

gherkin.formatter.JSONFormatter Maven / Gradle / Ivy

There is a newer version: 2.12.2
Show newest version
package gherkin.formatter;

import gherkin.deps.com.google.gson.Gson;
import gherkin.deps.com.google.gson.GsonBuilder;
import gherkin.deps.net.iharder.Base64;
import gherkin.formatter.model.Background;
import gherkin.formatter.model.Examples;
import gherkin.formatter.model.Feature;
import gherkin.formatter.model.Match;
import gherkin.formatter.model.Result;
import gherkin.formatter.model.Scenario;
import gherkin.formatter.model.ScenarioOutline;
import gherkin.formatter.model.Step;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JSONFormatter implements Reporter, Formatter {
    private final List> featureMaps = new ArrayList>();
    private final NiceAppendable out;

    private Map featureMap;
    private String uri;
    private Map currentStepOrHook;

    public JSONFormatter(Appendable out) {
        this.out = new NiceAppendable(out);
    }

    @Override
    public void uri(String uri) {
        this.uri = uri;
    }

    @Override
    public void feature(Feature feature) {
        featureMap = feature.toMap();
        featureMap.put("uri", uri);
        featureMaps.add(featureMap);
    }

    @Override
    public void background(Background background) {
        getFeatureElements().add(background.toMap());
    }

    @Override
    public void scenario(Scenario scenario) {
        getFeatureElements().add(scenario.toMap());
    }

    @Override
    public void scenarioOutline(ScenarioOutline scenarioOutline) {
        getFeatureElements().add(scenarioOutline.toMap());
    }

    @Override
    public void examples(Examples examples) {
        getAllExamples().add(examples.toMap());
    }

    @Override
    public void step(Step step) {
        currentStepOrHook = step.toMap();
        getSteps().add(currentStepOrHook);
    }

    @Override
    public void match(Match match) {
        currentStepOrHook.put("match", match.toMap());
    }

    @Override
    public void embedding(String mimeType, byte[] data) {
        final Map embedding = new HashMap();
        embedding.put("mime_type", mimeType);
        embedding.put("data", Base64.encodeBytes(data));
        getEmbeddings().add(embedding);
    }

    @Override
    public void write(String text) {
        getOutput().add(text);
    }

    @Override
    public void result(Result result) {
        currentStepOrHook.put("result", result.toMap());
    }

    @Override
    public void before(Match match, Result result) {
        addHook(match, result, "before");
    }

    @Override
    public void after(Match match, Result result) {
        addHook(match, result, "after");
    }

    private void addHook(final Match match, final Result result, String hook) {
        List hooks = getFeatureElement().get(hook);
        if (hooks == null) {
            hooks = new ArrayList();
            getFeatureElement().put(hook, hooks);
        }
        Map hookMap = new HashMap();
        hookMap.put("match", match.toMap());
        hookMap.put("result", result.toMap());
        hooks.add(hookMap);
    }

    @Override
    public void eof() {
    }

    @Override
    public void done() {
        out.append(gson().toJson(featureMaps));
        // We're *not* closing the stream here.
        // https://github.com/cucumber/gherkin/issues/151
        // https://github.com/cucumber/cucumber-jvm/issues/96
    }

    @Override
    public void close() {
        out.close();
    }

    @Override
    public void syntaxError(String state, String event, List legalEvents, String uri, Integer line) {
        throw new UnsupportedOperationException();
    }

    private List> getFeatureElements() {
        List> featureElements = (List) featureMap.get("elements");
        if (featureElements == null) {
            featureElements = new ArrayList>();
            featureMap.put("elements", featureElements);
        }
        return featureElements;
    }

    private Map> getFeatureElement() {
        return (Map) getFeatureElements().get(getFeatureElements().size() - 1);
    }

    private List getAllExamples() {
        List allExamples = getFeatureElement().get("examples");
        if (allExamples == null) {
            allExamples = new ArrayList();
            getFeatureElement().put("examples", allExamples);
        }
        return allExamples;
    }

    private List getSteps() {
        List steps = getFeatureElement().get("steps");
        if (steps == null) {
            steps = new ArrayList();
            getFeatureElement().put("steps", steps);
        }
        return steps;
    }

    private List> getEmbeddings() {
        List> embeddings = (List>) currentStepOrHook.get("embeddings");
        if (embeddings == null) {
            embeddings = new ArrayList>();
            currentStepOrHook.put("embeddings", embeddings);
        }
        return embeddings;
    }

    private List getOutput() {
        List output = (List) currentStepOrHook.get("output");
        if (output == null) {
            output = new ArrayList();
            currentStepOrHook.put("output", output);
        }
        return output;
    }

    protected Gson gson() {
        return new GsonBuilder().create();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy