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

com.automationrockstars.bmo.GunterStoryReporter Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2015, 2016 Automation RockStars Ltd.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     Automation RockStars - initial API and implementation
 *******************************************************************************/
package com.automationrockstars.bmo;

import static com.automationrockstars.base.ConfigLoader.config;
import static com.automationrockstars.gunter.events.EventBus.fireEvent;
import static com.automationrockstars.gunter.events.EventFactory.createAttachment;
import static com.automationrockstars.gunter.events.EventFactory.createExecutionFinish;
import static com.automationrockstars.gunter.events.EventFactory.createExecutionStart;
import static com.automationrockstars.gunter.events.EventFactory.createSuiteFinish;
import static com.automationrockstars.gunter.events.EventFactory.createSuiteStart;
import static com.automationrockstars.gunter.events.EventFactory.createTestCaseFinish;
import static com.automationrockstars.gunter.events.EventFactory.createTestCaseStart;
import static com.automationrockstars.gunter.events.EventFactory.createTestStepFinish;
import static com.automationrockstars.gunter.events.EventFactory.createTestStepStart;
import static com.automationrockstars.gunter.events.EventFactory.toJson;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import com.automationrockstars.gunter.events.TestCaseStart;
import com.automationrockstars.gunter.events.TestExecutionStart;
import com.automationrockstars.gunter.events.TestStepFinish;
import com.automationrockstars.gunter.events.TestStepStart;
import com.automationrockstars.gunter.events.TestSuiteStart;
import com.automationrockstars.gunter.events.impl.TestStepFinishImpl;
import com.automationrockstars.gunter.rabbit.RabbitEventBroker;
public class GunterStoryReporter implements StoryReporter{

	private static final AtomicBoolean isConnected = new AtomicBoolean(false);
	@Override
	public String name() {
		return "Guner Story Reporter";
	}

	private static final ThreadLocal start = new InheritableThreadLocal<>();

	
	@Override
	public void start() {
		try {
			RabbitEventBroker.init();
			isConnected.set(true);
		} catch (IllegalStateException e){
			isConnected.set(false);
		}
		if (isConnected.get()){
			start.set(createExecutionStart(config().getString("execution.name",config().getString("bdd.story.filter"))));
			fireEvent(toJson(start.get()));
		}
	}

	@Override
	public void finish() {
		if (isConnected.get()){
			fireEvent(toJson(createExecutionFinish(start.get(), "FINISHED")));
		}

	}

	private static final ThreadLocal story = new InheritableThreadLocal<>();
	@Override
	public void beforeStory(String name, String description, String path) {
		if (isConnected.get()){
			story.set(createSuiteStart(start.get(), name));
			fireEvent(toJson(story.get()));
		}
	}

	@Override
	public void afterStory() {
		if (isConnected.get()){
			fireEvent(toJson(createSuiteFinish(story.get(), "FINISHED")));
		}
	}

	private static final ThreadLocal scenario = new InheritableThreadLocal<>();
	@Override
	public void beforeScenario(String scenarioTitle) {
		if (isConnected.get()){
			scenario.set(createTestCaseStart(story.get(), scenarioTitle));
			fireEvent(toJson(scenario.get()));
		}
	}

	@Override
	public void afterScenario() {
		if (isConnected.get()){
			fireEvent(toJson(createTestCaseFinish(scenario.get(), "FINISHED")));
		}

	}

	private static final ThreadLocal> example =new InheritableThreadLocal<>();
	@Override
	public void example(Map tableRow) {
		example.set(tableRow);

	}

	private static final ThreadLocal step = new InheritableThreadLocal<>();
	@Override
	public void beforeStep(String stepName) {
		if (isConnected.get()){
			step.set(createTestStepStart(scenario.get(), stepName));
			fireEvent(toJson(step.get()));
		}
	}

	@Override
	public void successful(String stepName) {
		if (isConnected.get()){
			fireEvent(toJson(createTestStepFinish(step.get(), "SUCCESS")));
		}
	}

	@Override
	public void ignorable(String stepName) {
		if (isConnected.get()){
			fireEvent(toJson(createTestStepFinish(step.get(), "IGNORED")));
		}
	}

	@Override
	public void pending(String stepName) {
		if (isConnected.get()){
			fireEvent(toJson(createTestStepFinish(step.get(), "PENDING")));
		}
	}

	@Override
	public void notPerformed(String stepName) {
		if (isConnected.get()){
			fireEvent(toJson(createTestStepFinish(step.get(), "NOT PERFORMED")));
		}

	}

	@Override
	public void failed(String stepName, Throwable cause) {
		if (isConnected.get()){
			TestStepFinish finish = createTestStepFinish(step.get(), "FAILED");
			if (finish instanceof TestStepFinishImpl ){
				ByteArrayOutputStream content = new ByteArrayOutputStream();
				PrintStream print = new PrintStream(content);
				cause.printStackTrace(print);
				String throwable = content.toString();
				try {
					content.close();
					print.close();
				} catch (IOException e) {
				}

				((TestStepFinishImpl)finish).attributes().put("throwable", throwable);
			}
			fireEvent(toJson(finish));
		}
	}

	@Override
	public void attach(byte[] attachment, String title, String mimeType) {
		if (isConnected.get()){
			fireEvent(toJson(createAttachment(step.get(), mimeType, title, attachment)));
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy