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

com.ecfeed.junit.message.MessageHelper Maven / Gradle / Ivy

Go to download

An open library used to connect to the ecFeed service. It can be also used as a standalone testing tool. It is integrated with Junit5 and generates a stream of test cases using a selected algorithm (e.g. Cartesian, N-Wise). There are no limitations associated with the off-line version but the user cannot access the on-line computation servers and the model database.

The newest version!
package com.ecfeed.junit.message;

import java.util.List;

import com.ecfeed.core.model.ChoiceNode;
import com.ecfeed.core.model.FixedChoiceValueFactory;
import com.ecfeed.core.genservice.util.GenServiceProtocolHelper;
import com.ecfeed.core.genservice.schema.ChoiceSchema;
import com.ecfeed.core.genservice.schema.RequestUpdateSchema;
import com.ecfeed.core.genservice.schema.ResultErrorSchema;
import com.ecfeed.core.genservice.schema.ResultInfoSchema;
import com.ecfeed.core.genservice.schema.ResultProgressSchema;
import com.ecfeed.core.genservice.schema.ResultStatusSchema;
import com.ecfeed.core.genservice.schema.ResultTestCaseSchema;
import com.ecfeed.core.genservice.schema.ResultTotalProgressSchema;
import com.ecfeed.junit.utils.Localization;
import com.ecfeed.junit.utils.Logger;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public final class MessageHelper {

	private static ObjectMapper fMapper = new ObjectMapper();
	private static FixedChoiceValueFactory fFactory = new FixedChoiceValueFactory(null, false);
	
	private MessageHelper() {
		RuntimeException exception = new RuntimeException(Localization.bundle.getString("classInitializationError"));
		Logger.exception(exception);
		throw exception;
	}
	
	public static String resultStartDataSchema(String sessionId, boolean collectStats) {
		ResultStatusSchema result = new ResultStatusSchema();
		result.setStatus(GenServiceProtocolHelper.TAG_BEG_DATA);
		result.setId(sessionId);
		result.setCollectStats(collectStats);

		try {
			return fMapper.writeValueAsString(result);
		} catch (JsonProcessingException e) {
			RuntimeException exception = new RuntimeException(Localization.bundle.getString("messageHelperStartData"), e);
			exception.addSuppressed(e);
			Logger.exception(exception);
			throw exception;
		}
	}
	
	public static String resultStartChunkSchema(String sessionId) {
		ResultStatusSchema result = new ResultStatusSchema();
		result.setStatus(GenServiceProtocolHelper.TAG_BEG_CHUNK);
		result.setId(sessionId);

		try {
			return fMapper.writeValueAsString(result);
		} catch (JsonProcessingException e) {
			RuntimeException exception = new RuntimeException(Localization.bundle.getString("messageHelperStartChunk"), e);
			exception.addSuppressed(e);
			Logger.exception(exception);
			throw exception;
		}
	}
	
	public static String resultEndDataSchema(String sessionId) {
		ResultStatusSchema result = new ResultStatusSchema();
		result.setStatus(GenServiceProtocolHelper.TAG_END_DATA);
		result.setId(sessionId);

		try {
			return fMapper.writeValueAsString(result);
		} catch (JsonProcessingException e) {
			RuntimeException exception = new RuntimeException(Localization.bundle.getString("messageHelperEndData"), e);
			exception.addSuppressed(e);
			Logger.exception(exception);
			throw exception;
		}
	}
	
	public static String resultEndChunkSchema() {
		ResultStatusSchema result = new ResultStatusSchema();
		result.setStatus(GenServiceProtocolHelper.TAG_END_CHUNK);

		try {
			return fMapper.writeValueAsString(result);
		} catch (JsonProcessingException e) {
			RuntimeException exception = new RuntimeException(Localization.bundle.getString("messageHelperEndChunk"), e);
			exception.addSuppressed(e);
			Logger.exception(exception);
			throw exception;
		}
	}
	
	public static String resultErrorSchema(String error) {
		ResultErrorSchema result = new ResultErrorSchema();
		result.setError(error);
		
		try {
			return fMapper.writeValueAsString(result);
		} catch (JsonProcessingException e) {
			RuntimeException exception = new RuntimeException(Localization.bundle.getString("messageHelperError"), e);
			exception.addSuppressed(e);
			Logger.exception(exception);
			throw exception;
		}
	}
	
	public static String resultInfoSchema(String info) {
		ResultInfoSchema result = new ResultInfoSchema();
		result.setInfo(info);
		
		try {
			return fMapper.writeValueAsString(result);
		} catch (JsonProcessingException e) {
			RuntimeException exception = new RuntimeException(Localization.bundle.getString("messageHelperError"), e);
			exception.addSuppressed(e);
			Logger.exception(exception);
			throw exception;
		}
	}
	
	public static String resultAcknowledgeSchema() {
		ResultStatusSchema result = new ResultStatusSchema();
		result.setStatus(GenServiceProtocolHelper.TAG_ACKNOWLEDGED);
		
		try {
			return fMapper.writeValueAsString(result);
		} catch (JsonProcessingException e) {
			RuntimeException exception = new RuntimeException(Localization.bundle.getString("messageHelperAcknowledge"), e);
			exception.addSuppressed(e);
			Logger.exception(exception);
			throw exception;
		}
	}
	
	public static String resultTotalProgressSchema(int totalProgress) {
		ResultTotalProgressSchema result = new ResultTotalProgressSchema();
		result.setTotalProgress(totalProgress);
		
		try {
			return fMapper.writeValueAsString(result);
		} catch (JsonProcessingException e) {
			RuntimeException exception = new RuntimeException(Localization.bundle.getString("messageHelperTotalProgress"), e);
			exception.addSuppressed(e);
			Logger.exception(exception);
			throw exception;
		}
	}
	
	public static String resultProgressSchema(int progress) {
		ResultProgressSchema result = new ResultProgressSchema();
		result.setProgress(progress);
		
		try {
			return fMapper.writeValueAsString(result);
		} catch (JsonProcessingException e) {
			RuntimeException exception = new RuntimeException(Localization.bundle.getString("messageHelperProgress"), e);
			exception.addSuppressed(e);
			Logger.exception(exception);
			throw exception;
		}
	}
	
	public static String resultUpdateSchema() {
		RequestUpdateSchema result = new RequestUpdateSchema();
		
		try {
			return fMapper.writeValueAsString(result);
		} catch (JsonProcessingException e) {
			RuntimeException exception = new RuntimeException(Localization.bundle.getString("messageHelperUpdate"), e);
			exception.addSuppressed(e);
			Logger.exception(exception);
			throw exception;
		}
	}
	
	public static String resultTestSchema(List tuple, String parameterId) {
		ResultTestCaseSchema result = new ResultTestCaseSchema();
		
		ChoiceSchema[] parameter = new ChoiceSchema[tuple.size()];
		
		for (int i = 0 ; i < tuple.size() ; i++) {
			ChoiceNode choiceNode = tuple.get(i);
			
			parameter[i] = new ChoiceSchema();
			parameter[i].setName(choiceNode.getFullName());
			
			if (choiceNode.isRandomizedValue()) {
				parameter[i].setValue(fFactory.createValue(choiceNode) + "");
			} else {
				parameter[i].setValue(choiceNode.getValueString());
			}
			
		}
		
		result.setTestCase(parameter);
		result.setId(parameterId);
		
		try {
			return fMapper.writeValueAsString(result);
		} catch (JsonProcessingException e) {
			RuntimeException exception = new RuntimeException(Localization.bundle.getString("messageHelperTest"), e);
			exception.addSuppressed(e);
			Logger.exception(exception);
			throw exception;
		}
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy