package com.github.cqljmeter.sampler;

/*
 * #%L
 * CqlJmeter
 * %%
 * Copyright (C) 2014 Mikhail Stepura
 * %%
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * #L%
 */

import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.testbeans.TestBean;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

import com.datastax.driver.core.ConsistencyLevel;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.Statement;
import com.github.cqljmeter.config.ClusterHolder;

public abstract class AbstractCqlSampler extends AbstractSampler implements TestBean {
	private static final long serialVersionUID = -996507992186021290L;
	private static final Logger log = LoggingManager.getLoggerForClass();

	private String query = "";
	private String keySpace = "";
	private String clusterId = "";
	private String consistency = null;

	public SampleResult sample(Entry arg0) {
		SampleResult result = new SampleResult();
		result.setDataType(SampleResult.TEXT);
		result.setContentType("text/plain");
		result.setSampleLabel(getName());
		result.setSamplerData(query);

		// Assume we will be successful
		result.setSuccessful(true);
		result.setResponseMessageOK();
		result.setResponseCodeOK();

		Statement statement = configure(getStatement());
		result.sampleStart();
		try {
			ResultSet data = getSession(keySpace).execute(statement);
			result.setResponseData(data.toString().getBytes());
			result.setResponseMessage(data.toString());
		} catch (Exception ex) {
			log.error(String.format("Error executing CQL statement [%s]", getQuery()), ex);
			result.setResponseMessage(ex.toString());
			result.setResponseData(ex.getMessage().getBytes());
			result.setSuccessful(false);
		} 
		result.sampleEnd();
		return result;
	}

	private Statement configure(Statement statement) {
		if (StringUtils.isNotBlank(consistency)) {
			statement.setConsistencyLevel(ConsistencyLevel.valueOf(consistency));
		}
		return statement;
	}

	protected abstract Statement getStatement();

	private Session getSession(String input) {
		return ClusterHolder.getSesssion(getClusterId(), input);
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public String getKeySpace() {
		return keySpace;
	}

	public void setKeySpace(String keySpace) {
		this.keySpace = keySpace;
	}

	public String getClusterId() {
		return clusterId;
	}

	public void setClusterId(String clusterId) {
		this.clusterId = clusterId;
	}

	public String getConsistency() {
		return consistency;
	}

	public void setConsistency(String consistency) {
		this.consistency = consistency;
	}
}

@@ -1728,16 +1728,54 @@
 rHolder;
+%0Aimport com.google.common.base.Joiner;
 %0A%0Apublic
@@ -2656,30 +2656,34 @@
 nseData(
-data.toString(
+getStringFrom(data
 ).getByt
@@ -3903,10 +3903,102 @@
 ncy;%0A%09%7D%0A
+%09%0A%09private String getStringFrom(ResultSet input) %7B%0A%09%09return Joiner.on(%22%5Cn%22).join(input);%0A%09%7D%0A
 %7D%0A

Put entire result set as a response data